This repository has been archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Build.ps1
233 lines (193 loc) · 7.26 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
[CmdletBinding()]
param (
[string]
$BuildOutput = 'BuildOutput',
[string]
$ResourcesFolder = 'DscResources',
[string]
$ConfigDataFolder = 'DscConfigData',
[String]
$Environment = $(
$branch = (&git @('rev-parse', '--abbrev-ref', 'HEAD'))
$branch = if ($branch -eq 'master') { 'Prod' } else { 'Dev' }
if (Test-Path -Path ".\$ConfigDataFolder\AllNodes\$branch") {
$branch
}
),
[String]
$RoleName,
[string]
$ConfigurationsFolder = 'DscConfigurations',
[string]
$TestFolder = 'Tests',
[ScriptBlock]
$Filter = {},
[int]
$MofCompilationTaskCount = 1,
[string]
$GalleryRepository = 'PSGallery',
[uri]
$GalleryProxy,
[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
Add-Type -AssemblyName System.Threading
$m = [System.Threading.Mutex]::new($false, 'DscBuildProcessMutex')
if ($RoleName) {
if ((Get-ChildItem -Path ".\$ConfigDataFolder\DSC_ConfigData\Roles" -Filter "$RoleName*" -ErrorAction SilentlyContinue)) {
Write-Output "Role found with name $RoleName"
}
else {
Write-Output "No role found with the given name $RoleName"
}
}
$env:BHBuildStartTime = Get-Date
Write-Host "Current Process ID is '$PID'"
#changing the path is required to make PSDepend run without internet connection. It is required to download nutget.exe once first:
#Invoke-WebRequest -Uri 'https://aka.ms/psget-nugetexe' -OutFile C:\ProgramData\Microsoft\Windows\PowerShell\PowerShellGet\nuget.exe -ErrorAction Stop
$pathElements = $env:Path -split ';'
$pathElements += 'C:\ProgramData\Microsoft\Windows\PowerShell\PowerShellGet'
$env:Path = $pathElements -join ';'
#cannot be a default parameter value due to https://github.com/PowerShell/PowerShell/issues/4688
if (-not $ProjectPath) {
$ProjectPath = $PSScriptRoot
}
if (-not ([System.IO.Path]::IsPathRooted($BuildOutput))) {
$BuildOutput = Join-Path -Path $ProjectPath -ChildPath $BuildOutput
}
$buildModulesPath = Join-Path -Path $BuildOutput -ChildPath 'Modules'
if (-not (Test-Path -Path $buildModulesPath)) {
$null = mkdir -Path $buildModulesPath -Force
}
$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
$psModulePathElemets = $env:PSModulePath -split ';'
if ($buildModulesPath -notin $psModulePathElemets) {
$env:PSModulePath = $psModulePathElemets -join ';'
$env:PSModulePath += ";$buildModulesPath"
}
#importing all resources from .build directory
Get-ChildItem -Path "$PSScriptRoot/.build" -Recurse -Include *.ps1 |
ForEach-Object {
Write-Verbose "Importing file $($_.BaseName)"
try {
. $_.FullName
}
catch { }
}
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
}
if ($MyInvocation.ScriptName -notlike '*Invoke-Build.ps1') {
if ($ResolveDependency -or $PSBoundParameters['ResolveDependency']) {
$PSBoundParameters.Remove('ResolveDependency')
$PSBoundParameters['DownloadResourcesAndConfigurations'] = $true
}
if ($Help) {
Invoke-Build ?
}
else {
$PSBoundParameters.Remove('Tasks') | Out-Null
Invoke-Build -Tasks $Tasks -File $MyInvocation.MyCommand.Path @PSBoundParameters
if ($MofCompilationTaskCount -gt 1) {
$global:splittedNodes = Split-Array -List $ConfigurationData.AllNodes -ChunkCount $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 = 'WaitForMutex',
'TestDscResources',
'LoadDatumConfigData',
# 'CompileDatumRsop',
'CompileRootConfiguration' #,
#'CompileRootMetaMof'
Filter = [scriptblock]::Create($filterString)
MofCompilationTaskCount = $MofCompilationTaskCount
ProjectPath = $ProjectPath
BuildOutput = $buildOutput
ResourcesFolder = $ResourcesFolder
ConfigDataFolder = $ConfigDataFolder
ConfigurationsFolder = $ConfigurationsFolder
TestFolder = $TestFolder
}
}
Build-Parallel $mofCompilationTasks
}
}
if (($Tasks -contains 'CompileRootConfiguration' -or $Tasks -contains 'CompileRootMetaMof') -or -not $Tasks) {
Invoke-Build -File "$ProjectPath\PostBuild.ps1"
}
$m.Dispose()
Write-Host "Created $((Get-ChildItem -Path "$BuildOutput\MOF" -Filter *.mof).Count) MOF files in '$BuildOutput/MOF'" -ForegroundColor Green
#Debug Output
Write-Host "------------------------------------" -ForegroundColor Magenta
Write-Host "PowerShell Variables" -ForegroundColor Magenta
Get-Variable | Out-String | Write-Host -ForegroundColor Magenta
Write-Host "------------------------------------" -ForegroundColor Magenta
Write-Host "Environment Variables" -ForegroundColor Magenta
Get-ChildItem env: | Out-String | Write-Host -ForegroundColor Magenta
Write-Host "------------------------------------" -ForegroundColor Magenta
return
}
if ($TaskHeader) {
Set-BuildHeader $TaskHeader
}
if ($MofCompilationTaskCount -gt 1) {
task . Init,
CleanBuildOutput,
SetPsModulePath,
DownloadDependencies,
TestConfigData,
VersionControl,
LoadDatumConfigData
}
else {
if (-not $Tasks) {
task . Init,
CleanBuildOutput,
SetPsModulePath,
TestConfigData,
VersionControl,
LoadDatumConfigData,
# CompileDatumRsop,
TestDscResources,
CompileRootConfiguration #,
# CompileRootMetaMof
}
else {
task . $Tasks
}
}
Write-Host "Running the folling tasks:" -ForegroundColor Magenta
${*}.All[-1].Jobs | ForEach-Object { "`t$_" } | Write-Host
Write-Host