New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: PowerShell

My PowerShell Profile ala Git

377 Views
Copy Code Show/Hide Line Numbers
   1:  function ReadyEnvironment (
   2:              [string]$sharedDrive, 
   3:              [string]$userName, 
   4:              [string]$computerName)
   5:  {
   6:      set-variable tools "$sharedDrive\shared_tools" -scope 1
   7:      set-variable scripts "$sharedDrive\shared_scripts" -scope 1
   8:      set-variable rdpDirectory "$sharedDrive\shared_tools\RDP" -scope 1
   9:      set-variable desktop "C:\Users\$userName\DESKTOP" -scope 1
  10:      Write-Host "Setting environment for $computerName" -foregroundcolor cyan
  11:  }
  12:   
  13:  # General variables
  14:  $computer = get-content env:computername
  15:  switch ($computer)
  16:  {
  17:      "WORK_COMPUTER" { 
  18:          ReadyEnvironment "J:" "dlongnecker.usd259" $computer ; break }
  19:      "HOME_COMPUTER" { 
  20:          ReadyEnvironment "D:" "david" $computer ; break }
  21:      default { 
  22:          break; } 
  23:  }
  24:   
  25:  # Add Git executables to the mix.
  26:  [System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";" + (Join-Path $tools "\PortableGit-1.6.3.2\bin"), "Process")
  27:   
  28:  # Add our scripts directory in the mix.
  29:  [System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";" + $scripts, "Process")
  30:   
  31:  # Setup Home so that Git doesn't freak out.
  32:  [System.Environment]::SetEnvironmentVariable("HOME", (Join-Path $Env:HomeDrive $Env:HomePath), "Process")
  33:   
  34:  #$Global:CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()
  35:  #$UserType = "User"
  36:  #$CurrentUser.Groups | %{ 
  37:  #    if ($_.value -eq "S-1-5-32-544") {
  38:  #        $UserType = "Admin" } 
  39:  #    }
  40:   
  41:  function prompt {
  42:       # Fun stuff if using the standard PowerShell prompt; not useful for Console2.
  43:       # This, and the variables above, could be commented out.
  44:  #    if($UserType -eq "Admin") {
  45:  #      $host.UI.RawUI.WindowTitle = "" + $(get-location) + " : Admin"
  46:  #       $host.UI.RawUI.ForegroundColor = "white"
  47:  #      }
  48:  #     else {
  49:  #       $host.ui.rawui.WindowTitle = $(get-location)
  50:  #     }
  51:      
  52:      Write-Host("")
  53:      $status_string = ""
  54:      # check to see if this is a directory containing a symbolic reference, 
  55:      # fails (gracefully) on non-git repos.
  56:      $symbolicref = git symbolic-ref HEAD
  57:      if($symbolicref -ne $NULL) {
  58:          
  59:          # if a symbolic reference exists, snag the last bit as our
  60:          # branch name. eg "[master]"
  61:          $status_string += "GIT [" + `
  62:              $symbolicref.substring($symbolicref.LastIndexOf("/") +1) + "] "
  63:          
  64:          # grab the differences in this branch    
  65:          $differences = (git diff-index --name-status HEAD)
  66:          
  67:          # use a regular expression to count up the differences.
  68:          # M`t, A`t, and D`t refer to M {tab}, etc.
  69:          $git_update_count = [regex]::matches($differences, "M`t").count
  70:          $git_create_count = [regex]::matches($differences, "A`t").count
  71:          $git_delete_count = [regex]::matches($differences, "D`t").count
  72:          
  73:          # place those variables into our string.
  74:          $status_string += "c:" + $git_create_count + `
  75:              " u:" + $git_update_count + `
  76:              " d:" + $git_delete_count + " | "
  77:      }
  78:      else {
  79:          # Not in a Git environment, must be PowerShell!
  80:          $status_string = "PS "
  81:      }
  82:      
  83:      # write out the status_string with the approprate color. 
  84:      # prompt is done!
  85:      if ($status_string.StartsWith("GIT")) {
  86:          Write-Host ($status_string + $(get-location) + ">") `
  87:              -nonewline -foregroundcolor yellow
  88:      }
  89:      else {
  90:          Write-Host ($status_string + $(get-location) + ">") `
  91:              -nonewline -foregroundcolor green
  92:      }
  93:      return " "
  94:   }
  95:   
  96:  # set common aliases
  97:  find-to-set-alias 'c:\program files*\Microsoft Visual Studio 9.0\Common7\IDE' devenv.exe vs
  98:  find-to-set-alias 'c:\windows\system32\WindowsPowerShell\v1.0\' PowerShell_ISE.exe psise
  99:  find-to-set-alias 'c:\program files*\Notepad2' Notepad2.exe np
 100:   
 101:  set-alias ai assembly-info
 102:   
 103:  # creating a function since set-alias can't pass piped parameters
 104:  function aia {
 105:      get-childitem | ?{ $_.extension -eq ".dll" } | %{ ai $_ }
 106:  }
 107:   
 108:  function dc {
 109:      git diff | out-colordiff
 110:  }
 111:   
 112:  remove-item alias:ls
 113:  set-alias ls Get-ChildItemColor
 114:   
 115:  function Get-ChildItemColor {
 116:      $fore = $Host.UI.RawUI.ForegroundColor
 117:   
 118:      Invoke-Expression ("Get-ChildItem $args") |
 119:      %{
 120:        if ($_.GetType().Name -eq 'DirectoryInfo') {
 121:          $Host.UI.RawUI.ForegroundColor = 'White'
 122:          echo $_
 123:          $Host.UI.RawUI.ForegroundColor = $fore
 124:        } elseif ($_.Name -match '\.(zip|tar|gz|rar)$') {
 125:          $Host.UI.RawUI.ForegroundColor = 'Blue'
 126:          echo $_
 127:          $Host.UI.RawUI.ForegroundColor = $fore
 128:        } elseif ($_.Name -match '\.(exe|bat|cmd|py|pl|ps1|psm1|vbs|rb|reg)$') {
 129:          $Host.UI.RawUI.ForegroundColor = 'Green'
 130:          echo $_
 131:          $Host.UI.RawUI.ForegroundColor = $fore
 132:        } elseif ($_.Name -match '\.(txt|cfg|conf|ini|csv|sql|xml|config)$') {
 133:          $Host.UI.RawUI.ForegroundColor = 'Cyan'
 134:          echo $_
 135:          $Host.UI.RawUI.ForegroundColor = $fore
 136:        } elseif ($_.Name -match '\.(cs|asax|aspx.cs)$') {
 137:          $Host.UI.RawUI.ForegroundColor = 'Yellow'
 138:          echo $_
 139:          $Host.UI.RawUI.ForegroundColor = $fore
 140:         } elseif ($_.Name -match '\.(aspx|spark|master)$') {
 141:          $Host.UI.RawUI.ForegroundColor = 'DarkYellow'
 142:          echo $_
 143:          $Host.UI.RawUI.ForegroundColor = $fore
 144:         } elseif ($_.Name -match '\.(sln|csproj)$') {
 145:          $Host.UI.RawUI.ForegroundColor = 'Magenta'
 146:          echo $_
 147:          $Host.UI.RawUI.ForegroundColor = $fore
 148:         }
 149:          else {
 150:          $Host.UI.RawUI.ForegroundColor = $fore
 151:          echo $_
 152:        }
 153:      }
 154:  }
 155:   
 156:  # Use VS to either open the passed solution or the first (only) solution in the
 157:  # current directory.
 158:  function vsh {
 159:      param ($param)
 160:      
 161:      if ($param -eq $NULL) {
 162:          "A solution was not specified, opening the first one found."
 163:          $solutions = get-childitem | ?{ $_.extension -eq ".sln" }
 164:      }
 165:      else {
 166:          "Opening {0} ..." -f $param
 167:          vs $param
 168:          break
 169:      }
 170:      if ($solutions.count -gt 1) {
 171:          "Opening {0} ..." -f $solutions[0].Name
 172:          vs $solutions[0].Name
 173:      }
 174:      else {
 175:          "Opening {0} ..." -f $solutions.Name
 176:          vs $solutions.Name
 177:      }
 178:  }
by David R. Longnecker
  August 21, 2009 @ 6:39pm
Tags:
Comment:
PowerShell profile script from blog post at http://tiredblogger.wordpress.com/2009/08/21/using-git-and-everything-else-through-powershell/

Add a comment


Report Abuse
brought to you by:
West Wind Techologies


If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate