Are you one of those developers that create scripts to make their own life easier? coincidentally, do you like Ruby? and you have Windows?. Instead of work repetitively with the console executing your scripts manually in the console, you need to know that there's an easy way to execute them and even create little console applications with them in Windows. We are talking about creating .exe
(yeah, application files) files with ruby scripts, thanks to One-Click Ruby Application Builder. OCRA, builds Windows executables from Ruby source code ! these executables are a self-extracting, self-running executables that contains the Ruby interpreter, your source code and any additionally needed ruby libraries or DLL.
In this article, you'll learn how to create an executable from a ruby script easily using OCRA in windows.
Requirements
To create our executable, we are going to use the OCRA gem. To download OCRA, execute the following command in your command prompt (cmd.exe):
gem install ocra
The gem manager will start the download of ocra and the gem (package), at the end of the installation, should be able as an environment variable in the command prompt.
You can read more about this package in the official repository or in the documentation here. To check if OCRA was correctly installed, you can check if it's available in the console as an environment variable executing ocra --help
.
Alternatively you can download the gem at either rubygems.org/gems/ocra or github.com/larsch/ocra/releases/. Stand-alone Version: Get ocrasa.rb
from github.com/larsch/ocra/releases/. Requires nothing but a working Ruby installation on Windows.
Implementation
The creation of an executable using OCRA is very straightforward, customizable and very easy to do. All that you need to have is ocra as an environment variable (which should automatically happen with the installation of the gem) and know which file you want to pack.
In our example, we are going to create an executable of the following script. The filename of the script is file-creator.rb
and contains the following code (it just prompt the user for the name of a new file and the content):
# Declare start wizard method
def start_wizard
prompt = "> "
puts "Hi welcome to Text File Creator 2000"
puts prompt + "To get started, give a name to your file (without extension)"
filename = $stdin.gets.chomp
puts prompt + "Awesome, now provide the content of the file !"
content = $stdin.gets.chomp
puts """
Alright, so your filename will be #{filename}.
And the content will be #{content}. Do you want to continue (Yes[y]/No[n])?
"""
# Confirm the creation of the file
response = $stdin.gets.chomp
case response
# If confirm, create the file
when 'Y', 'y', 'j', 'J', 'yes', 'Yes'
out_file = File.new("#{filename}.txt", "w")
out_file.puts(content)
out_file.close
puts "Your file has been succesfully created, come back soon !"
# If not, ask if he wants to continue or just exit
when 'No','N','no'
puts prompt + "Oh :(, do you want to start again (Yes[y]/No[n])?"
response = $stdin.gets.chomp
case response
when 'Y', 'y', 'j', 'J', 'yes', 'Yes'
start_wizard()
when 'No','N','no'
exit
end
end
end
# Start execution
begin
start_wizard()
# Write a log in case of error
rescue Exception => e
File.open("except.log") do |f|
f.puts e.inspect
f.puts e.backtrace
end
end
As you can see, it is a simple console Ruby application. Now to create the executable, navigate with the console (cmd.exe) to the folder where the script of ruby is located (in this case Desktop\Ruby):
cd C:\Users\sdkca\Desktop\ruby
Now, proceed with the process using the following command:
ocra file-creator.rb
That's the most simple command to create an executable of your script, so that should be enough to create the executable in the folder where the script is located.
However, there's an interesting behaviour that probably you may face while you wrap your script. OCRA executes the script immediately in order to check for dependencies (.DLL and other files) to make your script as independent as possible and obviously, if your script requires interaction with the user (a prompt or a required value input) you will not want that OCRA executes your script for different reasons. To prevent this behaviour, you need to provide a parameter to make that OCRA ignores the execution of the script:
ocra text-creator.rb --no-dep-run
Now execute the command and build your first executable:
Finally, you can test the created executable in the same folder where the script is located:
Tips and suggestions
-
DLLs are detected automatically but only those located in your Ruby installation are included.
-
.rb
files will become console applications..rbw
files will become windowed application (without a console window popping up). Alternatively, use the “--console
” or “--windows
” options. - You can change the icon of your executable adding the icon parameter (with the path of the file as value) to the command (
ocra script.rb --icon=c:\path-to\icon.ico
).
Have fun !