-
Notifications
You must be signed in to change notification settings - Fork 9
/
ClassExplorer.build.ps1
225 lines (179 loc) · 7.52 KB
/
ClassExplorer.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
#requires -Version 5.1
[CmdletBinding()]
param(
[ValidateSet('Debug', 'Release')]
[string] $Configuration = 'Debug',
[switch] $GenerateCodeCoverage,
[switch] $Force
)
$moduleName = 'ClassExplorer'
$testModuleManifestSplat = @{
ErrorAction = 'Ignore'
WarningAction = 'Ignore'
Path = "$PSScriptRoot\module\$moduleName.psd1"
}
$manifest = Test-ModuleManifest @testModuleManifestSplat
$script:Settings = @{
Name = $moduleName
Manifest = $manifest
Version = $manifest.Version
ShouldTest = $true
}
$script:Folders = @{
PowerShell = "$PSScriptRoot\module"
CSharp = "$PSScriptRoot\src"
Build = '{0}\src\{1}\bin\{2}' -f $PSScriptRoot, $moduleName, $Configuration
Release = '{0}\Release\{1}\{2}' -f $PSScriptRoot, $moduleName, $manifest.Version
Docs = "$PSScriptRoot\docs"
Test = "$PSScriptRoot\test"
Results = "$PSScriptRoot\testresults"
}
$script:Discovery = @{
HasDocs = Test-Path ('{0}\{1}\*.md' -f $Folders.Docs, $PSCulture)
HasTests = Test-Path ('{0}\*.Tests.ps1' -f $Folders.Test)
IsUnix = $PSVersionTable.PSEdition -eq "Core" -and -not $IsWindows
}
$tools = "$PSScriptRoot\tools"
$script:GetDotNet = Get-Command $tools\GetDotNet.ps1
$script:AssertModule = Get-Command $tools\AssertRequiredModule.ps1
$script:GetOpenCover = Get-Command $tools\GetOpenCover.ps1
$script:GenerateSignatureMarkdown = Get-Command $tools\GenerateSignatureMarkdown.ps1
task AssertDotNet {
$script:dotnet = & $GetDotNet -Unix:$Discovery.IsUnix
}
task AssertOpenCover -If { $GenerateCodeCoverage.IsPresent } {
if ($Discovery.IsUnix) {
Write-Warning 'Generating code coverage from .NET core is currently unsupported, disabling code coverage generation.'
$script:GenerateCodeCoverage = $false
return
}
$script:openCover = & $GetOpenCover
}
task AssertRequiredModules {
& $AssertModule Pester 5.3.0 -Force:$Force.IsPresent
& $AssertModule InvokeBuild 5.8.4 -Force:$Force.IsPresent
& $AssertModule platyPS 0.14.2 -Force:$Force.IsPresent
& $AssertModule Yayaml 0.1.1 -Force:$Force.IsPresent
}
task AssertDevDependencies -Jobs AssertDotNet, AssertOpenCover, AssertRequiredModules
task Clean {
if ($PSScriptRoot -and (Test-Path $PSScriptRoot\Release)) {
Remove-Item $PSScriptRoot\Release -Recurse
}
$null = New-Item $Folders.Release -ItemType Directory
if (Test-Path $Folders.Results) {
Remove-Item $Folders.Results -Recurse
}
$null = New-Item $Folders.Results -ItemType Directory
& $dotnet clean --verbosity quiet -nologo
}
task BuildDocs -If { $Discovery.HasDocs } {
$sourceDocs = "$PSScriptRoot\docs\$PSCulture"
$releaseDocs = '{0}\{1}' -f $Folders.Release, $PSCulture
$null = New-Item $releaseDocs -ItemType Directory -Force -ErrorAction SilentlyContinue
$null = New-ExternalHelp -Path $sourceDocs -OutputPath $releaseDocs
& $GenerateSignatureMarkdown.Source -AboutHelp $releaseDocs\about_Type_Signatures.help.txt
& $GenerateSignatureMarkdown.Source $PSScriptRoot\docs\en-US\about_Type_Signatures.help.md
}
task BuildDll {
if (-not $Discovery.IsUnix) {
& $dotnet publish --configuration $Configuration --framework net471 --verbosity quiet -nologo
}
& $dotnet publish --configuration $Configuration --framework netcoreapp3.1 --verbosity quiet -nologo
}
task CopyToRelease {
$powershellSource = '{0}\*' -f $Folders.PowerShell
$release = $Folders.Release
$releaseDesktopBin = "$release\bin\Desktop"
$releaseCoreBin = "$release\bin\Core"
$sourceDesktopBin = '{0}\net471\publish\*' -f $Folders.Build
$sourceCoreBin = '{0}\netcoreapp3.1\publish\*' -f $Folders.Build
Copy-Item -Path $powershellSource -Destination $release -Recurse -Force
if (-not $Discovery.IsUnix) {
$null = New-Item $releaseDesktopBin -Force -ItemType Directory
Copy-Item -Path $sourceDesktopBin -Destination $releaseDesktopBin -Force
}
$null = New-Item $releaseCoreBin -Force -ItemType Directory
Copy-Item -Path $sourceCoreBin -Destination $releaseCoreBin -Force
}
task DoTest -If { $Discovery.HasTests -and $Settings.ShouldTest } {
if ($Discovery.IsUnix) {
$scriptString = '
$projectPath = "{0}"
Invoke-Pester "$projectPath" -OutputFormat NUnitXml -OutputFile "$projectPath\testresults\pester.xml"
' -f $PSScriptRoot
} else {
$scriptString = '
Set-ExecutionPolicy Bypass -Force -Scope Process
$projectPath = "{0}"
Invoke-Pester "$projectPath" -OutputFormat NUnitXml -OutputFile "$projectPath\testresults\pester.xml"
' -f $PSScriptRoot
}
$encodedCommand =
[convert]::ToBase64String(
[System.Text.Encoding]::Unicode.GetBytes(
$scriptString))
$powershellCommand = 'powershell'
if ($Discovery.IsUnix) {
$powershellCommand = 'pwsh'
}
$powershell = (Get-Command $powershellCommand).Source
if ($GenerateCodeCoverage.IsPresent) {
# OpenCover needs full pdb's. I'm very open to suggestions for streamlining this...
# & $dotnet clean
& $dotnet publish --configuration $Configuration --framework net471 --verbosity quiet -nologo /p:DebugType=Full
$moduleName = $Settings.Name
$release = '{0}\bin\Desktop\{1}' -f $Folders.Release, $moduleName
$coverage = '{0}\net471\{1}' -f $Folders.Build, $moduleName
Rename-Item "$release.pdb" -NewName "$moduleName.pdb.tmp"
Rename-Item "$release.dll" -NewName "$moduleName.dll.tmp"
Copy-Item "$coverage.pdb" "$release.pdb"
Copy-Item "$coverage.dll" "$release.dll"
& $openCover `
-target:$powershell `
-register:user `
-output:$PSScriptRoot\testresults\opencover.xml `
-hideskipped:all `
-filter:+[ClassExplorer*]* `
-targetargs:"-NoProfile -EncodedCommand $encodedCommand"
Remove-Item "$release.pdb"
Remove-Item "$release.dll"
Rename-Item "$release.pdb.tmp" -NewName "$moduleName.pdb"
Rename-Item "$release.dll.tmp" -NewName "$moduleName.dll"
} else {
& $powershell -NoProfile -EncodedCommand $encodedCommand
}
}
task DoInstall {
$sourcePath = '{0}\*' -f $Folders.Release
$installBase = $Home
if ($profile) { $installBase = $profile | Split-Path }
$installPath = '{0}\Modules\{1}\{2}' -f $installBase, $Settings.Name, $Settings.Version
if (-not (Test-Path $installPath)) {
$null = New-Item $installPath -ItemType Directory
}
Copy-Item -Path $sourcePath -Destination $installPath -Force -Recurse
}
task DoPublish {
if ($Configuration -eq 'Debug') {
throw 'Configuration must not be Debug to publish!'
}
if ($env:GALLERY_API_KEY) {
$apiKey = $env:GALLERY_API_KEY
} else {
$userProfile = [Environment]::GetFolderPath([Environment+SpecialFolder]::UserProfile)
if (Test-Path $userProfile/.PSGallery/apikey.xml) {
$apiKey = (Import-Clixml $userProfile/.PSGallery/apikey.xml).GetNetworkCredential().Password
}
}
if (-not $apiKey) {
throw 'Could not find PSGallery API key!'
}
Publish-Module -Name $Folders.Release -NuGetApiKey $apiKey -Force:$Force.IsPresent
}
task Build -Jobs AssertDevDependencies, Clean, BuildDll, CopyToRelease, BuildDocs
task Test -Jobs Build, DoTest
task PreRelease -Jobs Test
task Install -Jobs PreRelease, DoInstall
task Publish -Jobs PreRelease, DoPublish
task . Build