-
Notifications
You must be signed in to change notification settings - Fork 18
/
build.ps1
416 lines (349 loc) · 12.6 KB
/
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
[cmdletbinding(DefaultParameterSetName='build')]
param(
[Parameter(ParameterSetName='build',Position=0)]
[switch]$build,
[Parameter(ParameterSetName='setversion',Position=0)]
[switch]$setversion,
[Parameter(ParameterSetName='getversion',Position=0)]
[switch]$getversion,
# build parameters
[Parameter(ParameterSetName='build',Position=1)]
[switch]$CleanOutputFolder,
[Parameter(ParameterSetName='build',Position=2)]
[switch]$publishToNuget,
[Parameter(ParameterSetName='build',Position=3)]
[string]$nugetApiKey = ($env:NuGetApiKey),
[Parameter(ParameterSetName='build',Position=4)]
[switch]$noTests,
# setversion parameters
[Parameter(ParameterSetName='setversion',Position=1,Mandatory=$true)]
[string]$newversion,
[Parameter(ParameterSetName='setversion',Position=2)]
[string]$oldversion,
[Parameter(ParameterSetName='openciwebsite',Position=0)]
[Alias('openci')]
[switch]$openciwebsite,
[Parameter(ParameterSetName='updateDeps',Position=0)]
[switch]$updateDeps
)
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
$scriptDir = ((Get-ScriptDirectory) + "\")
[string]$script:defaultMSBuildPath=$null
<#
.SYNOPSIS
This will return the path to msbuild.exe. If the path has not yet been set
then the highest installed version of msbuild.exe will be returned.
#>
function Get-MSBuildExe{
[cmdletbinding()]
param()
process{
$path = $script:defaultMSBuildPath
if(!$path){
$path = Get-ChildItem "hklm:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\" |
Sort-Object {[double]$_.PSChildName} -Descending |
Select-Object -First 1 |
Get-ItemProperty -Name MSBuildToolsPath |
Select -ExpandProperty MSBuildToolsPath
$path = (Join-Path -Path $path -ChildPath 'msbuild.exe')
}
return Get-Item $path
}
}
<#
.SYNOPSIS
If nuget is in the tools
folder then it will be downloaded there.
#>
function Get-Nuget(){
[cmdletbinding()]
param(
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools.01\"),
$nugetDownloadUrl = 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe'
)
process{
$nugetDestPath = Join-Path -Path $toolsDir -ChildPath nuget.exe
if(!(Test-Path $nugetDestPath)){
'Downloading nuget.exe' | Write-Verbose
(New-Object System.Net.WebClient).DownloadFile($nugetDownloadUrl, $nugetDestPath)
# double check that is was written to disk
if(!(Test-Path $nugetDestPath)){
throw 'unable to download nuget'
}
}
# return the path of the file
$nugetDestPath
}
}
<#
.SYNOPSIS
This will inspect the publsish nuspec file and return the value for the Version element.
#>
function GetExistingVersion{
[cmdletbinding()]
param(
[ValidateScript({test-path $_ -PathType Leaf})]
$nuspecFile = (Join-Path $scriptDir 'psbuild.nuspec')
)
process{
([xml](Get-Content $nuspecFile)).package.metadata.version
}
}
$script:pkgDownloaderEnabled = $false
function Enable-PackageDownloader{
[cmdletbinding()]
param(
$toolsDir = "$env:LOCALAPPDATA\LigerShark\tools.01\package-downloader\v1\",
$pkgDownloaderDownloadUrl = 'http://go.microsoft.com/fwlink/?LinkId=524325') # package-downloader.psm1
process{
if(!($script:pkgDownloaderEnabled)){
if(get-module package-downloader){
remove-module package-downloader | Out-Null
}
if(!(get-module package-downloader)){
if(!(Test-Path $toolsDir)){ New-Item -Path $toolsDir -ItemType Directory -WhatIf:$false }
$expectedPath = (Join-Path ($toolsDir) 'package-downloader.psm1')
if(!(Test-Path $expectedPath)){
'Downloading [{0}] to [{1}]' -f $pkgDownloaderDownloadUrl,$expectedPath | Write-Verbose
(New-Object System.Net.WebClient).DownloadFile($pkgDownloaderDownloadUrl, $expectedPath)
}
if(!$expectedPath){throw ('Unable to download package-downloader.psm1')}
'importing module [{0}]' -f $expectedPath | Write-Output
Import-Module $expectedPath -DisableNameChecking -Force
$script:pkgDownloaderEnabled = $true
}
}
}
}
function Update-FilesWithCommitId{
[cmdletbinding()]
param(
[string]$commitId = ($env:APPVEYOR_REPO_COMMIT),
[Parameter(Position=2)]
[string]$filereplacerVersion = '0.2.0-beta'
)
process{
if(![string]::IsNullOrWhiteSpace($commitId)){
'Updating commitId from [{0}] to [{1}]' -f '$(COMMIT_ID)',$commitId | Write-Verbose
Enable-PackageDownloader
'trying to load file replacer' | Write-Verbose
Enable-NuGetModule -name 'file-replacer' -version $filereplacerVersion
$folder = $scriptDir
$include = '*.nuspec'
# In case the script is in the same folder as the files you are replacing add it to the exclude list
$exclude = "$($MyInvocation.MyCommand.Name);"
$replacements = @{
'$(COMMIT_ID)'="$commitId"
}
Replace-TextInFolder -folder $folder -include $include -exclude $exclude -replacements $replacements | Write-Verbose
'Replacement complete' | Write-Verbose
}
}
}
function SetVersion{
[cmdletbinding()]
param(
[Parameter(Position=0,Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$newversion,
[Parameter(Position=1)]
[ValidateNotNullOrEmpty()]
[string]$oldversion = (GetExistingVersion),
[Parameter(Position=2)]
[string]$filereplacerVersion = '0.2.0-beta'
)
process{
'Updating version from [{0}] to [{1}]' -f $oldversion,$newversion | Write-Verbose
Enable-PackageDownloader
'trying to load file replacer' | Write-Verbose
Enable-NuGetModule -name 'file-replacer' -version $filereplacerVersion
$folder = $scriptDir
$include = '*.nuspec;*.ps*1'
# In case the script is in the same folder as the files you are replacing add it to the exclude list
$exclude = "$($MyInvocation.MyCommand.Name);"
$replacements = @{
"$oldversion"="$newversion"
}
Replace-TextInFolder -folder $folder -include $include -exclude $exclude -replacements $replacements | Write-Verbose
# update the .psd1 file
$replacements = @{
($oldversion.Replace('-beta','.1'))=($newversion.Replace('-beta','.1'))
}
Replace-TextInFolder -folder $folder -include '*.ps*1' -exclude $exclude -replacements $replacements | Write-Verbose
'Replacement complete' | Write-Verbose
}
}
function PublishNuGetPackage{
[cmdletbinding()]
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string]$nugetPackages,
[Parameter(Mandatory=$true)]
$nugetApiKey
)
process{
foreach($nugetPackage in $nugetPackages){
$pkgPath = (get-item $nugetPackage).FullName
$cmdArgs = @('push',$pkgPath,'-ApiKey',$nugetApiKey,'-Source','https://api.nuget.org/v3/index.json','-NonInteractive')
'Publishing nuget package with the following args: [nuget.exe {0}]' -f ($cmdArgs -join ' ') | Write-Verbose
&(Get-Nuget) $cmdArgs
if($? -eq $false){
"Upload of [{0}] to appveyor failed; Failing Build..." -f $pkgPath | Write-Error
exit 1
}
}
}
}
function Clean-OutputFolder{
[cmdletbinding()]
param()
process{
$outputFolder = Get-OutputRoot
if(Test-Path $outputFolder){
'Deleting output folder [{0}]' -f $outputFolder | Write-Host
Remove-Item $outputFolder -Recurse -Force
}
}
}
function LoadPester{
[cmdletbinding()]
param(
$pesterDir = (resolve-path (Join-Path $scriptDir 'contrib\pester\'))
)
process{
if(!(Get-Module pester)){
if($env:PesterDir -and (test-path $env:PesterDir)){
$pesterDir = $env:PesterDir
}
if(!(Test-Path $pesterDir)){
throw ('Pester dir not found at [{0}]' -f $pesterDir)
}
$modFile = (Join-Path $pesterDir 'Pester.psm1')
'Loading pester from [{0}]' -f $modFile | Write-Verbose
Import-Module (Join-Path $pesterDir 'Pester.psm1')
}
}
}
function Get-OutputRoot{
[cmdletbinding()]
param()
process{
Join-Path $scriptDir "OutputRoot"
}
}
function Run-Tests{
[cmdletbinding()]
param(
$testDirectory = (join-path $scriptDir tests)
)
begin{
LoadPester
$previousToolsDir = $env:PSBuildToolsDir
$env:PSBuildToolsDir = (Join-Path (Get-OutputRoot) 'PSBuild\')
}
process{
# go to the tests directory and run pester
push-location
set-location $testDirectory
$pesterArgs = @{
'-PassThru' = $true
}
if($env:ExitOnPesterFail -eq $true){
$pesterArgs.Add('-EnableExit',$true)
}
if($env:PesterEnableCodeCoverage -eq $true){
$pesterArgs.Add('-CodeCoverage','..\src\psbuild.psm1')
}
$pesterResult = Invoke-Pester @pesterArgs
pop-location
if($pesterResult.FailedCount -gt 0){
throw ('Failed test cases: {0}' -f $pesterResult.FailedCount)
}
}
end{
$env:PSBuildToolsDir = $previousToolsDir
}
}
function Build{
[cmdletbinding()]
param()
process{
if($publishToNuget){ $CleanOutputFolder = $true }
if($CleanOutputFolder){
Clean-OutputFolder
}
Update-FilesWithCommitId
$projFilePath = get-item (Join-Path $scriptDir 'psbuild.proj')
$msbuildArgs = @()
$msbuildArgs += $projFilePath.FullName
$msbuildArgs += '/p:Configuration=Release'
$msbuildArgs += '/p:VisualStudioVersion=12.0'
$msbuildArgs += '/flp1:v=d;logfile=msbuild.d.log'
$msbuildArgs += '/flp2:v=diag;logfile=msbuild.diag.log'
$msbuildArgs += '/m'
& ((Get-MSBuildExe).FullName) $msbuildArgs
if(-not ($noTests)){
Run-Tests
}
# publish to nuget if selected
if($publishToNuget){
(Get-ChildItem -Path (Get-OutputRoot) 'psbuild*.nupkg').FullName | PublishNuGetPackage -nugetApiKey $nugetApiKey
}
}
}
<#
.SYNOPSIS
This will update the contents of the contrib folder for nuget-powershell and file-replacer
#>
function Update-Dependencies{
[cmdletbinding()]
param(
[string]$destDir
)
process{
if([string]::IsNullOrWhiteSpace($destDir)){
$destDir = (Join-Path $scriptDir 'contrib')
}
# create a new temp folder
$tempFolder = (Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath ('psbuild\build\{0}' -f [System.IO.Path]::GetRandomFileName().Replace('.','')))
New-Item -Path $tempFolder -ItemType Directory
# restore the packages to that folder
'Getting latest file-replacer' | Write-Output
$fpPath = Get-NuGetPackage -name 'file-replacer' -prerelease -cachePath $tempFolder -binpath
'Getting latest nuget-powershell' | Write-Output
$npPath = Get-NuGetPackage -name 'nuget-powershell' -prerelease -cachePath $tempFolder -binpath
#move the files to the dest dir
Copy-Item -path "$fpPath\*.ps*1" -Destination "$destDir"
Copy-Item -path "$fpPath\*.dll" -Destination "$destDir"
Copy-Item -path "$npPath\*.ps*1" -Destination "$destDir"
}
}
function OpenCiWebsite{
[cmdletbinding()]
param()
process{
start 'https://ci.appveyor.com/project/sayedihashimi/psbuild'
}
}
if(!$build -and !$setversion -and !$getversion -and !$openciwebsite -and !$updateDeps){
$build = $true
}
try{
if($build){ Build }
elseif($setversion){ SetVersion -newversion $newversion }
elseif($getversion){ GetExistingVersion | Write-Output }
elseif($updateDeps){ Update-Dependencies | Write-Output}
elseif($openciwebsite){ OpenCiWebsite }
else{
$cmds = @('-build','-setversion')
'Command not found or empty, please pass in one of the following [{0}]' -f ($cmds -join ' ') | Write-Error
}
}
catch{
"Build failed with an exception:`n{0}" -f ($_.Exception.Message) | Write-Error
exit 1
}