forked from dsccommunity/DscWorkshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.build.ps1
229 lines (188 loc) · 7.58 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
Param (
[String]
$BuildOutput = "BuildOutput",
[String]
$ResourcesFolder = "DSC_Resources",
[string]
$ConfigDataFolder = 'DSC_ConfigData',
[String]
$ConfigurationsFolder = "DSC_Configurations",
$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 = [switch]$true,
[Parameter(Position = 0)]
$Tasks,
[switch]
$ResolveDependency,
$ProjectPath = $BuildRoot,
[switch]
$DownloadResourcesAndConfigurations,
[switch]
$Help,
$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)"
''
}
)
begin {
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
Write-Host "Working in branch '$(&git @('rev-parse', '--abbrev-ref', 'HEAD'))' and configuration environment '$Environment'"
if (!([io.path]::IsPathRooted($BuildOutput))) {
$BuildOutput = Join-Path $PSScriptRoot $BuildOutput
}
$BuildModulesPath = Join-Path $BuildOutput 'modules'
if (!(test-Path $BuildModulesPath)) {
$null = mkdir $BuildModulesPath -force
}
if ($BuildModulesPath -notin ($Env:PSModulePath -split ';') ) {
$Env:PSmodulePath = $BuildModulesPath + ';' + $Env:PSmodulePath
}
function Resolve-Dependency {
[CmdletBinding()]
param()
if (!(Get-PackageProvider -Name NuGet -ForceBootstrap)) {
$providerBootstrapParams = @{
Name = 'nuget'
force = $true
ForceBootstrap = $true
}
if ($PSBoundParameters.ContainsKey('verbose')) { $providerBootstrapParams.add('verbose', $verbose)}
if ($GalleryProxy) { $providerBootstrapParams.Add('Proxy', $GalleryProxy) }
$null = Install-PackageProvider @providerBootstrapParams
#Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
}
Write-verbose "BootStrapping PSDepend"
"Parameter $BuildOutput"| Write-verbose
$InstallPSDependParams = @{
Name = 'PSDepend'
Path = $BuildModulesPath
Confirm = $false
}
if ($PSBoundParameters.ContainsKey('verbose')) { $InstallPSDependParams.add('verbose', $verbose)}
if ($GalleryRepository) { $InstallPSDependParams.Add('Repository', $GalleryRepository) }
if ($GalleryProxy) { $InstallPSDependParams.Add('Proxy', $GalleryProxy) }
if ($GalleryCredential) { $InstallPSDependParams.Add('ProxyCredential', $GalleryCredential) }
Save-Module @InstallPSDependParams
$PSDependParams = @{
Force = $true
Path = "$PSScriptRoot\PSDepend.build.psd1"
ErrorAction = 'Stop'
}
if ($PSBoundParameters.ContainsKey('verbose')) { $PSDependParams.add('verbose', $verbose)}
Invoke-PSDepend @PSDependParams
Write-Verbose "Project Bootstrapped, returning to Invoke-Build"
}
if ($ResolveDependency) {
Resolve-Dependency
}
}
process {
if ($MyInvocation.ScriptName -notlike '*Invoke-Build.ps1') {
if ($ResolveDependency -or $PSBoundParameters.ContainsKey('ResolveDependency')) {
$PSBoundParameters.Remove('ResolveDependency')
$PSBoundParameters.Add('DownloadResourcesAndConfigurations', $true)
}
if ($Help) {
Invoke-Build ?
}
else {
Invoke-Build $Tasks $MyInvocation.MyCommand.Path @PSBoundParameters
}
return
}
Get-ChildItem -Path "$PSScriptRoot/.build/" -Recurse -Include *.ps1 -Verbose |
Foreach-Object {
"Importing file $($_.BaseName)" | Write-Verbose
. $_.FullName
}
if ($TaskHeader) { Set-BuildHeader $TaskHeader }
$Env:BuildVersion = $BuildVersion
$Env:Environment = $Environment
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
$ConfigurationPath = Join-Path $ProjectPath $ConfigurationsFolder
$ResourcePath = Join-Path $ProjectPath $ResourcesFolder
$ConfigDataPath = Join-Path $ProjectPath $ConfigDataFolder
if (!$TestFolder) {
$TestFolder = 'tests'
}
task Download_DSC_Resources {
$PSDependResourceDefinition = '.\PSDepend.DSC_resources.psd1'
if (Test-Path $PSDependResourceDefinition) {
Invoke-PSDepend -Path $PSDependResourceDefinition -Confirm:$False -Target $ResourcePath
}
}
task Download_DSC_Configurations {
$PSDependConfigurationDefinition = '.\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 Deployment {
Set-BuildEnvironment -VariableNamePrefix $null -ErrorAction SilentlyContinue
$Params = @{
Path = $PSScriptRoot
Force = $true
Recurse = $false
Verbose = $true
}
Invoke-PSDeploy @Params
}
task zip_modules_for_pull_server {
if (!([io.path]::IsPathRooted($BuildOutput))) {
$BuildOutput = Join-Path $PSScriptRoot $BuildOutput
}
Import-Module DscBuildHelpers -ErrorAction Stop
Get-ModuleFromfolder -ModuleFolder (Join-Path $ProjectPath $ResourcesFolder) |
Compress-DscResourceModule -DscBuildOutputModules (Join-Path $BuildOutput 'DscModules') -Verbose:$false 4>$null
}
task Test_configData {
if (!([io.path]::IsPathRooted($BuildOutput))) {
$BuildOutput = Join-Path $PSScriptRoot $BuildOutput
}
#Write-Host (Get-Module Datum,DscBuildHelpers,Pester,PSSscriptAnalyser,PSDeploy -ListAvailable | FT -a | Out-String)
$TestResults = Invoke-Pester -Script (Join-Path $BuildRoot $TestFolder) -PassThru -OutputFile ([io.path]::combine($BuildOutput, 'testresults.xml')) -OutputFormat NUnitXml
assert ($TestResults.FailedCount -eq 0)
}
}