Last active
February 20, 2023 14:08
-
-
Save BroVic/0f1461b1e512009dde9cdb87a9119ae0 to your computer and use it in GitHub Desktop.
PowerShell script to automate common local actions taken during maintenance of the {RQDAassist} package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Control various actions to be taken on the RQDAassist repository/package. | |
.DESCRIPTION | |
The RQDAassist package repository is more complicated than some regular CRAN-compatible packages. | |
For instance, the RQDA packages and its dependencies have to be regularly cleaned up for testing | |
out both binary and source installation options. Also, at this point, the package | |
is only working with R versions lower than 4.2. So when developing, one has to always remember to | |
use that version, when it's not the one pointed to by PATH. This script makes it easy to carry out | |
these and other related tasks. | |
.PARAMETER Rversion | |
The version of R to be used for th operation (including the PATCH version). | |
.PARAMETER Type | |
One of 'Binary' (the default) or 'Source'. | |
.PARAMETER CleanOnly | |
Uninstall RQDA and its dependencies | |
.PARAMETER Install | |
Install RQDA and its dependencies. | |
.PARAMETER Check | |
Run R CMD check only, using the latest version of the package. No installation is performed | |
.PARAMETER Release | |
Build both source and binary packages ready for release | |
.LINK | |
https://github.com/BroVic/RQDAassist | |
https://gist.github.com/BroVic/0f1461b1e512009dde9cdb87a9119ae0 | |
.NOTES | |
Author: Victor A. Ordu | |
Last Edit: 2023-02-14 | |
#> | |
param ( | |
[Parameter(HelpMessage = "Version of R to use")] | |
[string]$Rversion, | |
[Parameter(HelpMessage = "Type of package to work with (binary or source)")] | |
[string]$Type = "Binary", | |
[Parameter(HelpMessage = "Uninstall RQDA and its dependencies to start afresh")] | |
[switch]$CleanOnly, | |
[Parameter(HelpMessage = "Run R CMD check on the package")] | |
[switch]$Check, | |
[Parameter(HelpMessage = "Install RQDA and dependencies via the helper package")] | |
[switch]$Install, | |
[Parameter(HelpMessage = "Release the package")] | |
[switch]$Release | |
) | |
function Set-Rexecutable | |
{ | |
param ( | |
[string]$ExecName, | |
[string]$VersionNumber | |
) | |
$binaryPath = Get-RbinPath $VersionNumber | |
Join-Path -Path $binaryPath -ChildPath "$ExecName.exe" | |
} | |
function Get-RbinPath | |
{ | |
param([string]$ver) | |
Join-Path -Path $Rpath -ChildPath "R-$ver/bin/x64" | |
} | |
$Rpath = "C:/Program Files/R" | |
$Rscript = Set-Rexecutable -ExecName "Rscript" -VersionNumber $Rversion | |
# $Rcmd = Join-Path -Path $RversionPath -ChildPath "Rcmd.exe" | |
if (-not (Test-Path $Rscript)) { | |
Write-Host "R $Rversion does not exist on this system" | |
if ($Rversion.Equals("4.1.3")) { | |
Write-Host "Install it from 'https://cran.r-project.org/bin/windows/base/old/4.1.3/R-4.1.3-win.exe'" | |
exit | |
} | |
Write-Host "The most recent version will be used, and if necessary, R 4.1.3 installed. " -NoNewline | |
$ans = Read-Host "Continue? (Y/N)" | |
if ("y" -ne $ans) { | |
exit | |
} | |
$versions = Get-ChildItem $Rpath "R*" | Sort-Object -Descending | Select-Object Name | |
$thisver = $versions.Name[0] | |
Write-Host "Using $thisver" | |
$Rscript = Set-Rexecutable -ExecName "Rscript" -VersionNumber $thisver.Replace("R-", "") | |
$Install = $true | |
} | |
if ($CleanOnly) { | |
Write-Host "Cleaning up RQDA and/or package dependencies: " | |
& $Rscript -e "pp <- c('cairoDevice', 'gWidgets', 'RGtk2', 'gWidgetsRGtk2', 'igraph', 'RQDA')" ` | |
-e "avl <- pp %in% .packages(all = TRUE); pp <- pp[avl]" ` | |
-e "if (any(avl)) { remove.packages(pp); cat('*', paste(pp, collapse = ', '), 'removed\n') } else cat('* Nothing done\n')" | |
} | |
if ($Install) { | |
$arg = $Type.ToLower() | |
& $Rscript -e "if (!requireNamespace('remotes', quietly = TRUE)) install.packages('remotes', repos = 'https://cran.rstudio.com')" ` | |
-e "if (!requireNamespace('RQDAassist', quietly = TRUE)) remotes::install_github('BroVic/RQDAassist', upgrade = 'never')" ` | |
-e "cat('Running RQDAassist version', as.character(packageVersion('RQDAassist')), fill = TRUE)" ` | |
-e "RQDAassist::install(type = '$arg', verbose = TRUE)" | |
} | |
$PackagePath = (Get-ChildItem -Filter "RQDAassist").FullName | |
$PackagePath = $PackagePath.Replace("\","/") | |
$Rcmd = Set-Rexecutable -ExecName "Rcmd" -VersionNumber $Rversion | |
if ($Check) { | |
Write-Host "Checking the package" | |
& $Rcmd check $PackagePath --as-cran | |
} | |
if ($Release) { | |
Write-Host "Building source package" | |
& $Rcmd build $PackagePath | |
Write-Host "Building binary package" | |
& $Rcmd INSTALL --build $PackagePath | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment