posts - 6,  comments - 13,  trackbacks - 0

没有什么magic,就是用Rake直接调用MsBuild编译csproj,下面是代码

CSPROJECTS = Rake::FileList["**/*.csproj"]
CONFIG = "Release"
ENV['_Framework'] = 'v3.5'

verbose(true)

def msbuild_all(target)
  msbuild_csprojects(target)
end

def msbuild_csprojects(target)
  CSPROJECTS.map {|x| msbuild(target, x)}
end

def msbuild(target, project, props={})
  platform = ":#{props['Platform']}" if props.key?('Platform')

  task "#{target}#{platform}:#{project}" do
    run_msbuild(target, project, props)
  end
end

def run_msbuild(target, project, props)
  cmd = "#{ENV['WinDir']}/Microsoft.NET/Framework/#{ENV['_Framework']}/msbuild.exe "
  cmd += "/nologo /m /v:m "
  props.each{|k,v| cmd += "/p:#{k}=\"#{v}\" "}
  cmd += "/p:Configuration=#{CONFIG} "
  cmd += "/t:#{target} #{project}"

  prop_desc = props.to_a.map{|x| "#{x[0]} = #{x[1]}"}.join(', ')
  desc = project
  desc += " (#{prop_desc})" if prop_desc.size != 0

  if target == 'clean' then
    puts "Cleaning #{desc}"
  else
    puts "Building #{desc}"
  end
  sh cmd
end

下面是Rakefile的内容:

require 'rake_msbuild'

task :default => :build

desc "build"
task :build => msbuild_all('build')

desc "clean"
task :clean => msbuild_all('clean')

desc 'rebuild'
task :rebuild => [:clean, :build]

posted on 2010-09-19 13:05 DarthVader 阅读(...) 评论(...) 编辑 收藏