Public/New-Package.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function New-Package {

    <#

    .SYNOPSIS
    Builds a R package

    .DESCRIPTION
    Executes the devtools build function.
    Returns the path to the created package archive.

    .PARAMETER Path
    Specifies the path to the package source

    .PARAMETER Library
    Specifies an additional library path that contains required R packages.

    .OUTPUTS
    string

    .EXAMPLE
    PS C:\> New-RPackage -Path $PackagePath


    #>


    [CmdletBinding()]
    param (
        [Parameter( Mandatory )]
        [ValidateScript({ Test-Path $_ -PathType Container })]
        [string] $Path,

        [ValidateScript( { Test-Path $_ -PathType Container })]
        [string] $Library
    )

    $commands = @()

    if ( $Library ) {
        $commands += @( Get-AddLibraryCommand $Library )
    }

    $commands += @( 'devtools::build()' )

    Push-Location $Path
    try {
        $archivePath = Invoke-RScript -ArgumentList $commands -Timeout $null -ParseOutput | ForEach-Object { $_.Trim('"') }
        if ( Test-Path $archivePath -PathType Leaf ) {
            Write-Verbose "Package $( ( Get-Item $archivePath ).Name ) was created."
        } else {
            throw "Package $( ( Get-Item $Path ).Name ) was not created."
        }
    }
    finally {
        Pop-Location
    }

    return $archivePath
}