-
Notifications
You must be signed in to change notification settings - Fork 28
/
build.ps1
291 lines (245 loc) · 8.81 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
[cmdletbinding(DefaultParameterSetName ='build',SupportsShouldProcess = $true)]
param(
[Parameter(Position=0)]
[Parameter(ParameterSetName='build')]
[string]$configuration = 'Release',
[Parameter(ParameterSetName='build')]
[string]$visualStudioVersion='12.0',
[Parameter(ParameterSetName='build')]
[switch]$cleanBeforeBuild,
[Parameter(ParameterSetName='build')]
[bool]$installPsbuildIfMissing = $true,
[Parameter(ParameterSetName='build')]
[string]$psbuildInstallUrl='https://raw.github.com/ligershark/psbuild/master/src/GetPSBuild.ps1',
[Parameter(ParameterSetName='build')]
[Parameter(ParameterSetName='publishToNuGet')]
[switch]$publishTemplateBuilderToNuget,
[Parameter(ParameterSetName='build')]
[Parameter(ParameterSetName='publishToNuGet')]
[switch]$publishFileReplacerToNuget,
[Parameter(ParameterSetName='build')]
[switch]$updateNuget,
[Parameter(ParameterSetName='publishToNuGet')]
[string]$nugetApiKey = ($env:NuGetApiKey),
[Parameter(ParameterSetName='setBuildProps')]
[switch]$setMSBuildOverrides,
[Parameter(ParameterSetName='clearBuildProps')]
[switch]$clearMSBuildOverrides
)
function Get-ScriptDirectory{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
$scriptDir = ((Get-ScriptDirectory) + "\")
$buildproj = (get-item (Join-Path $scriptDir 'main-build.proj'))
function Filter-String{
[cmdletbinding()]
param(
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)]
[string[]]$message
)
process{
foreach($msg in $message){
if($nugetApiKey){
$msg = $msg.Replace($nugetApiKey,'REMOVED-FROM-LOG')
}
$msg
}
}
}
function Write-Message{
[cmdletbinding()]
param(
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)]
[string[]]$message
)
process{
Filter-String -message $message | Write-Verbose
}
}
<#
.SYNOPSIS
This will throw an error if the psbuild module is not installed and available.
#>
function EnsurePsbuildInstalled{
[cmdletbinding()]
param(
[bool]$installPsbuildIfMissing,
[string]$psbuildInstallUrl='https://raw.github.com/ligershark/psbuild/master/src/GetPSBuild.ps1'
)
process{
if($installPsbuildIfMissing -and !(Get-Module -listAvailable 'psbuild')){
'Attempting to download psbuild install script from [{0}]' -f $psbuildInstallUrl | Write-Message
(new-object Net.WebClient).DownloadString($psbuildInstallUrl) | iex
}
if(!(Get-Module -listAvailable 'psbuild')){
$msg = ('psbuild is required for this script, but it does not look to be installed. Get psbuild from here: https://aka.ms/psbuild')
throw $msg
}
if(!(Get-Module 'psbuild')){
# add psbuild to the currently loaded session modules
import-module psbuild -Global;
}
}
}
<#
.SYNOPSIS
This will return the path to nuget.exe. If it is not in the tools
folder then it will be downloaded there and then the path will
be returned.
#>
function Get-Nuget{
[cmdletbinding()]
param(
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\"),
$nugetDownloadUrl = 'http://nuget.org/nuget.exe'
)
process{
$nugetDestPath = Join-Path -Path $toolsDir -ChildPath nuget.exe
if(!(Test-Path $nugetDestPath)){
$nugetDir = ([System.IO.Path]::GetDirectoryName($nugetDestPath))
if(!(Test-Path $nugetDir)){
New-Item -Path $nugetDir -ItemType Directory | Out-Null
}
'Downloading nuget.exe' | Write-Message
(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
}
}
function PublishNuGetPackage{
[cmdletbinding(SupportsShouldProcess=$true)]
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string[]]$nugetPackage,
[Parameter(Mandatory=$true)]
$nugetApiKey,
[Parameter()]
$source = 'https://www.nuget.org/api/v2/package'
)
process{
foreach($pkg in $nugetPackage){
$pkgPath = (get-item $pkg).FullName
$cmdArgs = @('push',$pkgPath,$nugetApiKey,'-source',$source,'-NonInteractive')
'Publishing nuget package [{0}]' -f $pkgPath | Write-Message
$filteredCmd = Filter-String ('Publishing nuget package with the following args: [nuget.exe {0}]' -f ($cmdArgs -join ' '))
if($PSCmdlet.ShouldProcess($env:COMPUTERNAME, $filteredCmd)){
&(Get-Nuget) $cmdArgs
if($LASTEXITCODE -ne 0){
throw ('nuget.exe failed with the following error code [{0}]' -f $LASTEXITCODE)
}
}
}
}
}
function Clean{
[cmdletbinding()]
param()
process{
'Clean started' | Write-Message
Invoke-MSBuild -projectsToBuild $buildproj.FullName -targets Clean -properties @{
'Configuration'=$configuration
'RestorePackages'='true'
}
}
}
function Build{
[cmdletbinding()]
param()
begin{
if($updateNuget){
& (Get-Nuget) update -self
}
}
process{
'Build started' | Write-Message
# MSBuild.exe main-build.proj /p:Configuration=Release /p:VisualStudioVersion=11.0 /p:RestorePackages=true /flp1:v=d;logfile=build.d.log /flp2:v=diag;logfile=build.diag.log
Push-Location
try{
'Restoring NuGet packages for directory [{0}]' -f (join-path $buildproj.Directory.FullName src) | Write-Host -ForegroundColor Green
Set-Location (join-path $buildproj.Directory.FullName src)
&(Get-Nuget) restore
}
finally{
Pop-Location
}
Invoke-MSBuild -projectsToBuild $buildproj.FullName -properties @{
'Configuration'=$configuration
'RestorePackages'='true'
'NugetExe'=(Get-Nuget)
}
}
}
function Set-MSBuildOverrides{
[cmdletbinding()]
param(
[System.IO.FileInfo]$targetsPath = (Join-Path $scriptDir 'tools\ligershark.templates.targets'),
[System.IO.DirectoryInfo]$tasksRoot = (Join-Path $scriptDir 'OutputRoot')
)
process{
'Setting msbuild override env vars' | Write-Output
' [TemplateBuilderTargets]=[{0}]' -f $targetsPath | Write-Output
' [ls-TasksRoot]=[{0}]' -f $tasksRoot | Write-Output
$env:TemplateBuilderTargets = $targetsPath
${env:ls-TasksRoot} = "$tasksRoot\"
}
}
function Clear-MSBuildOverrides{
[cmdletbinding()]
param()
process{
Remove-Item -Path env:TemplateBuilderTargets
Remove-Item -Path 'env:ls-TasksRoot'
}
}
if($setMSBuildOverrides){
Set-MSBuildOverrides
}
elseif($clearMSBuildOverrides){
Clear-MSBuildOverrides
}
else{
# Begin build script here
try{
EnsurePsbuildInstalled -psbuildInstallUrl $psbuildInstallUrl -installPsbuildIfMissing $installPsbuildIfMissing
if($cleanBeforeBuild -or $publishToNuget){
Clean
}
Build
$outputroot = (get-item (join-path ($buildproj.Directory.FullName) 'OutputRoot\')).FullName
if($env:APPVEYOR -eq $true){
$pkgs = (Get-ChildItem $outputroot *.nupkg)
foreach($pkg in $pkgs){
Push-AppveyorArtifact ($pkg.FullName) -FileName ($pkg.Name)
}
}
$nupkgToPublish = @()
if($publishTemplateBuilderToNuget){
$package = (Get-ChildItem $outputroot 'TemplateBuilder.*.nupkg')
if($package.count -gt 1){
throw ('Found more than one file [{0} found] matching ''TemplateBuilder.*.nupkg'' in the output folder [{1}] ' -f $package.count, $outputroot)
}
$nupkgToPublish += ($package.FullName)
}
if($publishFileReplacerToNuget){
$package = (Get-ChildItem $outputroot 'file-replacer.*.nupkg')
if($package.count -gt 1){
throw ('Found more than one file [{0} found] matching ''file-replacer.*.nupkg'' in the output folder [{1}] ' -f $package.count, $outputroot)
}
$nupkgToPublish += ($package.FullName)
}
if($nupkgToPublish.Length -gt 0){
'nupkgToPublish: [{0}]' -f $nupkgToPublish | Write-Verbose
# publish the nuget package
PublishNuGetPackage -nugetPackage $nupkgToPublish -nugetApiKey $nugetApiKey
}
}
catch{
throw ("An error has occurred.`nError: [{0}]" -f ($_.Exception))
}
}