-
Notifications
You must be signed in to change notification settings - Fork 0
/
Build.PSake.ps1
164 lines (147 loc) · 8.83 KB
/
Build.PSake.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#requires -Module VirtualEngine.Build, VirtualEngine.Compression;
#requires -Version 3;
Properties {
$currentDir = Resolve-Path -Path .;
$basePath = $psake.build_script_dir;
$buildDir = 'Build';
$releaseDir = 'Release';
$invocation = (Get-Variable MyInvocation -Scope 1).Value;
$thumbprint = 'D10BB31E5CE3048A7D4DA0A4DD681F05A85504D3';
$timeStampServer = 'http://timestamp.verisign.com/scripts/timestamp.dll';
$company = 'Virtual Engine Limited';
$author = 'Iain Brighton';
$githubTokenPath = '~\Github.apitoken';
$githubOwner = 'VirtualEngine';
$githubRepository = 'Compression'
$chocolateyTokenPath = '~\Chocolatey.apitoken';
$chocolateyPackageName = 'VirtualEngine-Compression';
}
Task Default -Depends Build;
Task Build -Depends Clean, Setup, Test, Deploy;
Task Stage -Depends Build, Version, Sign, Zip;
Task Publish -Depends Stage, Release, Chocolatey;
Task Clean {
## Remove build directory
$baseBuildPath = Join-Path -Path $psake.build_script_dir -ChildPath $buildDir;
if (Test-Path -Path $baseBuildPath) {
Write-Host (' Removing build base directory "{0}".' -f $baseBuildPath) -ForegroundColor Yellow;
Remove-Item $baseBuildPath -Recurse -Force -ErrorAction Stop;
}
}
Task Setup {
# Properties are not available in the script scope.
Set-Variable manifest -Value (Get-ModuleManifest) -Scope Script;
Set-Variable buildPath -Value (Join-Path -Path $psake.build_script_dir -ChildPath "$buildDir\$($manifest.Name)") -Scope Script;
Set-Variable chocolateyBuildPath -Value (Join-Path -Path $psake.build_script_dir -ChildPath "$buildDir\Chocolatey") -Scope Script;
Set-Variable releasePath -Value (Join-Path -Path $psake.build_script_dir -ChildPath $releaseDir) -Scope Script;
Set-Variable version -Value ($manifest.Version.ToString()) -Scope Script;
Write-Host (' Building module "{0}".' -f $manifest.Name) -ForegroundColor Yellow;
Write-Host (' Using module version "{0}".' -f $version) -ForegroundColor Yellow;
## Create the build directory
Write-Host (' Creating build directory "{0}".' -f $buildPath) -ForegroundColor Yellow;
[Ref] $null = New-Item $buildPath -ItemType Directory -Force -ErrorAction Stop;
## Create the release directory
if (!(Test-Path -Path $releasePath)) {
Write-Host (' Creating release directory "{0}".' -f $releasePath) -ForegroundColor Yellow;
[Ref] $null = New-Item $releasePath -ItemType Directory -Force -ErrorAction Stop;
}
}
Task Test {
$testResultsPath = Join-Path $buildPath -ChildPath 'NUnit.xml';
$testResults = Invoke-Pester -Path $basePath -OutputFile $testResultsPath -OutputFormat NUnitXml -PassThru -Strict;
if ($testResults.FailedCount -gt 0) {
Write-Error ('{0} unit tests failed.' -f $testResults.FailedCount);
}
}
Task Deploy {
## Update license
$licensePath = Join-Path -Path $buildPath -ChildPath LICENSE;
Write-Host (' Creating license file "{0}".' -f $licensePath) -ForegroundColor Yellow;
[Ref] $null = New-ModuleLicense -Path $licensePath -LicenseType MIT -FullName $company;
## Copy release files
Write-Host (' Copying release files to build directory "{0}".' -f $buildPath) -ForegroundColor Yellow;
$excludedFiles = @( '*.Tests.ps1','Build.PSake.ps1','.git*','*.png','Build','Release' );
Get-ModuleFile -Exclude $excludedFiles | % {
$destinationPath = '{0}{1}' -f $buildPath, $PSItem.FullName.Replace($basePath, '');
[Ref] $null = New-Item -ItemType File -Path $destinationPath -Force;
Copy-Item -Path $PSItem.FullName -Destination $destinationPath -Force;
}
}
Task Version {
## Version module manifest prior to build
$manifestPath = Join-Path $buildPath -ChildPath "$($manifest.Name).psd1";
Set-Variable version -Value ('{0}.{1}.{2}.{3}' -f $manifest.Version.Major, $manifest.Version.Minor, $manifest.Version.Build, (Get-GitRevision)) -Scope Script -Force;
Write-Host (' Versioning module manifest "{0}" as version "{1}".' -f $manifestPath, $version) -ForegroundColor Yellow;
Set-ModuleManifestProperty -Path $manifestPath -Version $version -CompanyName $company -Author $author;
## Reload module manifest to ensure the version number is picked back up
Set-Variable manifest -Value (Get-ModuleManifest -Path $manifestPath) -Scope Script -Force;
}
Task Sign {
Get-ChildItem -Path $buildPath -Include *.ps* -Recurse -File | % {
Write-Host (' Signing file "{0}":' -f $PSItem.FullName) -ForegroundColor Yellow -NoNewline;
$signResult = Set-ScriptSignature -Path $PSItem.FullName -Thumbprint $thumbprint -TimeStampServer $timeStampServer -ErrorAction Stop;
Write-Host (' {0}.' -f $signResult.Status) -ForegroundColor Green;
}
}
Task Zip {
## Creates the release files in the $releaseDir
$zipReleaseName = '{0}-v{1}.zip' -f $manifest.Name, $version;
$zipPath = Join-Path -Path $releasePath -ChildPath $zipReleaseName;
Write-Host (' Creating zip file "{0}".' -f $zipPath) -ForegroundColor Yellow;
## Zip the parent directory
$zipSourcePath = Split-Path -Path $buildPath -Parent;
$zipFile = New-ZipArchive -Path $zipSourcePath -DestinationPath $zipPath;
Write-Host (' Zip file "{0}" created.' -f $zipFile.Fullname) -ForegroundColor Yellow;
}
Task Release {
## Create a Github release
$githubApiKey = (New-Object System.Management.Automation.PSCredential 'OAUTH', (Get-Content -Path $githubTokenPath | ConvertTo-SecureString)).GetNetworkCredential().Password;
Write-Host (' Creating new Github "{0}" release in repository "{1}/{2}".' -f $version, $githubOwner, $githubRepository) -ForegroundColor Yellow;
$release = New-GitHubRelease -Version $version -Repository $manifest.Name -Owner $githubOwner -ApiKey $githubApiKey;
if ($release) {
## Creates the release files in the $releaseDir
$zipReleaseName = '{0}-v{1}.zip' -f $manifest.Name, $version;
$zipPath = Join-Path -Path $releasePath -ChildPath $zipReleaseName;
Write-Host (' Uploading asset "{0}".' -f $zipPath) -ForegroundColor Yellow;
$asset = Invoke-GitHubAssetUpload -Release $release -ApiKey $githubApiKey -Path $zipPath;
Set-Variable -Name assetUri -Value $asset.Browser_Download_Url -Scope Script -Force;
}
}
Task Chocolatey {
## Create the Chocolatey folder
Write-Host (' Creating Chocolatey directory "{0}".' -f $chocolateyBuildPath) -ForegroundColor Yellow;
[Ref] $null = New-Item $chocolateyBuildPath -ItemType Directory -Force -ErrorAction Stop;
$chocolateyToolsPath = New-Item "$chocolateyBuildPath\tools" -ItemType Directory -Force -ErrorAction Stop;
## Create the Chocolatey package
$nuspecFilename = '{0}.nuspec' -f $chocolateyPackageName;
$nuspecPath = Join-Path -Path $chocolateyBuildPath -ChildPath $nuspecFilename;
Write-Host (' Creating Nuget specification "{0}".' -f $nuspecPath) -ForegroundColor Yellow;
$nugetSpecParam = @{
Name = 'VirtualEngine-Compression';
Version = $version;
Title = 'VirtualEngine-Compression';
Authors = $manifest.Author;
Owners = $manifest.CompanyName;
Description = $manifest.Description;
ProjectUrl = $manifest.PrivateData.PSData.ProjectUri;
IconUrl = $manifest.PrivateData.PSData.IconUri;
LicenseUrl = $manifest.PrivateData.PSData.LicenseUri;
Tags = $manifest.PrivateData.PSData.Tags;
};
(New-NuGetNuspec @nugetSpecParam).Save($nuspecPath);
Write-Host ' Creating Chocolatey install files.' -ForegroundColor Yellow;
$zipReleaseName = '{0}-v{1}.zip' -f $manifest.Name, $version;
$assetUri = 'https://github.com/VirtualEngine/Compression/releases/download/v{0}/{1}' -f $version, $zipReleaseName;
New-ChocolateyInstallZipModule -Path $chocolateyToolsPath.FullName -PackageName $chocolateyPackageName -Uri $assetUri;
## Fix period replacement in package name - curses to the Chocolatey Gods :@
$chocolateyUninstallPath = '{0}\ChocolateyUninstall.ps1' -f $chocolateyToolsPath.FullName;
(Get-Content -Path $chocolateyUninstallPath) | % { $_ -replace $chocolateyPackageName, $Manifest.Name } | Set-Content -Path $chocolateyUninstallPath -Encoding UTF8;
Write-Host (' Creating Nuget package from "{0}".' -f $nuspecPath) -ForegroundColor Yellow;
$nugetOutput = Invoke-NuGetPack -Path $nuspecPath -DestinationPath $releasePath;
if ($nugetOutput) {
$nugetPackagePath = Join-Path -Path $releasePath -ChildPath ('{0}.{1}.nupkg' -f $chocolateyPackageName.ToLower(), $version);
Write-Host (' Chocolatey package "{0}" created.' -f $nugetPackagePath) -ForegroundColor Yellow;
$chocolateyApiKey = (New-Object System.Management.Automation.PSCredential 'OAUTH', (Get-Content -Path $chocolateyTokenPath | ConvertTo-SecureString)).GetNetworkCredential().Password;
Write-Host (' Pushing Chocolatey package "{0}".' -f $nugetPackagePath) -ForegroundColor Yellow;
}
}