This commit is contained in:
lksz 2021-04-16 15:45:54 +00:00
commit 2b51f37cb7
14 changed files with 176 additions and 66 deletions

View File

@ -1,6 +0,0 @@
{
"package": {
"Condition": [],
"Name": "GitHub"
}
}

1
base/Aliases/cat.ps1 Normal file
View File

@ -0,0 +1 @@
Get-Content

View File

@ -14,10 +14,10 @@
[string[]]$AlsoEditTheseScripts [string[]]$AlsoEditTheseScripts
) )
$local:FilePaths = @() + $( Get-Profiles | Where-Object { $_.Exists -or $Force } | Select-Object -ExpandProperty Path ) [string[]]$local:FilePaths = @() + $( Get-Profiles | Where-Object { $_.Exists -or $Force } | Select-Object -ExpandProperty Path )
foreach( $local:p in $AlsoEditTheseScripts ) { foreach( $local:p in $AlsoEditTheseScripts ) {
$local:sp = Join-Path $MyPSScriptRoot "$p`.ps1" $local:sp = Join-Path $MyPSScriptRoot "$p`.ps1"
$FilePaths += $sp $FilePaths += $sp
} }
Edit-TextFile -sudo:$sudo $FilePaths Edit-TextFile -sudo:$sudo -Path $FilePaths

View File

@ -1,31 +1,44 @@
[CmdletBinding(SupportsShouldProcess)]param( [CmdletBinding(SupportsShouldProcess)]param(
[switch]$sudo, [switch]$sudo,
[Parameter(Position = 0, ValueFromRemainingArguments = $true)] [Parameter(Position = 0, ValueFromPipeline, ValueFromRemainingArguments = $true)]
[string[]]$Path [string[]]$Path
) )
$local:editors = $env:EDITOR,'nvim','code' begin {
$local:editor = $null $PathForEditor = @()
foreach( $local:testEditor in $editors ) { }
if( $(try{Get-Command -Type Application $testEditor -ErrorAction SilentlyContinue}catch{}) ) { process {
$editor = $testEditor foreach( $local:P in $Path ) {
break; $PathForEditor += $P | Get-Path -Expand
} }
} }
if( $editor -match 'vim?$' ) { end {
$editor += ' -p' $local:editors = $env:EDITOR,'nvim','code'
} $local:editor = $null
foreach( $local:testEditor in $editors ) {
if( $(try{Get-Command -Type Application $testEditor -ErrorAction SilentlyContinue}catch{}) ) {
$editor = $testEditor
break;
}
}
if( $editor -match 'code(\.exe)?$' ) { if( $editor -match 'vim?$' ) {
if( -not (Get-Process -Name 'code' -ErrorAction SilentlyContinue) ) { $editor += ' -p'
Invoke-ExpressionEx -sudo:$sudo $editor }
if( $editor -match 'code(\.exe)?$' ) {
if( -not (Get-Process -Name 'code' -ErrorAction SilentlyContinue) ) {
Invoke-ExpressionEx -sudo:$sudo $editor
}
}
Write-Verbose $($Path | Out-String)
$local:arguments = $PathForEditor | Foreach-Object { "'$_'" }
if( $Path ) { $arguments = "$arguments" }
if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) {
Invoke-ExpressionEx -sudo:$sudo $editor "$arguments"
} }
} }
$local:arguments = $Path | Get-Path -Expand | Join-String -Separator "' '"
if( $Path ) { $arguments = "'$arguments'" }
if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) {
Invoke-ExpressionEx -sudo:$sudo $editor "$arguments"
}

View File

@ -5,7 +5,15 @@
) )
$local:possibleValues = $fullValueSet $local:possibleValues = $fullValueSet
if( $wordToComplete ) { 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) ) { if( -not $strict -and ($wordToComplete -notin $possibleValues) ) {
$possibleValues = $( $wordToComplete; $possibleValues ) $possibleValues = $( $wordToComplete; $possibleValues )
} }

View File

