forked from startersclan/docker-sourceservers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generate-GitBranches.ps1
288 lines (263 loc) · 10.6 KB
/
Generate-GitBranches.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<#
.SYNOPSIS
# 1. This script create / updates git branches (named <game_platform>-<game_engine>-<game>) based on games.json
# 2. By default it creates a git branch for each game found in games.json. To limit to one game, specify -GamePlatform, -GameEngine, and -Game
# 3. To build a game, checkout to its branch, edit .env, mutate .trigger, commit and push
.EXAMPLE
# Create branches for all games (dry-run)
./Generate-GitBranches.ps1 -Repo . -WhatIf
# Create branches for all games
./Generate-GitBranches.ps1 -Repo .
# Create branches for all games, and push to git remote 'upstream'
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Push
# Create branches for specific game(s)
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Push -GameEngine hlds -Game valve,cstrike
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Push -GameEngine srcds -Game cs2,csgo
# Create branches for specific game(s), and push to git remote 'upstream'
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Push -GameEngine hlds -Game valve,cstrike
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Push -GameEngine srcds -Game cs2,csgo
.EXAMPLE
# Update branches for all games, pulling and pushing to git remote 'origin'
./Generate-GitBranches.ps1 -Repo . -Pull -Push
# Update branches for all games, pulling and pushing to git remote 'upstream'
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Pull -Push
# Update branches for specific game(s), pulling and pushing to git remote 'origin'
./Generate-GitBranches.ps1 -Repo . -Pull -Push -GameEngine hlds -Game valve,cstrike
./Generate-GitBranches.ps1 -Repo . -Pull -Push -GameEngine srcds -Game cs2,csgo
# Update branches for specific game(s), pulling and pushing to git remote 'origin'
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Pull -Push -GameEngine hlds -Game valve,cstrike
./Generate-GitBranches.ps1 -Repo . -Remote upstream -Pull -Push -GameEngine srcds -Game cs2,csgo
#>
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory,HelpMessage="Target repo path")]
[ValidateNotNullOrEmpty()]
[string]$Repo
,
[Parameter(HelpMessage="Git remote. Default: 'origin'")]
[string]$Remote = 'origin'
,
[Parameter(HelpMessage="Whether to pull changes from remote repo before creating / updating branches")]
[switch]$Pull
,
[Parameter(HelpMessage="Whether to push changes after creating / updating branches")]
[switch]$Push
,
[Parameter(HelpMessage="Game platform. E.g. 'steam'. If unspecified, all game platforms are selected.")]
[string[]]$GamePlatform
,
[Parameter(HelpMessage="Game engine. E.g. 'hlds' or 'srcds'. If unspecified, all game engines are selected.")]
[string[]]$GameEngine
,
[Parameter(HelpMessage="Game. E.g. 'cstrike'. If unspecified, all games are selected.")]
[string[]]$Game
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# $ErrorView = 'NormalView'
# Get games
$games = Get-Content $PSScriptRoot/games.json -Encoding utf8 -Force | ConvertFrom-Json -AsHashtable
if ($GamePlatform) {
$games = $games | ? { $_['game_platform'] -in $GamePlatform }
}
if ($GameEngine) {
$games = $games | ? { $_['game_engine'] -in $GameEngine }
}
if ($Game) {
$games = $games | ? { $_['game'] -in $Game }
}
if ($games.Count -eq 0) {
throw "No games found"
}
function Execute-Command {
[CmdletBinding(DefaultParameterSetName='Default',SupportsShouldProcess)]
param (
[Parameter(Mandatory,ParameterSetName='Default',Position=0)]
[ValidateNotNull()]
[object]$Command
,
[Parameter(ValueFromPipeline,ParameterSetName='Pipeline')]
[object]$InputObject
)
process {
if ($InputObject) {
$Command = $InputObject
}
$scriptBlock = if ($Command -is [scriptblock]) {
$Command
}else {
# This is like Invoke-Expression, dangerous
[scriptblock]::Create($Command)
}
try {
"Command: $scriptBlock" | Write-Verbose
if ($PSCmdlet.ShouldProcess("$scriptBlock")) {
Invoke-Command $scriptBlock
}
"LASTEXITCODE: $global:LASTEXITCODE" | Write-Verbose
if ($ErrorActionPreference -eq 'Stop' -and $global:LASTEXITCODE) {
throw "Command exit code was $global:LASTEXITCODE. Command: $scriptBlock"
}
}catch {
if ($ErrorActionPreference -eq 'Stop') {
throw
}
if ($ErrorActionPreference -eq 'Continue') {
$_ | Write-Error -ErrorAction Continue
}
}
}
}
function Get-EnvFileKv ($file, $branch) {
$ErrorActionPreference = 'Stop'
$kv = [ordered]@{}
$branchFiles = { git ls-tree -r --name-only $branch } | Execute-Command
if ($branchFiles -contains $file) {
$content = { git --no-pager show "${branch}:${file}" } | Execute-Command
$content | % {
if ($_ -match '^#*\s*(\w+)=(.*)$') {
$kv[$matches[1]] = $matches[2]
}else {
throw "File '$file' is not in a valid k=v format. Invalid line: $_"
}
}
}
$kv
}
$sourceRef = ''
$isSameRepo = $false
try {
try {
$sourceRepo = { cd $PSScriptRoot; git rev-parse --show-toplevel; cd - } | Execute-Command -WhatIf:$false # Execute this even if -WhatIf is passed
}catch {
throw "$PSScriptRoot is not a git repo. Create a repo using: git init -b master; git commit -m 'Init' --allow-empty"
}
$sourceRef = { git rev-parse --abbrev-ref HEAD } | Execute-Command
try {
$Repo = { cd $Repo; git rev-parse --show-toplevel; cd - } | Execute-Command -WhatIf:$false # Execute this even if -WhatIf is passed
}catch {
throw "$Repo is not a git repo. Create a repo using: git init -b master; git commit -m 'Init' --allow-empty"
}
$isSameRepo = if ($Repo -eq $sourceRepo) { $true } else { $false }
Push-Location $Repo
foreach ($g in $games) {
$branch = "$( $g['game_platform'] )-$( $g['game_engine'] )-$( $g['game'] )"
{ git checkout -f master } | Execute-Command
if ($Pull) {
{ git pull "$Remote" master } | Execute-Command
}
$existingBranch = { git rev-parse --verify $branch 2>$null } | Execute-Command -ErrorAction SilentlyContinue
if ($existingBranch) {
"Updating branch '$branch'" | Write-Host -ForegroundColor Green
if ($Pull) {
{ git fetch "$Remote" } | Execute-Command
$existingRemoteBranch = { git rev-parse --verify "$Remote/$branch" 2>$null } | Execute-Command -ErrorAction SilentlyContinue
if ($existingRemoteBranch) {
{ git branch -f $branch "$Remote/$branch" } | Execute-Command
}
}
{ git checkout -f $branch } | Execute-Command
}else {
"Creating new branch '$branch'" | Write-Host -ForegroundColor Green
{ git checkout -b $branch } | Execute-Command
}
"Removing all tracked files" | Write-Host -ForegroundColor Green
# Get-ChildItem . -Exclude '.git' -Force | Remove-Item -Recurse -Force
{ git ls-files } | Execute-Command -WhatIf:$false | Remove-Item -Recurse -Force
"Checking out files" | Write-Host -ForegroundColor Green
if ($isSameRepo) {
{ git checkout $sourceRef -- build } | Execute-Command
{ git checkout $sourceRef -- update } | Execute-Command
{ git checkout $sourceRef -- build.sh } | Execute-Command
{ git checkout $sourceRef -- notify.sh } | Execute-Command
{ git checkout $sourceRef -- .gitlab-ci.yml } | Execute-Command
}else {
Copy-Item $sourceRepo/build . -Recurse -Force
Copy-Item $sourceRepo/update . -Recurse -Force
Copy-Item $sourceRepo/build.sh . -Force
Copy-Item $sourceRepo/notify.sh . -Force
Copy-Item $sourceRepo/.gitlab-ci.yml . -Force
}
$branchFiles = { git ls-tree -r --name-only $branch } | Execute-Command
$kv = Get-EnvFileKv '.env' $branch
if ($kv.Keys.Count) {
"Updating .env" | Write-Host -ForegroundColor Green
}else {
"Creating .env'" | Write-Host -ForegroundColor Green
}
@"
PIPELINE=update
GAME_UPDATE_COUNT=$( if ($kv.Contains('GAME_UPDATE_COUNT')) { $kv['GAME_UPDATE_COUNT'] } else { $g['game_update_count'] } )
GAME_VERSION=$( if ($kv.Contains('GAME_VERSION')) { $kv['GAME_VERSION'] } else { $g['game_version'] } )
APPID=$( $g['appid'] )
CLIENT_APPID=$( $g['client_appid'] )
GAME=$( $g['game'] )
MOD=$( $g['mod'] )
FIX_APPMANIFEST=
LATEST=true
CACHE=
NO_CACHE=
NO_PULL=
NO_TEST=
NO_PUSH=
DOCKER_REPOSITORY=$( $g['docker_repository'] )
#REGISTRY_USER=
#REGISTRY_PASSWORD=
STEAM_LOGIN=
#STEAM_USERNAME=
#STEAM_PASSWORD=
"@ | Out-File .env -Encoding utf8 -Force
$kv = Get-EnvFileKv '.state' $branch
if ($kv.Keys.Count) {
"Updating .state" | Write-Host -ForegroundColor Green
}else {
"Creating .state'" | Write-Host -ForegroundColor Green
}
@"
BUILD_STATUS=$( if ($kv.Contains('BUILD_STATUS')) { $kv['BUILD_STATUS'] } else { '' } )
BUILD_EPOCH=$( if ($kv.Contains('BUILD_EPOCH')) { $kv['BUILD_EPOCH'] } else { '0' } )
BUILD_PAUSE=$( if ($kv.Contains('BUILD_PAUSE')) { $kv['BUILD_PAUSE'] } else { '' } )
BASE_SIZE=$( if ($kv.Contains('BASE_SIZE')) { $kv['BASE_SIZE'] } else { '0' } )
LAYERED_SIZE=$( if ($kv.Contains('LAYERED_SIZE')) { $kv['LAYERED_SIZE'] } else { '0' } )
"@ | Out-File .state -Encoding utf8 -Force
if ($branchFiles -contains '.trigger') {
"Using existing '.trigger'" | Write-Host -ForegroundColor Green
{ git checkout "$branch" -- .trigger } | Execute-Command
}
"Creating .gitignore" | Write-Host -ForegroundColor Green
@"
/*
!/build/
/build/*
!/build/Dockerfile
!/update/
/update/*
!/update/Dockerfile
!/.env
!/.gitignore
!/.gitlab-ci.yml
!/.state
!/.trigger
!/build.sh
!/notify.sh
"@ | Out-File .gitignore -Encoding utf8 -Force
if (git status --porcelain 2>$null) {
"Committing files" | Write-Host -ForegroundColor Green
{ git add . } | Execute-Command
$msg = if ($existingBranch) { "Update files" } else { "Add files" }
{ git commit -m "$msg" } | Execute-Command
}else {
"Nothing to commit" | Write-Host -ForegroundColor Green
}
if ($Push) {
{ git push "$Remote" "$branch" } | Execute-Command
}
}
}catch {
throw
}finally {
if ($isSameRepo) {
{ git checkout $sourceRef } | Execute-Command # Restore the source repo's ref
}
Pop-Location
}