omarsafwany
omarsafwany

Reputation: 3811

Make ruby gem executable

I have built a ruby gem but I need to make it executable so it could be run from the terminal. I have googled and found that I need to make a file inside the bin/ directory, require my library and then call whatever I need from it. Also I tried chmod +x bin/myfile to make it executable but nothing worked.Basically, I have followed this link but nothing worked.

In the bin directory, I have already a console and setup along with my newly created file which is as follows:

#!/usr/bin/env ruby

require "sys_libs"
s = SysLibs

Here's the .gemspec file:

spec.files         = `git ls-files -z`.split("\x0").reject do |f|
    f.match(%r{^(test|spec|features)/})
  end
spec.bindir        = "exe"
spec.executables   = spec.files.grep(%r{^bin/}) { |f| f[3..-1] }
spec.require_paths = ["lib"]

Anytime I try to change the executable, I get stuck with failed build. Any idea what else am I missing so far?

Upvotes: 1

Views: 984

Answers (2)

Christopher Oezbek
Christopher Oezbek

Reputation: 26343

I just stumbled on the same issue and for me the culprit was how files for inclusion into the gem were defined (which is the standard boilerplate generated by bundler):

spec.files         = `git ls-files -z`.split("\x0").reject do |f|
  f.match(%r{^(test|spec|features)/})
end

What this does: Only files are included which have been added to git (retrieved in the script by git ls-files) are used for inclusion into the gem.

So the following then will just return an empty array:

spec.executables   = spec.files.grep(%r{^bin/}) { |f| f[3..-1] }

Once, I added the executable script to git, everything worked.

Upvotes: 0

the_storyteller
the_storyteller

Reputation: 2477

The spec.bindir = "exe" attribute should be spec.bindir = 'bin'.

spec.bindir        = 'bin'
spec.executables   = ['myfile']

bindir is optional if you place your executables in a bin subdirectory, because it is set by default in rubygems/specifications.rb and is prepended to each executable in the list of executables.

Upvotes: 2

Related Questions