From 31c8c620c7fa60580fe74149c28302d3ed16283c Mon Sep 17 00:00:00 2001 From: Jackbennett Date: Thu, 12 May 2016 13:26:18 +0100 Subject: [PATCH 1/9] Custom prompt hooks protected from later overwriting Add a pre and post function hook around the Cmder prompt. Specify the cmder prompt as a function that could be replaced by a user. Write a friendly message when the user profile template is created. Create the user profile with cmder prompt hooks ready to use. It was concerning to run any function with a specific name every prompt with no guarantee it remains what it was initally created as. Core functions have been explicitly called from their namespace like Microsoft.PowerShell.Utility\Write-Host to try and prevent clobbering. User supplied functions are passed in as script blocks, created as the session runs the profile script. By creating them as constants these function names cannot be declared again for the duration of the process. Since the prompt function already exists by this time, set the readOnly flag so to re-declare the prompt requires the use of -force. It is hoped these changes limit what could be the risk of any script redefining functions that are called automatically without user intent or input. --- vendor/profile.ps1 | 84 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 69 insertions(+), 15 deletions(-) diff --git a/vendor/profile.ps1 b/vendor/profile.ps1 index a2f75b5..fa6ee97 100644 --- a/vendor/profile.ps1 +++ b/vendor/profile.ps1 @@ -65,19 +65,6 @@ function checkGit($Path) { } } -# Set up a Cmder prompt, adding the git prompt parts inside git repos -function global:prompt { - $realLASTEXITCODE = $LASTEXITCODE - $Host.UI.RawUI.ForegroundColor = "White" - Write-Host $pwd.ProviderPath -NoNewLine -ForegroundColor Green - if($gitStatus){ - checkGit($pwd.ProviderPath) - } - $global:LASTEXITCODE = $realLASTEXITCODE - Write-Host "`nλ" -NoNewLine -ForegroundColor "DarkGray" - return " " -} - # Load special features come from posh-git if ($gitStatus) { Start-SshAgent -Quiet @@ -110,11 +97,78 @@ foreach ($x in ls *.ps1) { } popd +# +# Prompt Section +# Users should modify their user-profile.ps1 as it will be safe from updates. +# + +# Pre assign the hooks so the first run of cmder gets a working prompt. +[ScriptBlock]$PrePrompt = {} +[ScriptBlock]$PostPrompt = {} +[ScriptBlock]$CmderPrompt = { + $Host.UI.RawUI.ForegroundColor = "White" + Microsoft.PowerShell.Utility\Write-Host $pwd.ProviderPath -NoNewLine -ForegroundColor Green + if($gitStatus){ + checkGit($pwd.ProviderPath) + } +} + $CmderUserProfilePath = Join-Path $env:CMDER_ROOT "config\user-profile.ps1" if(Test-Path $CmderUserProfilePath) { # Create this file and place your own command in there. . "$CmderUserProfilePath" } else { - Write-Host "Creating user startup file: $CmderUserProfilePath" - "# Use this file to run your own startup commands" | Out-File $CmderUserProfilePath +# This multiline string cannot be indented, for this reason I've not indented the whole block + +Write-Host -BackgroundColor Darkgreen -ForegroundColor White "First Run: Creating user startup file: $CmderUserProfilePath" + +$UserProfileTemplate = @' +# Use this file to run your own startup commands + +## Prompt Customization +<# +.SYNTAX + + λ +.EXAMPLE + N:\Documents\src\cmder [master] + λ | +#> + +[ScriptBlock]$PrePrompt = { + } + +# Replace the cmder prompt entirely with this. +# [ScriptBlock]$CmderPrompt = {} + +[ScriptBlock]$PostPrompt = { + +} + +## + + +'@ + +New-Item -ItemType File -Path $CmderUserProfilePath -Value $UserProfileTemplate > $null + +} + +# Once Created these code blocks cannot be overwritten +Set-Item -Path function:\PrePrompt -Value $PrePrompt -Options Constant +Set-Item -Path function:\CmderPrompt -Value $CmderPrompt -Options Constant +Set-Item -Path function:\PostPrompt -Value $PostPrompt -Options Constant + +[ScriptBlock]$Prompt = { + $realLASTEXITCODE = $LASTEXITCODE + PrePrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline + CmderPrompt + Microsoft.PowerShell.Utility\Write-Host "`nλ " -NoNewLine -ForegroundColor "DarkGray" + PostPrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline + $global:LASTEXITCODE = $realLASTEXITCODE + return " " +} + +# ReadOnly at least requires `-force` to be overwritten +Set-Item -Path function:\prompt -Value $Prompt -Options ReadOnly From 76b2ed510d6cabc645ebcba5ece9e497532033ec Mon Sep 17 00:00:00 2001 From: Jackbennett Date: Mon, 23 May 2016 14:51:55 +0100 Subject: [PATCH 2/9] If you start doing remote work you can fire off your own start-sshAgent command. --- vendor/profile.ps1 | 5 ----- 1 file changed, 5 deletions(-) diff --git a/vendor/profile.ps1 b/vendor/profile.ps1 index fa6ee97..0327f33 100644 --- a/vendor/profile.ps1 +++ b/vendor/profile.ps1 @@ -65,11 +65,6 @@ function checkGit($Path) { } } -# Load special features come from posh-git -if ($gitStatus) { - Start-SshAgent -Quiet -} - # Move to the wanted location # This is either a env variable set by the user or the result of # cmder.exe setting this variable due to a commandline argument or a "cmder here" From 7a0a1adc02afcbefeedd26323288b6b0b182f95d Mon Sep 17 00:00:00 2001 From: Jackbennett Date: Mon, 23 May 2016 14:53:34 +0100 Subject: [PATCH 3/9] Use checkGit to hold the git state. Import posh-git at the last minute. Import-Git now finds if the module isn't installed at all and alerts the user. But only when in a git folder. --- vendor/profile.ps1 | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/vendor/profile.ps1 b/vendor/profile.ps1 index 0327f33..979ed46 100644 --- a/vendor/profile.ps1 +++ b/vendor/profile.ps1 @@ -46,16 +46,18 @@ try { $env:Path += $(";" + $env:CMDER_ROOT + "\vendor\git-for-windows\bin") } -try { - Import-Module -Name "posh-git" -ErrorAction Stop >$null - $gitStatus = $true -} catch { - Write-Warning "Missing git support, install posh-git with 'Install-Module posh-git' and restart cmder." - $gitStatus = $false +$gitLoaded = $false +function Import-Git($Loaded){ + if($Loaded) { return } + if(-not (Get-Module -Name Posh-Git -ListAvailable) ) { + Write-Warning "Missing git support, install posh-git with 'Install-Module posh-git' and restart cmder." + } + return $true } function checkGit($Path) { if (Test-Path -Path (Join-Path $Path '.git') ) { + $gitLoaded = Import-Git $gitLoaded Write-VcsStatus return } @@ -103,9 +105,7 @@ popd [ScriptBlock]$CmderPrompt = { $Host.UI.RawUI.ForegroundColor = "White" Microsoft.PowerShell.Utility\Write-Host $pwd.ProviderPath -NoNewLine -ForegroundColor Green - if($gitStatus){ - checkGit($pwd.ProviderPath) - } + checkGit($pwd.ProviderPath) } $CmderUserProfilePath = Join-Path $env:CMDER_ROOT "config\user-profile.ps1" From 010049a849bc0c46d5a7340c98477898c5378961 Mon Sep 17 00:00:00 2001 From: Jackbennett Date: Thu, 26 May 2016 15:47:00 +0100 Subject: [PATCH 4/9] Set window title to current folder unless there's git information. Unfortunately doesn't apply to conEmu's tab name. --- vendor/profile.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/vendor/profile.ps1 b/vendor/profile.ps1 index 979ed46..0394209 100644 --- a/vendor/profile.ps1 +++ b/vendor/profile.ps1 @@ -157,6 +157,7 @@ Set-Item -Path function:\PostPrompt -Value $PostPrompt -Options Constant [ScriptBlock]$Prompt = { $realLASTEXITCODE = $LASTEXITCODE + $host.UI.RawUI.WindowTitle = Split-Path $pwd.ProviderPath -Leaf PrePrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline CmderPrompt Microsoft.PowerShell.Utility\Write-Host "`nλ " -NoNewLine -ForegroundColor "DarkGray" From b349b1986927d3c2c0dae2c6c17ec4964042177a Mon Sep 17 00:00:00 2001 From: Jackbennett Date: Mon, 10 Oct 2016 10:30:01 +0100 Subject: [PATCH 5/9] Describe why PS functions are called by namespace As the prompt function is called all the time, specifically namespace the cmldets it uses to avoid them being hijacked in the user session. --- vendor/profile.ps1 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/vendor/profile.ps1 b/vendor/profile.ps1 index 0394209..fc0f324 100644 --- a/vendor/profile.ps1 +++ b/vendor/profile.ps1 @@ -155,9 +155,14 @@ Set-Item -Path function:\PrePrompt -Value $PrePrompt -Options Constant Set-Item -Path function:\CmderPrompt -Value $CmderPrompt -Options Constant Set-Item -Path function:\PostPrompt -Value $PostPrompt -Options Constant +<# +This scriptblock runs every time the prompt is returned. +Explicitly use functions from MS namespace to protect from being overridden in the user session. +Custom prompt functions are loaded in as constants to get the same behaviour +#> [ScriptBlock]$Prompt = { $realLASTEXITCODE = $LASTEXITCODE - $host.UI.RawUI.WindowTitle = Split-Path $pwd.ProviderPath -Leaf + $host.UI.RawUI.WindowTitle = Microsoft.PowerShell.Management\Split-Path $pwd.ProviderPath -Leaf PrePrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline CmderPrompt Microsoft.PowerShell.Utility\Write-Host "`nλ " -NoNewLine -ForegroundColor "DarkGray" @@ -166,5 +171,6 @@ Set-Item -Path function:\PostPrompt -Value $PostPrompt -Options Constant return " " } +# Functions can be made constant only at creation time # ReadOnly at least requires `-force` to be overwritten Set-Item -Path function:\prompt -Value $Prompt -Options ReadOnly From db909451c94943f3d0c916f4865590038ae7b2d9 Mon Sep 17 00:00:00 2001 From: Jackbennett Date: Mon, 10 Oct 2016 12:01:55 +0100 Subject: [PATCH 6/9] Version Check the Posh-Git module to allow module autoloading Posh-Git before this release does not export `Write-VcsStatus` thus powershell's autoloading cannot find the reqired module for the function. note that `get-module -listAvailable` can return an array of multiple versions. --- vendor/profile.ps1 | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/vendor/profile.ps1 b/vendor/profile.ps1 index fc0f324..7878ff2 100644 --- a/vendor/profile.ps1 +++ b/vendor/profile.ps1 @@ -49,9 +49,14 @@ try { $gitLoaded = $false function Import-Git($Loaded){ if($Loaded) { return } - if(-not (Get-Module -Name Posh-Git -ListAvailable) ) { + $GitModule = Get-Module -Name Posh-Git -ListAvailable + if($GitModule | select version | where version -le ([version]"0.6.1.20160330")){ + Import-Module Posh-Git > $null + } + if(-not ($GitModule) ) { Write-Warning "Missing git support, install posh-git with 'Install-Module posh-git' and restart cmder." } + # Make sure we only run once by alawys returning true return $true } From 0de26571aaa1a1dbe7bc7393fb58b38ba51e87e6 Mon Sep 17 00:00:00 2001 From: Benjamin Staneck Date: Sun, 23 Oct 2016 22:16:47 +0200 Subject: [PATCH 7/9] :arrow_up: Update Git to v2.10.1 Release notes: https://github.com/git-for-windows/git/releases/tag/v2.10.1.windows.1 --- vendor/sources.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vendor/sources.json b/vendor/sources.json index d027113..65744f9 100644 --- a/vendor/sources.json +++ b/vendor/sources.json @@ -1,8 +1,8 @@ [ { "name": "git-for-windows", - "version": "v2.9.0.windows.1", - "url": "https://github.com/git-for-windows/git/releases/download/v2.9.0.windows.1/PortableGit-2.9.0-32-bit.7z.exe" + "version": "v2.10.1.windows.1", + "url": "https://github.com/git-for-windows/git/releases/download/v2.10.1.windows.1/PortableGit-2.10.1-32-bit.7z.exe" }, { "name": "clink", From e6d4609bf4f7fb06f3d869c62028d9aced1e567b Mon Sep 17 00:00:00 2001 From: Benjamin Staneck Date: Sun, 23 Oct 2016 22:21:55 +0200 Subject: [PATCH 8/9] :arrow_up: Update ConEmu to 161022 (stable) Changelog: https://conemu.github.io/blog/2016/10/22/Build-161022.html --- .gitignore | 1 + vendor/sources.json | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 9fa67af..35c93e7 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ Version v* config/user-* config/aliases config/profile.d +.github_changelog_generator diff --git a/vendor/sources.json b/vendor/sources.json index 65744f9..61e05ec 100644 --- a/vendor/sources.json +++ b/vendor/sources.json @@ -11,8 +11,8 @@ }, { "name": "conemu-maximus5", - "version": "160612", - "url": "https://github.com/Maximus5/ConEmu/releases/download/v16.06.12/ConEmuPack.160612.7z" + "version": "161022", + "url": "https://github.com/Maximus5/ConEmu/releases/download/v16.10.22/ConEmuPack.161022.7z" }, { "name": "clink-completions", From 46d593680650dec9c92c5571bc2b4e64e6490555 Mon Sep 17 00:00:00 2001 From: Benjamin Staneck Date: Fri, 4 Nov 2016 00:25:50 +0100 Subject: [PATCH 9/9] :arrow_up: Update Git to v2.10.2 Release notes: https://github.com/git-for-windows/git/releases/tag/v2.10.2.windows.1 --- vendor/sources.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vendor/sources.json b/vendor/sources.json index 61e05ec..158b8e0 100644 --- a/vendor/sources.json +++ b/vendor/sources.json @@ -1,8 +1,8 @@ [ { "name": "git-for-windows", - "version": "v2.10.1.windows.1", - "url": "https://github.com/git-for-windows/git/releases/download/v2.10.1.windows.1/PortableGit-2.10.1-32-bit.7z.exe" + "version": "v2.10.2.windows.1", + "url": "https://github.com/git-for-windows/git/releases/download/v2.10.2.windows.1/PortableGit-2.10.2-32-bit.7z.exe" }, { "name": "clink",