-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
122 lines (99 loc) · 4.39 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
[CmdletBinding()]
param (
[string]$Script = "build.cake",
[string]$Target,
[string]$Configuration,
[ValidateSet("Quiet", "Minimal", "Normal", "Verbose", "Diagnostic")]
[string]$Verbosity,
[switch]$ShowDescription,
[Alias("WhatIf", "Noop")]
[switch]$DryRun,
[switch]$Experimental,
[switch]$Mono,
[switch]$SkipToolPackageRestore,
[Parameter(Position=0,Mandatory=$false,ValueFromRemainingArguments=$true)]
[string[]]$ScriptArgs
)
function Parse-CakeDirectives([string] $scriptPath) {
$cakeDirectiveOptions = @{}
try {
foreach ($optionMatch in
select-string -Path $scriptPath '(?im)^[^\S\n\r]*(?://[^\S\n\r]*)?#cake[^\S\n\r]+(?<options>.*)$' -AllMatches |
foreach Matches |
foreach { $_.Groups['options'].Value } |
select-string '(?<key>version|dir|source)\s*=\s*(?<value>[^"\s]+|"([^"]|"")*")' -AllMatches |
foreach Matches |
where { $_ -ne $null }
) {
$optionKey = $optionMatch.Groups['key'].Value
if ($cakeDirectiveOptions.ContainsKey($optionKey)) {
throw "`"#cake $optionKey`" must only appear once in '$scriptPath'."
}
$optionValue = $optionMatch.Groups['value'].Value
if ($optionValue.StartsWith('"')) {
$optionValue = $optionValue.Substring(1, $optionValue.Length - 2).Replace('""', '"')
}
$cakeDirectiveOptions.Add($optionKey, $optionValue)
}
}
catch [System.Management.Automation.ItemNotFoundException] {
throw "Cannot find build script at '$scriptPath'."
}
return $cakeDirectiveOptions
}
function Extract-ZipFromUrlToDirectory([uri] $zipUrl, [string] $targetDir) {
Add-Type -assembly System.IO.Compression.FileSystem
if ($zipUrl.IsFile)
{
[System.IO.Compression.ZipFile]::ExtractToDirectory(
[System.IO.Path]::Combine((get-location), $zipUrl.LocalPath),
[System.IO.Path]::Combine((get-location), $targetDir))
}
else
{
Add-Type -assembly System.IO.Compression
$webClient = [System.Net.WebClient]::new()
$downloadStream = $webClient.OpenRead($zipUrl)
$zip = [System.IO.Compression.ZipArchive]::new($downloadStream)
[System.IO.Compression.ZipFileExtensions]::ExtractToDirectory($zip,
[System.IO.Path]::Combine((get-location), $targetDir))
$zip.Dispose()
$downloadStream.Dispose()
$webClient.Dispose()
}
}
function Ensure-CakeInstall([string] $targetDir, [version] $pinnedVersion, [uri] $nugetSource) {
$cakePath = join-path $targetDir 'Cake.exe'
if (test-path $cakePath)
{
if ($pinnedVersion -eq $null) { return $cakePath }
$rawVersion = (get-item $cakePath).VersionInfo.ProductVersion
$buildMetadataIndex = $rawVersion.IndexOf('+')
$currentVersion = if ($buildMetadataIndex -eq -1) { $rawVersion } else { $rawVersion.Substring(0, $buildMetadataIndex) }
if ($currentVersion -eq $pinnedVersion) { return $cakePath }
Write-Host "Cake $currentVersion found; replacing with pinned version $pinnedVersion. Downloading..."
}
$downloadUrl =
if ($nugetSource.IsFile) { "$nugetSource\Cake.$pinnedVersion.nupkg" }
else { "$nugetSource/package/Cake/$pinnedVersion" }
Remove-Item -Recurse -Force $targetDir -ErrorAction Ignore
Extract-ZipFromUrlToDirectory $downloadUrl $targetDir
return $cakePath
}
$cakeDirectiveOptions = Parse-CakeDirectives $Script
$pinnedVersion = $cakeDirectiveOptions.version
$targetDir = if ($cakeDirectiveOptions.dir -ne $null) { $cakeDirectiveOptions.dir } else { 'tools\Cake' }
$nugetSource = if ($cakeDirectiveOptions.source -ne $null) { $cakeDirectiveOptions.source } else { 'https://www.nuget.org/api/v2' }
$cakePath = Ensure-CakeInstall $targetDir $pinnedVersion $nugetSource
$cakeArguments = @($Script)
if ($Target) { $cakeArguments += "--target=$Target" }
if ($Configuration) { $cakeArguments += "--configuration=$Configuration" }
if ($Verbosity) { $cakeArguments += "--verbosity=$Verbosity" }
if ($ShowDescription) { $cakeArguments += "--showdescription" }
if ($DryRun) { $cakeArguments += "--dryrun" }
if ($Experimental) { $cakeArguments += "--experimental" }
if ($Mono) { $cakeArguments += "--mono" }
$cakeArguments += $ScriptArgs
Write-Host "Running build script..."
&$cakePath $cakeArguments
exit $LASTEXITCODE