This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
Original Post: Churning Ruby into EXE
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
I’m greatly impressed with RubyScript2Exe by Erik Veenstra, which wraps your Ruby script and all its dependencies (including Ruby) up in a nice executable for Windows users. I’m using it on MouseHole and I can count on one hand how long it took to get it working. Easier than riding a bike, more like learning to use a coaster.
The only caveat is getting used to the idea that when the user clicks on the executable, your application is going to get unpacked in a temporary directory elsewhere. So if you have images or config files you want to store alongside the .exe, you’ll need to use some of RubyScript2EXE’s constants.
APPLICATION_DIR = File.dirname(__FILE__)
if defined? RUBYSCRIPT2EXE_APPEXE
APPLICATION_DIR = File.dirname( RUBYSCRIPT2EXE_APPEXE )
end
Under RubyScript2EXE, the __FILE__ constant won’t work like you think it will. It’ll give you that same obscure temp directory. But you can still sniff out the application’s directory by checking for the RUBYSCRIPT2EXE_APPEXE constant.
There aren’t a whole lot of Rake+RubyScript2EXE examples around, but if you’re using the traditional bin/, lib/, tests/ structure found in most RubyGems, the Rake recipe would look like:
RUBYSCRIPT2EXE = ENV['RUBYSCRIPT2EXE'] ||
File.join(Config::CONFIG['bindir'],'rubyscript2exe.rb')
RUBY_MAIN_SCRIPT = ENV['RUBYMAINSCRIPT'] || 'bin/mouseHole.rb'
EXEC_TARGET = File.join(RUBY_MAIN_SCRIPT.sub(/rb$/, 'exe') )
file :executable => [ RUBY_MAIN_SCRIPT ] do | t |
unless File.exist?(RUBYSCRIPT2EXE)
raise RuntimeError.new("rubyscript2exe.rb not found " +
"pass with RUBYSCRIPT2EXE=/path/to/rubyscript2.rb")
end
sh %{ruby "#{RUBYSCRIPT2EXE}" #{RUBY_MAIN_SCRIPT}}
File.move(EXEC_TARGET, 'build')
puts "Created executable file build/#{EXEC_TARGET}.exe"
end