简体   繁体   中英

Compile ruby script into exe?

Is it possible to compile a ruby script into an .exe on Windows? I have searched everywhere and I've tried the following (It looked like RubyScript2EXE, Shoes and Crate all seemed dead or abandoned.):

  1. http://ocra.rubyforge.org/
  2. http://exerb.sourceforge.jp/index.en.html

I'm using Ruby 1.8.7 on Windows 7 Ultimate (64bit.) from a clean system I do this:

  1. Install RubyInstaller 1.8.7-p358 from rubyinstaller.org
  2. gem install watir
  3. gem install ocra
  4. git clone git://github.com/snaury/exerb-mingw.git
  5. cd exerb-mingw
  6. ruby setup.rb

I have a fairly simple script that does this:

require 'rubygems'
require 'watir'
browser = Watir::Browser.new
browser.goto 'http://slashdot.org'

When I run Ocra I don't get any error messages, and nothing happens:

ocra --output test.exe test.rb
=== Loading script to check dependencies

Exerb seems like a better solution since it compiles to rbc, and it does actually do something:

ruby -r exerb/mkexy test.rb
# Window pops up and after I close it it writes out test.exy
C:\Users\jonathan\dev\Citation>exerb test.exy
C:/Ruby187/lib/ruby/site_ruby/1.8/exerb/recipe.rb:146:in `add_file_entry': test.
exy: no such file -- C:/Ruby187/lib/ruby/gems/1.8/gems/win32-api-1.4.8-x86-mingw32/lib/win32/ruby18/win32/api.so (RuntimeError)
    from C:/Ruby187/lib/ruby/site_ruby/1.8/exerb/recipe.rb:86:in `create_archive'
    from C:/Ruby187/lib/ruby/site_ruby/1.8/exerb/recipe.rb:85:in `each'
    from C:/Ruby187/lib/ruby/site_ruby/1.8/exerb/recipe.rb:85:in `create_archive'
    from C:/Ruby187/bin/exerb.bat:67:in `main'
    from C:/Ruby187/bin/exerb.bat:196

So it can't find win32/api.so. When I look in C:/Ruby187/lib/ruby/site_ruby/1.8/exerb/recipe.rb it looks like gems are not in the search path:

DIR: C:/Users/jonathan/dev/Citation
DIR: C:/Ruby187/lib/ruby/site_ruby/1.8
DIR: C:/Ruby187/lib/ruby/site_ruby/1.8/i386-msvcrt
DIR: C:/Ruby187/lib/ruby/site_ruby
DIR: C:/Ruby187/lib/ruby/vendor_ruby/1.8
DIR: C:/Ruby187/lib/ruby/vendor_ruby/1.8/i386-msvcrt
DIR: C:/Ruby187/lib/ruby/vendor_ruby
DIR: C:/Ruby187/lib/ruby/1.8
DIR: C:/Ruby187/lib/ruby/1.8/i386-mingw32
DIR: .

So after digging around I found out you can add search paths in the exy file like so:

path:
  - C:/Ruby187/lib/ruby/gems/
  - C:/Ruby187/lib/ruby/gems/1.8
  - C:/Ruby187/lib/ruby/gems/1.8/gems

After this it does show these paths in search_path, but it still gives me the same error. Obviously there must be some way to get rubygems paths to be included in the exy?

So, how does someone build a .exe on Windows these days?

I made some progress with Exerb, I found out you can run mkexy with the -rrubygems option to pull this in. And this works GREAT for most ruby projects. I've tried it for a few ruby scripts that use a number of different libraries without problems. For example:

mkexy -rrubygems test.rb
exerb test.exy
test.exe === WORKS!

Unfortunately, it doesn't work for watir. When I run an .exe built with watir I get the following:

s4t-utils/claims.rb:24:in `user_is_bewildered': Error in the default values: :br

owser's value must be one of 'safari', 'firefox', or 'ie', and '' doesn't look r ight. (StandardError)

I think I found the answer! The error message about missing win32/api.so is because mkexy is creating an invalid exy file. Checking line 303 it looks like it formats it incorrectly. I corrected it to:

win32/api.so:
    file: C:/Ruby187/lib/ruby/gems/1.8/gems/win32-api-1.4.8-x86-mingw32/lib/win32/ruby18/win32/api.so
    type: extension-library

And now it works. When I run it I still get the message about the "user is bewildered". The solution to this is to add:

Watir::Browser.default = "ie"

to the script. Next there are still a few missing libraries that exerb is not adding. I haven't figured out yet how to make them appear but you can just copy them to the exe's path and it works.

cp -r C:/Ruby187/lib/ruby/gems/1.8/gems/win32-api-1.4.8-x86-mingw32/lib/win32/ .
cp -r C:/Ruby187/lib/ruby/gems/1.8/gems/watir-2.0.4/lib/watir .

Problem goes away and the script executes! Also, you can use UPX to pack it.

upx --best test.exe

It becomes a 500kb file! With UPX + this extra stuff the whole app would be a mere 650kb or so. No script I could make with ocra was less than 1Mb. Not to mention the fact that it's really buggy and leaves .rb files in random locations if it crashes.

I think with some tweaking of the .exy file I can make it include all the files. I'll update this question with the answer if I find it.

According to the Ocra documentation, you can prevent your app from running during ocra compilation by checking the Ocra constant:

require 'rubygems'
require 'watir'

if not defined?(Ocra)
  browser = Watir::Browser.new
  browser.goto 'http://slashdot.org'
end

Perhaps this will help get through the compile.

Ruby is so light-weight that I never minded installing it on another system. I can see wanting to avoid dealing with all the devkits and whatnot, but you can create an image and copy that around with a couple minor changes to the system environment.

I was looking for the same thing and as August 2014 this is the best I could find:

This link has a nice history on the main projects that did/do or otherwise contributed: https://www.ruby-toolbox.com/categories/packaging_to_executables

As far as I can tell there are two main ones, OCRA (works just on windows) and Releasy (based on OCRA but works on macs and linux as far as I can tell)

https://github.com/larsch/ocra/

https://github.com/Spooner/releasy/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM