From 8a93a0f123533080a4dd5e61d725707aeaae8fc7 Mon Sep 17 00:00:00 2001 From: lksz Date: Sun, 4 Apr 2021 14:42:29 -0400 Subject: [PATCH] git related improvements and implementations + Get-GitRepo - Clone or update a repo, with tag support + Get-GitHubReleaseTag - Grab tag from latest release page + Refresh-GitRepo - Stage all possible changes, discarding any previously stageds changes = GitHub repo is now git repo - as some code isn't GitHub specific + cat Alias for Get-Content = Get-PossiblArguments improvement = Invoke-WebDownload fix = dcedit fixing bad argument processing --- GitHub/_.package.json | 6 ----- base/Aliases/cat.ps1 | 1 + base/Get-PossibleArguments.ps1 | 10 ++++++- base/Invoke-WebDownload.ps1 | 2 +- docker/Edit-DockerCompose.ps1 | 14 +++++++--- git/Edit-GitIgnore.ps1 | 2 ++ {GitHub => git}/Get-GitHubReleaseAssets.ps1 | 0 git/Get-GitHubReleaseTag.ps1 | 11 ++++++++ git/Get-GitRepo.ps1 | 29 +++++++++++++++++++++ git/Refresh-GitRepo.ps1 | 7 +++++ git/_.package.json | 14 ++++++++++ 11 files changed, 85 insertions(+), 11 deletions(-) delete mode 100644 GitHub/_.package.json create mode 100644 base/Aliases/cat.ps1 create mode 100644 git/Edit-GitIgnore.ps1 rename {GitHub => git}/Get-GitHubReleaseAssets.ps1 (100%) create mode 100644 git/Get-GitHubReleaseTag.ps1 create mode 100644 git/Get-GitRepo.ps1 create mode 100644 git/Refresh-GitRepo.ps1 create mode 100644 git/_.package.json diff --git a/GitHub/_.package.json b/GitHub/_.package.json deleted file mode 100644 index 5d501ae..0000000 --- a/GitHub/_.package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "package": { - "Condition": [], - "Name": "GitHub" - } -} diff --git a/base/Aliases/cat.ps1 b/base/Aliases/cat.ps1 new file mode 100644 index 0000000..630f589 --- /dev/null +++ b/base/Aliases/cat.ps1 @@ -0,0 +1 @@ +Get-Content diff --git a/base/Get-PossibleArguments.ps1 b/base/Get-PossibleArguments.ps1 index 0a1ade0..0de87a6 100644 --- a/base/Get-PossibleArguments.ps1 +++ b/base/Get-PossibleArguments.ps1 @@ -5,7 +5,15 @@ ) $local:possibleValues = $fullValueSet if( $wordToComplete ) { - $possibleValues = $possibleValues | Where-Object { $_ -match $wordToComplete } + $possibleValues = $possibleValues | + Where-Object { $_ -match $wordToComplete } | + ForEach-Object { + if( $_ -match ' ' ) { + "'$_'" + } else { + $_ + } + } if( -not $strict -and ($wordToComplete -notin $possibleValues) ) { $possibleValues = $( $wordToComplete; $possibleValues ) } diff --git a/base/Invoke-WebDownload.ps1 b/base/Invoke-WebDownload.ps1 index 35da14d..42033a3 100644 --- a/base/Invoke-WebDownload.ps1 +++ b/base/Invoke-WebDownload.ps1 @@ -33,7 +33,7 @@ process { $outputFileName = Join-Path $OutputPath $outputFileName if( (Test-Path $outputFileName) -and -not $Force) { - throw "$OutputPath already exists" + throw "$OutputFileName file already exists" } Write-Verbose "About to start download from:`n`t$Url`n`tto`n`t$outputFileName`n" diff --git a/docker/Edit-DockerCompose.ps1 b/docker/Edit-DockerCompose.ps1 index 3bd9ef4..ebda20c 100644 --- a/docker/Edit-DockerCompose.ps1 +++ b/docker/Edit-DockerCompose.ps1 @@ -6,7 +6,7 @@ ) -$local:EditCandidates = @('docker-compose.###','.base.docker-compose.###') +$local:EditCandidates = @('docker-compose.###','.base.docker-compose.###','secrets.env') $local:EditBonus = @('Dockerfile', '.env', '*.env') $local:editFiles = @() foreach( $local:ext in @('.yml', '.yaml') ) { @@ -15,7 +15,7 @@ foreach( $local:ext in @('.yml', '.yaml') ) { foreach( $local:path in $ProjectPath ) { $local:testPath = Join-Path $path $fileName if( $Force -or $(Test-Path $testPath) ) { - $editFiles += $testPath + $editFiles += $testPath | Get-Path $editFiles += Get-ChildItem $(Join-Path $path '*') -Include $EditBonus -Depth 1 | Select-Object -ExpandProperty FullName } @@ -37,9 +37,17 @@ $AdditionalFiles = $AdditionalFiles | Where-Object { $_ } | ForEach-Object { $local:FinalEditList = $editFiles | Where-Object {$_} | Sort-Object { $local:n = [System.IO.Path]::GetFileName($_) - Join-Path [PSystem.IO.ath]::GetDirectoryName($_) "$(if( $n[0] -eq '.' ) { "9" } else { "1" })$n" + $local:sorder = "$( + if( $n[0] -eq '.' ) { "2" } + elseif( $n -match 'docker-compose\.y' ) { "1" } + else { "9" } + )$n" + Write-Verbose "sorder($n)=$sorder" + $local:sorder = Join-Path $([System.IO.Path]::GetDirectoryName($_)) $sorder + $sorder } $FinalEditList = (@() + $FinalEditList + @() + $AdditionalFiles) | Select-Object -Unique +Write-Verbose "`$FinalEditList = `n$($FinalEditList | Out-String)" Edit-TextFile $FinalEditList diff --git a/git/Edit-GitIgnore.ps1 b/git/Edit-GitIgnore.ps1 new file mode 100644 index 0000000..9ffbc21 --- /dev/null +++ b/git/Edit-GitIgnore.ps1 @@ -0,0 +1,2 @@ +$local:workTree = git worktree list --porcelain | Select-String worktree | ForEach-Object { $_ -split 'worktree ' } | Where-Object { $_ } +Edit-TextFile $(Join-Path $workTree .gitignore) diff --git a/GitHub/Get-GitHubReleaseAssets.ps1 b/git/Get-GitHubReleaseAssets.ps1 similarity index 100% rename from GitHub/Get-GitHubReleaseAssets.ps1 rename to git/Get-GitHubReleaseAssets.ps1 diff --git a/git/Get-GitHubReleaseTag.ps1 b/git/Get-GitHubReleaseTag.ps1 new file mode 100644 index 0000000..ef496de --- /dev/null +++ b/git/Get-GitHubReleaseTag.ps1 @@ -0,0 +1,11 @@ +[CmdletBinding()]param( + [Parameter(Mandatory)] + [string]$GitHubProject, + [string]$tag='latest' +) + +Invoke-WebRequest https://github.com/$GitHubProject/releases/$tag -ErrorAction Stop | + Select-Object -ExpandProperty Links | + Where-Object title | + Where-Object href -match 'tree/[^/]+$' | + Select-Object -ExpandProperty title -First 1 diff --git a/git/Get-GitRepo.ps1 b/git/Get-GitRepo.ps1 new file mode 100644 index 0000000..342c77a --- /dev/null +++ b/git/Get-GitRepo.ps1 @@ -0,0 +1,29 @@ +[CmdletBinding(SupportsShouldProcess)]param( + [Parameter(Mandatory)] + [string]$LocalPath, + [Parameter(Mandatory)] + [string]$GitOrigin, + [string]$Branch='HEAD' +) +$local:gitPath = Join-Path 'themes' 'plextheme' | Get-Path +function doGit { git "--git-dir=`"$(Join-Path $LocalPath '.git')`"" "--work-tree=`"$LocalPath`"" $args } +$local:savedErrorActionPreference = $ErrorActionPreference +$ErrorActionPreference = 'Stop' +$local:exists = Test-Path $LocalPath +try { + if( $exists ) { + # Update code from git repo + Write-Verbose "Pulling from [$GitOrigin] into [$LocalPath] on branch/tag '$Branch'..." + doGit fetch --quiet + } else { + git clone $GitOrigin $LocalPath + } + doGit checkout $Branch --quiet + if( $exists ) { + doGit status + } +} catch { + throw $_ +} finally { + $ErrorActionPreference = $savedErrorActionPreference +} diff --git a/git/Refresh-GitRepo.ps1 b/git/Refresh-GitRepo.ps1 new file mode 100644 index 0000000..dab380d --- /dev/null +++ b/git/Refresh-GitRepo.ps1 @@ -0,0 +1,7 @@ +$local:workTree = git worktree list --porcelain | Select-String worktree | ForEach-Object { $_ -split 'worktree ' } | Where-Object { $_ } +Push-Location $workTree +$null = git reset HEAD +$null = git rm --cached -r . +$null = git add . +git status +Pop-Location diff --git a/git/_.package.json b/git/_.package.json new file mode 100644 index 0000000..fa2c07c --- /dev/null +++ b/git/_.package.json @@ -0,0 +1,14 @@ +{ + "package": { + "Condition": [ + { + "custom": "Get-Command git -Type Application", + "System": null, + "Hostname": null, + "Username": null, + "Logic": 0 + } + ], + "Name": "git" + } +}