Albacore Feature Preview: Building C# Code With CSC.exe

I’ve been meaning to do this for a very long time… it’s been asked for multiple times and it’s been on my list of things to do for a very long time… but it’s finally happening! I’m finally putting a ‘csc’ task into albacore so that you can build your c# code directly from the c-sharp compiler (csc.exe).

 

A Simple Example

Here’s a small sample of what a rakefile might look like, using the csc task:

   1: csc :build do |csc|

   2:   csc.command = "csc.exe"

   3:   csc.compile FileList["src/**/*.cs"].exclude("src/**/*Specs.cs")

   4:   csc.output = "myproject.dll"

   5:   csc.target = :library

   6: end

   7:  

   8: csc :build_tests => [:build] do |csc|

   9:   csc.command = "csc.exe"

  10:   csc.compile FileList["src/**/*Specs.cs"]

  11:   csc.output = "myproject.specs.dll"

  12:   csc.target = :library

  13:   csc.references "myproject.dll", "nunit.framework.dll"

  14: end

  15:  

  16: nunit :test => [:build_tests] do |nunit|

  17:   nunit.command = "nunit-console.exe"

  18:   nunit.assemblies "myproject.specs.dll"

  19: end

there are three tasks here: build, build_tests, and test.

  1. the build task will build all .cs files that do not end with “Specs.cs” and will produce a dll called “myproject.dll”
  2. the build_tests task will build all of the unit tests by compiling al files that end with “Specs.cs”, referencing the “myproject.dll” as well as “nunit.framework.dll” and producing a file called “myproject.specs.dll”
  3. and the test task will run the tests that are found in “myproject.specs.dll”

It’s a fairly simple script but that’s the point. By using a FileList to specify what files should be included / excluded from compilation, you can use naming conventions to determine what will or won’t be built into any given assembly. This means there is no need to have separate “tests” folder or project. You can have your tests living in files right next to the things that are being tested. With proper naming conventions being followed (whatever conventions your team wants to use), you don’t have to worry about your tests being built into your production code.

 

A Work In Progress

This is still a work in progress and not an official announcement of the csc task’s availability. The example above shows almost everything that the csc task is currently capable of… and as you can see, it doesn’t have a lot of the csc command line features built into it, yet. If you’d like to help add features and functionality, the task is currently being built in the dev branch of the albacore repository.

If you’d like to follow the progress of the csc task without wading through all the other work going on in the dev branch, I’ve started a small project over on github, called vimbacore. This is my personal playground for trying out two things: c# coding in vim (not just using vim from visual studio… but, only using vim), and the csc task in albacore. If you would like to see the evolution of the csc task in albacore, you can watch this project. I’m going to try and keep it up to date with new features and functionality related to the csc task and use this example project as a means of determining what features and functionality the csc task needs.


Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs SignalLeaf.com - the amazingly awesome podcast audio hosting service that everyone should be using, and WatchMeCode.net where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in .NET, Albacore, C#, Rake, Unit Testing, Vim. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.lostechies.com/members/louissalin/default.aspx Louis Salin

    This is nice! I might be able to use that on Linux and actually have build scripts that are compatible with Windows for our little craftsmanship club!

  • Rob Conery

    Ohhhhh HOTNESS! I’d love to contribute a task that builds the .csproj file using Builder or HAML – that way the project will be compatible with VS.

    This would be sooo very groovy.