Skip to content

Commit

Permalink
Add support for update existing .env and .state
Browse files Browse the repository at this point in the history
  • Loading branch information
leojonathanoh committed Sep 16, 2023
1 parent 92eabfd commit 9cc614c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 44 deletions.
34 changes: 5 additions & 29 deletions Generate-GitBranches.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,17 @@ Describe "Generate-GitBranches.ps1" {

It "Parameter validation" {
{
./Generate-GitBranches.ps1 -ErrorAction Stop
& "$PSScriptRoot/Generate-GitBranches.ps1" -ErrorAction Stop
} | Should -Throw "-Path cannot be empty"
}

It "Creates branches of a target repo" {
./Generate-GitBranches.ps1 -TargetRepoPath $destinationRepo -Pull -ErrorAction Stop

$branches = git branch | % { $_.Trim() } | ? { $_ -match '^steam-' }
$branches.Count | Should -Be $games.Count
foreach ($b in $branches) {
git ls-tree -r --name-only $b | Should -Be @(
'.env'
'.gitlab-ci.yml'
'.state'
'build.sh'
'build/Dockerfile'
'notify.sh'
'update/Dockerfile'
)
}
}

It "Updates branches of a target repo" {
# Create branches first
It "Creates and updates branches of a target repo" {
$currentBranch = git rev-parse --abbrev-ref HEAD
if ($LASTEXITCODE) { throw }
foreach ($g in $games) {
$branch = "$( $g['game_platform'] )-$( $g['game_engine'] )-$( $g['game'] )"
git checkout -b "$branch"
if ($LASTEXITCODE) { throw }
}
git checkout "$currentBranch"
if ($LASTEXITCODE) { throw }

./Generate-GitBranches.ps1 -TargetRepoPath $destinationRepo -Pull -ErrorAction Stop
& "$PSScriptRoot/Generate-GitBranches.ps1" -TargetRepoPath $destinationRepo -Pull -ErrorAction Stop # Create
git checkout $currentBranch
& "$PSScriptRoot/Generate-GitBranches.ps1" -TargetRepoPath $destinationRepo -Pull -ErrorAction Stop # Update

$branches = git branch | % { $_.Trim() } | ? { $_ -match '^steam-' }
$branches.Count | Should -Be $games.Count
Expand Down
73 changes: 58 additions & 15 deletions Generate-GitBranches.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ if ($games.Count -eq 0) {
throw "No games found"
}

function Get-EnvFileKv ($file, $branch) {
$kv = [ordered]@{}
$branchFiles = git ls-tree -r --name-only $branch
if ($LASTEXITCODE) { throw }
if ($branchFiles -contains $file) {
$content = git --no-pager show "${branch}:${file}"
if ($LASTEXITCODE) { throw }
$content | % {
if ($_ -match '^(\w+)=(.*)$') {
$kv[$matches[1]] = $matches[2]
}else {
throw "File '$file' is not in a valid k=v format. Invalid line: $_"
}
}
}
$kv
}

# Create new branch, remove all files except .git, create .trigger file, create .gitlab-ci.yml, commit files
try {
Push-Location $TargetRepoPath
Expand Down Expand Up @@ -66,10 +84,11 @@ try {
if ($LASTEXITCODE) { throw }
}

"Removing files" | Write-Host -ForegroundColor Green
"Removing all files" | Write-Host -ForegroundColor Green
$repoDir = git rev-parse --show-toplevel
if ($LASTEXITCODE) { throw }
Get-ChildItem . -Exclude '.git', '.env', '.state', '.trigger' -Force | Remove-Item -Recurse -Force
Get-ChildItem . -Exclude '.git' -Force | Remove-Item -Recurse -Force

"Checking out files" | Write-Host -ForegroundColor Green
git checkout master -- build
if ($LASTEXITCODE) { throw }
Expand All @@ -82,9 +101,18 @@ try {
git checkout master -- .gitlab-ci.yml
if ($LASTEXITCODE) { throw }

$branchFiles = git ls-tree -r --name-only $branch
if ($LASTEXITCODE) { throw }

$kv = Get-EnvFileKv '.env' $branch
if ($kv.Keys.Count) {
"Updating '.env" | Write-Host -ForegroundColor Green
}else {
"Creating '.env'"| Write-Host -ForegroundColor Green
}
@"
PIPELINE=build
GAME_UPDATE_COUNT=$( $g['game_update_count'] )
GAME_UPDATE_COUNT=$( if ($kv.Contains('GAME_UPDATE_COUNT')) { $kv['GAME_UPDATE_COUNT'] } else { $g['game_update_count'] } )
GAME_VERSION=$( $g['game_version'] )
GAME_PLATFORM=$( $g['game_platform'] )
GAME_ENGINE=$( $g['game_engine'] )
Expand All @@ -98,19 +126,34 @@ DOCKER_REPOSITORY=$( $g['docker_repository'] )
STEAM_LOGIN=
"@ | Out-File .env -Encoding utf8 -Force

@'
BUILD_STATUS=
BUILD_EPOCH=0
BASE_SIZE=0
LAYERED_SIZE=0
'@ | Out-File .state -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' } )
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

"Committing files" | Write-Host -ForegroundColor Green
git add .
if ($LASTEXITCODE) { throw }
$msg = if ($existingBranch) { "Update files" } else { "Add files" }
git commit -m "$msg"
if ($LASTEXITCODE) { throw }
if ($branchFiles -contains '.trigger') {
"Using existing '.trigger'" | Write-Host -ForegroundColor Green
git checkout "$branch" -- .trigger
}

if (git status --porcelain 2>$null) {
"Committing files" | Write-Host -ForegroundColor Green
git add .
if ($LASTEXITCODE) { throw }
$msg = if ($existingBranch) { "Update files" } else { "Add files" }
git commit -m "$msg"
if ($LASTEXITCODE) { throw }
}else {
"Nothing to commit" | Write-Host -ForegroundColor Green
}
}
}catch {
throw
Expand Down

0 comments on commit 9cc614c

Please sign in to comment.