-
Notifications
You must be signed in to change notification settings - Fork 1
/
push-to-github.ps1
189 lines (149 loc) · 4.89 KB
/
push-to-github.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
param (
[string]$CommitMessage = "Update to package",
[bool]$Tag = $false
)
$ProjectName = "wagtail-filerobot"
$PyPiName = "wagtail_filerobot"
$FolderName = "filerobot"
function IsNumeric ($Value) {
return $Value -match "^[\d\.]+$"
}
Function GITHUB_Upload {
param (
[parameter(Mandatory=$false)]
[string]$Version
)
git add .
if ($Tag) {
$gitVersion = "v${Version}"
git tag $gitVersion
git commit -m $CommitMessage
git push -u origin main --tags
} else {
git commit -m $CommitMessage
git push -u origin main
}
}
Function _NextVersionString {
param (
[string]$Version
)
$versionParts = $version -split "\."
$major = [int]$versionParts[0]
$minor = [int]$versionParts[1]
$patch = [int]$versionParts[2] + 1
# validate integers
if (-not (IsNumeric $major) -or -not (IsNumeric $minor) -or -not (IsNumeric $patch)) {
Write-Host "Invalid version format"
throw "Invalid version format"
}
if ($patch -gt 9) {
$patch = 0
$minor += 1
}
if ($minor -gt 9) {
$minor = 0
$major += 1
}
$newVersion = "$major.$minor.$patch"
return $newVersion
}
function PYPI_NextVersion {
param (
[string]$ConfigFile = ".\setup.cfg"
)
# Read file content
$fileContent = Get-Content -Path $ConfigFile
# Extract the version, increment it, and prepare the updated version string
$versionLine = $fileContent | Where-Object { $_ -match "version\s*=" }
$version = $versionLine -split "=", 2 | ForEach-Object { $_.Trim() } | Select-Object -Last 1
$newVersion = _NextVersionString -Version $version
return $newVersion
}
function InitRepo {
param (
[string]$ConfigFile = ".\setup.cfg"
)
Write-Host "Initialising repository..."
git init | Out-Host
git add . | Out-Host
git branch -M main | Out-Host
git remote add origin "git@github.com:Nigel2392/${ProjectName}.git" | Out-Host
$version = PYPI_NextVersion -ConfigFile $ConfigFile
Write-Host "Initial version: $version"
return $version
}
function GITHUB_NextVersion {
param (
[string]$ConfigFile = ".\setup.cfg",
[string]$PyVersionFile = ".\${FolderName}\__init__.py"
)
# Extract the version, increment it, and prepare the updated version string
$version = "$(git tag -l --format='VERSION=%(refname:short)' | Sort-Object -Descending | Select-Object -First 1)" -split "=v", 2 | ForEach-Object { $_.Trim() } | Select-Object -Last 1
if ($version) {
$newVersion = _NextVersionString -Version $version
Write-Host "Next version (git): $newVersion"
return $newVersion
} else {
$newVersion = InitRepo -ConfigFile $ConfigFile
Write-Host "Next version (init): $newVersion"
return $newVersion
}
}
Function GITHUB_UpdateVersion {
param (
[string]$ConfigFile = ".\setup.cfg",
[string]$PyVersionFile = ".\${FolderName}\__init__.py"
)
$newVersion = GITHUB_NextVersion -ConfigFile $ConfigFile
Write-Host "Updating version to $newVersion"
# First update the init file so that in case something goes wrong
# the version doesn't persist in the config file
if (Test-Path $PyVersionFile) {
$initContent = Get-Content -Path $PyVersionFile
$initContent = $initContent -replace "__version__\s*=\s*.+", "__version__ = '$newVersion'"
Set-Content -Path $PyVersionFile -Value $initContent
}
# Read file content
$fileContent = Get-Content -Path $ConfigFile
if (Test-Path $ConfigFile) {
# Update the version line in the file content
$updatedContent = $fileContent -replace "version\s*=\s*.+", "version = $newVersion"
# Write the updated content back to the file
Set-Content -Path $ConfigFile -Value $updatedContent
}
return $newVersion
}
Function _PYPI_DistName {
param (
[string]$Version,
[string]$Append = ".tar.gz"
)
return "$PyPiName-$Version$Append"
}
Function PYPI_Build {
py .\setup.py sdist
}
Function PYPI_Check {
param (
[string]$Version
)
$distFile = _PYPI_DistName -Version $Version
py -m twine check "./dist/${distFile}"
}
Function PYPI_Upload {
param (
[string]$Version
)
$distFile = _PYPI_DistName -Version $Version
py -m twine upload "./dist/${distFile}"
}
if ($Tag) {
$version = GITHUB_UpdateVersion # Increment the package version (setup.cfg)
GITHUB_Upload -Version $version # Upload the package (git push)
PYPI_Build # Build the package (python setup.py sdist)
PYPI_Check -Version $version # Check the package (twine check dist/<LATEST>)
PYPI_Upload -Version $version # Upload the package (twine upload dist/<LATEST>)
} else {
GITHUB_Upload # Upload the package
}