ruby 将ruby脚本编译成exe?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9998819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:57:33  来源:igfitidea点击:

Compile ruby script into exe?

rubycompilationexewatir

提问by Jonathan Jeffus

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.):

是否可以在 Windows 上将 ruby​​ 脚本编译为 .exe?我到处搜索,并尝试了以下方法(看起来 RubyScript2EXE、Shoes 和 Crate 似乎都死了或被遗弃了。):

  1. http://ocra.rubyforge.org/
  2. http://exerb.sourceforge.jp/index.en.html
  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:

我在 Windows 7 Ultimate(64 位)上使用 Ruby 1.8.7 从一个干净的系统我这样做:

  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
  1. 从 ruby​​installer.org 安装 RubyInstaller 1.8.7-p358
  2. 宝石安装水
  3. 宝石安装 ocra
  4. git 克隆 git://github.com/snaury/exerb-mingw.git
  5. cd exerb-mingw
  6. 红宝石设置.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 时,我没有收到任何错误消息,也没有任何反应:

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:

Exerb 似乎是一个更好的解决方案,因为它编译为 rbc,而且它确实做了一些事情:

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:

所以它找不到win32/api.so。当我查看 C:/Ruby187/lib/ruby/site_ruby/1.8/exerb/recipe.rb 时,看起来 gems 不在搜索路径中:

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:

所以在挖掘之后我发现你可以在 exy 文件中添加搜索路径,如下所示:

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?

在此之后,它确实在 search_path 中显示了这些路径,但它仍然给我同样的错误。显然必须有某种方法可以让 ruby​​gems 路径包含在 exy 中?

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

那么,如今有人如何在 Windows 上构建 .exe 呢?

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:

我在 Exerb 上取得了一些进展,我发现您可以使用 -rrubygems 选项运行 mkexy 以将其引入。这对大多数 ruby​​ 项目来说都非常有效。我已经尝试了一些 ruby​​ 脚本,这些脚本使用了许多不同的库而没有问题。例如:

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:

不幸的是,它不适用于 watir。当我运行使用 watir 构建的 .exe 时,我得到以下信息:

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)

所有者的值必须是 'safari'、'firefox' 或 'ie' 之一,并且 '' 看起来不正确。(标准错误)

回答by Jonathan Jeffus

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 的错误消息是因为 mkexy 正在创建无效的 exy 文件。检查第 303 行,它的格式似乎不正确。我更正为:

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.

到脚本。接下来还有一些缺少的库,exerb 没有添加。我还没有想出如何让它们出现,但你可以将它们复制到 exe 的路径中,它就可以工作了。

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 对其进行打包。

upx --best test.exe

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.

它变成了一个 500kb 的文件!有了 UPX + 这个额外的东西,整个应用程序将只有 650kb 左右。我用 ocra 制作的任何脚本都不小于 1Mb。更不用说它真的有问题,如果它崩溃,会在随机位置留下 .rb 文件。

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.

我认为通过对 .exy 文件进行一些调整,我可以使其包含所有文件。如果我找到答案,我会用答案更新这个问题。

回答by Mark Thomas

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

根据 Ocra 文档,您可以通过检查Ocra常量来阻止您的应用程序在 ocra 编译期间运行:

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.

也许这将有助于通过编译。

回答by Dave McNulla

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.

Ruby 非常轻量级,我从不介意将它安装在另一个系统上。我可以看到想要避免处理所有的 devkits 和诸如此类的东西,但是您可以创建一个映像并复制它,并对系统环境进行一些小的更改。

回答by unom

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

我一直在寻找同样的东西,作为 2014 年 8 月,这是我能找到的最好的:

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

此链接对做过/做过或以其他方式贡献的主要项目有很好的历史记录: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)

据我所知,有两个主要的,OCRA(仅适用于 Windows)和 Releasy(基于 OCRA,但据我所知适用于 macs 和 linux)

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

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

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

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