I am not clear on what certain specifications in the .gemspec file are doing. Specifically,
spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
Can someone explain how these relate to the functionality of a Ruby Gem and why they are necessary?
executables:
Executables included in the gem. For example, the rake gem has rake as an executable. These files must be executable Ruby files.
files:
Files included in the gem. These are the files that will be included in your gem when it is built.
require_paths:
Contains an Array of directories and files which should be added to the $LOAD_PATH on gem activation. By default it is ["lib"].
test_files
Test files included in the gem.
Related
I have a gem where I need to incude some assets that are just static files, never run. I have placed them in mygem/assests. The only issue is that they are not installed with the gem. I have tried the gemspec option require_paths but that hasn't help. How can I include the directory mygem/assets in any installation of my gem?
As by Rubygems documentation, all files to be included in gem distribution must be listed in spec.files array object:
Example stolen from specification reference:
spec.files = Dir['lib/ *.rb'] + Dir['bin/*']
spec.files += Dir['[A-Z]*'] + Dir['test/**/*']
spec.files.reject! { |fn| fn.include? "CVS" }
I created a gem and installed it on my system. The gem has 2 rb files, Myfile.rb and file_two.rb, one requires another, both are in lib folder. When I'm trying to load it in irb like require 'Myfile', I get an error
- LoadError: cannot load such file -- /usr/local/lib/ruby/gems/2.0.0/gems/Myfile-0.1.0/lib/file_two
In gem folder I see only 1 file. I believe the problem is in the following snippet of gemspec:
spec.files = Dir['Rakefile', '{bin,lib}/**/*',
'README*', 'LICENSE*'] & `git ls-files -z`.split("\0")
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
Please, advice how to rewrite it correctly, so both my files get into gem package.
Thanks in advance!
There's only one error I get when installing my custom gem: File not found: lib ERROR: While generating documentation
The install continues and says gem installed successfully. However, when I require 'boxes' I get LoadError: no such file to load -- boxes and when I look into the gem directory I notice that there is no lib/ folder in the installed gem. I'm not sure why this is or how to debug it. Any pointers? Below is my gemspec file.
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'boxes/version'
Gem::Specification.new do |gem|
gem.authors = ["Name"]
gem.email = ["http://somedomain.com"]
gem.description = %q{Boxes, a Sass framework}
gem.summary = %q{Boxes is a modular Sass-based framework that is heavily configurable, extremely modular and works as a Sass and a CSS Framework}
gem.homepage = "http://somedomain.com"
gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "boxes"
gem.require_paths = ["lib"]
gem.version = Boxes::VERSION
gem.license = "MIT"
gem.add_dependency "sass", [">= 3.2.0"]
gem.add_development_dependency "rake"
end
I'm new at ruby, and I'm just creating my first gem here, so please bear with me.
One thing I noticed is that on line 1, you're defining "lib" as "../lib", though in gem.require_paths "lib" is in the current directory. Is that true?
Another thing to look for is that lib is not in .gitignore, as you're using git ls-files for defining gem.files
edit
Noticed in your gemfile that you're using ruby 1.8.7, make sure you require 'rubygems' before you require your gem
I've created a program that I want to bundle into a gem that will add the executable located in the bin directory to the path. I'm wondering if there is something inside the gemspec that I have to set to make this happen, or if it's something else. I want to be able to use gem install gem_name and it will add the executable to the path. How do I do this?
Declare the executables in the gemspec file:
Gem::Specification.new do |s|
s.name = 'hola'
s.version = '0.0.1'
s.executables << 'hola'
end
http://guides.rubygems.org/make-your-own-gem/#adding-an-executable
I can use gems like RSpec or Rails or Pry by calling their respective gem names, e.g. rspec, rails, pry on the commandline. How can I achieve this with gems I create? I'm using bundler for the basic gem creation.
I actually had my executable in the /bin folder.
Turns out my issue was that bundler's gem template is too smart for it's own good, and only includes files that have been committed to git. I hadn't actually committed the executable yet, so it wasn't picking it up:
# gemname.gemspec
gem.files = `git ls-files`.split($\)
According to documentation of Gemspec file you must put your executable in bin/ folder.
To make your gem executable in CLI, you should set the followings up.
Place your executable file the bin folder, like bin/hello
Make that executable by set permissions (chmod u+x bin/hello)
Set up gemspec configuration accordingly (hello.gemspec)
spec.files = `git ls-files -Z`.split("\x0")
spec.bindir = 'bin'
spec.executables << 'hello'
spec.executables considers bin as default folder for binaries and executables, though you can change it.
You can find documentation about this here: Gemspec#executables.