CSC

rstackhouse edited this page Oct 3, 2011 · 4 revisions

The CSC task allows you to run the C# Compiler (csc.exe) directly, to compile your code and build your projects.

How to use the CSC task

You can specify an array of files to build – your .cs files – what level of debug information to produce, define compiler constants, and more:

csc :build do |csc|
  csc.compile "file1", "file2", ... "fileN"
  csc.ouput = "myproject.dll"
  csc.target :library
  csc.debug = true # true, false, :full, :pdbonly
  csc.define :whatever, :something, :etc
  csc.doc = "path/to/docfile.xml"
  csc.optimize = true # true or false
end

The resources can also be specified with Rake’s built in FileList, making it easy to use patterns to include or exclude specific files.

csc :build_app do |csc|
  # exclude the test folder, which contains unit test code
  csc.resources FileList["**/*.cs"].exclude "test/"
  csc.output = "myproject.dll"

  # other options, here
end

csc :build_tests do |csc|
  csc.resources FileList["test/**/*.cs"]

  # reference the project we already built, so our tests will find the classes they are testing
  csc.references "myproject.dll"

  # other options here
end

Specifying .NET Version

The CSC task provides a .use method that tells the task which version of the .NET framework to use, to find the “csc.exe” tool. This method sets the .command attribute for you, and can be called from the Albacore.configure section, to set the .NET version to use, for all instances of the CSC task.

The default version is .NET 4.0. You only need to specify the version to use, if you need something other than this.

Albacore.configure do |config|
  config.csc.use :net35
end

csc :build do |csc|
  # override the pre-configured value for this specific task instance
  csc.use :net20
end

Valid values for the .NET version to use, are:

  • :net2
  • :net20
  • :net35
  • :net4
  • :net40

Available attributes to specify how CSC works

The following parameters are available for the CSC task.

command

Set the location of the “csc.exe” tool.

compile

An array of files to compile.

references

An array of .NET assemblies to reference

resources

An array of resources to embed into the output.

output

The file to output, including exection: .exe, .dll, etc. For example “myproject.dll” or “myproject.exe”

target

The type of output to create, specified as a symbol. See the MSDN documentation for a complete list.

debug

Set the level of debug output.

  • true
  • false
  • :full
  • :pdbonly

define

Define compiler constants.

doc

Produce xml documentation at this location.

optimize

Turn on, or off, compiler optimizations: true or false