gem install bundlerOnce you've installed Bundler, you should change directories to the location that you want to create your gem folder (for me, that location is
~/code/gems
).
Once there you will create the gem like so:
bundle gem my_gemWhere
my_gem
is your gem's name.
Note that if you are building a command line tool you should add the -b
flag to this command
(bundle gem my_gem -b
).
You should see output that looks something like this:
create my_gem/Gemfile create my_gem/Rakefile create my_gem/.gitignore create my_gem/my_gem.gemspec create my_gem/lib/my_gem.rb create my_gem/lib/my_gem/version.rb Initializating git repo in /Users/mbleigh/code/gems/my_gemBundler has just generated a skeleton for your gem. You should recognize many of the files of the general structure of a gem.
Some of the nice things that Bundler has done for us include:
.gitignore
: There are certain files that you will not want to have to your git repository. Bundler automatically excludes the most common of these.
version.rb
: Bundler creates
my_gem/lib/my_gem/version.rb
automatically.
This file is used as a single place to track the current version of your library.
By default, Bundler sets the version to 0.0.1.
Rakefile
: The Rakefile created by Bundler is already set up with
Bundler's gem helpers.
These helpers allow you to release a new version of your gem (including creating a release tag in git and pushing the compiled gem to RubyGems) with a single command.
$ rake -T rake build # Build my_gem-0.0.1.gem into the pkg directory rake install # Build and install my_gem-0.0.1.gem into system gems rake release # Create tag v0.0.1 and build and push my_gem-0.0.1.gem to Rubygems rake spec # Run RSpec code examplesEstos son los contenidos del
Rakefile
:
$ cat Rakefile $:.unshift File.dirname(__FILE__) + 'lib' require "bundler/gem_tasks" require 'rspec/core/rake_task' RSpec::Core::RakeTask.new task :default => :spec
Gemfile
este es un ejemplo - ya modificado - del Gemfile
generado:
[~/srcLPPruby/building_gems/bleigh_tutorial_to_building_gems/my_gem(master)]$ cat Gemfile source 'https://rubygems.org' # Specify your gem's dependencies in my_gem.gemspec gemspec #gem 'guard' #gem 'guard-rspec' #gem 'guard-bundler' #gem 'rb-fsevent', '~> 0.9.1'
gemspec
$ cat -n my_gem.gemspec 1 # -*- encoding: utf-8 -*- 2 lib = File.expand_path('../lib', __FILE__) 3 $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 require 'my_gem/version' 5 6 Gem::Specification.new do |gem| 7 gem.name = "my_gem" 8 gem.version = MyGem::VERSION 9 gem.authors = ["John Smith"] 10 gem.email = ["some.name@pochomail.com"] 11 gem.description = %q{TODO: Write a gem description} 12 gem.summary = %q{TODO: Write a gem summary} 13 gem.homepage = "" 14 15 gem.files = `git ls-files`.split($/) 16 gem.executables = gem.files.grep(%r{^bin/} ).map{ |f| File.basename(f) } 17 gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 18 gem.require_paths = ["lib"] 19 20 gem.add_dependency 'sinatra' 21 gem.add_development_dependency 'rspec', '~>2.11' 22 end
version.rb
$ cat -n lib/my_gem/version.rb 1 module MyGem 2 VERSION = "0.0.1" 3 end
Casiano Rodriguez León 2015-01-07