-
Notifications
You must be signed in to change notification settings - Fork 8
/
Build.ps1
134 lines (107 loc) · 4.11 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
# Prepeares the repository for a new release.
param(
[string]
$Version,
[switch]
$Prerelease,
[switch]
$Publish,
[string]
$NugetApiKey = $null
)
function Prepare-Release {
$options = @(
[System.Management.Automation.Host.ChoiceDescription]::new('&No', 'No')
[System.Management.Automation.Host.ChoiceDescription]::new('&Yes', 'Yes')
)
$module = Get-Content "posh-sshell.psd1" -Raw
$moduleUnmodified = $module
# If no version is specified, infer it by extracting the version from the module file.
if (!$Version) {
if($module -match "ModuleVersion = '([0-9.]+)'") {
$Version = $Matches[1]
}
else {
throw "No version found in the module file."
}
}
$confirm = $host.ui.PromptForChoice("Preparing release $Version. Continue?", "", $options, 1)
if (!$confirm) {
return
}
# Update the release notes and license URL
$module = $module -replace "https://github.com/dahlbyk/posh-sshell/blob/master/LICENSE.txt", "https://github.com/dahlbyk/posh-sshell/blob/v$Version/LICENSE.txt"
$module = $module -replace "https://github.com/dahlbyk/posh-sshell/blob/master/CHANGELOG.md", "https://github.com/dahlbyk/posh-sshell/blob/v$Version/CHANGELOG.md"
# Remove the Prerelease info.
if (!$Prerelease) {
$module = $module -replace "(Prerelease = '[a-zA-Z0-9\.]+')", ""
}
# Commit the changed module file
$module | Out-File posh-sshell.psd1 -Encoding utf8
git add posh-sshell.psd1 | Out-Null
git commit -m "Preparing $Version release" | Out-Null
# Tag the release
$tag = "v$Version"
Write-Host "Tagging release $Version"
git tag -a "$tag" -m "Tagging $Version" | Out-Null
# Bump the version number's minor component
$versionBits = $Version.Split(".")
$versionBits[1] = ([int]$versionBits[1]) + 1
$Version = $versionBits -join "."
# Revert to original module file to bring back the prerelease and URLs.
$module = $moduleUnmodified
# Write the new version number to the module
$module = $module -replace "ModuleVersion = '([0-9.]+)'", "ModuleVersion = '$Version'"
# Write the module file back and create a new commit
$module | Out-File posh-sshell.psd1 -Encoding utf8
git add posh-sshell.psd1 | Out-Null
git commit -m "Increase version number" | Out-Null
# Prompt to push.
$confirm = $host.ui.PromptForChoice("", "Do you want to push tag $tag to github?", $options, 1)
if($confirm) {
git push origin --tags
Write-Host "Checking out tag ready for publish..."
git checkout "$tag"
}
Write-Host "Done."
}
function Publish {
if (!$NugetApiKey) {
$homePath = if ($Env:HOME) {$Env:HOME} else {$Home}
# Allow api key to be explicitly specified, or find it locally.
$keyfile = "$homePath\Dropbox\powershellgallery-access-key.txt"
if(!(Test-Path $keyfile)) {
throw "No NuGet access key specified."
}
else {
$NugetApiKey = (Get-Content $keyfile)
}
}
# Create .build
Remove-Item "$PSScriptRoot\.build" -Force -Recurse -ErrorAction Ignore
New-Item -Type Directory "$PSScriptRoot\.build" -ErrorAction Ignore | Out-Null
# Copy everything we want into .build
Copy-Item "posh-sshell.psd1" ".build"
Copy-Item "posh-sshell.psm1" ".build"
Copy-Item "LICENSE.txt" ".build"
Copy-Item "src\" ".build" -Recurse
$options = @(
[System.Management.Automation.Host.ChoiceDescription]::new('&No', 'No')
[System.Management.Automation.Host.ChoiceDescription]::new('&Yes', 'Yes')
)
$module = Invoke-Expression (Get-Content ".\posh-sshell.psd1" -Raw)
$version = $module.ModuleVersion
$confirm = $host.ui.PromptForChoice("Publish release $version to PS Gallery?", "", $options, 0)
if($confirm) {
Write-Host "Publishing..."
# Run publish-module against the build dir
Publish-Module -Name "$PSScriptRoot\.build\posh-sshell.psd1" -NuGetApiKey $NugetApiKey
Write-Host "Done."
}
}
if ($Publish) {
Publish
}
else {
Prepare-Release
}