22 May 2012

Setting Up A GitHub Specific PowerShell Profile

1 Comment Uncategorized

I was very excited to see that after several months of work Phil and Paul finally released their much talked about GitHub client for Windows. It has a great Metro style, and is very fluid and functional.  Here is an example of what my client looks like

As you can see it is very clean, and there are no typical windows borders or title bar which is common for Metro styled apps. As I was exploring the app, I stumbled on the fact that if you are using PowerShell as your default shell.

You can actually customize the PowerShell profile used by the app to be different and separate from your default Windows PowerShell profile. This can be useful if you want to install a separate set of modules or configure it in a different way than your default Windows PowerShell profile. To do this you create the PowerShell profile in the following path.

%USERPROFILE%\Documents\WindowsPowerShell\GitHub.PowerShell_profile.ps1

Here is how I configured my GitHub specific PowerShell profile.

###############################################################################
# global variables
###############################################################################
$profileDir = [System.IO.Path]::GetDirectoryName($profile)

###############################################################################
# Set up a simple prompt, adding the git prompt parts inside git repos
###############################################################################
function prompt {
    $realLASTEXITCODE = $LASTEXITCODE
    
    Write-Host

    # Reset color, which can be messed up by Enable-GitColors
    $Host.UI.RawUI.ForegroundColor = $GitPromptSettings.DefaultForegroundColor

    Write-VcsStatus

    Write-Host($pwd) -ForegroundColor Green

    $global:LASTEXITCODE = $realLASTEXITCODE
    
    return "$ "
}

###############################################################################
# Exposes the environment vars in a batch and sets them in this PS session
###############################################################################
function Get-Batchfile($file) 
{
    $theCmd = "`"$file`" & set" 
    cmd /c $theCmd | Foreach-Object {
        $thePath, $theValue = $_.split('=')
        Set-Item -path env:$thePath -value $theValue
    }
}


###############################################################################
# Sets the VS variables for this PS session to use
###############################################################################
function VsVars32($version = "10.0")
{
    $theKey = "HKLM:SOFTWARE\Microsoft\VisualStudio\" + $version
    $theVsKey = get-ItemProperty $theKey
    $theVsInstallPath = [System.IO.Path]::GetDirectoryName($theVsKey.InstallDir)
    $theVsToolsDir = [System.IO.Path]::GetDirectoryName($theVsInstallPath)
    $theVsToolsDir = [System.IO.Path]::Combine($theVsToolsDir, "Tools")
    $theBatchFile = [System.IO.Path]::Combine($theVsToolsDir, "vsvars32.bat")
    Get-Batchfile $theBatchFile
    [System.Console]::Title = "Visual Studio " + $version + " Windows Powershell"
}

###############################################################################
# aliases
###############################################################################

set-alias wide   format-wide

###############################################################################
# functions
###############################################################################

set-content function:\mklink "cmd /c mklink `$args"

###############################################################################
# setup posh-git
###############################################################################

Push-Location (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent)

# add Git to PATH
$env:path += ";" + (Get-Item "Env:ProgramFiles(x86)").Value + "\Git\bin"

# import posh-git
Import-Module posh-git

# customize git prompt display settings
$global:GitPromptSettings.BeforeText = '['
$global:GitPromptSettings.AfterText = '] '
$global:GitPromptSettings.BranchAheadForegroundColor = [ConsoleColor]::DarkGreen
$global:GitPromptSettings.WorkingForegroundColor = [ConsoleColor]::Magenta
$global:GitPromptSettings.UntrackedForegroundColor = [ConsoleColor]::Magenta

# set TERM to cygwin
Enable-GitColors

Pop-Location

Start-SshAgent -Quiet

Let me know your configuration and any tips you have about the new GitHub app. A big thanks should be given to Phil and Paul for making our lives better.

Tags: , ,
written by
Nick Berardi
subscribe
If you found this post valuable and would like to see more like it you can follow me.

One Response to “Setting Up A GitHub Specific PowerShell Profile”

  1. Reply Manuel says:

    Hi Nick, awesome…

    i replace the func Get-Batchfile with this one:
    function Invoke-CmdScript([string] $script, [string] $parameters)
    {
    $theCmd = “`”$script`” $parameters && set”
    cmd /c $theCmd | Foreach-Object {
    if ($_ -match “^(.*?)=(.*)$”)
    {
    Set-Content “env:\$($matches[1])” $matches[2]
    }
    }
    }

    replace the func VsVars32 with this one:
    function VsVars32($version = “10.0″, $parameters)
    {
    $envItem = Get-Item (“Env:\VS” + $version.Replace(“.”, “”) + “COMNTOOLS”)
    if ($envItem -ne $NULL)
    {
    $theBatchFile = Join-Path $envItem.Value “vsvars32.bat”
    Invoke-CmdScript $theBatchFile $parameters
    [System.Console]::Title = “Visual Studio ” + $version + ” Windows Powershell”
    }
    }

    and add this func for win sdk env vars:
    function WinSdkVars($version = “7.1″, $parameters)
    {
    $theKey = “HKLM:SOFTWARE\Microsoft\Microsoft SDKs\Windows\” + “v” + $version
    $theSdkKey = Get-ItemProperty $theKey
    $theSdkInstallPath = Resolve-Path $theSdkKey.InstallationFolder
    $theSdkBinDir = Join-Path $theSdkInstallPath “Bin”
    $theBatchFile = Join-Path $theSdkBinDir “SetEnv.Cmd”
    Invoke-CmdScript $theBatchFile $parameters
    [System.Console]::Title = “Windows SDK ” + $version + ” Windows Powershell”
    }

    this is my contribution.
    Many greetings, Manuel

Leave a Reply