Creating ruby script exe

Hi All,

I have been using the gem ‘ocra’ to create .exe files for my ruby
scripts. I run the following command:

ocra --console foo.rb

This creates the .exe but also runs the script once to create the .exe.
I am facing an issue where i have a script which uses ‘file-tail’ to
tail a log file. In this case when i run the above ‘ocra’ command, it
does not create the .exe as the script runs and starts to watch the log
file as it would normally do and hence ocra .exe creation does not
complete.

Any thoughts how to avoid the above?
Thanks

Looks like there’s an example in the docs:

You can detect whether OCRA is currently building your script by looking
for the Ocra constant. If it is defined, OCRA is currenly building the
executable from your script. For example, you can use this to avoid
opening
a GUI window when compiling executables:

app = MyApp.new
if not defined?(Ocra)
app.main_loop
end

Devin Christensen wrote in post #1112819:

Looks like there’s an example in the docs:

You can detect whether OCRA is currently building your script by looking
for the Ocra constant. If it is defined, OCRA is currenly building the
executable from your script. For example, you can use this to avoid
opening
a GUI window when compiling executables:

app = MyApp.new
if not defined?(Ocra)
app.main_loop
end

Hi David,
I will write the above at the beginning of my script? after the require
statements? or at the end?

Thanks

Ocra scans your script before it executes it anyway, but you might find
you’ll need to actively use some gems for them to picked up properly.

Here’s an excerpt from a class I use with Ocra builds sometimes:


def initialize

Allow EXE build without running.

if defined?(Ocra)
# Let OCRA pick up the cookies from Mechanize.
Mechanize.new.cookies
# Quit so Ocra can do its thing.
exit
end

Normal stuff goes here…

@agent = Mechanize.new

end

That’s probably because you’re using an old version of Ruby: version
1.9.2.

I noticed this message on a related error when googling the problem:
“NOTE: Gem::Specification#installation_path is deprecated, use base_dir.
It will be removed on or after 2011-10-01.”

You should probably install Ruby 1.9.3 or 2.0 and make sure you’re using
the latest version of Ocra.

If you can’t upgrade your Ruby version for some reason, you’ll probably
have to downgrade Ocra until you find a compatible version. Although
that’ll undo all the bugfixes since that version was released…

FYI, --console is the default when building an “.rb” file. You don’t
need to specify that.

Joel P. wrote in post #1113153:

That’s probably because you’re using an old version of Ruby: version
1.9.2.

I noticed this message on a related error when googling the problem:
“NOTE: Gem::Specification#installation_path is deprecated, use base_dir.
It will be removed on or after 2011-10-01.”

You should probably install Ruby 1.9.3 or 2.0 and make sure you’re using
the latest version of Ocra.

If you can’t upgrade your Ruby version for some reason, you’ll probably
have to downgrade Ocra until you find a compatible version. Although
that’ll undo all the bugfixes since that version was released…

FYI, --console is the default when building an “.rb” file. You don’t
need to specify that.

Thanks for the info Joel.
I upgraded ruby to 1.9.3p429. But now my gems dont work as they are in
the /lib folder of old ruby installation. Could I import those with a
command or i would need to re-install all of those gems again?

Joel P. wrote in post #1112855:

Ocra scans your script before it executes it anyway, but you might find
you’ll need to actively use some gems for them to picked up properly.

Here’s an excerpt from a class I use with Ocra builds sometimes:


def initialize

Allow EXE build without running.

if defined?(Ocra)
# Let OCRA pick up the cookies from Mechanize.
Mechanize.new.cookies
# Quit so Ocra can do its thing.
exit
end

Normal stuff goes here…

@agent = Mechanize.new

end

Hi Joel,All

I found a option for ocra:
–no-dep-run using this ocra does not run my script and created .exe.
This part is resolved.
Now my script has some require statements as well for gems. So i created
a bundler file using:
bundle init
Then added the following in my Gemfile:
source “https://rubygems.org
gem “file-tail”
gem “pony”

I then ran the: bundle install which created a Gemfile.lock

Then finally i ran the following ocra command:
C:code>ocra --console --gemfile Gemfile --no-dep-run tailtest.rb

Upon running it scans the Gemfile and then gives me error as below:

=== Scanning Gemfile
C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/specification.rb:1879:in
method_mi ssing': undefined method installation_path’ for
#<Gem::Specification:0x97b0e4 t
ins-0.8.2> (NoMethodError)
from
C:/Ruby192/lib/ruby/gems/1.9.1/gems/ocra-1.3.0/bin/ocra:557:in blo ck in find_gem_files' from C:/Ruby192/lib/ruby/gems/1.9.1/gems/ocra-1.3.0/bin/ocra:556:in fin
d_gem_files’
from
C:/Ruby192/lib/ruby/gems/1.9.1/gems/ocra-1.3.0/bin/ocra:701:in bui ld_exe' from C:/Ruby192/lib/ruby/gems/1.9.1/gems/ocra-1.3.0/bin/ocra:1138:in bl
ock in <top (required)>’

