-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.ps1
143 lines (120 loc) · 4.65 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
#requires -Module InvokeBuild
[cmdletbinding()]
param(
[Parameter(ParameterSetName = 'docs')]
[switch]
$PublishDocs,
[Parameter(ParameterSetName = 'publish')]
[string]
$GalleryKey,
[Parameter(ParameterSetName = 'publish')]
[switch]
$Prerelease,
[Parameter(ParameterSetName = 'release')]
[Parameter(ParameterSetName = 'publish')]
[switch]
$Release,
[Parameter(ParameterSetName = 'release')]
[Parameter(ParameterSetName = 'publish')]
[switch]
$Draft,
[Parameter(ParameterSetName = 'publish')]
[switch]
$SkipTests
)
Push-Location
Set-Location $PSScriptRoot
$moduleName = 'dbalight'
$staging = "$env:TEMP\dbalight_staging\"
$git = git status
if ($git[1] -notmatch "Your branch is up to date" -and (-not $PSBoundParameters.ContainsKey('PublishDocs'))) {
Pop-Location
throw "Local branch is dirty"
}
if (Test-Path $staging) {
Remove-Item -Recurse -Force $staging
}
if (Get-Module dbalight) {
Remove-Module dbalight -Force
}
$imported = Import-Module "$PSScriptRoot\src\output\dbalight.psd1" -Force -PassThru
if ($PSBoundParameters['PublishDocs']) {
if ($PSEdition -eq 'Desktop') {
Write-Warning "Doc processing has to run under PowerShell Core"
return
}
$docCommandPath = "$PSScriptRoot\docs\"
Write-Host "Removing old command docs [$docCommandPath]" -ForegroundColor Black -BackgroundColor DarkCyan
Remove-Item $docCommandPath -Filter *.md -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue -Verbose
Import-Module platyPS
$cmdParams = @{
Module = $moduleName
CommandType = 'Cmdlet'
}
$commands = Get-Command @cmdParams
Write-Host "Generating new command docs [$docCommandPath]" -ForegroundColor Black -BackgroundColor DarkCyan
foreach ($cmd in $commands) {
switch ($cmd.Name) {
{ $_ -match 'Db' } { $category = 'database' }
{ $_ -match 'Instance' } { $category = 'instance' }
default { $category = 'general' }
}
$metadata = @{
'category' = $category
'title' = $cmd.Name
}
New-MarkdownHelp -OutputFolder $docCommandPath -Command $cmd.Name -Metadata $metadata -Force
}
return
}
if (-not $PSBoundParameters['SkipTests']) {
Import-Module Pester
$tests = Invoke-Pester -Path "$PSScriptRoot\tests" -Output Minimal -PassThru
}
if ($PSBoundParameters['Prerelease']) {
$foundModule = Find-Module -Name $moduleName -AllowPrerelease:$Prerelease -Repository PSGallery
} else {
$foundModule = Find-Module -Name $moduleName -Repository PSGallery
}
if ($foundModule.Version -ge $imported.Version) {
Write-Warning "PowerShell Gallery version of $moduleName is more recent ($($foundModule.Version) >= $($imported.Version))"
}
if ($tests.FailedCount -eq 0 -or $PSBoundParameters['SkipTests']) {
$moduleTempPath = Join-Path $staging $moduleName
Write-Host "Staging directory: $moduleTempPath"
$imported | Split-Path | Copy-Item -Destination $moduleTempPath -Recurse
if ($PSBoundParameters['GalleryKey']) {
try {
Write-Host "Publishing $moduleName [$($imported.Version)] to PowerShell Gallery"
Publish-Module -Path $moduleTempPath -NuGetApiKey $gallerykey -Repository PSGallery
Write-Host "successfully published to PS Gallery"
} catch {
Write-Warning "Publish failed: $_"
}
}
if ($PSBoundParameters['Release']) {
if ((gh config get prompt) -eq 'enabled') {
Invoke-Expression "gh config set prompt disabled"
}
$moduleData = Import-PowerShellDataFile "$staging\$moduleName\$moduleName.psd1"
$changeLog = [IO.Path]::Combine([string]$PSScriptRoot, 'release.md')
Compress-Archive "$staging\$moduleName\*" -DestinationPath "$staging\$moduleName.zip" -CompressionLevel Fastest -Force
$ghArgs = "release create `"v$($moduleData.ModuleVersion)`" `"$($staging)$($moduleName).zip#$($moduleName).zip`" --title `"dbalight $($moduleData.ModuleVersion)`" --notes-file $changeLog"
if ($PSBoundParameters['Prerelease']) {
$ghArgs = $ghArgs + " --prerelease"
}
if ($PSBoundParameters['Draft']) {
$ghArgs = $ghArgs + " --draft"
}
Write-Host "gh command to execute: $ghArgs" -ForegroundColor DarkYellow
Invoke-Expression "gh $ghArgs"
if ((gh config get prompt) -eq 'disabled') {
Invoke-Expression "gh config set prompt enabled"
}
}
Remove-Item -Recurse -Force $staging
} else {
Remove-Item -Recurse -Force $staging
Write-Host "Tests failures detected; cancelling and cleaning up"
}
Pop-Location