@ -33,7 +33,7 @@ process {
$outputFileName = Join-Path $OutputPath $outputFileName $outputFileName = Join-Path $OutputPath $outputFileName
if( (Test-Path $outputFileName) -and -not $Force) { 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" Write-Verbose "About to start download from:`n`t$Url`n`tto`n`t$outputFileName`n"

View File

@ -1,8 +1,31 @@
[CmdletBinding(SupportsShouldProcess)]param() [CmdletBinding(SupportsShouldProcess)]param()
try { Get-Alias Get-MyAliases -ErrorAction Stop | ForEach-Object { Remove-Item "Alias:$($_.Name)" } } catch {} function RemoveAlias {
param([string]$Name)
$local:a = $Name | Where-Object { $_ } | Get-Alias -ErrorAction SilentlyContinue
if ( -not $a ) { return }
if ( Get-Command Remove-Alias -ErrorAction SilentlyContinue ) {
$a | Remove-Alias -Scope $MyAliasScope -ErrorAction SilentlyContinue
}
else {
Remove-Item "Alias:$a"
}
}
function SafeSetAlias {
param([string]$Name, [string]$Value, [string]$Description)
. RemoveAlias $Name
try {
Set-Alias $Name $Value -Scope $MyAliasScope -Description:$Description -ErrorAction Stop
}
catch {
Set-Alias $Name $Value -Scope $MyAliasScope -Description:$Description -Option AllScope
}
}
try { Get-Alias Get-MyAliases -ErrorAction Stop | ForEach-Object { . RemoveAlias $($_.Name) } } catch {}
function Get-MyAliases { function Get-MyAliases {
[CmdletBinding(SupportsShouldProcess)]param([switch]$ScriptsOnly) [CmdletBinding()]param([switch]$ScriptsOnly)
$local:allAliases = @() $local:allAliases = @()
@ -13,16 +36,17 @@ function Get-MyAliases {
try { try {
$local:newAliases += Get-Alias -Scope $_scope | $local:newAliases += Get-Alias -Scope $_scope |
Where-Object { Where-Object {
($_.Definition -match "^$MyPSScriptRoot") -or (-not $ScriptsOnly -and ($_.Description -match '#MyAlias')) ($_.Definition -like "$MyPSScriptRoot*") -or (-not $ScriptsOnly -and ($_.Description -match '#MyAlias'))
} }
if( $newAliases ) { if ( $newAliases ) {
$allAliases += $newAliases $allAliases += $newAliases
$MyAliasScope = $_scope; $MyAliasScope = $_scope;
Write-Verbose "`$MyAliasScope is now set to $MyAliasScope" Write-Verbose "`$MyAliasScope is now set to $MyAliasScope"
} }
} catch { }
catch {
Write-Verbose "catch: $($_.Exception.Message)" Write-Verbose "catch: $($_.Exception.Message)"
$_done = $_.Exception.Message -match 'The scope .* exceeds' $_done = $_.Exception.Message -match 'The scope .* exceeds'
} }
$_scope += 1 $_scope += 1
} until ( $_done ) } until ( $_done )
@ -30,8 +54,9 @@ function Get-MyAliases {
$allAliases $allAliases
} }
function getScriptName{param([string]$FullPath) function getScriptName {
$FullPath -replace '\.ps1$','' -replace "^$([regex]::Escape($MyPSScriptRoot)).",'' param([string]$FullPath)
$FullPath -replace '\.ps1$', '' -replace "^$([regex]::Escape($MyPSScriptRoot)).", ''
} }
$local:IsVerbose = [bool]($PSBoundParameters['Verbose']) $local:IsVerbose = [bool]($PSBoundParameters['Verbose'])
@ -39,7 +64,8 @@ $local:IsVerbose = [bool]($PSBoundParameters['Verbose'])
# Loads mandatory Package code # Loads mandatory Package code
try { try {
$null = [Packagesz] $null = [Packagesz]
} catch { }
catch {
$local:PackagePath = $(Join-Path $MyPSScriptRoot 'base') $local:PackagePath = $(Join-Path $MyPSScriptRoot 'base')
Join-Path $(Join-Path $PackagePath profile.d) 'classes.ps1' | Join-Path $(Join-Path $PackagePath profile.d) 'classes.ps1' |
Where-Object { Test-Path $_ } | ForEach-Object { . $_ } Where-Object { Test-Path $_ } | ForEach-Object { . $_ }
@ -62,21 +88,17 @@ $local:oldAliases = Get-MyAliases
$oldAliases = Get-Alias -Scope $MyAliasScope | $oldAliases = Get-Alias -Scope $MyAliasScope |
Where-Object Name -in $($oldAliases.Name + $myAliases.Keys) Where-Object Name -in $($oldAliases.Name + $myAliases.Keys)
if( $oldAliases -and $IsVerbose ) { if ( $oldAliases -and $IsVerbose ) {
Write-Verbose "Removing: $($oldAliases.Name -join ', ')" Write-Verbose "Removing: $($oldAliases.Name -join ', ')"
} }
if( Get-Command Remove-Alias -ErrorAction SilentlyContinue ) { $oldAliases | ForEach-Object { . RemoveAlias $($_.Name) }
$oldAliases | Remove-Alias -Scope $MyAliasScope
} else {
$oldAliases | ForEach-Object { Remove-Item "Alias:$($_.Name)" }
}
if( $(. Get-ScopeDepth) -gt 0 ) { if ( $(. Get-ScopeDepth) -gt 0 ) {
Write-Host -ForegroundColor Red "Try sourcing Reload-MyScripts instead of just running it" Write-Host -ForegroundColor Red "Try sourcing Reload-MyScripts instead of just running it"
} }
foreach( $local:PackagePath in $myPackages ) { foreach ( $local:PackagePath in $myPackages ) {
$local:PackageName = $PackagePath.Name #$local:PackageName = $PackagePath.Name
Join-Path $PackagePath 'profile.d' | Where-Object { Test-Path $_ } | Join-Path $PackagePath 'profile.d' | Where-Object { Test-Path $_ } |
Get-ChildItem -Filter '*.ps1' | Get-ChildItem -Filter '*.ps1' |
@ -88,24 +110,25 @@ foreach( $local:PackagePath in $myPackages ) {
$local:CommandsToAlias = ( $local:CommandsToAlias = (
@( $PackagePath ) + $( @( $PackagePath ) + $(
[SystemName]::_GetValidValues("",$true,$true) | [SystemName]::_GetValidValues("", $true, $true) |
ForEach-Object { ForEach-Object {
Join-Path $PackagePath "sys.$_" Join-Path $PackagePath "sys.$_"
} }
)) | )) |
Where-Object { Test-Path $_ } | Where-Object { Test-Path $_ } |
ForEach-Object { ForEach-Object {
Get-ChildItem (Join-Path $_ '*.ps1') | Where-Object Name -notmatch '\.inc\.ps1$' Get-ChildItem (Join-Path $_ '*.ps1') | Where-Object Name -notmatch '\.inc\.ps1$'
} }
$CommandsToAlias | ForEach-Object { foreach ( $local:newAlias in $CommandsToAlias ) {
Write-Verbose "Creating alias for $(getScriptName $_.FullName) Script..." Write-Verbose "Creating alias for $(getScriptName $newAlias.FullName) Script..."
Set-Alias $($_.BaseName) $_.FullName -Scope $MyAliasScope . SafeSetAlias $($newAlias.BaseName) $($newAlias.FullName)
} }
} }
foreach( $local:alias in $myAliases.Keys ) { foreach ( $local:alias in $myAliases.Keys ) {
Write-Verbose "Adding $($alias) alias..." Write-Verbose "Adding $($alias) alias..."
Set-Alias -Name $alias -Value $myAliases[$alias] -Description '#MyAlias' -Scope $MyAliasScope . SafeSetAlias $alias $myAliases[$alias] '#MyAlias'
} }
Remove-Item Function:\RemoveAlias, Function:\SafeSetAlias -ErrorAction SilentlyContinue

View File

@ -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:EditBonus = @('Dockerfile', '.env', '*.env')
$local:editFiles = @() $local:editFiles = @()
foreach( $local:ext in @('.yml', '.yaml') ) { foreach( $local:ext in @('.yml', '.yaml') ) {
@ -15,7 +15,7 @@ foreach( $local:ext in @('.yml', '.yaml') ) {
foreach( $local:path in $ProjectPath ) { foreach( $local:path in $ProjectPath ) {
$local:testPath = Join-Path $path $fileName $local:testPath = Join-Path $path $fileName
if( $Force -or $(Test-Path $testPath) ) { if( $Force -or $(Test-Path $testPath) ) {
$editFiles += $testPath $editFiles += $testPath | Get-Path
$editFiles += Get-ChildItem $(Join-Path $path '*') -Include $EditBonus -Depth 1 | $editFiles += Get-ChildItem $(Join-Path $path '*') -Include $EditBonus -Depth 1 |
Select-Object -ExpandProperty FullName Select-Object -ExpandProperty FullName
} }
@ -37,9 +37,17 @@ $AdditionalFiles = $AdditionalFiles | Where-Object { $_ } | ForEach-Object {
$local:FinalEditList = $editFiles | Where-Object {$_} | Sort-Object { $local:FinalEditList = $editFiles | Where-Object {$_} | Sort-Object {
$local:n = [System.IO.Path]::GetFileName($_) $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 $FinalEditList = (@() + $FinalEditList + @() + $AdditionalFiles) | Select-Object -Unique
Write-Verbose "`$FinalEditList = `n$($FinalEditList | Out-String)"
Edit-TextFile $FinalEditList Edit-TextFile $FinalEditList

2
git/Edit-GitIgnore.ps1 Normal file
View File

@ -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)

View File

@ -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

29
git/Get-GitRepo.ps1 Normal file
View File

@ -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
}

7
git/Refresh-GitRepo.ps1 Normal file
View File

@ -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

14
git/_.package.json Normal file
View File

@ -0,0 +1,14 @@
{
"package": {
"Condition": [
{
"custom": "Get-Command git -Type Application",
"System": null,
"Hostname": null,
"Username": null,
"Logic": 0
}
],
"Name": "git"
}
}