Psake is a build automation tool that can be used as an alternative to the ubiqutous MsBuild XML files for .NET developers. Psake is a module for Powershell and this means your build scripts will have easy access to other resources such as remote Web Servers and file systems or other required resources in the build process. In addition, building software is primarily a procedural process and using a script with the aid of Psake makes more sense than using MSBuild XML files. It is also quicker to avoid creating MSBuild tasks and so on.
Let't take a look at a sample Psake build script:
#tasks.ps1
properties {
$config = 'debug'; #debug or release
}
task -name PackageZip -depends Build -description "proceduces a zip archive of the build output" -action {
import-module pscx
write -host "Packaging files"
write-host $config
Push-Location 'C:\Users\Tore Aurstad\Documents\visual studio 2012\Projects\TestLDAP\TestLDAP\bin'
dir $config\ | write-zip -output $pwd\$config\TestLDAP.zip
Pop-Location
}
task -name ValidateConfig -action {
assert ( 'debug', 'release' -contains $config) `
"Invalid config: $config; valid values are debug and release";
}
task -name Build -depends ValidateConfig -description "builds outdated source files" -action {
write-host 'The build task is now running';
exec {
msbuild 'C:\Users\Tore Aurstad\Documents\visual studio 2012\Projects\TestLDAP\TestLDAP.sln' /p:Configuration=$config
}
}
task -name Clean -description "deletes all build artifacts" -action {
write-host 'The clean task is now running';
exec {
msbuild 'C:\Users\Tore Aurstad\Documents\visual studio 2012\Projects\TestLDAP\TestLDAP.sln' /t:Clean /p:Configuration=$config
}
}
task -name Rebuild -depends Clean,Build -action {
write-host 'The rebuild task is now running';
}
task default -depends Build;
The following command then will invoke one of the build targets in the Psake build scripts:
PS C:\users\Tore Aurstad\Documents\WindowsPowerShell\scripts> invoke-psake -buildfile .\script1.ps1 -task PackageZip
Before running the
script1.ps script file, which I have put in the Scripts folder of the parent folder of the
$env:psmodulepath. In addition, psake had to be installed. To install psake, download the .zip file of this Powershell module from the following url:
Psake Github
Save the .zip file to your hard disk, unblock the zip file using file properties and click
Anvanced button. Unzip the file contents and move the files into the $env:psmodulespath folder, in a subfolder called
psake.
When psake is ready on your system, check that you have the correct execution policy. Run if necessary:
Set-ExecutionPolicy RemoteSigned in Powershell.
The psake script contains of multiple task declarations. The task
default is obligatory. The other tasks has action blocks, and some of these calls
msbuild inside an
exec block.
The module
pscx is also used to zip the output of running msbuild. The module is available here:
Pscx module. This module is the Powershell Community Extensions powershell module. The module is a central extension module of Powershell. By
piping the output of the dir command against the output files and using the cmdlet
write-zip and the argument -output following by the file name of the zip file to create, the output is zipped together.
Also make note that tasks can depend upon oter tasks with their -depends flag. To toggle between the
debug or
release configuration, it is possible to set this by using the -properties flag on the
invoke-psake command. To build in release, simply specify this as in the following example:
PS C:\users\Tore Aurstad\Documents\WindowsPowerShell\scripts> invoke-psake -buildfile .\script1.ps1 -task Rebuild -properties @{ 'config' = 'release' }
By setting -properties flag and passing in a hashtable where the key 'config' is set to the value 'release', it is possible to build in release mode instead of debug mode. There is also a ValidateConfig task that will check that the provided value is either 'debug' or 'release' or some other value. The
assert expression will check that the specified configuration is one of the two valid values. There is a correspondence between the property in the -properties flag and the property declared in the psake script.
Psake can be integrated into TeamCity and you can transform the functionality of the MSBuild Xml file (usually a .proj file) into a psake script. Once this is done, it is much more flexible to make changes since the build script now is inside a Powershell script. Forget creating MSBuild tasks which are compiled, this will instead be other Powershell scripts.
Here is a tutorial on how to integrate psake in Team City:
Psake and Team City