-
Notifications
You must be signed in to change notification settings - Fork 41
/
VMware.vSphereDSC.build.ps1
278 lines (212 loc) · 10.7 KB
/
VMware.vSphereDSC.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
274
275
276
277
278
<#
Copyright (c) 2018-2021 VMware, Inc. All rights reserved
The BSD-2 license (the "License") set forth below applies to all parts of the Desired State Configuration Resources for VMware project. You may not use this file except in compliance with the License.
BSD-2 License
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#>
$script:ModuleRoot = $PSScriptRoot
$script:ProjectRoot = (Get-Item -Path $script:ModuleRoot).Parent.Parent.FullName
$script:ModuleName = Split-Path -Path $script:ModuleRoot -Leaf
$script:PsmPath = Join-Path -Path $script:ModuleRoot -ChildPath "$($script:ModuleName).psm1"
$script:PsdPath = Join-Path -Path $script:ModuleRoot -ChildPath "$($script:ModuleName).psd1"
$script:LicensePath = Join-Path -Path $script:ProjectRoot -ChildPath "LICENSE.txt"
# This is used to skip the lines from LICENSE.txt containing the repository name and the empty line after it.
$script:LicenseSkipLines = 2
$script:LicenseFileContent = Get-Content -Path $script:LicensePath | Select-Object -Skip $script:LicenseSkipLines
$script:EnumsFolder = Join-Path -Path $script:ModuleRoot -ChildPath "Enums"
$script:ClassesFolder = Join-Path -Path $script:ModuleRoot -ChildPath "Classes"
$script:ClassesFiles = Get-ChildItem -Path $script:ClassesFolder -File -Filter *.ps1 -Recurse
$script:DSCResourcesFolder = Join-Path -Path $script:ModuleRoot -ChildPath "DSCResources"
class Node {
[string] $Name
[string] $BaseName
[string] $FileName
[Node[]] $Edge
[void] AddEdge($edge) {
if ($null -eq $this.Edge) {
$this.Edge = @()
}
$this.Edge += $edge
}
}
function Add-DependenciesBetweenClasses {
[CmdletBinding()]
param (
[System.Object[]] $Files
)
$allClasses = @()
foreach ($file in $Files) {
$fileContent = Get-Content -Path $file.FullName
$errors = $null
$tokens = [System.Management.Automation.PSParser]::Tokenize($fileContent, [ref] $errors)
$classTokens = $tokens | Where-Object { $_.Content -eq 'class' }
foreach ($classToken in $classTokens) {
$classLine = $fileContent[$classToken.StartLine - 1]
$classLine = $classLine.Substring('class '.Length)
$classLineBaseInheritor = $classLine.Split(':').Split('{') | ForEach-Object { $_.Trim() }
$baseName = $null
$name = $null
# If the class does not have a base class that inherits from, the array will contain only two values: <class name> and <empty string>
if ($classLineBaseInheritor.Length -gt 2) {
$name = $classLineBaseInheritor[0]
$baseName = $classLineBaseInheritor[1]
}
else {
$name = $classLineBaseInheritor[0]
}
$currentClass = [Node]::new()
$currentClass.Name = $name
$currentClass.BaseName = $baseName
$currentClass.FileName = $file.Name
$allClasses += $currentClass
}
}
foreach ($class in $allClasses) {
if (![string]::IsNullOrEmpty($class.BaseName)) {
$baseClass = $allClasses | Where-Object { $_.Name -eq $class.BaseName }
$class.AddEdge($baseClass)
}
}
return $allClasses
}
function Get-OrderedClasses {
[CmdletBinding()]
param (
[Node] $Class,
[ref] $ResolvedClasses
)
foreach ($edge in $Class.Edge) {
$containedEdge = $ResolvedClasses.Value | Where-Object { $_.Name -eq $edge.Name }
if ($null -eq $containedEdge) {
Get-OrderedClasses -Class $edge -ResolvedClasses ([ref] $ResolvedClasses.Value)
}
}
$containedClass = $ResolvedClasses.Value | Where-Object { $_.Name -eq $Class.Name }
if ($null -eq $containedClass) {
$ResolvedClasses.Value.Add($Class)
}
}
function Get-OrderedFiles {
[CmdletBinding()]
param (
[System.Object[]] $Files
)
$classes = Add-DependenciesBetweenClasses -Files $Files
$resolvedClasses = New-Object System.Collections.Generic.List[Node]
foreach ($class in $classes) {
Get-OrderedClasses -Class $class -ResolvedClasses ([ref] $resolvedClasses)
}
$orderedFiles = @()
foreach ($class in $resolvedClasses) {
$containedFile = $orderedFiles | Where-Object { $_.Name -eq $class.FileName }
if ($null -eq $containedFile) {
$file = $Files | Where-Object { $_.Name -eq $class.FileName }
$orderedFiles += $file
}
}
return $orderedFiles
}
function Get-LinesRange {
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param(
[System.Object[]] $FileContent,
[string] $StartLinePattern,
[string] $EndLinePattern
)
$range = @{}
$index = 1
$startLine = 0
$endLine = 0
foreach ($line in $FileContent) {
if ($line -Like $StartLinePattern) {
$startLine = $index
}
if ($line.Trim().EndsWith($EndLinePattern) -and $startLine -ne 0) {
$endLine = $index
break
}
$index++
}
$range.StartLine = $startLine
$range.EndLine = $endLine
return $range
}
function Update-ContentOfModuleFile {
[CmdletBinding()]
param(
[string] $Folder,
[System.Object[]] $Files
)
if (Test-Path -Path $Folder) {
if ($null -eq $Files) {
$Files = Get-ChildItem -Path $Folder -File -Filter *.ps1 -Recurse
}
foreach ($file in $Files) {
$fileContent = Get-Content -Path $file.FullName
$range = Get-LinesRange -FileContent $fileContent -StartLinePattern '*Copyright*' -EndLinePattern '#>'
$endLine = $range.EndLine
# We skip the comment lines for the License and the License text for each file.
$fileContent = $fileContent[$endLine..($fileContent.Length - 1)]
$fileContent | ForEach-Object { $_ | Out-File -FilePath $script:PsmPath -Encoding Default -Append }
}
}
}
function Update-ModuleVersion {
[CmdletBinding()]
[OutputType([System.Object[]])]
param(
[System.Object[]] $FileContent
)
$moduleVersionPattern = "(?<=ModuleVersion = ')(\d*.\d*.\d*.\d*)"
$moduleVersionMatch = $FileContent | Select-String -Pattern $moduleVersionPattern
[System.Version] $currentVersion = $moduleVersionMatch.Matches[0].Value
$newVersion = (New-Object -TypeName 'System.Version' $currentVersion.Major, $currentVersion.Minor, $currentVersion.Build, ($currentVersion.Revision + 1)).ToString()
return $FileContent -Replace $moduleVersionPattern, $newVersion
}
# Add License to psm1 file.
"<#" | Out-File -FilePath $script:PsmPath -Encoding Default
$script:LicenseFileContent | ForEach-Object { $_ | Out-File -FilePath $script:PsmPath -Encoding Default -Append }
"#>" + [System.Environment]::NewLine | Out-File -FilePath $script:PsmPath -Encoding Default -Append
# Add helper module to psm1 file.
"Using module '.\VMware.vSphereDSC.Helper.psm1'" | Out-File -FilePath $script:PsmPath -Encoding Default -Append
# Add logging module to psm1 file.
"Using module '.\VMware.vSphereDSC.Logging.psm1'" | Out-File -FilePath $script:PsmPath -Encoding Default -Append
# Updating VMware.vSphereDSC.psm1 content with enums.
Update-ContentOfModuleFile -Folder $script:EnumsFolder
<#
Updating VMware.vSphereDSC.psm1 content with classes.
The files need to be ordered by the inheritance of the classes inside and not alphabetically.
Because we can have cases where the child class is defined before the parent class which will result in an exception.
Example:
class DatastoreInventory : Inventory {
}
class Inventory {
}
So with this function we first order the classes based on the inheritance(the first classes in the array are the ones that do not inherit from anything and then
after them we order the classes that inherit from them and so on). And the last classes in the array are the ones that are not base classes to any other class.
After we order the classes, we order the files based on the classes defined inside. With this step we guarantee that when a class is placed in the psm1 file, if the file has a
class it inherits from, that class will be already defined and no exception will be thrown like the above example.
After ordering the files we pass them to the Update-ContentOfModuleFile function to place the content in the module file.
#>
$orderedClassesFiles = Get-OrderedFiles -Files $script:ClassesFiles
Update-ContentOfModuleFile -Folder $script:ClassesFolder -Files $orderedClassesFiles
# Updating VMware.vSphereDSC.psm1 content with DSC Resources.
Update-ContentOfModuleFile -Folder $script:DSCResourcesFolder
# Updating VMware.vSphereDSC.psd1 content with DSC Resources to export.
if (Test-Path -Path $script:DSCResourcesFolder) {
$resources = (Get-ChildItem -Path $script:DSCResourcesFolder -File -Filter *.ps1 -Recurse | Select-Object -ExpandProperty BaseName) -join "', '"
$resources = "'{0}'" -f $resources
$dscResourcesToExport = "DscResourcesToExport = @($resources)"
$psdFileContent = Get-Content -Path $script:PsdPath
# Updating the module version in the psd1 file.
$psdFileContent = Update-ModuleVersion -FileContent $psdFileContent
$range = Get-LinesRange -FileContent $psdFileContent -StartLinePattern '*DscResourcesToExport*' -EndLinePattern ')'
$startLine = $range.StartLine
$endLine = $range.EndLine
$psdFileContent = $psdFileContent[0..($startLine - 2)], $dscResourcesToExport, $psdFileContent[$endLine..($psdFileContent.Length - 1)]
$psdFileContent | Out-File -FilePath $script:PsdPath -Encoding Default
}