Setup-Profile fixes + Edit-TextFile fixes

Setup-Profile: GitClone verbosity
Fixed: Existing $profile code will not be overwritten,
   instead profile code will be inserted before existing code.
Fixed: status message is reflected correctly:
  * Setup (replace/append)
  * Init (file created)
  * Clenaup (removal of code / deletion)
When profile code is removed, a placeholder remains behind,
  this will allow correct replacement if another setup is performed

Edit-TextFile:
Switched to Invoke-ExpressionEx for sudo invocation.
This commit is contained in:
Gal Szkolnik 2020-09-30 16:43:07 -04:00
parent 7117e78a7a
commit dc9f29f471
2 changed files with 228 additions and 218 deletions

View File

@ -7,7 +7,7 @@
$local:editors = $env:EDITOR,'nvim','code'
$local:editor = $null
foreach( $local:testEditor in $editors ) {
if( Get-Command -Type Application $testEditor -ErrorAction SilentlyContinue ) {
if( $(try{Get-Command -Type Application $testEditor -ErrorAction SilentlyContinue}catch{}) ) {
$editor = $testEditor
break;
}
@ -17,13 +17,9 @@ if( $editor -match 'vim?$' ) {
$editor += ' -p'
}
if( $sudo ) {
$editor = "/usr/bin/env sudo $editor"
}
if( $editor -match 'code(\.exe)?$' ) {
if( -not (Get-Process -Name 'code' -ErrorAction SilentlyContinue) ) {
& $editor
Invoke-ExpressionEx -sudo:$sudo $editor
}
}
@ -31,5 +27,5 @@ $local:arguments = $Path -join "' '"
if( $Path ) { $arguments = "'$arguments'" }
if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) {
Invoke-Expression "$editor $arguments"
Invoke-ExpressionEx -sudo:$sudo $editor "$arguments"
}

View File

@ -37,7 +37,7 @@ function ProfileCode_post_common {
Get-Command Reload-MyScripts -ErrorAction SilentlyContinue | ForEach-Object { . $_.Name }
}
#######################################################################
## Setup-Profile code continues below
## _Setup Logic starts here
#######################################################################
if ( $GitClone -and -not (Test-Path $(Join-Path $MyPSScriptRoot '.git')) ) {
@ -49,20 +49,25 @@ if ( $GitClone -and -not (Test-Path $(Join-Path $MyPSScriptRoot '.git')) ) {
$local:tmpGitDir = New-TemporaryFile
Remove-Item $tmpGitDir
New-Item -Type Directory $tmpGitDir.FullName | Out-Null
Write-Verbose "Cloning git repo [$GitURL] into temporary location ('$($tmpGitDir.FullName)')..."
$local:GitOutput = "$(& git clone $GitURL $tmpGitDir.FullName --no-checkout)"
Write-Verbose $GitOutput
Write-Verbose "Moving git repo from temp location to $MyPSScriptRoot . . ."
Move-Item (Join-Path $tmpGitDir.FullName .git) ./.git
Remove-Item $tmpGitDir.FullName
Write-Verbose "Checking out repo..."
$GitOutput = "$(& git checkout --force)"
Write-Verbose $GitOutput
Pop-Location
Write-Verbose "-GitClone step done."
}
}
$local:ProfileSignature = [PSCustomObject]([ordered]@{
Begin = '#### SZ Auto Profile Setup - BEGIN ####'
End = '#### SZ Auto Profile Setup - END ####'
Removed = '#### SZ Auto Profile Setup'
Removed = '#### SZ Auto Profile Setup - ----- ####'
Match = '#### SZ Auto Profile Setup'
})
function New-ProfileSetupStatus {
@ -96,7 +101,8 @@ $_profiles | Foreach-Object {
$local:exists = Test-Path $p
$local:userFile = $($_ -match 'CurrentUser')
$local:cleanupValue = ''
if ( $exists -and ((Get-Content -Path $p | Out-String ) -match $ProfileSignature.Begin ) ) {
if ( $exists ) {
if ( (Get-Content -Path $p | Out-String ) -match $ProfileSignature.Match ) {
Write-Verbose "Found code in [$_]'$p', preparing..."
$local:gate = $true;
$profileContent = Get-Content $p | ForEach-Object {
@ -106,15 +112,22 @@ $_profiles | Foreach-Object {
}
}
else {
if ( $_ -match $ProfileSignature.Match ) {
if ( $_ -match $ProfileSignature.Begin ) {
$gate = $false;
}
$cleanupValue = $ProfileSignature.Removed
return $randomSeed;
}
return $_
}
}
} else {
$profileContent += "`n`n$(Get-Content $p -Raw)"
}
}
if( -not( $profileContent -match $randomSeed ) ) { $profileContent = "$randomSeed`n$profileContent" }
$local:insertProfileCode = $false;
if ( ($p -eq $_profile) -and -not $RemoveOnly ) {
@ -135,23 +148,24 @@ $_profiles | Foreach-Object {
$ProfileSignature.End
) -join "`n" ).Trim().Replace(('$' + '_'), ('$$' + '_')))
}
$profileContent = $($profileContent -replace $randomSeed, $cleanupValue).Trim()
$local:shouldSudo = $sudo -and -not $userFile
$local:status = 'Skipped'
if ( $VerbosePreference -eq 'Continue' ) { $ShowSkipped = $true }
# Test if new profile Content will be empty
if ( -not ([string]::IsNullOrWhiteSpace($profileContent)) ) {
$status = "Need $(if($insertProfileCode){'Setup'}else{'Cleanup'})"
$status = "Need $(if($insertProfileCode){if($exists){'Setup'}else{'Init'}}else{'Cleanup'})"
if ( $sudo -or $userFile ) {
Write-Verbose "Writing content to $($_)..."
$local:tmpOutput = New-TemporaryFile -WhatIf:$false
$profileContent | Out-File $tmpOutput.FullName -WhatIf:$false
$local:tmpOutput = New-TemporaryFile -WhatIf:$false -Confirm:$false
$profileContent | Out-File $tmpOutput.FullName -WhatIf:$false -Confirm:$false
if ( $shouldSudo -and $( $PSVersionTable.Platform -eq 'Unix' ) ) {
# Make sure the file has the right permissions on a Unix machine
Invoke-ExpressionEx -sudo:$shouldSudo chmod 'a+r' $tmpOutput.FullName
}
try {
function BreakPointHere {} BreakPointHere;
$local:errMsg = $(Invoke-ExpressionEx -sudo:$shouldSudo "Move-Item $($tmpOutput.FullName) $p -Force:`$$Force -ErrorAction Stop | Out-Null" 2>&1)
if ( $errMsg ) {
Write-Error "$errMsg"
@ -350,7 +364,7 @@ $local:src = ""
foreach ( $local:func in $FunctionName ) {
if ( -not $NoHeader ) { $src += "`nfunction $func {" }
$src += "`n"
$src += $((Get-Command -Type Function $func).Definition) ### .Replace(('$'+'_'),('$$'+'_'))
$src += $((Get-Command -Type Function $func).Definition)
if ( -not $NoHeader ) { $src += "`n}" }
$src += "`n"
}