Public/New-LocalChocolateyPackage.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<#
.SYNOPSIS
    Generates local chocolatey packages
.DESCRIPTION
    Generates local chocolatey packages and outputs a nupkg
.EXAMPLE Generates local Chocolatey package
    You must have a Chocolatey specified folder structure for nuspec generation:
    C:\Packages\PackageName\
        C:\Packages\PackageName.nuspec
        C:\Packages\tools\chocolateyInstall.ps1
    C:\NewPackages\

    PS :> New-LocalChocolateyPackage -SourcePath 'C:\Packages' -OutputPath 'C:\NewPackages'
.PARAMETER SourcePath
    The folder that contains the package or packages you want to generate
.PARAMETER OutputPath
    The folder location to place the generated nupkg package(s)
.NOTES
    Name: New-LocalChocolateyPackage
    Author: Josh Rickard (MSAdministrator)
    DateCreated: 07/13/2018
.FUNCTIONALITY
    Install a Chocolatey package
#>

function New-LocalChocolateyPackage {
    [CmdletBinding(DefaultParameterSetName = 'Parameter Set 1',
        PositionalBinding = $false,
        HelpUri = 'http://www.microsoft.com/',
        ConfirmImpact = 'Medium')]
    Param (
        # The folder that contains the package or packages you want to generate
        [Parameter(Mandatory = $true,
            Position = 0,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = 'Parameter Set 1')]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [string]$SourcePath,

        # The folder location to place the generated nupkg package(s)
        [Parameter(Mandatory = $true,
            Position = 1,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = 'Parameter Set 1')]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [string]$OutputPath
    )
    begin {
        Write-Verbose -Message 'Building local Chocolately packages'

        if (-not (Test-Path $OutputPath)) {
            New-Item $OutputPath -Type Directory
            Write-Verbose -Message "Creating $OutputPath"
        }

        $pkgs = Get-ChildItem -Directory $srcPath
    }
    process {
        foreach ($pkg in $pkgs) {
            $pkgPath = "$SourcePath\$pkg"

            Push-Location $pkgPath
            Write-Verbose -Message "Trying to build NuGet package for $pkg"

            choco pack --outputdirectory "$OutputPath" --use-system-powershell\

            if ($LASTEXITCODE -ne 0) {
                Write-Warning -Message "Failed to build Chocolatey package for $pkg"
                Write-Error -ErrorRecord $Error[0] 
                Pop-Location

                Exit -1
            }
            Pop-Location
        }
    }
    end {
        Write-Verbose -Message 'Successfully built Chocolatey packages'
    }
}