My script has the following statements in the beginning:

require ‘rubygems’
require ‘bundler/setup’
Bundler.require

Thanks in advance!

Joel P. wrote in post #1113211:

You could install them from the old gems (I think they’re in the cache
folder near the extracted gem folders), but it would be best to just
install the ones you need, thereby ensuring that you have the latest
versions.

Anyway, it’s easy to install them together:
gem install this_gem this_gem_too and_this_gem

Hi Joel.
Thanks for your help. Issue resolved.

Cheers

You could install them from the old gems (I think they’re in the cache
folder near the extracted gem folders), but it would be best to just
install the ones you need, thereby ensuring that you have the latest
versions.

Anyway, it’s easy to install them together:
gem install this_gem this_gem_too and_this_gem

That’s a side-effect of using the command --no-dep-run

Here’s the code sample I suggested before, with an extra note:

def initialize

Allow EXE build without running.

if defined?(Ocra)
# Let OCRA pick up the cookies from Mechanize.
Mechanize.new.cookies
# Let OCRA pick up file-tail
… something that uses file-tail here…
# Quit so Ocra can do its thing.
exit
end

Normal stuff goes here…

@agent = Mechanize.new

end

Joel P. wrote in post #1113564:

That’s a side-effect of using the command --no-dep-run

Here’s the code sample I suggested before, with an extra note:

def initialize

Allow EXE build without running.

if defined?(Ocra)
# Let OCRA pick up the cookies from Mechanize.
Mechanize.new.cookies
# Let OCRA pick up file-tail
… something that uses file-tail here…
# Quit so Ocra can do its thing.
exit
end

Normal stuff goes here…

@agent = Mechanize.new

end

Hi Joel,
Thanks. your suggestion worked. But when i run the exe i am getting
error:
“uninitialized constant yaml” and the exe exits.
I am using the YAML.load_file method in my script to read from a yaml
file

Any thoughts?

Rochit S. wrote in post #1113269:

Joel P. wrote in post #1113211:

You could install them from the old gems (I think they’re in the cache
folder near the extracted gem folders), but it would be best to just
install the ones you need, thereby ensuring that you have the latest
versions.

Anyway, it’s easy to install them together:
gem install this_gem this_gem_too and_this_gem

Hi Joel.
Thanks for your help. Issue resolved.

Cheers

Hi Joel,

Its seems there is till some issue. I had created an exe using the
following command:

ocra --gemfile Gemfile --no-dep-run script.rb

This creates a script.exe file, also while creating script i successful
messaging that the gems are being read from the Gemfile. When in run the
exe, i get a error in loading one of the gems. In this case its
file-tail gem. I have attached a screenshot of the error.
What could be the issue?

Am 26.06.2013 19:04, schrieb Rochit S.:

 Mechanize.new.cookies

Hi Joel,
Thanks. your suggestion worked. But when i run the exe i am getting
error:
“uninitialized constant yaml” and the exe exits.
I am using the YAML.load_file method in my script to read from a yaml
file

That would rather be

NameError: uninitialized constant YAML

You probably forgot to require ‘yaml’.

Regards,
Marcus

Am 26.06.2013 19:35, schrieb Rochit S.:

Please ignore my previous email. Issue is resolved :slight_smile:

Thanks for your help.

For future posts: good netiquette would be to also post
the solution in your “never mind, I figured it out” email.

Regards,
Marcus

Rochit S. wrote in post #1113647:

Joel P. wrote in post #1113564:

That’s a side-effect of using the command --no-dep-run

Here’s the code sample I suggested before, with an extra note:

def initialize

Allow EXE build without running.

if defined?(Ocra)
# Let OCRA pick up the cookies from Mechanize.
Mechanize.new.cookies
# Let OCRA pick up file-tail
… something that uses file-tail here…
# Quit so Ocra can do its thing.
exit
end

Normal stuff goes here…

@agent = Mechanize.new

end

Hi Joel,
Thanks. your suggestion worked. But when i run the exe i am getting
error:
“uninitialized constant yaml” and the exe exits.
I am using the YAML.load_file method in my script to read from a yaml
file

Any thoughts?

Hi Joel,
Please ignore my previous email. Issue is resolved :slight_smile:

Thanks for your help.

unknown wrote in post #1113652:

Am 26.06.2013 19:35, schrieb Rochit S.:

Please ignore my previous email. Issue is resolved :slight_smile:

Thanks for your help.

For future posts: good netiquette would be to also post
the solution in your “never mind, I figured it out” email.

Regards,
Marcus

Agreed… just being lazy @ midnight