-
Notifications
You must be signed in to change notification settings - Fork 8
/
PSConsoleTheme.build.ps1
149 lines (132 loc) · 4.93 KB
/
PSConsoleTheme.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
param (
[ValidateSet('Release','Debug')]
[string]$Configuration = (property 'Configuration' 'Debug'),
[string]$NuGetApiKey = (property 'NuGetApiKey' ''),
[switch]$User,
[string]$Version
)
function Get-Version {
$manifest = Import-PowerShellDataFile 'PSConsoleTheme/PSConsoleTheme.psd1'
[System.Version]::Parse($manifest.ModuleVersion)
}
$currentVersion = Get-Version
$targetDir = Join-Path $BuildRoot "module/$Configuration"
if ($Configuration -eq 'Debug') {
$Global:PSConsoleThemeDebugSessionPath = $targetDir
}
$mamlHelpParams = @{
Inputs = (Get-ChildItem docs/*.md -Exclude about_*.md)
Outputs = (Join-Path $targetDir 'en-US/PSConsoleTheme-help.xml')
}
# Synopsis: Build external help file from markdown documentation
task BuildMamlHelp @mamlHelpParams -If ($Configuration -eq 'Release') {
$oldProgress = $Global:ProgressPreference
$Global:ProgressPreference = 'SilentlyContinue'
PlatyPS\New-ExternalHelp -Path docs -OutputPath $targetDir\en-US\PSConsoleTheme-help.xml -Force | Out-Null
$Global:ProgressPreference = $oldProgress
}
# Synopsis: Remove all build related artifacts
task Clean {
Remove-Item module -Recurse -Force -ErrorAction Ignore
if ($User -and (($userProfile = Join-Path $env:USERPROFILE '.psconsoletheme') | Test-Path)) {
Remove-Item $userProfile -Recurse -Force -Confirm
}
}
# Synopsis: Import the module for use in the current session
task Install LayoutModule, {
switch ($Configuration) {
Debug {
Remove-Module PSConsoleTheme -Force -ErrorAction Ignore
Import-Module (Join-Path $targetDir 'PSConsoleTheme.psm1') -Force
}
Release {
$paths = $env:PSModulePath -split ';' | Where-Object { $_ -like "${env:USERPROFILE}*" }
foreach ($path in $paths) {
if (!(Test-Path $path)) {
New-Item $path -ItemType Directory -Force | Out-Null
}
$modulePath = "$path\PSConsoleTheme\$currentVersion"
try {
if (Test-Path $modulePath) {
Remove-Item $modulePath -Recurse -Force -ErrorAction Stop
}
Copy-Item $targetDir $modulePath -Recurse
}
catch {
Write-Error "Cannot install to $path. Module might be in use."
}
}
}
}
}
$layoutModuleParams = @{
Inputs = {
Get-ChildItem `
PSConsoleTheme/*.ps*,
PSConsoleTheme/Private/*.ps1,
PSConsoleTheme/Public/*.ps1,
PSConsoleTheme/Themes/*.json
}
Outputs = {
process {
Join-Path $targetDir ($_ -replace [regex]::Escape($BuildRoot + '\PSConsoleTheme\'), '')
}
}
}
# Synopsis: Copy all of the files that belong in the module to one place for installation
task LayoutModule -Partial @layoutModuleParams BuildMamlHelp, {
process {
if (-Not (Test-Path (Split-Path $2) -PathType Container)) {
New-Item (Split-Path $2) -ItemType Directory -Force | Out-Null
}
Write-Verbose "Copying $($_ -replace [regex]::Escape($BuildRoot + '\'), '') -> $($2 -replace [regex]::Escape($BuildRoot + '\'), '')"
Copy-Item $_ $2 -Force
}
}
# Synopsis: Publish the module to PSGallery
task Publish -If ($Configuration -eq 'Release') LayoutModule, {
if ($NuGetApiKey -eq '') {
throw "Cannot publish. NuGet API key not set."
}
$publishParams = @{
Path = $targetDir
NuGetApiKey = $NuGetApiKey
Repository = 'PSGallery'
}
Publish-Module @publishParams
}
# Synopsis: Update the module version in the manifest file
task UpdateVersion {
if ($Version -eq '') {
throw 'No version specified'
}
switch ($Version) {
Major {
$update = "$($currentVersion.Major + 1).0.0"
}
Minor {
$update = "$($currentVersion.Major).$($currentVersion.Minor + 1).0"
}
Patch {
$update = "$($currentVersion.Major).$($currentVersion.Minor).$($currentVersion.Build + 1)"
}
Default {
if ([System.Version]::TryParse($Version, [ref]$null)) {
$update = $Version
} else {
throw "Invalid version specified: $Version"
}
}
}
Write-Host "Updating version from [$currentVersion] to [$update]"
$file = Resolve-Path 'PSConsoleTheme/PSConsoleTheme.psd1'
$manifest = Get-Content $file -Raw
$manifest = [regex]::Replace($manifest, "ModuleVersion = '.*'", "ModuleVersion = '$update'")
$manifest | Set-Content $file -Encoding UTF8 -NoNewline
}
# Synopsis: Create an archive of the module for release
task ZipRelease -If ($Configuration -eq 'Release') LayoutModule, {
$version = Get-Version
Compress-Archive $targetDir -DestinationPath ((Split-Path $targetDir) + "/PSConsoleTheme-$version.zip") -Force
}
task . LayoutModule