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:
parent
7117e78a7a
commit
dc9f29f471
|
@ -7,7 +7,7 @@
|
||||||
$local:editors = $env:EDITOR,'nvim','code'
|
$local:editors = $env:EDITOR,'nvim','code'
|
||||||
$local:editor = $null
|
$local:editor = $null
|
||||||
foreach( $local:testEditor in $editors ) {
|
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
|
$editor = $testEditor
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -17,13 +17,9 @@ if( $editor -match 'vim?$' ) {
|
||||||
$editor += ' -p'
|
$editor += ' -p'
|
||||||
}
|
}
|
||||||
|
|
||||||
if( $sudo ) {
|
|
||||||
$editor = "/usr/bin/env sudo $editor"
|
|
||||||
}
|
|
||||||
|
|
||||||
if( $editor -match 'code(\.exe)?$' ) {
|
if( $editor -match 'code(\.exe)?$' ) {
|
||||||
if( -not (Get-Process -Name 'code' -ErrorAction SilentlyContinue) ) {
|
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( $Path ) { $arguments = "'$arguments'" }
|
||||||
|
|
||||||
if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) {
|
if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) {
|
||||||
Invoke-Expression "$editor $arguments"
|
Invoke-ExpressionEx -sudo:$sudo $editor "$arguments"
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,22 +25,22 @@ function _Setup {
|
||||||
## ProfileCode function (source) will contain the code to be written
|
## ProfileCode function (source) will contain the code to be written
|
||||||
#######################################################################
|
#######################################################################
|
||||||
function ProfileCode_pre_common {
|
function ProfileCode_pre_common {
|
||||||
# # If TMUX isn't loaded, start byobu, then exit this script (and current shell)
|
# # If TMUX isn't loaded, start byobu, then exit this script (and current shell)
|
||||||
# if ( -not ( Test-Path env:TMUX ) ) { byobu; exit }
|
# if ( -not ( Test-Path env:TMUX ) ) { byobu; exit }
|
||||||
}
|
}
|
||||||
#######################################################################
|
#######################################################################
|
||||||
## common Profile Code - receommended not to change this
|
## common Profile Code - receommended not to change this
|
||||||
## Code will be loaded from profile.d sub-directory after this.
|
## Code will be loaded from profile.d sub-directory after this.
|
||||||
#######################################################################
|
#######################################################################
|
||||||
function ProfileCode_post_common {
|
function ProfileCode_post_common {
|
||||||
# This loads the personal profile section from the $MyPSScriptRoot/profile.d directory
|
# This loads the personal profile section from the $MyPSScriptRoot/profile.d directory
|
||||||
Get-Command Reload-MyScripts -ErrorAction SilentlyContinue | ForEach-Object { . $_.Name }
|
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')) ) {
|
if ( $GitClone -and -not (Test-Path $(Join-Path $MyPSScriptRoot '.git')) ) {
|
||||||
if ( -not [bool]$(Get-Command git -ErrorAction SilentlyContinue) ) {
|
if ( -not [bool]$(Get-Command git -ErrorAction SilentlyContinue) ) {
|
||||||
throw "No git command found, you may either omit the -GitClone switch or install git and try again."
|
throw "No git command found, you may either omit the -GitClone switch or install git and try again."
|
||||||
}
|
}
|
||||||
|
@ -49,54 +49,60 @@ if ( $GitClone -and -not (Test-Path $(Join-Path $MyPSScriptRoot '.git')) ) {
|
||||||
$local:tmpGitDir = New-TemporaryFile
|
$local:tmpGitDir = New-TemporaryFile
|
||||||
Remove-Item $tmpGitDir
|
Remove-Item $tmpGitDir
|
||||||
New-Item -Type Directory $tmpGitDir.FullName | Out-Null
|
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)"
|
$local:GitOutput = "$(& git clone $GitURL $tmpGitDir.FullName --no-checkout)"
|
||||||
Write-Verbose $GitOutput
|
Write-Verbose $GitOutput
|
||||||
|
Write-Verbose "Moving git repo from temp location to $MyPSScriptRoot . . ."
|
||||||
Move-Item (Join-Path $tmpGitDir.FullName .git) ./.git
|
Move-Item (Join-Path $tmpGitDir.FullName .git) ./.git
|
||||||
Remove-Item $tmpGitDir.FullName
|
Remove-Item $tmpGitDir.FullName
|
||||||
|
Write-Verbose "Checking out repo..."
|
||||||
$GitOutput = "$(& git checkout --force)"
|
$GitOutput = "$(& git checkout --force)"
|
||||||
Write-Verbose $GitOutput
|
Write-Verbose $GitOutput
|
||||||
Pop-Location
|
Pop-Location
|
||||||
|
Write-Verbose "-GitClone step done."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$local:ProfileSignature = [PSCustomObject]([ordered]@{
|
$local:ProfileSignature = [PSCustomObject]([ordered]@{
|
||||||
Begin = '#### SZ Auto Profile Setup - BEGIN ####'
|
Begin = '#### SZ Auto Profile Setup - BEGIN ####'
|
||||||
End = '#### SZ Auto Profile Setup - END ####'
|
End = '#### SZ Auto Profile Setup - END ####'
|
||||||
Removed = '#### SZ Auto Profile Setup'
|
Removed = '#### SZ Auto Profile Setup - ----- ####'
|
||||||
|
Match = '#### SZ Auto Profile Setup'
|
||||||
})
|
})
|
||||||
|
|
||||||
function New-ProfileSetupStatus {
|
function New-ProfileSetupStatus {
|
||||||
param($Name, $Path, $Status, $Exist)
|
param($Name, $Path, $Status, $Exist)
|
||||||
return [PSCustomObject]([ordered]@{
|
return [PSCustomObject]([ordered]@{
|
||||||
Name = $Name
|
Name = $Name
|
||||||
Path = $Path
|
Path = $Path
|
||||||
Status = $Status
|
Status = $Status
|
||||||
Exist = $Exist
|
Exist = $Exist
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
$local:_profiles = $PROFILE | fl * -Force | Out-String -Stream | ForEach-Object {
|
$local:_profiles = $PROFILE | fl * -Force | Out-String -Stream | ForEach-Object {
|
||||||
$local:p = $($_ -split ' : [/A-Z\\]');
|
$local:p = $($_ -split ' : [/A-Z\\]');
|
||||||
if ( $p[0] -match 'User' ) {
|
if ( $p[0] -match 'User' ) {
|
||||||
$p[0].Trim()
|
$p[0].Trim()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$local:_profile = $profile.CurrentUserAllHosts;
|
$local:_profile = $profile.CurrentUserAllHosts;
|
||||||
$local:randomSeed = "####$(Get-Random)####";
|
$local:randomSeed = "####$(Get-Random)####";
|
||||||
|
|
||||||
if ( $RemoveOnly -and $ConfirmPreference -eq 'High' -and (-not $PSBoundParameters -or -not $PSBoundParameters.ContainsKey('Confirm')) ) {
|
if ( $RemoveOnly -and $ConfirmPreference -eq 'High' -and (-not $PSBoundParameters -or -not $PSBoundParameters.ContainsKey('Confirm')) ) {
|
||||||
$script:ConfirmPreference = 'Low'
|
$script:ConfirmPreference = 'Low'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if ( $sudo -or $(Test-IsAdmin) ) { $_profile = $profile.AllUsersAllHosts }
|
if ( $sudo -or $(Test-IsAdmin) ) { $_profile = $profile.AllUsersAllHosts }
|
||||||
$_profiles | Foreach-Object {
|
$_profiles | Foreach-Object {
|
||||||
$local:p = Invoke-Expression "`$profile.$_"
|
$local:p = Invoke-Expression "`$profile.$_"
|
||||||
$local:profileContent = $randomSeed;
|
$local:profileContent = $randomSeed;
|
||||||
$local:exists = Test-Path $p
|
$local:exists = Test-Path $p
|
||||||
$local:userFile = $($_ -match 'CurrentUser')
|
$local:userFile = $($_ -match 'CurrentUser')
|
||||||
$local:cleanupValue = ''
|
$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..."
|
Write-Verbose "Found code in [$_]'$p', preparing..."
|
||||||
$local:gate = $true;
|
$local:gate = $true;
|
||||||
$profileContent = Get-Content $p | ForEach-Object {
|
$profileContent = Get-Content $p | ForEach-Object {
|
||||||
|
@ -106,15 +112,22 @@ $_profiles | Foreach-Object {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
if ( $_ -match $ProfileSignature.Match ) {
|
||||||
if ( $_ -match $ProfileSignature.Begin ) {
|
if ( $_ -match $ProfileSignature.Begin ) {
|
||||||
$gate = $false;
|
$gate = $false;
|
||||||
|
}
|
||||||
$cleanupValue = $ProfileSignature.Removed
|
$cleanupValue = $ProfileSignature.Removed
|
||||||
return $randomSeed;
|
return $randomSeed;
|
||||||
}
|
}
|
||||||
return $_
|
return $_
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
$profileContent += "`n`n$(Get-Content $p -Raw)"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if( -not( $profileContent -match $randomSeed ) ) { $profileContent = "$randomSeed`n$profileContent" }
|
||||||
|
|
||||||
$local:insertProfileCode = $false;
|
$local:insertProfileCode = $false;
|
||||||
if ( ($p -eq $_profile) -and -not $RemoveOnly ) {
|
if ( ($p -eq $_profile) -and -not $RemoveOnly ) {
|
||||||
|
@ -135,23 +148,24 @@ $_profiles | Foreach-Object {
|
||||||
$ProfileSignature.End
|
$ProfileSignature.End
|
||||||
) -join "`n" ).Trim().Replace(('$' + '_'), ('$$' + '_')))
|
) -join "`n" ).Trim().Replace(('$' + '_'), ('$$' + '_')))
|
||||||
}
|
}
|
||||||
|
|
||||||
$profileContent = $($profileContent -replace $randomSeed, $cleanupValue).Trim()
|
$profileContent = $($profileContent -replace $randomSeed, $cleanupValue).Trim()
|
||||||
|
|
||||||
$local:shouldSudo = $sudo -and -not $userFile
|
$local:shouldSudo = $sudo -and -not $userFile
|
||||||
$local:status = 'Skipped'
|
$local:status = 'Skipped'
|
||||||
if ( $VerbosePreference -eq 'Continue' ) { $ShowSkipped = $true }
|
if ( $VerbosePreference -eq 'Continue' ) { $ShowSkipped = $true }
|
||||||
|
# Test if new profile Content will be empty
|
||||||
if ( -not ([string]::IsNullOrWhiteSpace($profileContent)) ) {
|
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 ) {
|
if ( $sudo -or $userFile ) {
|
||||||
Write-Verbose "Writing content to $($_)..."
|
Write-Verbose "Writing content to $($_)..."
|
||||||
$local:tmpOutput = New-TemporaryFile -WhatIf:$false
|
$local:tmpOutput = New-TemporaryFile -WhatIf:$false -Confirm:$false
|
||||||
$profileContent | Out-File $tmpOutput.FullName -WhatIf:$false
|
$profileContent | Out-File $tmpOutput.FullName -WhatIf:$false -Confirm:$false
|
||||||
if ( $shouldSudo -and $( $PSVersionTable.Platform -eq 'Unix' ) ) {
|
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
|
Invoke-ExpressionEx -sudo:$shouldSudo chmod 'a+r' $tmpOutput.FullName
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
function BreakPointHere {} BreakPointHere;
|
|
||||||
|
|
||||||
$local:errMsg = $(Invoke-ExpressionEx -sudo:$shouldSudo "Move-Item $($tmpOutput.FullName) $p -Force:`$$Force -ErrorAction Stop | Out-Null" 2>&1)
|
$local:errMsg = $(Invoke-ExpressionEx -sudo:$shouldSudo "Move-Item $($tmpOutput.FullName) $p -Force:`$$Force -ErrorAction Stop | Out-Null" 2>&1)
|
||||||
if ( $errMsg ) {
|
if ( $errMsg ) {
|
||||||
Write-Error "$errMsg"
|
Write-Error "$errMsg"
|
||||||
|
@ -190,9 +204,9 @@ $_profiles | Foreach-Object {
|
||||||
if ( $shouldSudo -and $status -ne 'Skipped' ) { $status = "$status via sudo" }
|
if ( $shouldSudo -and $status -ne 'Skipped' ) { $status = "$status via sudo" }
|
||||||
New-ProfileSetupStatus -Name $_ -Path $p -Status $status -Exist $(Test-Path $p)
|
New-ProfileSetupStatus -Name $_ -Path $p -Status $status -Exist $(Test-Path $p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Remove-Item function:ProfileCode* -Confirm:$false
|
Remove-Item function:ProfileCode* -Confirm:$false
|
||||||
}
|
}
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
|
@ -218,10 +232,10 @@ function Invoke-ExpressionEx {
|
||||||
[string[]]$expr
|
[string[]]$expr
|
||||||
)
|
)
|
||||||
|
|
||||||
function do_sudo {
|
function do_sudo {
|
||||||
[CmdletBinding()]param(
|
[CmdletBinding()]param(
|
||||||
[string]$sudo_cmd
|
[string]$sudo_cmd
|
||||||
)
|
)
|
||||||
|
|
||||||
function do_sudo_win {
|
function do_sudo_win {
|
||||||
[CmdletBinding()]param(
|
[CmdletBinding()]param(
|
||||||
|
@ -257,9 +271,9 @@ function do_sudo {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $sudo -and -not $(Test-IsAdmin) ) {
|
if ( $sudo -and -not $(Test-IsAdmin) ) {
|
||||||
$local:tmpOutputFile = $null
|
$local:tmpOutputFile = $null
|
||||||
$local:tmpScriptFile = $null
|
$local:tmpScriptFile = $null
|
||||||
|
|
||||||
|
@ -301,10 +315,10 @@ if ( $sudo -and -not $(Test-IsAdmin) ) {
|
||||||
if ( $tmpScriptFile ) { Remove-Item $tmpScriptFile }
|
if ( $tmpScriptFile ) { Remove-Item $tmpScriptFile }
|
||||||
if ( $tmpOutputFile ) { Import-Clixml $tmpOutputFile; Remove-Item $tmpOutputFile }
|
if ( $tmpOutputFile ) { Import-Clixml $tmpOutputFile; Remove-Item $tmpOutputFile }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
Write-Verbose "Perofrming the following expression in-line:`n{$($expr -join ' ')}"
|
Write-Verbose "Perofrming the following expression in-line:`n{$($expr -join ' ')}"
|
||||||
Invoke-Expression "$expr"
|
Invoke-Expression "$expr"
|
||||||
}
|
}
|
||||||
|
|
||||||
function Test-IsAdmin {
|
function Test-IsAdmin {
|
||||||
|
@ -346,15 +360,15 @@ param(
|
||||||
[string[]]$FunctionName
|
[string[]]$FunctionName
|
||||||
)
|
)
|
||||||
|
|
||||||
$local:src = ""
|
$local:src = ""
|
||||||
foreach ( $local:func in $FunctionName ) {
|
foreach ( $local:func in $FunctionName ) {
|
||||||
if ( -not $NoHeader ) { $src += "`nfunction $func {" }
|
if ( -not $NoHeader ) { $src += "`nfunction $func {" }
|
||||||
$src += "`n"
|
$src += "`n"
|
||||||
$src += $((Get-Command -Type Function $func).Definition) ### .Replace(('$'+'_'),('$$'+'_'))
|
$src += $((Get-Command -Type Function $func).Definition)
|
||||||
if ( -not $NoHeader ) { $src += "`n}" }
|
if ( -not $NoHeader ) { $src += "`n}" }
|
||||||
$src += "`n"
|
$src += "`n"
|
||||||
}
|
}
|
||||||
return $src.Trim()
|
return $src.Trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
# Are we running in Unix or Windows?
|
# Are we running in Unix or Windows?
|
||||||
|
|
Loading…
Reference in New Issue