forked from iainbrighton/PScribo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuild.PSake.ps1
254 lines (212 loc) · 10.1 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#requires -Version 5;
#requires -Modules VirtualEngine.Build;
$psake.use_exit_on_error = $true;
Properties {
$currentDir = Resolve-Path -Path (Get-Location -PSProvider FileSystem);
$basePath = $psake.build_script_dir;
$buildDir = 'Build';
$releaseDir = 'Release';
$company = 'Iain Brighton';
$author = 'Iain Brighton';
$thumbprint = '3DACD0F2D1E60EB33EC774B9CFC89A4BEE9037AF';
$timeStampServer = 'http://timestamp.verisign.com/scripts/timestamp.dll';
}
Task Default -Depends Build;
Task Build -Depends Clean, Setup, Test, Deploy;
Task Stage -Depends Build, Version, Bundle, Sign, Zip;
#Task Publish -Depends Stage, Release;
Task Test {
$testResult = Invoke-Pester -Path $basePath -OutputFile "$buildPath\TestResult.xml" -OutputFormat NUnitXml -Strict -PassThru;
if ($testResult.FailedCount -gt 0) {
Write-Error ('Failed "{0}" unit tests.' -f $testResult.FailedCount);
}
}
Task Clean {
Write-Host (' Base directory "{0}".' -f $basePath) -ForegroundColor Yellow;
## 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 (TrimPath -Path $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 releasePath -Value (Join-Path -Path $psake.build_script_dir -ChildPath $releaseDir) -Scope Script;
$newModuleVersion = New-Object -TypeName System.Version -ArgumentList $manifest.Version.Major, $manifest.Version.Minor,$manifest.Version.Build,(Get-GitRevision);
Set-Variable version -Value ($newModuleVersion.ToString()) -Scope Script;
Write-Host (' Building module "{0}".' -f $manifest.Name) -ForegroundColor Yellow;
Write-Host (' Using version number "{0}".' -f $version) -ForegroundColor Yellow;
## Create the build directory
Write-Host (' Creating build directory "{0}".' -f (TrimPath -Path $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 (TrimPath -Path $releasePath)) -ForegroundColor Yellow;
[Ref] $null = New-Item $releasePath -ItemType Directory -Force -ErrorAction Stop;
}
}
Task Deploy {
## Copy release files
Write-Host (' Copying release files to build directory "{0}".' -f (TrimPath -Path $buildPath)) -ForegroundColor Yellow;
$excludedFiles = @(
'*.Tests.ps1',
'Build.PSake.ps1',
'.git*',
'*.png',
'Build',
'Release',
'readme.md',
'bin',
'obj',
'*.sln',
'*.suo',
'*.pssproj',
'PScribo Test Doc.*',
'PScriboExample.*',
'TestResult.xml',
'.vscode',
'System.IO.Packaging.dll'
);
Get-ModuleFile -Exclude $excludedFiles | ForEach-Object {
$destinationPath = '{0}{1}' -f $buildPath, $PSItem.FullName.Replace($basePath, '');
Write-Host (' Copying release file "{0}".' -f (TrimPath -Path $destinationPath)) -ForegroundColor DarkCyan;
[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";
Write-Host (' Versioning module manifest "{0}".' -f $manifestPath) -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 (TrimPath -Path $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 (TrimPath -Path $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 (TrimPath -Path $zipFile.Fullname)) -ForegroundColor Yellow;
}
Task Bundle {
$bundlePath = Join-Path -Path $releasePath -ChildPath "PScribo-v$version-Bundle.ps1";
Write-Host (' Creating bundle file "{0}".' -f (TrimPath -Path $bundlePath)) -ForegroundColor Yellow;
$bundleFiles = "$buildPath\Src\Public","$buildPath\Src\Private","$buildPath\Src\Plugins\Public";
$excludedFiles = '*.Tests.ps1','*.Internal.ps1','System.IO.Packaging.dll';
New-Bundle -Path $bundleFiles -DestinationPath $bundlePath -Exclude $excludedFiles -Verbose;
}
<#
Task DocumentBundle {
$bundlePath = Join-Path -Path $buildPath -ChildPath "PScribo-$version-DocumentBundle.ps1";
$bundleFiles = "$currentDir\LICENSE","$currentDir\Functions";
New-Bundle -Path $bundleFiles -DestinationPath $bundlePath -Verbose;
}
Task OutputBundle {
$bundlePath = Join-Path -Path $buildPath -ChildPath "PScribo-$version-OutputBundle.ps1";
$bundleFiles = "$currentDir\LICENSE","$currentDir\Plugins";
New-Bundle -Path $bundleFiles -DestinationPath $bundlePath -Verbose;
}
#>
Task Minify { }
function TrimPath {
<#
.SYNOPSIS
Trims a directory path to a relative path to avoid wrapping issues
#>
[CmdletBinding()]
[OutputType([System.String])]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[System.String] $Path
)
return ($Path.Replace($basePath, ''));
}
function Join-BundleFile {
[CmdletBinding()]
param (
## Files to bundle.
[Parameter(Mandatory)]
[System.String[]] $Path,
## Output filename.
[Parameter(Mandatory)]
[System.String] $DestinationPath,
## Excluded files.
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String[]] $Exclude = @('*.Tests.ps1')
)
process {
foreach ($file in (Get-ChildItem -Path $Path -Exclude $Exclude)) {
Write-Host (' Bundling file "{0}".' -f (TrimPath -Path $file)) -ForegroundColor DarkCyan;
$internalFunctionContent = "";
$content = Get-Content -Path $file.FullName -Raw;
if ($content -match '(?<=<#!\s?)\S+.Internal.ps1(?=\s?!#>)') {
$internalFunctionDirectory = Join-Path -Path (Split-Path -Path $file.DirectoryName -Parent) -ChildPath Private;
$internalFunctionPath = Join-Path -Path $internalFunctionDirectory -ChildPath $Matches[0];
if (Test-Path -Path $internalFunctionPath) {
if (Test-Path -Path $internalFunctionPath) {
Write-Host (' Bundling internal file "{0}".' -f (TrimPath -Path $internalFunctionPath)) -ForegroundColor DarkCyan;
$internalFunctionContent = Get-Content -Path $internalFunctionPath -Raw;
if ($content -match '<#!\s?\S+.Internal.ps1\s?!#>') {
$replacementString = $Matches[0];
Write-Debug ('Replacing text "{0}"...' -f $replacementString);
## Cannot use the -replace method as it replaces occurences of $_ too ?!
$content = $content.Replace($replacementString, $internalFunctionContent.Trim());
}
}
}
}
## Now remove empty lines
$compressedContent = New-Object -TypeName System.Text.StringBuilder;
$content.Split("`r`n") | ForEach-Object {
if (-not [System.String]::IsNullOrWhitespace($_)) {
[ref] $null = $compressedContent.AppendLine($_);
}
}
$compressedContent.ToString() | Add-Content -Path $DestinationPath;
}
}
}
function New-Bundle {
[CmdletBinding()]
param (
## Files to bundle.
[Parameter(Mandatory)]
[System.String[]] $Path,
## Output filename.
[Parameter(Mandatory)]
[System.String] $DestinationPath,
## Excluded files.
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String[]] $Exclude = @('*.Tests.ps1','*.Internal.ps1')
)
Write-Host (' Creating bundle header.') -ForegroundColor Cyan;
Set-Content -Path $DestinationPath -Value "#region PScribo Bundle v$version";
Add-Content -Path $DestinationPath -Value "#requires -Version 3`r`n";
## Import LICENSE
Write-Host (' Creating bundle license.') -ForegroundColor Cyan;
Get-Content -Path "$currentDir\LICENSE" | Add-Content -Path $DestinationPath;
## TODO: Support localised bundles, eg en-US and fr-FR
Write-Host (' Creating bundle resources.') -ForegroundColor Cyan;
Add-Content -Path $DestinationPath -Value "`r`n`$localized = DATA {";
Get-Content -Path "$currentDir\en-US\PScribo.Resources.psd1" | Add-Content -Path $DestinationPath;
Add-Content -Path $DestinationPath -Value "}`r`n";
Join-BundleFile -Path $Path -DestinationPath $DestinationPath -Exclude $Exclude;
Write-Host (' Creating bundle footer.') -ForegroundColor Cyan;
Add-Content -Path $DestinationPath -Value "#endregion PScribo Bundle v$version";
}