From 0c1ab3fbb1cf21f8988c2808263f82e6fe2f7c60 Mon Sep 17 00:00:00 2001 From: David Refoua Date: Sun, 6 Nov 2022 11:34:27 +0330 Subject: [PATCH] powershell: fixes for first time launch --- vendor/profile.ps1 | 52 ++++++++-------- vendor/psmodules/Cmder.ps1 | 123 ++++++++++++++++++++----------------- 2 files changed, 91 insertions(+), 84 deletions(-) diff --git a/vendor/profile.ps1 b/vendor/profile.ps1 index 021dc3b..9d1f19b 100644 --- a/vendor/profile.ps1 +++ b/vendor/profile.ps1 @@ -1,6 +1,6 @@ # Init Script for PowerShell # Created as part of Cmder project -# This file must be saved using UTF-8 with BOM encoding for prompt to work correctly. +# NOTE: This file must be saved using UTF-8 with BOM encoding for prompt symbol to work correctly. # !!! THIS FILE IS OVERWRITTEN WHEN CMDER IS UPDATED # !!! Use "%CMDER_ROOT%\config\user_profile.ps1" to add your own startup commands @@ -13,7 +13,7 @@ if (!$PSScriptRoot) { } if ($ENV:CMDER_USER_CONFIG) { - # Write-Host "CMDER IS ALSO USING INDIVIDUAL USER CONFIG FROM '$ENV:CMDER_USER_CONFIG'!" + Write-Verbose "CMDER IS ALSO USING INDIVIDUAL USER CONFIG FROM '$ENV:CMDER_USER_CONFIG'!" } # We do this for Powershell as Admin Sessions because CMDER_ROOT is not being set. @@ -43,18 +43,18 @@ if(-not $moduleInstallerAvailable -and -not $env:PSModulePath.Contains($CmderMod } $gitVersionVendor = (readVersion -gitPath "$ENV:CMDER_ROOT\vendor\git-for-windows\cmd") -# Write-Host "GIT VENDOR: ${gitVersionVendor}" +Write-Debug "GIT VENDOR: ${gitVersionVendor}" # Get user installed Git Version[s] and Compare with vendored if found. foreach ($git in (Get-Command -ErrorAction SilentlyContinue 'git')) { - # Write-Host "GIT PATH: " + $git.Path + Write-Debug "GIT PATH: {$git.Path}" $gitDir = Split-Path -Path $git.Path $gitDir = isGitShim -gitPath $gitDir $gitVersionUser = (readVersion -gitPath $gitDir) - # Write-Host "GIT USER: ${gitVersionUser}" + Write-Debug "GIT USER: ${gitVersionUser}" $useGitVersion = compare_git_versions -userVersion $gitVersionUser -vendorVersion $gitVersionVendor - # Write-Host "Using GIT Version: ${useGitVersion}" + Write-Debug "Using Git Version: ${useGitVersion}" # Use user installed Git if ($null -eq $gitPathUser) { @@ -66,7 +66,7 @@ foreach ($git in (Get-Command -ErrorAction SilentlyContinue 'git')) { } if ($useGitVersion -eq $gitVersionUser) { - # Write-Host "Using GIT Dir: ${gitDir}" + Write-Debug "Using Git Dir: ${gitDir}" $ENV:GIT_INSTALL_ROOT = $gitPathUser $ENV:GIT_INSTALL_TYPE = 'USER' break @@ -79,10 +79,10 @@ if ($null -eq $ENV:GIT_INSTALL_ROOT -and $null -ne $gitVersionVendor) { $ENV:GIT_INSTALL_TYPE = 'VENDOR' } -# Write-Host "GIT_INSTALL_ROOT: ${ENV:GIT_INSTALL_ROOT}" -# Write-Host "GIT_INSTALL_TYPE: ${ENV:GIT_INSTALL_TYPE}" +Write-Debug "GIT_INSTALL_ROOT: ${ENV:GIT_INSTALL_ROOT}" +Write-Debug "GIT_INSTALL_TYPE: ${ENV:GIT_INSTALL_TYPE}" -if (-Not ($null -eq $ENV:GIT_INSTALL_ROOT)) { +if ($null -ne $ENV:GIT_INSTALL_ROOT) { $env:Path = Configure-Git -gitRoot "$ENV:GIT_INSTALL_ROOT" -gitType $ENV:GIT_INSTALL_TYPE -gitPathUser $gitPathUser } @@ -95,21 +95,20 @@ if (Get-Module PSReadline -ErrorAction "SilentlyContinue") { } # Pre-assign default prompt hooks so the first run of cmder gets a working prompt. -$env:gitLoaded = $false +$env:gitLoaded = $null [ScriptBlock]$PrePrompt = {} [ScriptBlock]$PostPrompt = {} [ScriptBlock]$CmderPrompt = { + # Check if we're currently running under Admin privileges. $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = [Security.Principal.WindowsPrincipal] $identity $adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator $color = "White" if ($principal.IsInRole($adminRole)) { $color = "Red" } $Host.UI.RawUI.ForegroundColor = "White" - Microsoft.PowerShell.Utility\Write-Host -NoNewline "PS " -ForegroundColor $color + Microsoft.PowerShell.Utility\Write-Host "PS " -NoNewline -ForegroundColor $color Microsoft.PowerShell.Utility\Write-Host $pwd.ProviderPath -NoNewLine -ForegroundColor Green - if (Get-Command git -ErrorAction SilentlyContinue) { - checkGit($pwd.ProviderPath) - } + checkGit($pwd.ProviderPath) Microsoft.PowerShell.Utility\Write-Host "`nλ" -NoNewLine -ForegroundColor "DarkGray" } @@ -118,33 +117,31 @@ $env:Path = "$Env:CMDER_ROOT\bin;$Env:CMDER_ROOT\vendor\bin;$env:Path;$Env:CMDER # Drop *.ps1 files into "$ENV:CMDER_ROOT\config\profile.d" # to source them at startup. -if (-Not (Test-Path -PathType container "$ENV:CMDER_ROOT\config\profile.d")) { +if (-not (Test-Path -PathType container "$ENV:CMDER_ROOT\config\profile.d")) { New-Item -ItemType Directory -Path "$ENV:CMDER_ROOT\config\profile.d" } Push-Location $ENV:CMDER_ROOT\config\profile.d foreach ($x in Get-ChildItem *.psm1) { - # Write-Host Write-Host Sourcing $x + Write-Verbose Write-Host Sourcing $x Import-Module $x } - foreach ($x in Get-ChildItem *.ps1) { - # Write-Host Write-Host Sourcing $x + Write-Verbose Write-Host Sourcing $x . $x } Pop-Location # Drop *.ps1 files into "$ENV:CMDER_USER_CONFIG\config\profile.d" # to source them at startup. Requires using cmder.exe /C [cmder_user_root_path] argument -if ($ENV:CMDER_USER_CONFIG -ne "" -And (Test-Path "$ENV:CMDER_USER_CONFIG\profile.d")) { +if ($ENV:CMDER_USER_CONFIG -ne "" -and (Test-Path "$ENV:CMDER_USER_CONFIG\profile.d")) { Push-Location $ENV:CMDER_USER_CONFIG\profile.d foreach ($x in Get-ChildItem *.psm1) { - # Write-Host Write-Host Sourcing $x + Write-Verbose Write-Host Sourcing $x Import-Module $x } - foreach ($x in Get-ChildItem *.ps1) { - # Write-Host Write-Host Sourcing $x + Write-Verbose Write-Host Sourcing $x . $x } Pop-Location @@ -175,8 +172,10 @@ if ($ENV:CMDER_USER_CONFIG) { } } -if (-Not (Test-Path $CmderUserProfilePath)) { - Write-Host -BackgroundColor DarkGreen -ForegroundColor White "First Run: Creating user startup file: $CmderUserProfilePath" +if (-not (Test-Path $CmderUserProfilePath)) { + $CmderUserProfilePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($CmderUserProfilePath) + Write-Host -NoNewline "`r" + Write-Host -BackgroundColor Green -ForegroundColor Black "First Run: Creating user startup file: $CmderUserProfilePath" Copy-Item "$env:CMDER_ROOT\vendor\user_profile.ps1.default" -Destination $CmderUserProfilePath } @@ -200,7 +199,7 @@ if ( $(Get-Command prompt).Definition -match 'PS \$\(\$executionContext.SessionS $realLASTEXITCODE = $LASTEXITCODE $host.UI.RawUI.WindowTitle = Microsoft.PowerShell.Management\Split-Path $pwd.ProviderPath -Leaf Microsoft.PowerShell.Utility\Write-Host -NoNewline "$([char]0x200B)`r$([char]0x1B)[K" - if ($lastSUCCESS -Or ($LASTEXITCODE -ne 0)) { + if ($lastSUCCESS -or ($LASTEXITCODE -ne 0)) { Microsoft.PowerShell.Utility\Write-Host } PrePrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline @@ -210,7 +209,6 @@ if ( $(Get-Command prompt).Definition -match 'PS \$\(\$executionContext.SessionS return " " } - # Once Created these code blocks cannot be overwritten # if (-not $(Get-Command PrePrompt).Options -match 'Constant') {Set-Item -Path function:\PrePrompt -Value $PrePrompt -Options Constant} # if (-not $(Get-Command CmderPrompt).Options -match 'Constant') {Set-Item -Path function:\CmderPrompt -Value $CmderPrompt -Options Constant} diff --git a/vendor/psmodules/Cmder.ps1 b/vendor/psmodules/Cmder.ps1 index 0c5a590..a42e40c 100644 --- a/vendor/psmodules/Cmder.ps1 +++ b/vendor/psmodules/Cmder.ps1 @@ -1,7 +1,7 @@ function readVersion($gitPath) { $gitExecutable = "${gitPath}\git.exe" - if (!(test-path "$gitExecutable")) { + if (-not (Test-Path "$gitExecutable")) { return $null } @@ -20,44 +20,44 @@ function readVersion($gitPath) { function isGitShim($gitPath) { # check if there's shim - and if yes follow the path - if (test-path "${gitPath}\git.shim") { - $shim = (get-content "${gitPath}\git.shim") - ($trash, $gitPath) = $shim.replace(' ','').split('=') + if (Test-Path "${gitPath}\git.shim") { + $shim = (get-content "${gitPath}\git.shim") + ($trash, $gitPath) = $shim.replace(' ', '').split('=') - $gitPath=$gitPath.replace('\git.exe','') + $gitPath = $gitPath.replace('\git.exe', '') } return $gitPath.toString() } function compareVersions($userVersion, $vendorVersion) { - if (-not($userVersion -eq $null)) { + if ($null -ne $userVersion) { ($userMajor, $userMinor, $userPatch, $userBuild) = $userVersion.split('.', 4) } else { return -1 } - if (-not($vendorVersion -eq $null)) { + if ($null -ne $vendorVersion) { ($vendorMajor, $vendorMinor, $vendorPatch, $vendorBuild) = $vendorVersion.split('.', 4) } else { return 1 } - if (($userMajor -eq $vendorMajor) -and ($userMinor -eq $vendorMinor) -and ($userPatch -eq $vendorPatch) -and ($userBuild -eq $vendorBuild)) { + if (($userMajor -eq $vendorMajor) -and ($userMinor -eq $vendorMinor) -and ($userPatch -eq $vendorPatch) -and ($userBuild -eq $vendorBuild)) { return 1 } - if ($userMajor -gt $vendorMajor) {return 1} - if ($userMajor -lt $vendorMajor) {return -1} + if ($userMajor -gt $vendorMajor) { return 1 } + if ($userMajor -lt $vendorMajor) { return -1 } - if ($userMinor -gt $vendorMinor) {return 1} - if ($userMinor -lt $vendorMinor) {return -1} + if ($userMinor -gt $vendorMinor) { return 1 } + if ($userMinor -lt $vendorMinor) { return -1 } - if ($userPatch -gt $vendorPatch) {return 1} - if ($userPatch -lt $vendorPatch) {return -1} + if ($userPatch -gt $vendorPatch) { return 1 } + if ($userPatch -lt $vendorPatch) { return -1 } - if ($userBuild -gt $vendorBuild) {return 1} - if ($userBuild -lt $vendorBuild) {return -1} + if ($userBuild -gt $vendorBuild) { return 1 } + if ($userBuild -lt $vendorBuild) { return -1 } return 0 } @@ -65,45 +65,48 @@ function compareVersions($userVersion, $vendorVersion) { function compare_git_versions($userVersion, $vendorVersion) { $result = compareVersions -userVersion $userVersion -vendorVersion $vendorVersion - # write-host "Compare Versions Result: ${result}" + Write-Debug "Compare Versions Result: ${result}" if ($result -ge 0) { return $userVersion - } else { + } + else { return $vendorVersion } } -function Configure-Git($gitRoot, $gitType, $gitPathUser){ +function Configure-Git($gitRoot, $gitType, $gitPathUser) { # Proposed Behavior - # Modify the path if we are using VENDORED Git do nothing if using USER Git. - # If User Git is installed but older match its path config adding paths + # Modify the path if we are using VENDORED Git, do nothing if using USER Git. + # If User Git is installed but is older, match its path config adding paths # in the same path positions allowing a user to configure Cmder Git path # using locally installed Git Path Config. if ($gitType -eq 'VENDOR') { # If User Git is installed replace its path config with Newer Vendored Git Path - if ($gitPathUser -ne '' -and $gitPathUser -ne $null) { - # write-host "Cmder 'profile.ps1': Replacing older user Git path '$gitPathUser' with newer vendored Git path '$gitRoot' in the system path..." + if (($null -ne $gitPathUser) -and ($gitPathUser -ne '')) { + Write-Verbose "Cmder 'profile.ps1': Replacing older user Git path '$gitPathUser' with newer vendored Git path '$gitRoot' in the system path..." $newPath = ($env:path -ireplace [regex]::Escape($gitPathUser), $gitRoot) - } else { - if (!($env:Path -match [regex]::Escape("$gitRoot\cmd"))) { - # write-host "Adding $gitRoot\cmd to the path" + } + else { + if (-not ($env:Path -match [regex]::Escape("$gitRoot\cmd"))) { + Write-Debug "Adding $gitRoot\cmd to the path" $newPath = $($gitRoot + "\cmd" + ";" + $env:Path) } # Add "$gitRoot\mingw[32|64]\bin" to the path if exists and not done already - if ((test-path "$gitRoot\mingw32\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\mingw32\bin"))) { - # write-host "Adding $gitRoot\mingw32\bin to the path" + if ((Test-Path "$gitRoot\mingw32\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\mingw32\bin"))) { + Write-Debug "Adding $gitRoot\mingw32\bin to the path" $newPath = "$newPath;$gitRoot\mingw32\bin" - } elseif ((test-path "$gitRoot\mingw64\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\mingw64\bin"))) { - # write-host "Adding $gitRoot\mingw64\bin to the path" + } + elseif ((Test-Path "$gitRoot\mingw64\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\mingw64\bin"))) { + Write-Debug "Adding $gitRoot\mingw64\bin to the path" $newPath = "$newPath;$gitRoot\mingw64\bin" } # Add "$gitRoot\usr\bin" to the path if exists and not done already - if ((test-path "$gitRoot\usr\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\usr\bin"))) { - # write-host "Adding $gitRoot\usr\bin to the path" + if ((Test-Path "$gitRoot\usr\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\usr\bin"))) { + Write-Debug "Adding $gitRoot\usr\bin to the path" $newPath = "$newPath;$gitRoot\usr\bin" } } @@ -114,53 +117,59 @@ function Configure-Git($gitRoot, $gitType, $gitPathUser){ return $env:path } -function Import-Git(){ +function Import-Git() { $GitModule = Get-Module -Name Posh-Git -ListAvailable - if($GitModule | select version | where version -le ([version]"0.6.1.20160330")){ + if ($GitModule | Select-Object version | Where-Object version -le ([version]"0.6.1.20160330")) { Import-Module Posh-Git > $null } - if($GitModule | select version | where version -ge ([version]"1.0.0")){ + if ($GitModule | Select-Object version | Where-Object version -ge ([version]"1.0.0")) { Import-Module Posh-Git > $null $GitPromptSettings.AnsiConsole = $false } - if(-not ($GitModule) ) { - Write-Warning "Missing git support, install posh-git with 'Install-Module posh-git' and restart cmder." + if (-not $GitModule) { + Write-Host -NoNewline "`r`n" + Write-Warning "Missing git support, install posh-git with 'Install-Module posh-git' and restart Cmder." + Write-Host -NoNewline "`r$([char]0x1B)[A" + return $false } - # Make sure we only run once by alawys returning true + # Make sure we only run once by always returning true return $true } function checkGit($Path) { - if (Test-Path -Path (Join-Path $Path '.git') ) { - if($env:gitLoaded -eq 'false') { - $env:gitLoaded = Import-Git - } - - if (getGitStatusSetting -eq $true) { - Write-VcsStatus - } else { + if (-not (Get-Command git -ErrorAction SilentlyContinue)) { + return + } + if (-not (Test-Path -Path (Join-Path $Path '.git'))) { + $SplitPath = Split-Path $path + if ($SplitPath) { checkGit($SplitPath) } + return + } + if (getGitStatusSetting -eq $true) { + if ($null -eq $env:gitLoaded) { + $env:gitLoaded = Import-Git + } + if ($env:gitLoaded -eq $true) { + Write-VcsStatus + } + } + else { $headContent = Get-Content (Join-Path $Path '.git/HEAD') if ($headContent -like "ref: refs/heads/*") { $branchName = $headContent.Substring(16) - } else { + } + else { $branchName = "HEAD detached at $($headContent.Substring(0, 7))" } Write-Host " [$branchName]" -NoNewline -ForegroundColor White - } - - return - } - $SplitPath = split-path $path - if ($SplitPath) { - checkGit($SplitPath) } } function getGitStatusSetting() { - $gitStatus = (git --no-pager config -l) | out-string + $gitStatus = (git --no-pager config -l) | Out-String - ForEach ($line in $($gitStatus -split "`r`n")) { - if ($line -match 'cmder.status=false' -or $line -match 'cmder.psstatus=false') { + foreach ($line in $($gitStatus -split "`r`n")) { + if (($line -match 'cmder.status=false') -or ($line -match 'cmder.psstatus=false')) { return $false } }