打包脚本时Ocra会抛出错误

时间:2014-12-27 11:45:32

标签: ruby scripting compiler-errors packaging ocra

  

SCENARIO

我已经安装了Ruby 2.1.5 (x64)包,然后我成功安装了Ocra gem:

gem install ocra
Fetching: ocra-1.3.3.gem (100%)
Successfully installed ocra-1.3.3
Parsing documentation for ocra-1.3.3
Installing ri documentation for ocra-1.3.3
Done installing documentation for ocra after 0 seconds
1 gem installed
  

问题

我试图打包测试脚本:

# -*- coding: UTF-8 -*-

# require ''
exit if Object.const_defined?(:Ocra)

print "Hello World!"
sleep 3

__END__

但是当我尝试使用 Ocra 时,它会抛出错误:

ocra "TestScript.rb"

=== Loading script to check dependencies
=== Detected gem ocra-1.3.3 (loaded, files)
===     6 files, 190931 bytes
=== Detected gem io-console-0.4.2 (loaded, files)
C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:86:in `open':
 No such file or directory @ dir_initialize - C:/Program Files/Ruby/lib/ruby/gem
s/2.1.0/gems/io-console-0.4.2 (Errno::ENOENT)
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
86:in `entries'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
86:in `entries'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
92:in `find_all_files'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
665:in `block (2 levels) in find_gem_files'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
658:in `each'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
658:in `block in find_gem_files'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
611:in `each'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
611:in `find_gem_files'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
728:in `build_exe'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
1165:in `block in <top (required)>'
  

问题

为什么会这样?如何解决?

2 个答案:

答案 0 :(得分:2)

问题源于安装irb1.8 而不是irb依赖包。如果您已经安装了irbl.8,只需执行“apt-get install irb”即可解决问题。

答案 1 :(得分:1)

tl; dr:我能够使用Ocra与Ruby版本2.0.0p481和rubygems 2.0.14从您的脚本成功构建可执行文件。如果你可以切换Ruby版本,你应该能够得到相同的结果。 (Here's a write-up I did on using multiple Ruby versions on Windows with the awesome uru。)

深潜:

这个io-console问题困扰了许多Ocra gem的用户,目前在github上有一个未解决的问题:

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

Ocra在脚本中所有加载的gem上调用Gem::Specification#gem_dir,其中包含io-console。该操作返回不存在的路径,因此返回错误。

(以下是来自rubygems的gem_dir方法的链接,供您参考。)

你可以自己尝试一下:

irb(main):001:0>require "io/console"
=> true
irb(main):002:0>io_console_spec = Gem.loaded_specs["io-console"]
=> #<Gem::Specification:0xblahblah io-console-0.4.2>
irb(main):003:0>proposed_path = io_console_spec.gem_dir
=> "$YOUR_RUBY_DIRECTORY/lib/ruby/gems/2.1.0/gems/io-console-0.4.2"
irb(main):004:0>File.directory? proposed_path
=> false

使用Ruby 2.0.0p481时,Ocra成功地从此脚本创建了一个可执行文件:

ocra-success

这让我相信Gem.loaded_specs的结果在rubygems 2.0.14(运行在我的2.0.0p481版本上的版本)和rubygems 2.2.2(运行的版本)之间发生了变化。我的2.1.5版本)。深入了解Gem::Specification#gem_dir io-console {{1}}为什么我会更多地工作(我对Ocra和Rubygems的内部工作很新),但希望这很有帮助。< / p>