-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Build.ps1
184 lines (138 loc) · 5.49 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
#requires -Version 5
[CmdletBinding()]
Param
(
$RootProjectDir,
$Configuration = 'Release',
$EnableFusionLog = $false,
$ModuleName = 'TfsCmdlets',
$ModuleAuthor = 'Igor Abade V. Leite',
$ModuleDescription = 'PowerShell Cmdlets for Azure DevOps and Team Foundation Server',
$Targets = @("Package"),
$RepoCreationDate = (Get-Date '2014-10-24'),
[switch] $SkipTests,
[switch] $SkipReleaseNotes,
[switch] $Incremental
)
Function Install-Dependencies {
$NugetPackages = @('GitVersion.CommandLine')
$PsModules = @('psake', 'PsScriptAnalyzer', 'VSSetup', 'powershell-yaml', 'ps1xmlgen', 'PlatyPS')
$script:PackagesDir = Join-Path $RootProjectDir 'packages'
Write-Verbose "Restoring missing dependencies. Packages directory: $PackagesDir"
Install-Nuget
Write-Verbose "Restoring NuGet package(s) ($($NugetPackages -join ', '))"
foreach ($pkg in $NugetPackages) {
Install-NugetPackage $pkg
}
Write-Verbose "Restoring PowerShell module(s) ($($PsModules -join ', '))"
foreach ($mod in $PsModules) {
Install-PsModule $mod
}
}
Function Install-Nuget {
Write-Verbose "Restoring Nuget client"
$BuildToolsDir = Join-Path $RootProjectDir 'BuildTools'
$script:NugetExePath = Join-Path $BuildToolsDir 'nuget.exe'
if (-not (Test-Path $PackagesDir -PathType Container)) {
mkdir $PackagesDir -Force | Write-Verbose
}
if (-not (Test-Path $BuildToolsDir -PathType Container)) {
mkdir $BuildToolsDir -Force | Write-Verbose
}
if (-not (Test-Path $NugetExePath -PathType Leaf)) {
Write-Verbose "Nuget.exe not found. Downloading from https://dist.nuget.org"
Invoke-WebRequest -Uri https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -OutFile $NugetExePath | Write-Verbose
}
else {
Write-Verbose "NuGet client found; Skipping..."
}
Write-Verbose "NugetExePath: $NugetExePath"
}
Function Install-NugetPackage($Package) {
Write-Verbose "Restoring NuGet package $Package"
$modulePath = Join-Path $RootProjectDir "packages/$Package"
if (-not (Test-Path "$modulePath/*" -PathType Leaf)) {
Write-Verbose "Package not found. Downloading from Nuget.org"
& $NugetExePath Install $Package -ExcludeVersion -OutputDirectory packages *>&1 | Write-Verbose
}
else {
Write-Verbose "NuGet package $Package found; Skipping..."
}
}
Function Install-PsModule($Module) {
Write-Verbose "Restoring module $Module"
if (-not (Get-Module $Module -ListAvailable)) {
if (-not (Get-PackageProvider -Name Nuget -ListAvailable -ErrorAction SilentlyContinue)) {
Write-Verbose "Installing required Nuget package provider in order to install modules from PowerShell Gallery"
Install-PackageProvider Nuget -Force -Scope CurrentUser | Write-Verbose
}
Install-Module $Module -Scope CurrentUser -Force | Write-Verbose
}
else {
Write-Verbose "PowerShell module $Module found; Skipping..."
}
}
try {
if (-not $RootProjectDir) {
$RootProjectDir = $PSScriptRoot
}
Write-Host "Building $ModuleName ($ModuleDescription)`n" -ForegroundColor Cyan
Write-Verbose "SolutionDir: $RootProjectDir"
Push-Location $RootProjectDir
# Restore/install dependencies
Write-Verbose "=== RESTORE DEPENDENCIES ==="
Install-Dependencies
# Set build name
Write-Verbose "=== SET BUILD NAME ==="
$GitVersionPath = Join-Path $RootProjectDir 'packages\gitversion.commandline\tools\GitVersion.exe'
$VersionMetadata = (& $GitVersionPath | ConvertFrom-Json)
$ProjectBuildNumber = ((Get-Date) - $RepoCreationDate).Days
$BuildName = $VersionMetadata.FullSemVer.Replace('+', "+$ProjectBuildNumber.")
$VersionMetadata | Write-Verbose
Write-Verbose "Outputting build name $BuildName to host"
Write-Host "- Build $BuildName`n" -ForegroundColor Cyan
$isCI = $false
if ($env:BUILD_BUILDURI) {
Write-Output "##vso[build.updatebuildnumber]$BuildName"
$isCI = $true
}
elseif ($env:GITHUB_ACTIONS) {
Write-Output "BUILD_NAME=$BuildName" >> $env:GITHUB_OUTPUT
$isCI = $true
}
# Run Psake
$IsVerbose = [bool] ($PSBoundParameters['Verbose'].IsPresent -or ($VerbosePreference -eq 'Continue'))
$psakeScript = (Resolve-Path 'psake.ps1')
Write-Verbose "=== BEGIN PSAKE ==="
Write-Verbose "Invoking Psake script $psakeScript"
Invoke-Psake -Nologo -BuildFile $psakeScript -TaskList $Targets -Verbose:$IsVerbose -ErrorAction SilentlyContinue `
-Parameters @{
RootProjectDir = $RootProjectDir
Configuration = $Configuration
ModuleName = $ModuleName
ModuleAuthor = $ModuleAuthor
ModuleDescription = $ModuleDescription
BuildName = $BuildName
BuildNumber = $ProjectBuildNumber
VersionMetadata = $VersionMetadata
SkipTests = $SkipTests.IsPresent
SkipReleaseNotes = $SkipReleaseNotes.IsPresent
Incremental = $Incremental.IsPresent
IsCI = $isCI
}
Write-Verbose "=== END PSAKE ==="
}
finally {
Pop-Location
}
if (-not $psake.build_success) {
foreach($logFile in (Get-ChildItem (Join-Path $RootProjectDir 'out/*.log')))
{
Write-Host -ForegroundColor Red @"
========== MSBUILD LOG ===========
$(Get-Content $logFile -Raw)
======== END MSBUILD LOG =========
"@
}
throw "Build failed. See log above for details."
}