Skip to content

Instantly share code, notes, and snippets.

@dragan
Created May 3, 2012 04:56
Show Gist options
  • Save dragan/2583261 to your computer and use it in GitHub Desktop.
Save dragan/2583261 to your computer and use it in GitHub Desktop.
cross plat dev .net rakefile
# encoding: utf-8
require 'rake/clean'
load "VERSION"
COMPILE_TARGET = ENV['CONFIG'].nil? ? "Debug" : ENV['CONFIG']
CLR_TOOLS_VERSION = "v4.0.30319"
NUGET_PACKAGE_CONFIGS = ["src/Mulder.Cli/packages.config", "src/Mulder.Base/packages.config", "src/Mulder.Features/packages.config", "src/Mulder.Tests/packages.config"]
ARTIFACTS_DIR = "artifacts"
#Add the folders that should be cleaned as part of the clean task
CLEAN.include(ARTIFACTS_DIR)
desc("Default task, allows not having to specify a task. Comparable to rake merge.")
task :default => [:compile,:test,:merge]
desc("Writes out the CommonAssemblyInfo.cs file.")
task :assembly_info do
version, debug_version = get_versions
puts "Version: #{debug_version ? debug_version : version}"
write_common_assembly_info(version, debug_version)
end
desc("Creates necessary directories used by Rakefile.")
task :prepare => [:assembly_info] do
Dir.mkdir(ARTIFACTS_DIR)
end
desc("Compiles Mulder.")
task :compile => [:clean, :prepare] do
puts "Compiling: src/Mulder.sln"
verbosity = "normal"
properties = { "configuration" => "#{COMPILE_TARGET}" }
targets = ["rebuild"]
compile_solution("src/Mulder.sln", verbosity, properties, targets)
end
desc("Runs the Mulder unit tests.")
task :test => [:compile] do
puts "Running Unit Tests On: src/Mulder.Tests/bin/#{COMPILE_TARGET}/Mulder.Tests.dll"
args = []
args << "-nodots"
args << "-xml=\"#{ARTIFACTS_DIR}/test_results.xml\""
args << "\"src/Mulder.Tests/bin/#{COMPILE_TARGET}/Mulder.Tests.dll\""
run("nunit-console.exe", "src/packages/NUnit.Runners.2.6.0.12051/tools", args)
end
desc("Runs the Mulder feature tests.")
task :feature_test => [:compile] do
puts "Running Feature Tests On: src/Mulder.Features/bin/#{COMPILE_TARGET}/Mulder.Features.dll"
args = []
args << "-nodots"
args << "-xml=\"#{ARTIFACTS_DIR}/feature_test_results.xml\""
args << "\"src/Mulder.Features/bin/#{COMPILE_TARGET}/Mulder.Features.dll\""
run("nunit-console.exe", "src/packages/NUnit.Runners.2.6.0.12051/tools", args)
end
desc("Merge Mulder into a single application file.")
task :merge => [:test] do
args = []
args << "/out:#{ARTIFACTS_DIR}/Mulder.exe"
args << "/targetplatform:v4"
args << "src/Mulder.Cli/bin/#{COMPILE_TARGET}/Mulder.Cli.exe"
args << "#{FileList["src/Mulder.Cli/bin/#{COMPILE_TARGET}/*.dll"].to_s}"
if on_windows?
executable = "ILMerge.exe"
path = "tools/ilmerge"
else
executable = "ILRepack.exe"
path = "tools/ilrepack.1.9"
end
puts "Merging Binaries:"
puts " #{executable} #{args.join(' ')}"
run(executable, path, args)
end
# Work around for MonoDevelop not supporting NuGet yet
desc("Bootstraps project for development after clean clone.")
task :bootstrap => [:assembly_info] do
restore_packages
end
# Helper Methods
# Will probably contribute this to Albacore once I'm happy with them
# Then trim this build script down by using albacore
# Methods for writing CommonAssemblyInfo.cs
def write_common_assembly_info(version, debug_version="")
common_assembly_info = <<EOS
using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyCompany("Mulder")]
[assembly: AssemblyProduct("Mulder")]
[assembly: AssemblyCopyright("Copyright (C) Dale Ragan")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyTrademark("#{debug_version}")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("#{version}")]
[assembly: AssemblyFileVersion("#{version}")]
EOS
File.open('src/CommonAssemblyInfo.cs', 'w') { |io| io.write(common_assembly_info) }
end
def get_commit
begin
commit = `git log -1 --pretty=format:%h`
rescue
commit = "unavailable"
end
commit
end
# We use semantic versioning: http://semver.org
def get_versions
if COMPILE_TARGET == "Debug"
commit = get_commit
version = "#{BUILD_VERSION}"
debug_version = "#{BUILD_VERSION}+build.#{commit}"
else
version_array = BUILD_VERSION.split "."
build_num = version_array[2].to_i + 1
version_array[2] = "#{build_num}"
version = version_array.join(".")
end
[version, debug_version]
end
# Methods for compiling
def compile_solution(solution_path, verbosity, properties, targets)
if !on_windows?
command = "xbuild"
else
command = "MSBuild"
end
args = []
args << "\"#{solution_path}\""
args << "\/verbosity:#{verbosity}" unless verbosity.nil?
args << build_properties(properties) unless properties.nil?
args << "\/target:\"#{build_targets(targets)}\"" if targets != nil
run(command, "", args)
end
def build_targets(targets)
targets.join ";"
end
def build_properties(properties)
option_text = []
properties.each do |key, value|
option_text << "/p:#{key}\=\"#{value}\""
end
option_text.join(" ")
end
# Methods for restoring NuGet packages
def restore_package_config(config_path)
puts "Restoring Project Packages Using: #{config_path}"
args = []
args << "install"
args << config_path
args << "-o src/packages"
run("NuGet.exe", "src/.nuget", args)
end
def restore_packages
puts "Restoring NuGet Packages"
NUGET_PACKAGE_CONFIGS.each do |path|
restore_package_config(path)
end
end
# Methods for running commands, no matter if we're on Windows or *nix
def run(command, path, arguments)
begin
args = Array.new
args << arguments unless arguments.nil?
cmd = get_command(command, path, args)
Dir.chdir(Dir.pwd) do
return system(cmd)
end
rescue Exception => e
puts "Error Running Command: #{e}"
end
end
def on_windows?
if RUBY_VERSION[0..2] == "1.9"
RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? true : false
else
Config::CONFIG['host_os'] =~ /mswin|mingw/ ? true : false
end
end
def get_command(command, path, args)
if path.empty?
executable = command
else
executable = File.expand_path(command, path)
end
runWithMono = !on_windows? && (File.extname(executable) == ".exe")
cmd = runWithMono ? "mono --runtime=#{CLR_TOOLS_VERSION} " : ""
cmd += runWithMono && COMPILE_TARGET == "Debug" ? "--debug " : ""
cmd += "\"#{executable}\""
cmd += " #{args.join(' ')}" if args.length > 0
cmd
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment