-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ps1
404 lines (344 loc) · 12.5 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
[cmdletbinding(DefaultParameterSetName='build')]
param(
[Parameter(ParameterSetName='build',Position=0)]
[switch]$build,
[Parameter(ParameterSetName='build',Position=1)]
[string]$configuration = 'Release',
[Parameter(ParameterSetName='build',Position=2)]
[System.IO.DirectoryInfo]$outputPath,
[Parameter(ParameterSetName='build',Position=3)]
[switch]$cleanBeforeBuild,
[Parameter(ParameterSetName='build',Position=4)]
[switch]$publishToNuget,
[Parameter(ParameterSetName='build',Position=5)]
[string]$nugetApiKey = ($env:NuGetApiKey),
[Parameter(ParameterSetName='build',Position=6)]
[switch]$notests,
# version parameters
[Parameter(ParameterSetName='setversion',Position=0)]
[switch]$setversion,
[Parameter(ParameterSetName='setversion',Position=1,Mandatory=$true)]
[string]$newversion,
[Parameter(ParameterSetName='getversion',Position=0)]
[switch]$getversion,
# clean parameters
[Parameter(ParameterSetName='clean',Position=0)]
[switch]$clean,
[Parameter(ParameterSetName='openciwebsite',Position=0)]
[Alias('openci')]
[switch]$openciwebsite
)
Set-StrictMode -Version Latest
function Get-ScriptDirectory{
split-path (((Get-Variable MyInvocation -Scope 1).Value).MyCommand.Path)
}
$scriptDir = ((Get-ScriptDirectory) + "\")
if([string]::IsNullOrWhiteSpace($outputPath)){
$outputPath = (Join-Path $scriptDir 'OutputRoot')
}
$env:GeoffreyBinPath = $outputPath
[System.IO.DirectoryInfo]$outputPathNuget = (Join-Path $outputPath '_nuget-pkg')
function EnsurePsbuildInstlled{
[cmdletbinding()]
param(
[string]$psbuildInstallUri = 'https://raw.githubusercontent.com/ligershark/psbuild/master/src/GetPSBuild.ps1'
)
process{
if(-not (Get-Command "Invoke-MsBuild" -errorAction SilentlyContinue)){
'Installing psbuild from [{0}]' -f $psbuildInstallUri | Write-Verbose
(new-object Net.WebClient).DownloadString($psbuildInstallUri) | iex
}
# make sure it's loaded and throw if not
if(-not (Get-Command "Invoke-MsBuild" -errorAction SilentlyContinue)){
throw ('Unable to install/load psbuild from [{0}]' -f $psbuildInstallUri)
}
}
}
function EnsureNuGetPowerShellInstlled{
[cmdletbinding()]
param(
[string]$installUri = 'https://raw.githubusercontent.com/ligershark/nuget-powershell/master/get-nugetps.ps1'
)
process{
if(-not (Get-Command -Name Get-NuGetPackage -Module nuget-powershell -errorAction SilentlyContinue)){
'Installing nuget-powershell from [{0}]' -f $installUri | Write-Verbose
(new-object Net.WebClient).DownloadString($installUri) | iex
}
# make sure it's loaded and throw if not
if(-not (Get-Command -Name Get-NuGetPackage -Module nuget-powershell -errorAction SilentlyContinue)){
throw ('Unable to install/load nuget-powershell from [{0}]' -f $installUri)
}
}
}
function EnsureFileReplacerInstlled{
[cmdletbinding()]
param()
begin{
EnsureNuGetPowerShellInstlled
}
process{
if(-not (Get-Command -Module file-replacer -Name Replace-TextInFolder -errorAction SilentlyContinue)){
$fpinstallpath = (Get-NuGetPackage -name file-replacer -version '0.4.0-beta' -binpath)
if(-not (Test-Path $fpinstallpath)){ throw ('file-replacer folder not found at [{0}]' -f $fpinstallpath) }
Import-Module (Join-Path $fpinstallpath 'file-replacer.psm1') -DisableNameChecking
}
# make sure it's loaded and throw if not
if(-not (Get-Command -Module file-replacer -Name Replace-TextInFolder -errorAction SilentlyContinue)){
throw ('Unable to install/load file-replacer')
}
}
}
function Initalize{
[cmdletbinding()]
param()
process{
EnsurePsbuildInstlled
EnsureNuGetPowerShellInstlled
Update-NuGet
# load pester
Import-Pester -pesterVersion '3.3.11'
}
}
[hashtable]$buildProperties = @{
'Configuration'=$configuration
'DeployExtension'='false'
'OutputPath'=$outputPath.FullName
'VisualStudioVersion'='14.0'
}
function Build-Projects{
[cmdletbinding()]
param()
process {
$env:IsDeveloperMachine=$true
if($outputPath -eq $null){throw 'outputpath is null'}
$projectToBuild = Join-Path $scriptDir 'projects\Geoffrey.sln'
if(-not (Test-Path $projectToBuild)){
throw ('Could not find the project to build at [{0}]' -f $projectToBuild)
}
if(-not (Test-Path $OutputPath)){
'Creating output folder [{0}]' -f $outputPath | Write-Output
New-Item -Path $outputPath -ItemType Directory
}
Invoke-MSBuild $projectToBuild -properties $buildProperties
# copy other files to the output folder
[System.IO.FileInfo[]]$filesToCopy = "$scriptDir\geoffrey.nuspec","$scriptDir\geoffrey.psm1","$scriptDir\LICENSE","$scriptDir\readme.md"
Copy-Item -Path $filesToCopy -Destination $outputPath
[System.IO.DirectoryInfo]$modsFolder = (Join-Path $scriptDir 'gmodules')
$destModsFolder = (Join-Path $outputPath 'gmodules')
if(Test-Path $destModsFolder){
Remove-Item $destModsFolder -Recurse
}
Copy-Item ($modsFolder.FullName) -Destination $outputPath -Recurse
}
}
function Build-NuGetPackage{
[cmdletbinding()]
param()
process{
if(-not (Test-Path $outputPathNuget)){
New-Item -Path $outputPathNuget -ItemType Directory
}
Push-Location
try{
[System.IO.FileInfo[]]$nuspecFilesToBuild = @()
$nuspecFilesToBuild += (Join-Path $outputPath 'geoffrey.nuspec')
$nuspecFilesToBuild += (Get-ChildItem -Path (Join-Path $outputPath 'gmodules') *.nuspec -Recurse -File)
foreach($nufile in $nuspecFilesToBuild){
Push-Location
try{
Set-Location -Path ($nufile.Directory.FullName)
'Building nuget package for [{0}]' -f ($nufile.FullName) | Write-Verbose
Invoke-CommandString -command (Get-Nuget) -commandArgs @('pack',($nufile.Name),'-NoPackageAnalysis','-OutputDirectory',($outputPathNuget.FullName))
}
finally{
Pop-Location
}
}
}
finally{
Pop-Location
}
}
}
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,$nugetApiKey,'-NonInteractive')
'Publishing nuget package with the following args: [nuget.exe {0}]' -f ($cmdArgs -join ' ') | Write-Verbose
&(Get-Nuget) $cmdArgs
}
}
}
function Clean{
[cmdletbinding()]
param()
process {
[System.IO.FileInfo]$projectToBuild = Join-Path $scriptDir 'projects\Geoffrey.sln'
if(-not (Test-Path $projectToBuild)){
throw ('Could not find the project to build at [{0}]' -f $projectToBuild)
}
Invoke-MSBuild $projectToBuild -targets Clean -properties $buildProperties
[System.IO.DirectoryInfo[]]$foldersToDelete = (Get-ChildItem $scriptDir -Include bin,obj -Recurse -Directory)
$foldersToDelete += $outputPath
foreach($folder in $foldersToDelete){
if(Test-Path $folder){
Remove-Item $folder -Recurse -Force
}
}
}
}
# TODO: Figure out a way to run the tests in a new powershell session
# so that the watch assembly can be unloaded
function Run-Tests{
[cmdletbinding()]
param()
process{
try{
Push-Location
Set-Location (Join-Path $scriptDir 'tests')
$pesterArgs = @{
'-PassThru' = $true
}
if($env:ExitOnPesterFail -eq $true){
$pesterArgs.Add('-EnableExit',$true)
}
if($env:PesterEnableCodeCoverage -eq $true){
$pesterArgs.Add('-CodeCoverage','..\geoffrey.psm1')
}
$pesterResult = (Invoke-Pester @pesterArgs)
# TODO: Check the result to see if there was a failure or not
# if($pesterResult.FailedCount -gt 0){
# throw ('Failed test cases: {0}' -f $pesterResult.FailedCount)
# }
}
finally{
Pop-Location
}
}
}
function Update-FilesWithCommitId{
[cmdletbinding()]
param(
[string]$commitId = ($env:APPVEYOR_REPO_COMMIT),
[Parameter(Position=2)]
[string]$filereplacerVersion = '0.4.0-beta'
)
begin{
EnsureFileReplacerInstlled
}
process{
if(![string]::IsNullOrWhiteSpace($commitId)){
'Updating commitId from [{0}] to [{1}]' -f '$(COMMIT_ID)',$commitId | Write-Verbose
$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
}
}
}
<#
.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 'geoffrey.nuspec')
)
process{
([xml](Get-Content $nuspecFile)).package.metadata.version
}
}
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.4.0-beta'
)
begin{
EnsureFileReplacerInstlled
}
process{
$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);"
$exclude += ';build.ps1'
$replacements = @{
"$oldversion"="$newversion"
}
Replace-TextInFolder -folder $folder -include $include -exclude $exclude -replacements $replacements | Write-Verbose
# update the .psd1 file if there is one
$replacements = @{
($oldversion.Replace('-beta','.1'))=($newversion.Replace('-beta','.1'))
}
Replace-TextInFolder -folder $folder -include '*.psd1' -exclude $exclude -replacements $replacements | Write-Verbose
'Replacement complete' | Write-Verbose
}
}
function OpenCiWebsite{
[cmdletbinding()]
param()
process{
start 'https://ci.appveyor.com/project/sayedihashimi/geoffrey'
}
}
function Build-All{
[cmdletbinding()]
param()
process{
Update-FilesWithCommitId
Build-Projects
if(-not $notests){
Run-Tests
}
else{
$importscriptpath = (Join-Path $scriptDir 'tests\import-geoffrey.ps1')
. $importscriptpath
}
Build-NuGetPackage
# publish to nuget if selected
if($publishToNuget){
(Get-ChildItem -Path ($outputPathNuget) 'geoffrey*.nupkg').FullName | PublishNuGetPackage -nugetApiKey $nugetApiKey
}
}
}
# being script
Initalize
if(!$build -and !$setversion -and !$getversion -and !$openciwebsite){
$build = $true
}
try{
if($build){ Build-All }
elseif($setversion){ SetVersion -newversion $newversion }
elseif($getversion){ GetExistingVersion | Write-Output }
elseif($openciwebsite){ OpenCiWebsite }
else{
$cmds = @('-build','-setversion','-getversion','-openciwebsite')
'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
}