-
Notifications
You must be signed in to change notification settings - Fork 17
/
build.ps1
244 lines (175 loc) · 6.47 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
<#
.SYNOPSIS
This is a Powershell script to bootstrap a Cake build.
.DESCRIPTION
This Powershell script will download NuGet if missing, restore NuGet tools (including Cake)
and execute your Cake build script with the parameters you provide.
.LINK
https://cakebuild.net
.PARAMETER Script
The build script file to run.
.PARAMETER Tools
The tools directory to use.
.PARAMETER Target
The build script target to run.
.PARAMETER Configuration
The build configuration to use.
.PARAMETER Verbosity
Specifies the amount of information to be displayed.
.PARAMETER WhatIf
Performs a dry run of the build script.
No tasks will be executed.
.PARAMETER ScriptArgs
Remaining arguments are added here.
#>
###########################################################################
# Define Parameters
###########################################################################
[CmdletBinding()]
Param(
[string]$Script = "build.cake",
[string]$Tools,
[string]$Target = "Default",
[ValidateSet("Release", "Debug")]
[string]$Configuration = "Release",
[ValidateSet("Quiet", "Minimal", "Normal", "Verbose", "Diagnostic")]
[string]$Verbosity = "Verbose",
[switch]$WhatIf,
[Parameter(Position=0,Mandatory=$false,ValueFromRemainingArguments=$true)]
[string[]]$ScriptArgs
)
$CakeVersion = "1.0.0"
$DotNetChannel = "Current";
$DotNetVersion = "5.0.103";
$DotNetInstallerUri = "https://dot.net/v1/dotnet-install.ps1";
$NugetUrl = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
# Temporarily skip verification and opt-in to new in-proc NuGet
$ENV:CAKE_SETTINGS_SKIPPACKAGEVERSIONCHECK='true'
# Use TLS 1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
###########################################################################
# Define Paths
###########################################################################
# Get Script root
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
# Find tools
if($Tools)
{
# Parameter
Write-Verbose -Message "Using tools parameter"
$TOOLS_DIR = Join-Path $PSScriptRoot $Tools
}
elseif (Test-Path "C:/Tools/")
{
# Shared location
Write-Verbose -Message "Using shared tools"
$TOOLS_DIR = "C:/Tools"
}
else
{
# Local path
Write-Verbose -Message "Using local tools"
$TOOLS_DIR = Join-Path $PSScriptRoot "tools"
}
# Make sure tools folder exists
if (!(Test-Path $TOOLS_DIR))
{
Write-Verbose "Creating tools directory..."
New-Item -Path $TOOLS_DIR -Type directory | out-null
}
# Define directories
$ADDINS_DIR = Join-Path $TOOLS_DIR "/Addins"
$MODULES_DIR = Join-Path $TOOLS_DIR "/Modules"
Write-Verbose -Message $ADDINS_DIR
# Save paths to environment for use in child processes
$ENV:CAKE_PATHS_TOOLS = $TOOLS_DIR
$ENV:CAKE_PATHS_ADDINS = $ADDINS_DIR
$ENV:CAKE_PATHS_MODULES = $MODULES_DIR
###########################################################################
# INSTALL .NET CORE CLI
###########################################################################
Function Remove-PathVariable([string]$VariableToRemove)
{
$SplitChar = ';'
if ($IsMacOS -or $IsLinux) {
$SplitChar = ':'
}
$path = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($path -ne $null)
{
$newItems = $path.Split($SplitChar, [StringSplitOptions]::RemoveEmptyEntries) | Where-Object { "$($_)" -inotlike $VariableToRemove }
[Environment]::SetEnvironmentVariable("PATH", [System.String]::Join($SplitChar, $newItems), "User")
}
$path = [Environment]::GetEnvironmentVariable("PATH", "Process")
if ($path -ne $null)
{
$newItems = $path.Split($SplitChar, [StringSplitOptions]::RemoveEmptyEntries) | Where-Object { "$($_)" -inotlike $VariableToRemove }
[Environment]::SetEnvironmentVariable("PATH", [System.String]::Join($SplitChar, $newItems), "Process")
}
}
# Get .NET Core CLI path if installed.
$FoundDotNetCliVersion = $null;
if (Get-Command dotnet -ErrorAction SilentlyContinue)
{
$FoundDotNetCliVersion = dotnet --version;
}
if($FoundDotNetCliVersion -ne $DotNetVersion)
{
$InstallPath = Join-Path $TOOLS_DIR "DotNet"
if (!(Test-Path $InstallPath)) {
New-Item -Path $InstallPath -ItemType Directory -Force | Out-Null;
}
if ($IsMacOS -or $IsLinux) {
$ScriptPath = Join-Path $InstallPath 'dotnet-install.sh'
(New-Object System.Net.WebClient).DownloadFile($DotNetUnixInstallerUri, $ScriptPath);
& bash $ScriptPath --version "$DotNetVersion" --install-dir "$InstallPath" --channel "$DotNetChannel" --no-path
Remove-PathVariable "$InstallPath"
$env:PATH = "$($InstallPath):$env:PATH"
}
else {
$ScriptPath = Join-Path $InstallPath 'dotnet-install.ps1'
(New-Object System.Net.WebClient).DownloadFile($DotNetInstallerUri, $ScriptPath);
& $ScriptPath -Channel $DotNetChannel -Version $DotNetVersion -InstallDir $InstallPath;
Remove-PathVariable "$InstallPath"
$env:PATH = "$InstallPath;$env:PATH"
}
$env:DOTNET_ROOT=$InstallPath
}
###########################################################################
# INSTALL NUGET
###########################################################################
# Make sure nuget.exe exists.
$NugetPath = Join-Path $TOOLS_DIR "nuget.exe"
if (!(Test-Path $NugetPath))
{
Write-Host "Downloading NuGet.exe..."
(New-Object System.Net.WebClient).DownloadFile($NugetUrl, $NugetPath);
}
###########################################################################
# INSTALL CAKE
###########################################################################
# Make sure Cake has been installed.
$CakePath = Join-Path $TOOLS_DIR "Cake.$CakeVersion/Cake.exe"
if (!(Test-Path $CakePath))
{
Write-Host "Installing Cake..."
Invoke-Expression "&`"$NugetPath`" install Cake -Version $CakeVersion -OutputDirectory `"$TOOLS_DIR`"" | Out-Null;
if ($LASTEXITCODE -ne 0)
{
Throw "An error occured while restoring Cake from NuGet."
}
}
###########################################################################
# RUN BUILD SCRIPT
###########################################################################
# Build the argument list.
$Arguments = @{
target=$Target;
configuration=$Configuration;
verbosity=$Verbosity;
dryrun=$WhatIf;
}.GetEnumerator() | %{"--{0}=`"{1}`"" -f $_.key, $_.value };
# Start Cake
Write-Host "Running build script..."
Invoke-Expression "& `"$CakePath`" `"$Script`" $Arguments $ScriptArgs"
exit $LASTEXITCODE