2015-10-15 18:15:19 +08:00
|
|
|
|
# Init Script for PowerShell
|
2015-10-15 01:50:49 +08:00
|
|
|
|
# Created as part of cmder project
|
|
|
|
|
|
|
|
|
|
# !!! THIS FILE IS OVERWRITTEN WHEN CMDER IS UPDATED
|
2018-09-01 06:02:56 +08:00
|
|
|
|
# !!! Use "%CMDER_ROOT%\config\user_profile.ps1" to add your own startup commands
|
2015-10-15 01:50:49 +08:00
|
|
|
|
|
2017-06-23 18:39:36 +08:00
|
|
|
|
# Compatibility with PS major versions <= 2
|
|
|
|
|
if(!$PSScriptRoot) {
|
|
|
|
|
$PSScriptRoot = Split-Path $Script:MyInvocation.MyCommand.Path
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-13 23:38:27 +08:00
|
|
|
|
if ($ENV:CMDER_USER_CONFIG) {
|
|
|
|
|
# write-host "CMDER IS ALSO USING INDIVIDUAL USER CONFIG FROM '$ENV:CMDER_USER_CONFIG'!"
|
|
|
|
|
}
|
|
|
|
|
|
Added/enhanced bash with cmder.sh/user-cmder.sh, organized tasks menu
added personal files to .gitignore so they never get uploaded to the repo and added support for msys2 bash in the new git for windows
added autocreate of config/user-cmder.sh if iot does not exist and added it to the .gitignore
Added tasks: cmd::Cmder, cmd::Cmder as Admin, bash::bash, bash::bash as Admin, bash::mintty, bash::mintty as admin, powershell::powershell, powershell::powershell as Admin. Set default task to cmd::Cmder. Cot rid of init.bat running before /bin/bash, fixes double exit requirement
Added running git for windows post-install.bat on first cmder launch
fixed file/path not found errors when launching powershell as admin
fixed file/path not found errors when launching bash/mintty as admin
fixed PATH in vendor/cmder.sh
Added sourcing ~/.bashrc if it exists.
changed .gitignore to ignore anything with path of config/user-*
removed my personal files from .gitignore, left in config/user-*
Make sure $CMDER_ROOT does not have a trailing '/'
%CMDER_ROOT% does not have trailing '\'. allow user to specify a conemu.xml on the command line
Removed '\' from %CMDER_ROOT%
2015-11-09 11:25:42 +08:00
|
|
|
|
# We do this for Powershell as Admin Sessions because CMDER_ROOT is not beng set.
|
|
|
|
|
if (! $ENV:CMDER_ROOT ) {
|
2017-06-23 18:39:36 +08:00
|
|
|
|
if ( $ENV:ConEmuDir ) {
|
|
|
|
|
$ENV:CMDER_ROOT = resolve-path( $ENV:ConEmuDir + "\..\.." )
|
|
|
|
|
} else {
|
|
|
|
|
$ENV:CMDER_ROOT = resolve-path( $PSScriptRoot + "\.." )
|
|
|
|
|
}
|
Added/enhanced bash with cmder.sh/user-cmder.sh, organized tasks menu
added personal files to .gitignore so they never get uploaded to the repo and added support for msys2 bash in the new git for windows
added autocreate of config/user-cmder.sh if iot does not exist and added it to the .gitignore
Added tasks: cmd::Cmder, cmd::Cmder as Admin, bash::bash, bash::bash as Admin, bash::mintty, bash::mintty as admin, powershell::powershell, powershell::powershell as Admin. Set default task to cmd::Cmder. Cot rid of init.bat running before /bin/bash, fixes double exit requirement
Added running git for windows post-install.bat on first cmder launch
fixed file/path not found errors when launching powershell as admin
fixed file/path not found errors when launching bash/mintty as admin
fixed PATH in vendor/cmder.sh
Added sourcing ~/.bashrc if it exists.
changed .gitignore to ignore anything with path of config/user-*
removed my personal files from .gitignore, left in config/user-*
Make sure $CMDER_ROOT does not have a trailing '/'
%CMDER_ROOT% does not have trailing '\'. allow user to specify a conemu.xml on the command line
Removed '\' from %CMDER_ROOT%
2015-11-09 11:25:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Remove trailing '\'
|
|
|
|
|
$ENV:CMDER_ROOT = (($ENV:CMDER_ROOT).trimend("\"))
|
|
|
|
|
|
2017-07-14 17:30:19 +08:00
|
|
|
|
# do not load bundled psget if a module installer is already available
|
|
|
|
|
# -> recent PowerShell versions include PowerShellGet out of the box
|
|
|
|
|
$moduleInstallerAvailable = [bool](Get-Command -Name 'Install-Module' -ErrorAction SilentlyContinue | Out-Null)
|
|
|
|
|
|
2015-05-19 22:44:32 +08:00
|
|
|
|
# Add Cmder modules directory to the autoload path.
|
2015-03-18 23:33:55 +08:00
|
|
|
|
$CmderModulePath = Join-path $PSScriptRoot "psmodules/"
|
2014-09-17 15:55:15 +08:00
|
|
|
|
|
2017-07-14 17:30:19 +08:00
|
|
|
|
if(-not $moduleInstallerAvailable -and -not $env:PSModulePath.Contains($CmderModulePath) ){
|
2015-03-18 23:33:55 +08:00
|
|
|
|
$env:PSModulePath = $env:PSModulePath.Insert(0, "$CmderModulePath;")
|
2014-09-17 15:55:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-12 12:28:04 +08:00
|
|
|
|
try {
|
|
|
|
|
Get-command -Name "vim" -ErrorAction Stop >$null
|
|
|
|
|
} catch {
|
2015-11-24 08:10:22 +08:00
|
|
|
|
# # You could do this but it may be a little drastic and introduce a lot of
|
|
|
|
|
# # unix tool overlap with powershel unix like aliases
|
|
|
|
|
# $env:Path += $(";" + $env:CMDER_ROOT + "\vendor\git-for-windows\usr\bin")
|
|
|
|
|
# set-alias -name "vi" -value "vim"
|
|
|
|
|
# # I think the below is safer.
|
2016-01-12 05:09:23 +08:00
|
|
|
|
|
2015-11-25 00:58:58 +08:00
|
|
|
|
new-alias -name "vim" -value $($ENV:CMDER_ROOT + "\vendor\git-for-windows\usr\bin\vim.exe")
|
|
|
|
|
new-alias -name "vi" -value vim
|
2015-11-12 12:28:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-25 20:22:07 +08:00
|
|
|
|
try {
|
2015-05-19 22:47:06 +08:00
|
|
|
|
# Check if git is on PATH, i.e. Git already installed on system
|
2015-03-25 20:22:07 +08:00
|
|
|
|
Get-command -Name "git" -ErrorAction Stop >$null
|
2015-05-19 22:47:06 +08:00
|
|
|
|
} catch {
|
2016-03-03 01:59:12 +08:00
|
|
|
|
$env:Path += $(";" + $env:CMDER_ROOT + "\vendor\git-for-windows\cmd")
|
|
|
|
|
# for bash.exe, which in the cmd version is found as <GIT>\usr\bin\bash.exe
|
Added/enhanced bash with cmder.sh/user-cmder.sh, organized tasks menu
added personal files to .gitignore so they never get uploaded to the repo and added support for msys2 bash in the new git for windows
added autocreate of config/user-cmder.sh if iot does not exist and added it to the .gitignore
Added tasks: cmd::Cmder, cmd::Cmder as Admin, bash::bash, bash::bash as Admin, bash::mintty, bash::mintty as admin, powershell::powershell, powershell::powershell as Admin. Set default task to cmd::Cmder. Cot rid of init.bat running before /bin/bash, fixes double exit requirement
Added running git for windows post-install.bat on first cmder launch
fixed file/path not found errors when launching powershell as admin
fixed file/path not found errors when launching bash/mintty as admin
fixed PATH in vendor/cmder.sh
Added sourcing ~/.bashrc if it exists.
changed .gitignore to ignore anything with path of config/user-*
removed my personal files from .gitignore, left in config/user-*
Make sure $CMDER_ROOT does not have a trailing '/'
%CMDER_ROOT% does not have trailing '\'. allow user to specify a conemu.xml on the command line
Removed '\' from %CMDER_ROOT%
2015-11-09 11:25:42 +08:00
|
|
|
|
$env:Path += $(";" + $env:CMDER_ROOT + "\vendor\git-for-windows\bin")
|
2015-05-19 22:47:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-23 21:53:34 +08:00
|
|
|
|
$gitLoaded = $false
|
|
|
|
|
function Import-Git($Loaded){
|
|
|
|
|
if($Loaded) { return }
|
2016-10-10 19:01:55 +08:00
|
|
|
|
$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) ) {
|
2016-05-23 21:53:34 +08:00
|
|
|
|
Write-Warning "Missing git support, install posh-git with 'Install-Module posh-git' and restart cmder."
|
|
|
|
|
}
|
2016-10-10 19:01:55 +08:00
|
|
|
|
# Make sure we only run once by alawys returning true
|
2016-05-23 21:53:34 +08:00
|
|
|
|
return $true
|
2015-03-25 20:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-25 20:29:10 +08:00
|
|
|
|
function checkGit($Path) {
|
2016-02-10 21:42:12 +08:00
|
|
|
|
if (Test-Path -Path (Join-Path $Path '.git') ) {
|
2016-05-23 21:53:34 +08:00
|
|
|
|
$gitLoaded = Import-Git $gitLoaded
|
2015-03-25 20:29:10 +08:00
|
|
|
|
Write-VcsStatus
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
$SplitPath = split-path $path
|
|
|
|
|
if ($SplitPath) {
|
|
|
|
|
checkGit($SplitPath)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 18:50:37 +08:00
|
|
|
|
if (Get-Module PSReadline -ErrorAction "SilentlyContinue") {
|
|
|
|
|
Set-PSReadlineOption -ExtraPromptLineCount 1
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-03 22:06:41 +08:00
|
|
|
|
# Enhance Path
|
2018-11-02 21:00:41 +08:00
|
|
|
|
$env:Path = "$Env:CMDER_ROOT\bin;$Env:CMDER_ROOT\vendor\bin;$env:Path;$Env:CMDER_ROOT"
|
2015-10-15 00:50:52 +08:00
|
|
|
|
|
2016-05-12 20:26:18 +08:00
|
|
|
|
#
|
|
|
|
|
# Prompt Section
|
2018-09-01 06:02:56 +08:00
|
|
|
|
# Users should modify their user_profile.ps1 as it will be safe from updates.
|
2016-05-12 20:26:18 +08:00
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
# 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
|
2016-05-23 21:53:34 +08:00
|
|
|
|
checkGit($pwd.ProviderPath)
|
2016-05-12 20:26:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-01 06:25:11 +08:00
|
|
|
|
<#
|
|
|
|
|
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 = 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"
|
|
|
|
|
PostPrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline
|
|
|
|
|
$global:LASTEXITCODE = $realLASTEXITCODE
|
|
|
|
|
return " "
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Drop *.ps1 files into "$ENV:CMDER_ROOT\config\profile.d"
|
|
|
|
|
# to source them at startup.
|
|
|
|
|
if (-not (test-path "$ENV:CMDER_ROOT\config\profile.d")) {
|
|
|
|
|
mkdir "$ENV:CMDER_ROOT\config\profile.d"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pushd $ENV:CMDER_ROOT\config\profile.d
|
2018-11-18 08:55:24 +08:00
|
|
|
|
foreach ($x in Get-ChildItem *.psm1) {
|
2017-03-01 06:25:11 +08:00
|
|
|
|
# write-host write-host Sourcing $x
|
2018-08-29 19:38:59 +08:00
|
|
|
|
Import-Module $x
|
2017-03-01 06:25:11 +08:00
|
|
|
|
}
|
2018-11-18 08:55:24 +08:00
|
|
|
|
|
|
|
|
|
foreach ($x in Get-ChildItem *.ps1) {
|
|
|
|
|
# write-host write-host Sourcing $x
|
|
|
|
|
. $x
|
|
|
|
|
}
|
2017-03-01 06:25:11 +08:00
|
|
|
|
popd
|
|
|
|
|
|
2018-03-13 23:38:27 +08:00
|
|
|
|
# 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
|
2018-04-04 01:20:49 +08:00
|
|
|
|
if ($ENV:CMDER_USER_CONFIG -ne "" -and (test-path "$ENV:CMDER_USER_CONFIG\profile.d")) {
|
2018-03-13 23:38:27 +08:00
|
|
|
|
pushd $ENV:CMDER_USER_CONFIG\profile.d
|
2018-11-18 08:55:24 +08:00
|
|
|
|
foreach ($x in Get-ChildItem *.psm1) {
|
2018-03-13 23:38:27 +08:00
|
|
|
|
# write-host write-host Sourcing $x
|
2018-08-29 19:38:59 +08:00
|
|
|
|
Import-Module $x
|
2018-03-13 23:38:27 +08:00
|
|
|
|
}
|
2018-11-18 08:55:24 +08:00
|
|
|
|
|
|
|
|
|
foreach ($x in Get-ChildItem *.ps1) {
|
|
|
|
|
# write-host write-host Sourcing $x
|
|
|
|
|
. $x
|
|
|
|
|
}
|
2018-03-13 23:38:27 +08:00
|
|
|
|
popd
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-01 06:02:56 +08:00
|
|
|
|
# Renaming to "config\user_profile.ps1" to "user_profile.ps1" for consistency.
|
|
|
|
|
if (test-path "$env:CMDER_ROOT\config\user-profile.ps1") {
|
|
|
|
|
rename-item "$env:CMDER_ROOT\config\user-profile.ps1" user_profile.ps1
|
|
|
|
|
}
|
2018-03-13 23:38:27 +08:00
|
|
|
|
|
2018-09-01 06:02:56 +08:00
|
|
|
|
$CmderUserProfilePath = Join-Path $env:CMDER_ROOT "config\user_profile.ps1"
|
2018-03-13 23:38:27 +08:00
|
|
|
|
if (Test-Path $CmderUserProfilePath) {
|
2015-10-15 00:50:52 +08:00
|
|
|
|
# Create this file and place your own command in there.
|
2018-11-18 08:55:24 +08:00
|
|
|
|
. "$CmderUserProfilePath" # user_profile.ps1 is not a module DO NOT USE import-module
|
2018-03-13 23:38:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($ENV:CMDER_USER_CONFIG) {
|
2018-09-01 06:02:56 +08:00
|
|
|
|
# Renaming to "$env:CMDER_USER_CONFIG\user-profile.ps1" to "user_profile.ps1" for consistency.
|
|
|
|
|
if (test-path "$env:CMDER_USER_CONFIG\user-profile.ps1") {
|
|
|
|
|
rename-item "$env:CMDER_USER_CONFIG\user-profile.ps1" user_profile.ps1
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-02 21:00:41 +08:00
|
|
|
|
$env:Path = "$Env:CMDER_USER_CONFIG\bin;$env:Path"
|
|
|
|
|
|
2018-09-01 06:02:56 +08:00
|
|
|
|
$CmderUserProfilePath = Join-Path $ENV:CMDER_USER_CONFIG "user_profile.ps1"
|
2018-06-03 06:15:33 +08:00
|
|
|
|
if (Test-Path $CmderUserProfilePath) {
|
2018-11-18 08:55:24 +08:00
|
|
|
|
. "$CmderUserProfilePath" # user_profile.ps1 is not a module DO NOT USE import-module
|
2018-06-03 06:15:33 +08:00
|
|
|
|
}
|
2018-03-13 23:38:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-03 06:15:33 +08:00
|
|
|
|
if (! (Test-Path $CmderUserProfilePath) ) {
|
2018-12-14 04:13:17 +08:00
|
|
|
|
Write-Host -BackgroundColor Darkgreen -ForegroundColor White "First Run: Creating user startup file: $CmderUserProfilePath"
|
|
|
|
|
Copy-Item "$env:CMDER_ROOT\vendor\user_profile.ps1.default" -Destination $CmderUserProfilePath
|
2016-05-12 20:26:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
2016-10-10 17:30:01 +08:00
|
|
|
|
# Functions can be made constant only at creation time
|
2016-05-12 20:26:18 +08:00
|
|
|
|
# ReadOnly at least requires `-force` to be overwritten
|
|
|
|
|
Set-Item -Path function:\prompt -Value $Prompt -Options ReadOnly
|