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:


posted on Tuesday, February 24, 2009 9:18:45 PM (GMT Standard Time, UTC+00:00)  #    Add Comment | Comments [0]
 Thursday, February 19, 2009
Nikhil Kothari a software architect on Microsoft's .NET Developer Platform group has created a free tool called Script# that allows you write C# code that will be compiled into JavaScript. Using Script# you can take advantage of all your existing .NET tools and the power of Visual Studio. According to nikhil's own site Script# is used extensively within Microsoft when building Ajax experiences in Windows Live and Office along with external companies such as Facebook.

The Script# web site is http://projects.nikhilk.net/ScriptSharp, you can download Script# for Visual Studio 2005 or 2008. Along with the plug-ins for Visual Studio there is a very good PDF Readme that contains lots of examples of using Script#.

Nikhi also has a roadmap for Script# two notable additions are planned for 2009 Unit testing support and jQuery support. This is something I'm playing with and I think is worth checking out.

posted on Thursday, February 19, 2009 11:46:27 PM (GMT Standard Time, UTC+00:00)  #    Add Comment | Comments [0]
 Tuesday, February 17, 2009
With the advent of Windows PowerShell automating tasks in windows should now be a sinch for a .NET developer as PowerShell's scripting language is relatively easy to a C# programmer to pickup (trust me I've come down the path). One of the things I have a lot of is MP3 files from my extensive music collection and various podcats that I listen to, so I needed a way to manipulate these tags for example change some details such as adding missing information or even move the files into my music folderr structure.

A library that allowed me to do this easily was TagLib Sharp. It's avaliable from Novell and will work with .NET and Mono.

Because this is a .NET assembly you are free to use it in your C#, Visual Basic .NET. To use the library with any of thse languages simply add the assembly as a reference to your project and add the appropriate using or imports statement to the top of the class file that will use the library. However as PowerShell is my new best friend I'll show you how to use the library with PowerShell.

To use any assembly in your PowerShell script you can use the Assembly class in the System.Reflection namespace. The Assembly classes has the static LoadFile method that allows you to specify a location to load a dll from.

[Reflection.Assembly]::LoadFile("C:\Users\Alan\Code\PowerShell\MyScripts\taglib-sharp-dotnet20.dll")


Once you have imported the assembly as shown above you can begin to use it to modify settings on you media files. For example the line below will create a File object that represents the specified physical media file.

$media = [TagLib.File]::Create( "$filePath" )

Once this has been done you simply access the various tags using $media.Tag.XXX. The script below will set the Title and Track information.

$media.Tag.Title = $matches["title"]
$media.Tag.Track = $trackNum.ToString()

Once you have set the tags to the values you want you save the changes by calling the Save method on the media file object.

$media.Save()

One thing I did notice when using this library was that it failed to set the tags if they previously had no value so to get around this I manually set the tags to a character for sting values and 1 for numberic values suchas year and track number.




posted on Tuesday, February 17, 2009 3:16:51 PM (GMT Standard Time, UTC+00:00)  #    Add Comment | Comments [0]
 Sunday, February 15, 2009
This is one of the books from Manning with the cool covers that I like. This book has been written by Bruce Payette one of the designers of PowerShell. Because of this the book offers in-depth information on many aspects of PowerShell.  The book is split into two sections. Part one focuses on the basics, it covers using PowerShell at the command line and writing and run your own scripts. I read the nine chapters in this section from start to finish and was please to find lots of working and easy to follow code examples which I could play with and easily refer back to as I wrote my own scripts. I found this section greatly enhanced my knowledge of PowerShell in a short space of time, I would put this down to a good balance between verbose text and script examples to experiment with and if you are like me and learn beat by doing this book will suit you. The second section covered more advanced topics such as:
  1. Processing text files and XML
  2. .NET and WinForms
  3. Windows objects: COM and WMI
  4. Security

These were also detailed chapters with good examples however to date I have gotten more use from chapters 10 and 11 this is no fault  with the book it’s more of a reflection on what I’ve been working with recently. Since taking the time to sit down with this book I have become very keen on PowerShell especially having seen and used the scripting tools available on Linux and UNIX.

I found the book well structured and importantly for me it contains lots of script examples. Previously I had tried to learn PowerShell simply from various web sites however lack of discipline kept getting in the way. With the book I was able to discipline myself better and managed to work through this book in just over one week. Now I’m able to write my own scripts and when I’ve needed to I’ve found it very easy to refer back to the book. The back cover says that this is a book for sysadmins and developers, I would agree with this and would recommend this book for people with previous scripting of programming experience if you don’t have these then a more basic beginners book may allow you to make more progress.

The books web site: http://www.manning.com/payette/

posted on Sunday, February 15, 2009 6:18:46 PM (GMT Standard Time, UTC+00:00)  #    Add Comment | Comments [0]
 Thursday, February 12, 2009
If you are writing any scripts for PowerShell I'd recommend looking at PowerGUI Script Editor. It's a free comunity project that has some really nice features you now expect when writting code such as intelli-sense, syntax highlighting and the ability to debug your script.

The overall aim of the project is to create a GUI for Windows PowerShell where you can manipulate objects in PowerShell by pointing and clicking. For example there is bundled functionality to list all process running on the current system invoke actions on these processes such as stopping the process. Personally I just use the script editor to edit my scripts and as I have customized my PowerShell as described in a previous post, it easy for me to then execute these scripts as I need.




posted on Thursday, February 12, 2009 2:26:33 PM (GMT Standard Time, UTC+00:00)  #    Add Comment | Comments [0]