* Create your FREE website now *

Rake cscript task

Tags:  

I really hate to post this, I had a task to accomplish where there already existed a vbscript.  At first I tried to rewrite the vbscript into ruby.  But I ran into limitations with the Ruby .Net Bridge.  I also tried IronRuby but it is still alpha (as of July 1, 2008).  I could have done the task in C# but I didn't want to build a .Net object just to perform a simple task.  What I had working well is a vbscript file.  So my answer was to call cscript from rake.  While I'd rather avoid this, it is a simple solution that gets the job done.

So in my rakefile I can write

task :foobar do
  cscript "foobar.vbs"
end


Here is the source for the rake cscript task...


require 'win32/open3'

# runs a cscript script for example:
#
#  cscript "myscript.vbs arg1 arg2 /namedarg:value"
#
# if no block is given, it will output the stdout from the script and
# raise anything received on stderr.
#
# if a block is provided, the resulting output and error (nil if no error) is
# passed to the block.  
#
# cscript "myscript.vbs arg1 arg2 /namedarg:value" do |out, error|
#    puts "out = #{out.inspect}"
#    puts "error = #{error.inspect}"
#    if error
#      raise error
#    end
# end

def cscript( file_and_arguments )
  cmd = "cscript /Nologo #{file_and_arguments}"
  puts cmd
  Open3.popen3(cmd) do | io_in, io_out, io_err |
    result = io_out.readlines.join
    error = io_err.readlines
    if block_given?
      yield result, (error.size==0) ? nil : error.join
    else
      puts result
      raise error.join if error.size != 0
    end
  end
end






 RSS of this page

Written by:   Written by:   Version:   Last Edited By:   Modified