Last active
January 5, 2021 17:27
-
-
Save AMCN41R/8e7ba6d1c59f930fff957a45b73caff4 to your computer and use it in GitHub Desktop.
My PowerShell Profile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
# UI | |
$console = $host.ui.RawUI | |
# VARIABLES | |
$me = $env:userprofile | |
$appData = $me + "\AppData" | |
$dev = "C:\Dev" | |
$oneDrive = $me + "\OneDrive" | |
$profileSource = $oneDrive + "\Settings\powershell-profile.ps1" | |
# REMOVE SYSTEM ALIASES | |
if (Test-Path Alias:gp) { Remove-Item Alias:gp -Force } | |
if (Test-Path Alias:gc) { Remove-Item Alias:gc -Force } | |
if (Test-Path Alias:gl) { Remove-Item Alias:gl -Force } | |
if (Test-Path Alias:gm) { Remove-Item Alias:gm -Force } | |
# TERMINAL | |
function term-aboutme() { | |
wt -d "C:\dev\about-me" `; split-pane -d "C:\dev\about-me" | |
} | |
function term-review() { | |
wt -d "C:\dev\re-view\app" `; split-pane -d "C:\dev\re-view\app\packages\client" | |
} | |
# PROMPT | |
function Prompt { | |
$console.WindowTitle = GetTitle | |
$branchDetails = CurrentBranchDetails | |
Write-Host "" | |
Write-Host ("> ") -NoNewline -ForegroundColor Cyan | |
Write-Host ($(Get-Location)) -NoNewline -ForegroundColor DarkMagenta | |
if ($branchDetails -ne $null) { | |
Write-Host (" > ") -NoNewline -ForegroundColor Cyan | |
if ($branchDetails[1]) { | |
Write-Host ("[" + $branchDetails[0] + "] ") -ForegroundColor Green | |
} | |
else { | |
Write-Host ("[" + $branchDetails[0] + "] ") -ForegroundColor Red | |
} | |
} | |
else { | |
Write-Host "" | |
} | |
Write-Host ("$") -NoNewline -ForegroundColor Cyan | |
return " " | |
} | |
function GetTitle() { | |
[string]$loc = Get-Location | |
# Dev Repos | |
if ($loc.StartsWith("$dev\")) { | |
return "DEV > " + (Get-Item .).Name | |
} | |
# default to path | |
return $loc | |
} | |
function IsGitRepo() { | |
try { | |
git status | Out-Null | |
return $true | |
} | |
catch { | |
return $false | |
} | |
} | |
function CurrentBranchDetails() { | |
try { | |
$status = git status -sb | Select-Object -first 1 | |
$letterCount = $status.IndexOf("...") - 3 | |
if ($letterCount -lt 0) { | |
return @($status.Substring(3), $false) | |
} | |
else { | |
return @($status.Substring(3, $letterCount), $true) | |
} | |
} | |
catch { | |
return $null | |
} | |
} | |
# TAB EXPANSION | |
function TabExpansion($line, $lastWord) { | |
# git expansion - only if we're not piping | |
if (-not $line.Contains('|')) { | |
# handles custom git shortcuts | |
if ($line -match '^(gco|gpup) (.*)') { | |
gitBranches($matches[2]) | |
} | |
# handles regular git commands | |
elseif ($line -match '^git (.*)') { | |
gitTabExpansion($line) | |
} | |
# handles docker container names | |
elseif ($line -match '^docker (.*)') { | |
dockerTabExpansion($matches[1]) | |
} | |
} | |
} | |
function dockerTabExpansion($statement) { | |
DockerContainerNames($statement.split()[-1]) | |
} | |
function gitTabExpansion($statement) { | |
switch -regex ($statement) { | |
# handles git branch -d <branch name> | |
'git branch -(d|D) (.+)$' { | |
gitBranches($matches[2]) | |
return | |
} | |
# handles git checkout <branch name> | |
# handles git merge <branch name> | |
'git (checkout|merge) (.+)$' { | |
gitBranches($matches[2]) | |
return | |
} | |
# fallback to git command completion | |
'git ([A-Z]*)$' { | |
gitCommands($matches[1]) | |
return | |
} | |
} | |
} | |
function gitCommands($filter) { | |
$cmdList = @() | |
$output = git help | |
foreach ($line in $output) { | |
if ($line -match '^ ([A-Z]+) (.*)') { | |
$cmd = $matches[1] | |
if ($filter -and $cmd.ToLower().StartsWith($filter.ToLower())) { | |
$cmdList += $cmd.Trim() | |
} | |
elseif (-not $filter) { | |
$cmdList += $cmd.Trim() | |
} | |
} | |
} | |
$cmdList | Sort-Object | |
} | |
function gitBranches($filter) { | |
$remotes = @(git branch -r | | |
ForEach-Object { $_.Trim().Substring(($_.Trim().IndexOf('/') + 1)) } | | |
Where-Object { -Not $_.StartsWith("HEAD ->") }) | |
$locals = @(git branch | | |
ForEach-Object { $_.Trim('*', ' ') }) | |
$branches = $locals + $remotes | Sort-Object -Unique | |
$cats = $branches | | |
Where-Object { $_.IndexOf("/") -ne -1 } | | |
ForEach-Object { $_.Substring(0, $_.IndexOf("/") + 1) } | | |
Sort-Object -Unique | |
$target = $branches + $cats | Sort-Object -Unique | |
$target | ForEach-Object { | |
if ($filter -and $_.ToLower().StartsWith($filter.ToLower())) { | |
$_ | |
} | |
elseif (-not $filter) { | |
$_ | |
} | |
} | |
} | |
# PROGRAM SHORTCUTS | |
function P() { Invoke-Item C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe } | |
Set-Alias np "C:\Program Files (x86)\Notepad++\notepad++.exe" | |
Set-Alias sysProps SystemPropertiesAdvanced.exe | |
Set-Alias vim "C:\Program Files (x86)\Vim\vim80\vim.exe" | |
# EXPLORER SHORTCUTS | |
function Me() { Set-Location $me } | |
function Dev() { Set-Location $dev } | |
function AppData() { explorer $appData } | |
function O() { Invoke-Item . } | |
function Ex() { Invoke-Item $args[0] } | |
function Hosts() { np C:\Windows\System32\drivers\etc\hosts } | |
# DOCKER SHORTCUTS | |
Set-Alias dc docker-compose | |
function DPS() { docker ps } | |
function DPA() { docker ps -a } | |
function DIM() { | |
if ($args[0] -eq "-f") { | |
docker images | Where-Object { !$_.Contains("<none>") } | |
} | |
else { | |
docker images | |
} | |
} | |
function DockerContainerNames($filter) { | |
$target = @(docker container ls -a --no-trunc --format "{{json .Names}}" | ForEach-Object { $_.Trim("`"") } | Sort-Object -Unique) | |
$target | ForEach-Object { | |
if ($filter -and $_.ToLower().StartsWith($filter.ToLower())) { | |
$_ | |
} | |
elseif (-not $filter) { | |
$_ | |
} | |
} | |
} | |
# GIT SHORTCUTS | |
function GS() { gitStatus } | |
function GP() { git push } | |
function GC() { | |
git commit -m $args[0] | |
Write-Host "" | |
Write-Host "# LOG #" -ForegroundColor Magenta | |
gl -1 | |
gitStatus | |
} | |
function GA() { | |
git add $args[0] | |
gitStatus | |
} | |
function GF() { | |
git fetch --prune | |
gitStatus | |
} | |
function GB() { git branch $args[0] } | |
function GM() { git merge --ff-only $args[0] } | |
function GL() { git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit $args[0] $args[1] } | |
function GLL() { git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all } | |
function GPL() { git pull --ff-only } | |
function GPLR() { git pull --rebase } | |
function GCO() { git checkout $args[0] } | |
function GCOB() { git checkout -b $args[0] } | |
function GPUP() { git push --set-upstream origin $args[0] } | |
function GCAN() { git commit --amend --no-edit } | |
function gitStatus() { | |
Write-Host "" | |
Write-Host "# STATUS #" -ForegroundColor Magenta | |
git status | |
} | |
# NPM SHORTCUTS | |
function npmGlobals() { | |
Write-Host "running: 'npm ls -g --depth=0'" | |
npm ls -g --depth=0 | |
} | |
function npmInstallGlobals() { | |
npm install -g gulp-cli karma-cli create-react-app typescript tslint hexo-cli | |
} | |
function setNodeEnv() { | |
$arg = $args[0] | |
if ($arg -eq "p") { | |
$arg = "production" | |
} | |
elseif ($arg -eq "d") { | |
$arg = "development" | |
} | |
$env:NODE_ENV = $arg | |
} | |
function nodeEnv() { | |
Write-Host $env:NODE_ENV | |
} | |
# DONTNET SHORTCUTS | |
Set-Alias dn dotnet | |
# OTHER | |
Set-Alias iot iotedgehubdev | |
function sln() { | |
$slns = Get-ChildItem *.sln | |
if ($slns.length -lt 1) { | |
"No solutions found." | |
} else { | |
$sln = $slns | Select-Object -first 1 | |
Invoke-Item $sln.FullName | |
} | |
} | |
function ver() { $PSVersionTable.PSVersion } | |
function CleanAll() { | |
if (IsGitRepo -eq $true) { | |
git clean -df | |
} | |
CleanBin | |
} | |
function CleanBin() { | |
"Deleting /bin contents..." | |
Get-ChildItem -inc bin -rec | Remove-Item -rec -force | |
"Deleting /bin contents...DONE" | |
"Deleting /obj contents..." | |
Get-ChildItem -inc obj -rec | Remove-Item -rec -force | |
"Deleting /obj contents...DONE" | |
} | |
# | |
Clear-Host | |
} | |
catch { | |
Write-Host ($_) | |
} | |
finally { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment