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.):
I'm using Ruby 1.8.7 on Windows 7 Ultimate (64bit.) from a clean system I do this:
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)
ocra. OCRA (One-Click Ruby Application) builds Windows executables from Ruby source code. The executable is a self-extracting, self-running executable that contains the Ruby interpreter, your source code and any additionally needed ruby libraries or DLL.
You can make the script executable with the following command: chmod +x hello. rb . chmod is a shell command that allows us to change the permissions for a file. The +x specifies that the script should be executable.
https://github.com/rdp/ruby_tutorials_core/wiki/ruby-talk-faq#wiki-ruby_to_exe was a list of them at one time.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With