-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathModule.build.ps1
206 lines (177 loc) · 7.31 KB
/
Module.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
$Script:ModuleName = 'AzurePipelinesPS'
$Script:Folders = 'Private', 'Public', 'Tests'
$Script:Output = Join-Path -Path $BuildRoot -ChildPath 'Output'
$Script:DocsPath = Join-Path -Path $BuildRoot -ChildPath 'Docs'
$Script:Destination = Join-Path -Path $Output -ChildPath $ModuleName
$Script:Source = Join-Path -Path $BuildRoot -ChildPath $ModuleName
$Script:ModulePath = Join-Path -Path $Destination -ChildPath "$ModuleName.psm1"
$Script:ManifestPath = Join-Path -Path $Destination -ChildPath "$ModuleName.psd1"
$Script:CatalogFilePath = Join-Path -Path $Destination -ChildPath "$ModuleName.cat"
$Script:TestsPath = Join-Path -Path $Source -ChildPath 'Tests'
$Script:TestFile = "$PSScriptRoot\Output\TestResults.xml"
task Default Clean, Build, Test, UpdateSourceManifest
task Build Copy, BuildModule, BuildManifest
task Test ImportModule, FullTests
task DevBuild Build, UpdateSourceManifest
task Clean {
Write-Output 'Cleaning Output directories...'
$null = Get-ChildItem -Path $Output -Directory -Recurse |
Remove-Item -Recurse -Force -ErrorAction 'Ignore'
}
task Copy {
Write-Output "Creating Directory [$Destination]..."
$null = New-Item -ItemType 'Directory' -Path $Destination -ErrorAction 'Ignore'
$files = Get-ChildItem -Path $Source -File |
Where-Object 'Name' -NotMatch "$ModuleName\.ps[dm]1"
foreach ($file in $files)
{
Write-Output "Creating [$($file.Name)]..."
Copy-Item -Path $file.FullName -Destination $Destination -Force
}
$directories = Get-ChildItem -Path $Source -Directory |
Where-Object 'Name' -NotIn $Folders
foreach ($directory in $directories)
{
Write-Output "Creating [$($directory.Name)]..."
Copy-Item -Path $directory.FullName -Destination $Destination -Recurse -Force
}
}
task BuildModule @{
Inputs = (Get-ChildItem -Path $Source -File -Filter '*.ps1' -Recurse)
Outputs = $ModulePath
Jobs = {
$sb = [Text.StringBuilder]::new()
[void] $sb.AppendLine('$Script:PSModuleRoot = $PSScriptRoot')
[void] $sb.AppendLine('$Script:ModuleName = "AzurePipelinesPS"')
[void] $sb.AppendLine('$Script:APAppDataPath = [Environment]::GetFolderPath(''ApplicationData'')')
[void] $sb.AppendLine('$Script:ModuleDataRoot = (Join-Path -Path $Script:APAppDataPath -ChildPath $Script:ModuleName)')
[void] $sb.AppendLine('$Script:ModuleDataPath = (Join-Path -Path $Script:ModuleDataRoot -ChildPath "ModuleData.json")')
[void] $sb.AppendLine('if (-not (Test-Path $Script:ModuleDataRoot)) {New-Item -ItemType Directory -Path $Script:ModuleDataRoot -Force}')
foreach ($folder in $Folders)
{
if (Test-Path -Path "$Source\$folder")
{
[void] $sb.AppendLine("# Imported from [$Source\$folder]")
$files = Get-ChildItem -Path "$Source\$folder\*" -Filter '*.ps1' -Exclude '*.Tests.ps1', '*.Pending.ps1'
foreach ($file in $files)
{
$name = $file.Name
"Importing [$name]..."
[void] $sb.AppendLine("# $name")
[void] $sb.AppendLine([IO.File]::ReadAllText($file.FullName))
}
}
}
Write-Output "Creating Module [$ModulePath]..."
Set-Content -Path $ModulePath -Value $sb.ToString() -Encoding 'UTF8'
}
}
task BuildManifest @{
Inputs = (Get-ChildItem -Path $Source -Recurse -File)
Outputs = $ManifestPath
Jobs = {
Write-Output "Building [$ManifestPath]..."
Copy-Item -Path "$Source\$ModuleName.psd1" -Destination $ManifestPath
$functions = Get-ChildItem -Path "$ModuleName\Public\*" -Filter '*.ps1' -Exclude '*.Tests.ps1', '*.Pending.ps1'
if ($env:BUILD_BUILDNUMBER)
{
Write-Output "Located Azure Pipelines build number, updating [$ManifestPath]..."
Update-ModuleManifest -Path $ManifestPath -FunctionsToExport $functions.BaseName -ModuleVersion $env:BUILD_BUILDNUMBER
}
else
{
Write-Output "Updating [$ManifestPath]..."
Update-ModuleManifest -Path $ManifestPath -FunctionsToExport $functions.BaseName
}
}
}
task ImportModule {
if (-not(Test-Path -Path $ManifestPath))
{
Write-Output "Module [$ModuleName] is not built; cannot find [$ManifestPath]."
Write-Error "Could not find module manifest [$ManifestPath]. You may need to build the module first." -ErrorAction Stop
}
else
{
$loaded = Get-Module -Name $ModuleName -All
if ($loaded)
{
Write-Output "Unloading Module [$ModuleName] from a previous import..."
$loaded | Remove-Module -Force
}
Write-Output "Importing Module [$ModuleName] from [$ManifestPath]..."
Import-Module -FullyQualifiedName $ManifestPath -Force
}
}
task FullTests {
Write-Output "Executing tests from [$Script:TestsPath]..."
$params = @{
OutputFile = $testFile
OutputFormat = 'NUnitXml'
Path = $Script:TestsPath
Show = 'All'
}
Invoke-Pester @params
}
task UpdateSourceManifest {
Copy-Item -Path $ManifestPath -Destination "$Source\$ModuleName.psd1"
}
task Install {
$version = [version] (Get-Metadata -Path $ManifestPath -PropertyName 'ModuleVersion')
$path = $env:PSModulePath.Split(';') | Select-Object -First 1
if ($path -and (Test-Path -Path $path))
{
$path = Join-Path -Path $path -ChildPath $ModuleName
$path = Join-Path -Path $path -ChildPath $version
Write-Output "Creating directory at [$path]"
$null = New-Item -Path $path -ItemType 'Directory' -Force -ErrorAction 'Ignore'
Write-Output "Copying items from [$Destination] to [$path]"
Copy-Item -Path "$Destination\*" -Destination $path -Recurse -Force
}
}
task Uninstall {
Write-Output 'Removing module from session'
Get-Module -Name $ModuleName -ErrorAction 'Ignore' | Remove-Module
$modules = Get-Module $ModuleName -ErrorAction 'Ignore' -ListAvailable
foreach ($module in $modules)
{
Write-Output "Uninstalling [$($module.Name)] version [$($module.Version)]"
Uninstall-Module -Name $module.Name -RequiredVersion $module.Version -Force
}
Write-Output 'Removing manually installed Modules'
$path = $env:PSModulePath.Split(';') | Select-Object -First 1
$path = Join-Path -Path $path -ChildPath $ModuleName
If ($path -and (Test-Path -Path $path))
{
Write-Output 'Removing folders'
Remove-Item $path -Recurse -Force
}
}
task Analyze {
Write-Output "Analyzing [$ModulePath]"
$invokeScriptAnalyzerSplat = @{
Path = $ModulePath
Settings = "$BuildRoot\ScriptAnalyzerSettings.psd1"
Severity = 'Warning'
}
$results = Invoke-ScriptAnalyzer @invokeScriptAnalyzerSplat
If ($results)
{
$results | Format-Table
}
}
task Catalog {
Write-Output "Cleaning catalog"
If (Test-Path $CatalogFilePath)
{
Remove-Item -Path $CatalogFilePath -Force
}
Write-Output "Creating catalog"
$fileCatalogSplat = @{
Path = $Destination
CatalogFilePath = $CatalogFilePath
}
$null = New-FileCatalog @fileCatalogSplat -CatalogVersion 2.0
Write-Output "Validating catalog"
Test-FileCatalog @fileCatalogSplat
}