-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathPSIni.build.ps1
More file actions
169 lines (138 loc) · 6.95 KB
/
Copy pathPSIni.build.ps1
File metadata and controls
169 lines (138 loc) · 6.95 KB
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
[CmdletBinding()]
param(
[ValidateSet('None', 'Normal' , 'Detailed', 'Diagnostic')]
[String] $PesterVerbosity = 'Normal',
[Parameter()]
[String] $VersionToPublish,
[Parameter()]
[String] $PSGalleryAPIKey
)
Import-Module "$PSScriptRoot/tools/BuildTools.psm1" -Force
Import-Module (Get-Dependency) -Force -ErrorAction Stop
Remove-Item -Path env:\BH* -ErrorAction SilentlyContinue
Set-BuildEnvironment -BuildOutput '$ProjectPath/release' -ErrorAction SilentlyContinue
$OS, $OSVersion = Get-HostInformation
# Add-ToModulePath -Path $env:BHBuildOutput
if ($VersionToPublish) {
$VersionToPublish = $VersionToPublish.TrimStart('v') -as [Version]
}
$currentVersion = (Get-Metadata -Path $env:BHPSModuleManifest) -as [Version]
$builtManifestPath = "$env:BHBuildOutput/$env:BHProjectName/$env:BHProjectName.psd1"
Task ShowDebugInfo {
Write-Build Gray
Write-Build Gray ('BHBuildSystem: {0}' -f $env:BHBuildSystem)
Write-Build Gray '-------------------------------------------------------'
Write-Build Gray ('BHProjectName {0}' -f $env:BHProjectName)
Write-Build Gray ('BHProjectPath: {0}' -f $env:BHProjectPath)
Write-Build Gray ('BHModulePath: {0}' -f $env:BHModulePath)
Write-Build Gray ('BHPSModuleManifest: {0}' -f $env:BHPSModuleManifest)
Write-Build Gray ('BHBuildOutput: {0}' -f $env:BHBuildOutput)
Write-Build Gray ('builtManifestPath: {0}' -f $builtManifestPath)
Write-Build Gray '-------------------------------------------------------'
Write-Build Gray ('BHBranchName: {0}' -f $env:BHBranchName)
Write-Build Gray ('BHCommitHash: {0}' -f $env:BHCommitHash)
Write-Build Gray ('BHCommitMessage: {0}' -f $env:BHCommitMessage)
Write-Build Gray ('BHBuildNumber {0}' -f $env:BHBuildNumber)
Write-Build Gray ('CurrentVersion {0}' -f $currentVersion)
Write-Build Gray ('VersionToPublish {0}' -f $VersionToPublish)
Write-Build Gray '-------------------------------------------------------'
Write-Build Gray ('PowerShell version: {0}' -f $PSVersionTable.PSVersion.ToString())
Write-Build Gray ('OS: {0}' -f $OS)
Write-Build Gray ('OS Version: {0}' -f $OSVersion)
Write-Build Gray
}
Task Clean {
Remove-Item $env:BHBuildOutput -Force -Recurse -ErrorAction SilentlyContinue
Remove-Item "Test*.xml" -Force -ErrorAction SilentlyContinue
}
Task Build Clean, {
if (-not (Test-Path "$env:BHBuildOutput/$env:BHProjectName")) {
$null = New-Item -Path "$env:BHBuildOutput", "$env:BHBuildOutput/$env:BHProjectName" -ItemType Directory
}
# TODO:
# replace data in manifest
}, CopyModuleFiles, CompileModule, UpdateManifest
# Synopsis: Generate ./release structure
Task CopyModuleFiles {
Copy-Item -Path "$env:BHModulePath/*" -Destination "$env:BHBuildOutput/$env:BHProjectName" -Recurse -Force
Copy-Item -Path @(
"$env:BHProjectPath/CHANGELOG.md"
"$env:BHProjectPath/LICENSE"
"$env:BHProjectPath/README.md"
) -Destination "$env:BHBuildOutput/$env:BHProjectName" -Force
$null = New-Item -Path "$env:BHBuildOutput/Tests" -ItemType Directory -ErrorAction SilentlyContinue
Copy-Item -Path "$env:BHProjectPath/Tests" -Destination $env:BHBuildOutput -Recurse -Force
Copy-Item -Path "$env:BHProjectPath/PSScriptAnalyzerSettings.psd1" -Destination $env:BHBuildOutput -Force
}
# Synopsis: Compile all functions into the .psm1 file
Task CompileModule {
$regionsToKeep = @('Dependencies', 'Configuration')
$targetFile = "$env:BHBuildOutput/$env:BHProjectName/$env:BHProjectName.psm1"
$content = Get-Content -Encoding UTF8 -LiteralPath $targetFile
$capture = $false
$compiled = ""
foreach ($line in $content) {
if ($line -match "^#region ($($regionsToKeep -join "|"))$") {
$capture = $true
}
if (($capture -eq $true) -and ($line -match "^#endregion")) {
$capture = $false
}
if ($capture) {
$compiled += "$line`r`n"
}
}
$PublicFunctions = @( Get-ChildItem -Path "$env:BHBuildOutput/$env:BHProjectName/Public/*.ps1" -ErrorAction SilentlyContinue )
$PrivateFunctions = @( Get-ChildItem -Path "$env:BHBuildOutput/$env:BHProjectName/Private/*.ps1" -ErrorAction SilentlyContinue )
foreach ($function in @($PublicFunctions + $PrivateFunctions)) {
$compiled += (Get-Content -Path $function.FullName -Raw)
$compiled += "`r`n"
}
Set-Content -LiteralPath $targetFile -Value $compiled -Encoding UTF8 -Force
Remove-Utf8Bom -Path $targetFile
"Private", "Public" | ForEach-Object { Remove-Item -Path "$env:BHBuildOutput/$env:BHProjectName/$_" -Recurse -Force }
}
# Synopsis: Update the manifest of the module
Task UpdateManifest {
Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue
Import-Module $env:BHPSModuleManifest -Force
$moduleAlias = Get-Alias | Where-Object { $_.ModuleName -eq "$env:BHProjectName" }
$moduleFunctions = (Get-ChildItem "$env:BHModulePath/Public/*.ps1").BaseName
Metadata\Update-Metadata -Path $builtManifestPath -PropertyName "FunctionsToExport" -Value @($moduleFunctions)
Metadata\Update-Metadata -Path $builtManifestPath -PropertyName "AliasesToExport" -Value ''
if ($moduleAlias) {
Metadata\Update-Metadata -Path $builtManifestPath -PropertyName "AliasesToExport" -Value @($moduleAlias.Name)
}
}
Task SetVersion {
Assert-True { $VersionToPublish -as [Version] } "Invalid version format: $VersionToPublish"
Assert-True { $VersionToPublish -gt $currentVersion } "Version must be greater than the current version: $currentVersion"
Metadata\Update-Metadata -Path $builtManifestPath -PropertyName "ModuleVersion" -Value $VersionToPublish.ToString()
}
Task Test {
$pesterConfig = [PesterConfiguration]::Default
$pesterConfig.Output.Verbosity = $PesterVerbosity
$pesterConfig.Run.path = @("$env:BHBuildOutput/Tests")
$pesterConfig.Run.PassThru = $true
$PesterConfig.TestResult.Enabled = $true
$pesterConfig.TestResult.OutputFormat = 'NUnitXml'
$pesterConfig.TestResult.OutputPath = "TestResults.xml"
if ((Invoke-Pester -Configuration $pesterConfig).FailedCount -gt 0) {
throw "Tests failed"
}
}
Task Publish SetVersion, SignCode, Package, {
Assert-True (-not [String]::IsNullOrEmpty($PSGalleryAPIKey)) "No key for the PSGallery"
Publish-Module -Path "$env:BHBuildOutput/$env:BHProjectName" -NuGetApiKey $PSGalleryAPIKey
}
Task SignCode {
# TODO: waiting for certificates
}
Task Package {
$source = "$env:BHBuildOutput\$env:BHProjectName"
$destination = "$env:BHBuildOutput\$env:BHProjectName.zip"
Assert-True { Test-Path $source } "Missing files to package"
Remove-Item $destination -ErrorAction SilentlyContinue
$null = Compress-Archive -Path $source -DestinationPath $destination
}
Task . Clean, Build, Test