-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path.Build.ps1
273 lines (231 loc) · 7.93 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
[CmdletBinding()]
param (
[String]
$buildOutput = 'BuildOutput',
[String]
$ResourcesFolder = 'DSC_Resources',
[String]
$ConfigDataFolder = 'DSC_ConfigData',
[String]
$ConfigurationsFolder = 'DSC_Configurations',
[String]
$TestFolder = 'Tests',
[ScriptBlock]
$Filter = {},
[int]$MofCompilationTaskCount,
[switch]$RandomWait,
$Environment = $(
$branch = $env:BranchName
$branch = if ($branch -eq 'master')
{ 'Prod'
}
else
{ 'Dev'
}
if (Test-Path -Path ".\$ConfigDataFolder\AllNodes\$branch")
{
$branch
}
else
{
'Dev'
}
),
$BuildVersion = $(
if ($gitshortid = (& git rev-parse --short HEAD))
{$gitshortid
}
else
{'0.0.0'
}
),
[String[]]
$GalleryRepository, #used in ResolveDependencies, has default
[Uri]
$GalleryProxy, #used in ResolveDependencies, $null if not specified
[Switch]
$ForceEnvironmentVariables = $true,
[Parameter(Position = 0)]
$Tasks,
[Switch]
$ResolveDependency,
[String]
$ProjectPath,
[Switch]
$DownloadResourcesAndConfigurations,
[Switch]
$Help,
[ScriptBlock]
$TaskHeader = {
Param($Path)
''
'=' * 79
Write-Build Cyan "`t`t`t$($Task.Name.Replace('_',' ').ToUpper())"
Write-Build DarkGray "$(Get-BuildSynopsis $Task)"
'-' * 79
Write-Build DarkGray " $Path"
Write-Build DarkGray " $($Task.InvocationInfo.ScriptName):$($Task.InvocationInfo.ScriptLineNumber)"
''
}
)
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
#cannot be a default parameter value due to https://github.com/PowerShell/PowerShell/issues/4688
if (-not $ProjectPath)
{
$ProjectPath = $PSScriptRoot
}
if (!([System.IO.Path]::IsPathRooted($buildOutput)))
{
$buildOutput = Join-Path -Path $ProjectPath -ChildPath $buildOutput
}
$buildModulesPath = Join-Path -Path $buildOutput -ChildPath 'Modules'
if (!(Test-Path -Path $buildModulesPath))
{
$null = mkdir -Path $buildModulesPath -Force
}
if ($buildModulesPath -notin ($Env:PSModulePath -split ';'))
{
$env:PSModulePath = "$buildModulesPath;$Env:PSModulePath"
}
if (-not (Get-Module -Name InvokeBuild -ListAvailable) -and -not $ResolveDependency)
{
Write-Error "Requirements are missing. Please call the script again with the switch 'ResolveDependency'"
return
}
if ($ResolveDependency)
{
. $PSScriptRoot/.build/BuildHelpers/Resolve-Dependency.ps1
Resolve-Dependency
}
Get-ChildItem -Path "$PSScriptRoot/.build/" -Recurse -Include *.ps1 |
ForEach-Object {
Write-Verbose "Importing file $($_.BaseName)"
try {
. $_.FullName
}
catch { }
}
if ($MyInvocation.ScriptName -notlike '*Invoke-Build.ps1')
{
if ($ResolveDependency -or $PSBoundParameters['ResolveDependency'])
{
$PSBoundParameters.Remove('ResolveDependency')
$PSBoundParameters['DownloadResourcesAndConfigurations'] = $true
}
if ($Help)
{
Invoke-Build ?
}
else
{
Invoke-Build -Tasks $Tasks -File $MyInvocation.MyCommand.Path @PSBoundParameters
if ($MofCompilationTaskCount)
{
$global:splittedNodes = Split-Array -List $ConfigurationData.AllNodes -ChunkCount $MofCompilationTaskCount
if ($MofCompilationTaskCount)
{
$mofCompilationTasks = foreach ($nodeSet in $global:splittedNodes)
{
$nodeNamesInSet = "'$($nodeSet.Name -join "', '")'"
$filterString = '$_.NodeName -in {0}' -f $nodeNamesInSet
$PSBoundParameters.Filter = [scriptblock]::Create($filterString)
@{
File = $MyInvocation.MyCommand.Path
Task = 'PSModulePath_BuildModules',
'Load_Datum_ConfigData',
'Compile_Datum_Rsop',
'Compile_Root_Configuration',
'Compile_Root_Meta_Mof'
Filter = [scriptblock]::Create($filterString)
RandomWait = $true
BuildOutput = $buildOutput
ResourcesFolder = $ResourcesFolder
ConfigDataFolder = $ConfigDataFolder
ConfigurationsFolder = $ConfigurationsFolder
TestFolder = $TestFolder
Environment = $Environment
}
}
Build-Parallel $mofCompilationTasks
}
}
return
}
}
if ($TaskHeader)
{
Set-BuildHeader $TaskHeader
}
if ($MofCompilationTaskCount)
{
task . Clean_BuildOutput,
Download_All_Dependencies,
PSModulePath_BuildModules,
Test_ConfigData,
Load_Datum_ConfigData
#Create_Mof_Checksums, # or use the meta-task: Compile_Datum_DSC,
#Zip_Modules_For_Pull_Server
}
else
{
task . Clean_BuildOutput,
#Download_All_Dependencies,
PSModulePath_BuildModules,
Test_ConfigData,
Load_Datum_ConfigData,
Compile_Datum_Rsop,
Compile_Root_Configuration,
Compile_Root_Meta_Mof
#Create_Mof_Checksums, # or use the meta-task: Compile_Datum_DSC,
#Zip_Modules_For_Pull_Server
#Deployment
}
task Download_All_Dependencies -if ($DownloadResourcesAndConfigurations -or $Tasks -contains 'Download_All_Dependencies') Download_DSC_Configurations, Download_DSC_Resources -Before PSModulePath_BuildModules
$configurationPath = Join-Path -Path $ProjectPath -ChildPath $ConfigurationsFolder
$resourcePath = Join-Path -Path $ProjectPath -ChildPath $ResourcesFolder
$configDataPath = Join-Path -Path $ProjectPath -ChildPath $ConfigDataFolder
$testsPath = Join-Path -Path $ProjectPath -ChildPath $TestFolder
task Download_DSC_Resources {
$PSDependResourceDefinition = "$ProjectPath\PSDepend.DSC_Resources.psd1"
if (Test-Path $PSDependResourceDefinition)
{
Invoke-PSDepend -Path $PSDependResourceDefinition -Confirm:$false -Target $resourcePath
}
}
task Download_DSC_Configurations {
$PSDependConfigurationDefinition = "$ProjectPath\PSDepend.DSC_Configurations.psd1"
if (Test-Path $PSDependConfigurationDefinition)
{
Write-Build Green 'Pull dependencies from PSDepend.DSC_Configurations.psd1'
Invoke-PSDepend -Path $PSDependConfigurationDefinition -Confirm:$false -Target $configurationPath
}
}
task Clean_DSC_Resources_Folder {
Get-ChildItem -Path "$ResourcesFolder" -Recurse | Remove-Item -Force -Recurse -Exclude README.md
}
task Clean_DSC_Configurations_Folder {
Get-ChildItem -Path "$ConfigurationsFolder" -Recurse | Remove-Item -Force -Recurse -Exclude README.md
}
task Zip_Modules_For_Pull_Server {
if (!([System.IO.Path]::IsPathRooted($buildOutput)))
{
$BuildOutput = Join-Path $PSScriptRoot -ChildPath $BuildOutput
}
Import-Module DscBuildHelpers -ErrorAction Stop
Get-ModuleFromfolder -ModuleFolder (Join-Path $ProjectPath -ChildPath $ResourcesFolder) |
Compress-DscResourceModule -DscBuildOutputModules (Join-Path $BuildOutput -ChildPath 'DscModules') -Verbose:$false 4>$null
}
task Test_ConfigData {
if (!(Test-Path -Path $testsPath))
{
Write-Build Yellow "Path for tests '$testsPath' does not exist"
return
}
if (!([System.IO.Path]::IsPathRooted($BuildOutput)))
{
$BuildOutput = Join-Path -Path $PSScriptRoot -ChildPath $BuildOutput
}
$testResultsPath = Join-Path -Path $BuildOutput -ChildPath TestResults.xml
$testResults = Invoke-Pester -Script $testsPath -PassThru -OutputFile $testResultsPath -OutputFormat NUnitXml
assert ($testResults.FailedCount -eq 0)
}