Rayne
Rayne

Reputation: 32645

How do you generate executables within a gem with Rake?

I've been learning Ruby recently, and I've not gotten into the dirty recesses of learning Rake yet. I've been playing around with NetBeans, and I made a little Ruby project with a file that simply prints "Hello, World!". I was looking at the Rakefile that NetBeans generates, and I noticed that it had commented out the s.executables line, so I uncommented, and tried to build it. Of course it failed with:

Don't know how to build task 'bin/your_executable_here'

What I'm trying to do, is figure out how to make that work. I've googled around, and I can't find any information on how to correctly generate an executable. Here is the Rakefile generated by NetBeans:

require 'rubygems'
require 'rake'
require 'rake/clean'
require 'rake/gempackagetask'
require 'rake/rdoctask'
require 'rake/testtask'

spec = Gem::Specification.new do |s|
  s.name = 'Learning'
  s.version = '0.0.1'
  s.has_rdoc = true
  s.extra_rdoc_files = ['README', 'LICENSE']
  s.summary = 'Your summary here'
  s.description = s.summary
  s.author = ''
  s.email = ''
  s.executables = ['your_executable_here']
  s.files = %w(LICENSE README Rakefile) + Dir.glob("{bin,lib,spec}/**/*")
  s.require_path = "lib"
  s.bindir = "bin"
end

Rake::GemPackageTask.new(spec) do |p|
  p.gem_spec = spec
  p.need_tar = true
  p.need_zip = true
end

Rake::RDocTask.new do |rdoc|
  files =['README', 'LICENSE', 'lib/**/*.rb']
  rdoc.rdoc_files.add(files)
  rdoc.main = "README" # page to start on
  rdoc.title = "Learning Docs"
  rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
  rdoc.options 

I'm sorry if this is a stupid question, I honestly did try to find the information myself.

EDIT: I was unaware that there had to be an executable file by the same name as the default one you specify in ./bin in your project. I figured it all out.

Upvotes: 1

Views: 1837

Answers (2)

Julik
Julik

Reputation: 7856

The s.executables array must contain the names of the executables in the bin directory of your gem

 s.executables = %w( my_awesome_commandline_churner )

Upvotes: 1

John F. Miller
John F. Miller

Reputation: 27227

This code is used to make a gem file. Gems are ruby's package management devices. Some gems come with executable script files to be run from the command line. They are placed in the ./bin directory when the gem is built, and hen it is deployed they will be copied into the same folder as the ruby executable.

To make a file executable you will need to add a shabang (#!/user/local/bin/ruby) to the first line and change the file permission to allow execution.

Upvotes: 1

Related Questions