-
Notifications
You must be signed in to change notification settings - Fork 19
/
build.ps1
185 lines (155 loc) · 5.33 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
## Copyright (c) Microsoft Corporation.
## Licensed under the MIT License.
[CmdletBinding(DefaultParameterSetName="build")]
param (
[Parameter(ParameterSetName="build")]
[string]
$Configuration = "Debug",
[Parameter(ParameterSetName="package")]
[switch]
$NoBuild,
[Parameter(ParameterSetName="package")]
[switch]
$Package,
[Parameter(ParameterSetName="package")]
[switch]
$signed,
[Parameter(ParameterSetName="package")]
[Parameter(ParameterSetName="test")]
[switch]
$test,
[Parameter()]
[switch]
$Clean,
[Parameter()]
[switch]
$GetPackageVersion,
[Parameter(ParameterSetName="bootstrap")]
[switch]
$Bootstrap
)
$moduleFileManifest = @(
@{ Sign = $true ; File = "Microsoft.PowerShell.TextUtility.format.ps1xml" }
@{ Sign = $true ; File = "Microsoft.PowerShell.TextUtility.psd1" }
@{ Sign = $false; File = "dictionary.txt" }
@{ Sign = $true ; File = "Microsoft.PowerShell.TextUtility.dll" }
)
$moduleName = "Microsoft.PowerShell.TextUtility"
$repoRoot = git rev-parse --show-toplevel
$testRoot = "${repoRoot}/test"
#
function Get-ModuleInfo {
import-powershelldatafile "$repoRoot/src/${moduleName}.psd1"
}
# this takes the files for the module and publishes them to a created, local repository
# so the nupkg can be used to publish to the PSGallery
function Export-Module
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
param($packageRoot)
if ( -not (test-path $packageRoot)) {
throw "'$packageRoot' does not exist"
}
# now construct a nupkg by registering a local repository and calling publish module
$repoName = [guid]::newGuid().ToString("N")
try {
Register-PSRepository -Name $repoName -SourceLocation "${repoRoot}/out" -InstallationPolicy Trusted
Publish-Module -Path $packageRoot -Repository $repoName
}
catch {
throw $_
}
finally {
if (Get-PackageSource -Name $repoName) {
Unregister-PSRepository -Name $repoName
}
}
Get-ChildItem -Recurse -Name $packageRoot | Write-Verbose -Verbose
# construct the package path and publish it
$nupkgName = "{0}.{1}" -f $moduleName,$moduleInfo.ModuleVersion
$pre = $moduleInfo.PrivateData.PSData.Prerelease
if ($pre) { $nupkgName += "-${pre}" }
$nupkgName += ".nupkg"
$nupkgPath = Join-Path $repoRoot out $nupkgName
Write-Verbose -Verbose "package path: ${nupkgPath} (exists:$(Test-Path $nupkgPath))"
}
function Test-Module {
try {
$PSVersionTable | Out-String -Stream | Write-Verbose -Verbose
$importTarget = "Import-Module ${PSScriptRoot}/out/${ModuleName}"
$importPester = "Import-Module Pester -Max 4.10.1"
$invokePester = "Invoke-Pester -OutputFormat NUnitXml -EnableExit -OutputFile ../testResults.xml"
$sb = [scriptblock]::Create("${importTarget}; ${importPester}; ${invokePester}")
Push-Location $testRoot
# calculate the shell to run rather than hardcoding it.
$PSEXE = (Get-Process -id $PID).MainModule.FileName
& $PSEXE -noprofile -command $sb
}
finally {
Pop-Location
}
}
function Invoke-Bootstrap
{
$neededPesterModule = Get-Module -Name Pester -ListAvailable | Where-Object { $_.Version -eq "4.10.1" }
$neededPesterVersion = [version]"4.10.1"
if ($neededPesterModule.Version -eq $neededPesterVersion)
{
Write-Verbose -Verbose -Message "Required pester version $neededPesterVersion is available."
return
}
Write-Verbose -Verbose -Message "Attempting install of Pester version ${neededPesterVersion}."
Install-Module -Name Pester -Scope CurrentUser -RequiredVersion 4.10.1 -Force -SkipPublisherCheck
$neededPesterModule = Get-Module -Name Pester -ListAvailable | Where-Object { $_.Version -eq $neededPesterVersion }
if ($neededPesterModule.Version -ne $neededPesterVersion)
{
throw "Pester install failed"
}
Write-Verbose -Verbose -Message "Pester version $neededPesterVersion installed."
return
}
try {
Push-Location "$PSScriptRoot/src/code"
$script:moduleInfo = Get-ModuleInfo
if ($GetPackageVersion) {
return $moduleInfo.ModuleVersion
}
if ($Bootstrap) {
Invoke-Bootstrap
return
}
$outPath = "$PSScriptRoot/out/${moduleName}"
if ($Clean) {
if (Test-Path $outPath) {
Write-Verbose "Deleting $outPath"
Remove-Item -recurse -force -path $outPath
}
dotnet clean
}
if (-not $NoBuild) {
dotnet publish --verbosity d --output $outPath --configuration $Configuration
Remove-Item -Path "$outPath/Microsoft.PowerShell.TextUtility.deps.json" -ErrorAction SilentlyContinue
if ($Configuration -eq "Release") {
Remove-Item -Path "$outPath/Microsoft.PowerShell.TextUtility.pdb" -ErrorAction SilentlyContinue
}
}
if ($Test) {
Test-Module
return
}
if ($Package) {
if ($Signed) {
$pkgBase = "${PSScriptRoot}/signed/${moduleName}"
}
else {
$pkgBase = "${PSScriptRoot}/out/${moduleName}"
}
if (-not (Test-Path $pkgBase)) {
throw "Directory '$pkgBase' does not exist"
}
Export-Module -packageRoot $pkgBase
}
}
finally {
Pop-Location
}