Monday, February 07, 2011

Quick Script for Building .sln Files With Configurations

Repeatedly typing out msbuild command lines with configuration switches (release, debug) and clean/regular, FTL.

Here’s a quick Ruby script to let you specify –d, –r, –c for debug, release, and clean, respectively. –d and –r are mutually exclusive, but you need one.

Oh! the joy of optparse. Dunno how many times I’ve written a command line parser for just this sort of use case. Love how well this works OOB.

require 'optparse'
 
def process_command_args
    $options = {}
 
    opts = OptionParser.new 
    opts.banner = "Usage: build [options]"
 
    opts.on( "-r", "--release", "release configuration" ) do
        $options[:releaseconfig] = true
    end
 
    opts.on( "-d", "--debug", "debug config" ) do
        $options[:debugconfig] = true
    end
 
    opts.on( "-c", "--clean", "clean build" ) do
        $options[:clean] = true
    end
    
    opts.on("-h", "--help", "Display this screen") do
        puts opts
        exit
    end
 
    opts.parse(ARGV)    
    
    if ($options[:releaseconfig].nil? && $options[:debugconfig].nil?)
        puts "Red pill or blue, your choice. Try -d or -r.ac"
        puts opts
        exit
    end
    
    if ($options[:releaseconfig] && $options[:debugconfig])
        puts "Make up your mind: release OR debug, not both"
        puts opts
        exit
    end
 
end
 
 
def do_the_work
    if $options[:releaseconfig]
        $config= "release"         
    end
 
    if $options[:debugconfig]
        $config = "debug"         
    end
    
        
    $sln = Dir.glob("*.sln")
    if ($sln.length > 1)
        puts "More than one .sln file here. Can't figure it out, bailing."
        exit
    end
    
    $command = "msbuild " + $sln[0] + " /p:configuration=" + $config 
    
    if $options[:clean]
        $command += " /t:clean"
    end
    
    system($command)
    
end
process_command_args()
do_the_work()

No comments:

Subscribe (RSS)

The Leadership Journey