-
Notifications
You must be signed in to change notification settings - Fork 16
/
build.ps1
168 lines (143 loc) · 4.79 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
## 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
)
$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
$pesterInstallations = Get-Module -ListAvailable -Name Pester
if ($pesterInstallations.Version -notcontains "4.10.1") {
Install-Module -Name Pester -RequiredVersion 4.10.1 -Force -Scope CurrentUser
}
$importTarget = "Import-Module ${PSScriptRoot}/out/${ModuleName}"
$importPester = "Import-Module Pester -Max 4.10.1"
$invokePester = "Invoke-Pester -OutputFormat NUnitXml -EnableExit -OutputFile ../Microsoft.PowerShell.TextUtility.xml"
$command = "${importTarget}; ${importPester}; ${invokePester}"
Push-Location $testRoot
pwsh -noprofile -command $command
}
finally {
Pop-Location
}
}
try {
Push-Location "$PSScriptRoot/src/code"
$script:moduleInfo = Get-ModuleInfo
if ($GetPackageVersion) {
return $moduleInfo.ModuleVersion
}
$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
<#
$script = [ScriptBlock]::Create("try {
Import-Module '${repoRoot}/out/${moduleName}/'
Import-Module -Name Pester -Max 4.99
Push-Location '${repoRoot}/test'
Invoke-Pester
}
finally {
Pop-Location
}")
pwsh -c $script
#>
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
}