-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertTo-ChocolateyPackage.ps1
More file actions
112 lines (90 loc) · 3.53 KB
/
ConvertTo-ChocolateyPackage.ps1
File metadata and controls
112 lines (90 loc) · 3.53 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<#
.SYNOPSIS
Wraps an existing PSAppDeployToolkit v4 deployment in a Chocolatey package.
.DESCRIPTION
Copies a PSADT deployment into a templated Chocolatey package, with handling for various results.
.EXAMPLE
ConvertTo-ChocolateyPackage -Id
.EXAMPLE
Get-ChildItem $PSADTPackagesFolder | ConvertTo-ChocolateyPackage
.LINK
https://github.com/chocolatey-community/PSADT-Webinar
#>
[CmdletBinding(DefaultParameterSetName = 'Default')]
param(
# The ID to use for the Chocolatey package
[Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'Default')]
[Parameter(ParameterSetName = 'Large',Mandatory)]
[Alias('BaseName','PackageId')]
[String]$Id = $(Split-Path $PSADTPath -Leaf),
# The path to the PSADT package you want to convert
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, Mandatory, ParameterSetName = 'Default')]
[ValidateScript({Test-Path $_})]
[Alias('PSPath')]
[String]$PSADTPath,
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, Mandatory, ParameterSetName = 'Large')]
[ValidateScript({Test-Path $_})]
[Alias('PSAdtArchive')]
[String]$LargeAppArchive,
# The directory to create the Chocolatey package in
[Parameter(ParameterSetName = 'Default')]
[Parameter(ParameterSetName = 'Large')]
[String]$OutputDirectory = $PSScriptRoot,
# The name of a specific Chocolatey package template to use
[Parameter(ParameterSetName = 'Default')]
[Parameter(Mandatory, ParameterSetName = 'Template')]
[Parameter(ParameterSetName = 'Large')]
[String]$Template,
# Arguments to pass to the Chocolatey package template
[Parameter(ParameterSetName = 'Default')]
[Parameter(ParameterSetName = 'Template')]
[Parameter(ParameterSetName = 'Large')]
[Hashtable]$TemplateArgs,
# If passed, compiles the package at the end of the script
[switch]$CompilePackage
)
begin {
$choco = try {
(Get-Command choco -ErrorAction Stop).Source
}
catch {
throw 'Chocolatey installation required for this script to work. See https://chocolatey.org/install (FOSS), or contact your system administrator!'
}
if ($Template) {
$templateExists = Join-Path $env:ChocolateyInstall -ChildPath "templates\$Template"
if (-not (Test-Path $templateExists)) {
throw "Template $Template not found on this workstation, package creation cannot continue."
}
}
}
end {
# Create the Chocolatey package
$requiredArgs = @('new', $Id, "--output-directory='$OutputDirectory'")
if($Template){
$requiredArgs += "--template='$Template'"
if ($TemplateArgs) {
$TemplateArgs.GetEnumerator() | Foreach-Object {
$requiredArgs += $("{0}='{1}'" -f $_.Key, $_.Value)
}
}
}
# Process any template values
& $choco @requiredArgs
# Get the tools folder
$toolsDir = Join-Path $OutputDirectory -ChildPath "$Id\tools"
# Copy PSADT application files to tools dir
switch($PSCmdlet.ParameterSetName){
'Large' {
$requiredArgs += $("Location='{0}" -f $LargeAppArchive)
}
'Default' {
Copy-Item $PSADTPath -Recurse -Destination $toolsDir
}
}
# Compile the package if requested
if ($CompilePackage) {
$nuspec = (Get-ChildItem $OutputDirectory -Filter "$($Id).nuspec" -Recurse).FullName
$packArgs = @('pack', $nuspec, "--output-directory='$OutputDirectory'")
& $choco @packArgs
}
}