Here is a SlightlyPosher environment

 

On my personal laptop I use Console2 with a PowerShell environment loaded with useful modules like PSCX, PowerTab and LINQ. I needed to be able to use this on client machines, so I cleaned up the paths to be relative and uploaded it to a DropBox.

Now this worked great for me, however I wanted to share it with my work mates, so I needed an easier way of distributing it. Since I’ve gone through the effort why not share it with everyone.

https://github.com/naeemkhedarun/SlightlyPosher

Getting Started

 

  1. Download the latest from: https://github.com/naeemkhedarun/SlightlyPosher/zipball/master
  2. Unzip
  3. Run Install.bat – This appends the SlightlyPosher profile to your PowerShell profile.
  4. Launch Console2
  5. That’s it!

Updating your environment

 

You can update the entire environment using update.bat. This will download the latest from GitHub and unzip it into your local directory.

This will wipe out any changes to make to existing files (but not delete new ones). So please fork the project on GitHub and send me pull requests for any changes you make. It will not override changes you make to the configuration for Console2 since everyone likes their console slightly different.

What you will get

 

Console2

clip_image001

Tab Completion

clip_image005

Balloon Notifications

clip_image007 

clip_image009

Visual Studio Command Prompt

clip_image011

Get-SystemInternalsSuite (downloaded, unzipped and configured into your path)

clip_image013

clip_image015

Command line history

image

Even more stuff in modules

You can browse all the functions that are available and pre-loaded in the modules folder.

Credits

 

Since I’ve pulled in quite a bit of stuff from the web, it’s only fair that I credit others. I’ve done my best do put credits directly in the code. For example:

image

If you see code I’ve not credited, please let me know and I’ll add it in!

Enjoy

 

You can reach me @NaeemKhedarun

XmlLists with Psake / Powershell

On a POSH roll here, this time I needed to list out the Include attributes for references in an msbuild file. Let’s take a look at the POSH script equivalent of XmlList:

function xmlList([string]$file, [string]$xpath, [hashtable]$namespaces) { 
    [xml] $fileXml = Get-Content $file 
    $xmlNameTable = new-object System.Xml.NameTable
    $xmlNameSpace = new-object System.Xml.XmlNamespaceManager($xmlNameTable)

    foreach($key in $namespaces.keys)
    {
        $xmlNameSpace.AddNamespace($key, $namespaces.$key);
    }
    $nodes = @()
    $node = $fileXml.SelectNodes($xpath, $xmlNameSpace) 
    $node | ForEach-Object { $nodes += @($_.Value)}
    
    return $nodes
}

Now to use this we can do this:

$namespaces = @{ "xsi" = "http://schemas.microsoft.com/developer/msbuild/2003"}
$xpath = "//xsi:Reference/xsi:HintPath/../@Include"

$nodes = xmlList "$examplesdest_dir\NBehave.Examples.csproj" $xpath $namespaces

And it gives us a list of the references in the msbuild file.

image

XmlPoking vsixmanifests with Psake / Powershell

 

Nant had some very useful XmlPoke and XmlPeek tasks for working with xml files. When manually installing visual studio plug-ins, the vsixmanifest needs to have some fields prodded to work correctly, specifically the InstalledByMsi and Version.

So here’s a POSH version of XmlPoke:

function xmlPoke([string]$file, [string]$xpath, $value, [hashtable]$namespaces) { 
    [xml] $fileXml = Get-Content $file 
    $xmlNameTable = new-object System.Xml.NameTable
    $xmlNameSpace = new-object System.Xml.XmlNamespaceManager($xmlNameTable)

    foreach($key in $namespaces.keys)
    {
        $xmlNameSpace.AddNamespace($key, $namespaces.$key);
    }
    
    $node = $fileXml.SelectSingleNode($xpath, $xmlNameSpace) 
    if ($node) { 
        $node.InnerText = $value 

        $fileXml.Save($file)  
    } 
}

 

And the usage for vsixmanifests is below, you will need to adjust the namespaces based on the xml you are prodding.

$namespaces = @{ "vsx" = "http://schemas.microsoft.com/developer/vsx-schema/2010"}
$xpath = "/vsx:Vsix/vsx:Identifier/vsx:"
xmlPoke "$destination\extension.vsixmanifest" $xpath"InstalledByMsi" "true" $namespaces
xmlPoke "$destination\extension.vsixmanifest" $xpath"Version" $version $namespaces

ILMerging assemblies with Psake / Powershell

 

On NBehave I am migrating the existing build framework from Nant over to Psake, and am thoroughly enjoying the ability to debug and step through my build scripts.

We needed to merge some of our output assemblies before packaging, and this was extremely easy in POSH:

function ilmerge($key, $directory, $name, $assemblies, $extension)
{    
    # Create a new temp directory as we cannot overwrite the main assembly being merged.
    new-item -path $directory -name "temp_merge" -type directory -ErrorAction SilentlyContinue
    
    # Unfortuntately we need to tell ILMerge its merging CLR 4 assemblies.
    if($framework -eq "4.0")
    {
        Exec { tools\ilmerge\ilmerge.exe /keyfile:$key /out:"$directory\temp_merge\$name.$extension" "$directory\$name.$extension" $assemblies /targetplatform:"v4,$env:ProgramFiles\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0" }
    }
    else
    {
        Exec { tools\ilmerge\ilmerge.exe /keyfile:$key /out:"$directory\temp_merge\$name.$extension" "$directory\$name.$extension" $assemblies }
    }
    
    Get-ChildItem "$directory\temp_merge\**" -Include *.dll, *.pdb | Copy-Item -Destination $directory
    Remove-Item "$directory\temp_merge" -Recurse -ErrorAction SilentlyContinue
}

The parameters are straightforward and as you’d expect, here’s an example usage:

$key = "$solution_dir\NBehave.snk"
$directory = "$build_dir\dist\v$framework"
$name = "NBehave.Narrator.Framework"

$assemblies = @("$directory\gherkin.dll", 
                "$directory\NBehave.Spec.Framework.dll", 
                "$directory\Should.Core.dll", 
                "$directory\Should.Fluent.dll")

ilmerge $key $directory $name $assemblies "dll"

 

The extension should be “.dll” or “.exe” depending on what the main artifact your trying to merge is.

Newer Posts Older Posts