Tuesday, February 24, 2009
When reading "The Productive Programmer" one of the things that really impressed me was the ability to  write an ant build script using groovy. Currently make Java days seem to be more and more on ice as I travel further down the road of .NET I wanted this ability when generating build scripts for my .Net  
solutions. Searching the web lead me to psake. This is a project that allows you to use PowerShell syntax in your build scripts.

Psake was started by James Kovacs and is in the early stages of development. You can download the source  (a single Ps1 file) from Google code using the following address svn url:

    http://psake.googlecode.com/svn/trunk/ psake-read-only

On windows I would recommend Tortoise SVN. Once you checkout the project you will get have a single ps1 file called "psake.ps1" and two folders "examples" and "images". The examples folder contains some useful examples on using psake.

The basic usage of psake is:

psake buildFile

Where buildFile is a PowerShell script file that contains the build tasks. If no build file is specified  
psake will assume default.ps1. For additional information on using psake from the command line use psake -help.

Psake is a wrapper around PowerShell that adds functionality specific for creating a build script. Because  it has been built on top of PowerShell anything you can do in PowerShell is available to you in psake. One  other useful point to mention is that psake will automatically add the correct version of the .NET  framework to its path thus calling framework tools like msbuild.exe, csc.exe or vbc.exe extremely straight forward.

An example build script that is included with the download is:

properties {
  $testMessage = 'Executed Test!'
  $compileMessage = 'Executed Compile!'
  $cleanMessage = 'Executed Clean!'
}

task default -depends Test

task Test -depends Compile, Clean {
  Write-Host $testMessage
}

task Compile -depends Clean {
  Write-Host $compileMessage
}

task Clean {
  Write-Host $cleanMessage
}



The default entry point for your script is the default task defined as:

task default

You then specify what tasks the default task depends on using the '-depends' switch. So in the build  script above the default task depends on the Test task the test task depends on the Compile and Clean tasks, therefore unsurprisingly the clean task will execute followed by the compile and finally the script  in the Test task will execute.


Here is s simple example that builds a Hello World solution:


task default -depends Build

task Build
-depends Clean{
  msbuild "C:\Users\Alan\Code\PowerShell\psake\HelloWorld\HelloWorld.sln"
}

task Clean {
  msbuild
"C:\Users\Alan\Code\PowerShell\psake\HelloWorld\HelloWorld.sln" /t:clean
}



This is a very simple script that cleans and builds a solution using MsBuild. The screenshot below shows the output from executing the script, you can see that it reports the task that it is executing and displays a green success message. If the task fails a red error message is displayed detailing why the task failed.




Although this is still a work in progress I think this project definitely has potential as script such as  that shown above can be much cleaner and hence easier to read than XML therefore this is a project I would hotly tip for the future.

Some useful resources, I recommend the podcast from PowerScripting as this is an interview with James Kovacs where he really explains the pros of psake:


Comments are closed.