| 1 |
$ErrorActionPreference = "Stop" |
| 2 |
|
| 3 |
$template_path = $args[0] |
| 4 |
$template_manifest = Join-Path -Path $template_path -ChildPath template.psd1 |
| 5 |
$template_script = Join-Path -Path $template_path -ChildPath template.psm1 |
| 6 |
$template_nuspec = Join-Path -Path $template_path -ChildPath template.nuspec |
| 7 |
$template_license = Join-Path -Path $template_path -ChildPath license.txt |
| 8 |
$nuget_exe = Join-Path -Path $template_path -ChildPath nuget.exe |
| 9 |
$sign_cert = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList @( |
| 10 |
(Join-Path -Path $template_path -ChildPath sign.pfx), |
| 11 |
'password', |
| 12 |
# We need to use MachineKeySet so we can load the pfx without using become |
| 13 |
# EphemeralKeySet would be better but it is only available starting with .NET 4.7.2 |
| 14 |
[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeySet |
| 15 |
) |
| 16 |
|
| 17 |
$packages = @( |
| 18 |
@{ name = "ansible-test1"; version = "1.0.0"; repo = "PSRepo 1"; function = "Get-AnsibleTest1" } |
| 19 |
@{ name = "ansible-test1"; version = "1.0.5"; repo = "PSRepo 1"; function = "Get-AnsibleTest1" } |
| 20 |
@{ name = "ansible-test1"; version = "1.1.0"; repo = "PSRepo 1"; function = "Get-AnsibleTest1" } |
| 21 |
@{ name = "ansible-test2"; version = "1.0.0"; repo = "PSRepo 1"; function = "Get-AnsibleTest2" } |
| 22 |
@{ name = "ansible-test2"; version = "1.0.0"; repo = "PSRepo 2"; function = "Get-AnsibleTest2" } |
| 23 |
@{ name = "ansible-test2"; version = "1.0.1"; repo = "PSRepo 1"; function = "Get-AnsibleTest2"; signed = $false } |
| 24 |
@{ name = "ansible-test2"; version = "1.1.0"; prerelease = "beta1"; repo = "PSRepo 1"; function = "Get-AnsibleTest2" } |
| 25 |
@{ name = "ansible-clobber"; version = "0.1.0"; repo = "PSRepo 1"; function = "Enable-PSTrace" } |
| 26 |
@{ name = "ansible-licensed" ; version = "1.1.1"; repo = "PSRepo 1" ; require_license = $true; function = "Get-AnsibleLicensed" } |
| 27 |
) |
| 28 |
|
| 29 |
foreach ($package in $packages) { |
| 30 |
$tmp_dir = Join-Path -Path $template_path -ChildPath $package.name |
| 31 |
if (Test-Path -LiteralPath $tmp_dir) { |
| 32 |
Remove-Item -LiteralPath $tmp_dir -Force -Recurse |
| 33 |
} |
| 34 |
New-Item -Path $tmp_dir -ItemType Directory > $null |
| 35 |
|
| 36 |
Copy-Item -LiteralPath $template_license -Destination $tmp_dir -Force |
| 37 |
|
| 38 |
try { |
| 39 |
$ps_data = @("LicenseUri = 'https://choosealicense.com/licenses/mit/'") |
| 40 |
$nuget_version = $package.version |
| 41 |
if ($package.ContainsKey("prerelease")) { |
| 42 |
$ps_data += "Prerelease = '$($package.prerelease)'" |
| 43 |
$nuget_version = "$($package.version)-$($package.prerelease)" |
| 44 |
} |
| 45 |
if ($package.ContainsKey("require_license")) { |
| 46 |
$ps_data += "RequireLicenseAcceptance = `$$($package.require_license)" |
| 47 |
} |
| 48 |
|
| 49 |
$manifest = [System.IO.File]::ReadAllText($template_manifest) |
| 50 |
$manifest = $manifest.Replace('--- NAME ---', $package.name).Replace('--- VERSION ---', $package.version) |
| 51 |
$manifest = $manifest.Replace('--- GUID ---', [Guid]::NewGuid()).Replace('--- FUNCTION ---', $package.function) |
| 52 |
|
| 53 |
$manifest = $manifest.Replace('PSData = @{}', "PSData = @{`n$($ps_data -join "`n")`n}") |
| 54 |
$manifest_path = Join-Path -Path $tmp_dir -ChildPath "$($package.name).psd1" |
| 55 |
Set-Content -LiteralPath $manifest_path -Value $manifest |
| 56 |
|
| 57 |
$script = [System.IO.File]::ReadAllText($template_script) |
| 58 |
$script = $script.Replace('--- NAME ---', $package.name).Replace('--- VERSION ---', $package.version) |
| 59 |
$script = $script.Replace('--- REPO ---', $package.repo).Replace('Get-Function', $package.function) |
| 60 |
$script_path = Join-Path -Path $tmp_dir -ChildPath "$($package.name).psm1" |
| 61 |
Set-Content -LiteralPath $script_path -Value $script |
| 62 |
|
| 63 |
$signed = if ($package.ContainsKey("signed")) { $package.signed } else { $true } |
| 64 |
if ($signed) { |
| 65 |
Set-AuthenticodeSignature -Certificate $sign_cert -LiteralPath $manifest_path > $null |
| 66 |
Set-AuthenticodeSignature -Certificate $sign_cert -LiteralPath $script_path > $null |
| 67 |
} |
| 68 |
|
| 69 |
# We should just be able to use Publish-Module but it fails when running over WinRM for older hosts and become |
| 70 |
# does not fix this. It fails to respond to nuget.exe push errors when it canno find the .nupkg file. We will |
| 71 |
# just manually do that ourselves. This also has the added benefit of being a lot quicker than Publish-Module |
| 72 |
# which seems to take forever to publish the module. |
| 73 |
$nuspec = [System.IO.File]::ReadAllText($template_nuspec) |
| 74 |
$nuspec = $nuspec.Replace('--- NAME ---', $package.name).Replace('--- VERSION ---', $nuget_version) |
| 75 |
$nuspec = $nuspec.Replace('--- FUNCTION ---', $package.function) |
| 76 |
$nuspec = $nuspec.Replace('--- LICACC ---', ($package.require_license -as [bool]).ToString().ToLower()) |
| 77 |
Set-Content -LiteralPath (Join-Path -Path $tmp_dir -ChildPath "$($package.name).nuspec") -Value $nuspec |
| 78 |
|
| 79 |
&$nuget_exe pack "$tmp_dir\$($package.name).nuspec" -outputdirectory $tmp_dir |
| 80 |
|
| 81 |
$repo_path = Join-Path -Path $template_path -ChildPath $package.repo |
| 82 |
$nupkg_filename = "$($package.name).$($nuget_version).nupkg" |
| 83 |
Copy-Item -LiteralPath (Join-Path -Path $tmp_dir -ChildPath $nupkg_filename) ` |
| 84 |
-Destination (Join-Path -Path $repo_path -ChildPath $nupkg_filename) |
| 85 |
} |
| 86 |
finally { |
| 87 |
Remove-Item -LiteralPath $tmp_dir -Force -Recurse |
| 88 |
} |
| 89 |
} |