forked from PowerShell/PSReadLine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PSReadLine.build.ps1
314 lines (258 loc) · 9.71 KB
/
PSReadLine.build.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#
# To build, make sure you've installed InvokeBuild
# Install-Module -Repository PowerShellGallery -Name InvokeBuild -RequiredVersion 3.1.0
#
# Then:
# Invoke-Build
#
# Or:
# Invoke-Build -Task ZipRelease
#
# Or:
# Invoke-Build -Configuration Debug
#
# etc.
#
[CmdletBinding()]
param([switch]$Install,
[string]$Configuration = (property Configuration Release))
use 15.0 MSBuild
# Final bits to release go here
$targetDir = "bin/$Configuration/PSReadLine"
<#
Synopsis: Ensure nuget is installed
#>
task CheckNugetInstalled `
{
$script:nugetExe = (Get-Command nuget.exe -ea Ignore).Path
if ($null -eq $nugetExe)
{
$script:nugetExe = "${env:TEMP}/nuget.exe"
if (!(Test-Path $nugetExe))
{
Invoke-WebRequest https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -OutFile $nugetExe
}
}
}
<#
Synopsis: Ensure platyPS is installed
#>
task CheckPlatyPSInstalled `
{
if ($null -eq (Get-Module -List platyPS))
{
Install-Module -Scope CurrentUser -Repository PSGallery -Name platyPS
}
}
$restoreNugetParameters = @{
Inputs = "PSReadLine/packages.config"
# We could look for other files, but this is probably good enough.
Outputs = "PSReadLine/packages/Microsoft.PowerShell.5.ReferenceAssemblies.1.0.0/Microsoft.PowerShell.5.ReferenceAssemblies.1.0.0.nupkg"
}
<#
Synopsis: Restore PowerShell reference assemblies
#>
task RestoreNugetPackages @restoreNugetParameters CheckNugetInstalled,{
exec { & $nugetExe restore PSReadLine/PSReadLine.sln }
}
$buildMamlParams = @{
Inputs = { Get-ChildItem docs/*.md }
Outputs = "$targetDir/en-US/Microsoft.PowerShell.PSReadLine2.dll-help.xml"
}
<#
Synopsis: Generate maml help from markdown
#>
task BuildMamlHelp @buildMamlParams {
platyPS\New-ExternalHelp docs -Force -OutputPath $targetDir/en-US/Microsoft.PowerShell.PSReadLine2.dll-help.xml
}
$buildAboutTopicParams = @{
Inputs = {
Get-ChildItem docs/about_PSReadLine.help.txt
"PSReadLine/bin/$Configuration/Microsoft.PowerShell.PSReadLine2.dll"
"$PSScriptRoot/GenerateFunctionHelp.ps1"
"$PSScriptRoot/CheckHelp.ps1"
}
Outputs = "$targetDir/en-US/about_PSReadLine.help.txt"
}
<#
Synopsis: Generate about topic with function help
#>
task BuildAboutTopic @buildAboutTopicParams {
# This step loads the dll that was just built, so only do that in another process
# so the file isn't locked in any way for the rest of the build.
$generatedFunctionHelpFile = New-TemporaryFile
powershell -NoProfile -NonInteractive -File $PSScriptRoot/GenerateFunctionHelp.ps1 $Configuration $generatedFunctionHelpFile.FullName
assert ($LASTEXITCODE -eq 0) "Generating function help failed"
$functionDescriptions = Get-Content -Raw $generatedFunctionHelpFile
$aboutTopic = Get-Content -Raw $PSScriptRoot/docs/about_PSReadLine.help.txt
$newAboutTopic = $aboutTopic -replace '{{FUNCTION_DESCRIPTIONS}}', $functionDescriptions
$newAboutTopic = $newAboutTopic -replace "`r`n","`n"
$newAboutTopic | Out-File -FilePath $targetDir\en-US\about_PSReadLine.help.txt -NoNewline -Encoding ascii
powershell -NoProfile -NonInteractive -File $PSScriptRoot/CheckHelp.ps1 $Configuration
assert ($LASTEXITCODE -eq 0) "Checking help and function signatures failed"
}
$binaryModuleParams = @{
Inputs = { Get-ChildItem PSReadLine/*.cs, PSReadLine/PSReadLine.csproj, PSReadLine/PSReadLineResources.resx }
Outputs = "PSReadLine/bin/$Configuration/Microsoft.PowerShell.PSReadLine2.dll"
}
<#
Synopsis: Build main binary module
#>
task BuildMainModule @binaryModuleParams RestoreNugetPackages, {
exec { msbuild PSReadLine/PSReadLine.csproj /t:Rebuild /p:Configuration=$Configuration /p:Platform=AnyCPU }
}
<#
Synopsis: Generate the file catalog
#>
task GenerateCatalog {
exec {
Remove-Item -ea Ignore $PSScriptRoot/bin/$Configuration/PSReadLine/PSReadLine.cat
$null = New-FileCatalog -CatalogFilePath $PSScriptRoot/bin/$Configuration/PSReadLine/PSReadLine.cat `
-Path $PSScriptRoot/bin/$Configuration/PSReadLine `
-CatalogVersion 2.0
}
}
$buildTestParams = @{
Inputs = { Get-ChildItem TestPSReadLine/*.cs, TestPSReadLine/TestPSReadLine.csproj }
Outputs = "TestPSReadLine/bin/$Configuration/TestPSReadLine.exe"
}
<#
Synopsis: Build executable for interactive testing/development
#>
task BuildTestHost @buildTestParams BuildMainModule, {
exec { msbuild TestPSReadLine/TestPSReadLine.csproj /t:Rebuild /p:Configuration=$Configuration /p:Platform=AnyCPU }
}
$buildUnitTestParams = @{
Inputs = { Get-ChildItem test/*.cs, test/PSReadLine.Tests.csproj }
Outputs = "test/bin/$Configuration/PSReadLine.Tests.dll"
}
<#
Synopsis: Build the unit tests
#>
task BuildTests @buildUnitTestParams BuildMainModule, {
exec { msbuild test/PSReadLine.tests.csproj /t:Rebuild /p:Configuration=$Configuration /p:Platform=AnyCPU }
}
<#
Synopsis: Run the unit tests
#>
task RunTests BuildTests, {
exec {
$env:PSREADLINE_TESTRUN = 1
$runner = "$PSScriptRoot\PSReadLine\packages\xunit.runner.console.2.3.1\tools\net452\xunit.console.exe"
if ($env:APPVEYOR)
{
$outXml = "$PSScriptRoot\xunit-results.xml"
& $runner $PSScriptRoot\test\bin\$Configuration\PSReadLine.Tests.dll -appveyor -xml $outXml
$wc = New-Object 'System.Net.WebClient'
$wc.UploadFile("https://ci.appveyor.com/api/testresults/xunit/$($env:APPVEYOR_JOB_ID)", $outXml)
}
else
{
& $runner $PSScriptRoot\test\bin\$Configuration\PSReadLine.Tests.dll
}
Remove-Item env:PSREADLINE_TESTRUN
}
}
<#
Synopsis: Copy all of the files that belong in the module to one place in the layout for installation
#>
task LayoutModule BuildMainModule, BuildMamlHelp, {
$extraFiles =
'PSReadLine/Changes.txt',
'PSReadLine/License.txt',
'PSReadLine/SamplePSReadLineProfile.ps1',
'PSReadLine/PSReadLine.format.ps1xml',
'PSReadLine/PSReadLine.psm1'
foreach ($file in $extraFiles)
{
Copy-Item $file $targetDir
}
Copy-Item PSReadLine/bin/$Configuration/Microsoft.PowerShell.PSReadLine2.dll $targetDir
Copy-Item PSReadLine/bin/$Configuration/System.Runtime.InteropServices.RuntimeInformation.dll $targetDir
# Copy module manifest, but fix the version to match what we've specified in the binary module.
$version = (Get-ChildItem -Path $targetDir/Microsoft.PowerShell.PSReadLine2.dll).VersionInfo.FileVersion
$moduleManifestContent = Get-Content -Path 'PSReadLine/PSReadLine.psd1' -Raw
$b = Get-Content -Encoding Byte -Raw ./bin/$Configuration/PSReadLine/Microsoft.PowerShell.PSReadLine2.dll
$a = [System.Reflection.Assembly]::Load($b)
$semVer = ($a.GetCustomAttributes([System.Reflection.AssemblyInformationalVersionAttribute], $false)).InformationalVersion
if ($semVer -match "(.*)-(.*)")
{
# Make sure versions match
if ($matches[1] -ne $version) { throw "AssemblyFileVersion mismatch with AssemblyInformationalVersion" }
$prerelease = $matches[2]
# Put the prerelease tag in private data
$moduleManifestContent = [regex]::Replace($moduleManifestContent, "}", "PrivateData = @{ PSData = @{ Prerelease = '$prerelease' } }$([System.Environment]::Newline)}")
}
$moduleManifestContent = [regex]::Replace($moduleManifestContent, "ModuleVersion = '.*'", "ModuleVersion = '$version'")
$moduleManifestContent | Set-Content -Path $targetDir/PSReadLine.psd1
# Make sure we don't ship any read-only files
foreach ($file in (Get-ChildItem -Recurse -File $targetDir))
{
$file.IsReadOnly = $false
}
}, BuildAboutTopic
<#
Synopsis: Zip up the binary for release.
#>
task ZipRelease RunTests, LayoutModule, {
Compress-Archive -Force -LiteralPath $targetDir -DestinationPath "bin/$Configuration/PSReadLine.zip"
}
<#
Synopsis: Install newly built PSReadLine
#>
task Install LayoutModule, {
function Install($InstallDir) {
if (!(Test-Path -Path $InstallDir))
{
New-Item -ItemType Directory -Force $InstallDir
}
try
{
if (Test-Path -Path $InstallDir\PSReadLine)
{
Remove-Item -Recurse -Force $InstallDir\PSReadLine -ErrorAction Stop
}
Copy-Item -Recurse $targetDir $InstallDir
}
catch
{
Write-Error -Message "Can't install, module is probably in use."
}
}
Install "$HOME\Documents\WindowsPowerShell\Modules"
Install "$HOME\Documents\PowerShell\Modules"
}
<#
Synopsis: Publish to PSGallery
#>
task Publish -If ($Configuration -eq 'Release') LayoutModule, {
$manifest = Import-PowerShellDataFile $PSScriptRoot/bin/Release/PSReadLine/PSReadLine.psd1
$version = $manifest.ModuleVersion
if ($null -ne $manifest.PrivateData)
{
$psdata = $manifest.PrivateData['PSData']
if ($null -ne $psdata)
{
$prerelease = $psdata['Prerelease']
if ($null -ne $prerelease)
{
$version = $version + '-' + $prerelease
}
}
}
$yes = Read-Host "Publish version $version (y/n)"
if ($yes -ne 'y') { throw "Publish aborted" }
$nugetApiKey = Read-Host "Nuget api key for PSGallery"
$publishParams = @{
Path = "$PSScriptRoot/bin/Release/PSReadLine"
NuGetApiKey = $nugetApiKey
Repository = "PSGallery"
ReleaseNotes = (Get-Content -Raw $PSScriptRoot/bin/Release/PSReadLine/Changes.txt)
ProjectUri = 'https://github.com/lzybkr/PSReadLine'
}
Publish-Module @publishParams
}
<#
Synopsis: Default build rule - build and create module layout
#>
task . LayoutModule, RunTests