Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make 'bundle init' include gems by default?

When I run bundle init to start a new project I get a standard Gemfile:

# A sample Gemfile
source "https://rubygems.org"

# gem "rails"

How can I customize this?

My goal is to have a few gems that I use with almost every project included by default.

I see on the bundle init documentation that it can be used with a --gemspec=FILE option, but is there a way to customize the default version that appears when just using bundle init ?

like image 592
cwd Avatar asked Sep 08 '15 20:09

cwd


2 Answers

You've got the right idea. I've got a template I like to use too, it looks like this:

~/.gemspec_template

Gem::Specification.new do |spec|
  spec.add_development_dependency "bundler", "~> 1.7"
  spec.add_development_dependency "rake", "~> 10.0"
  spec.add_development_dependency "pry"
end

From there, I just run:

bundle init --gemspec=~/.gemspec_template 

I get a Gemfile that looks like:

# Generated from /Users/anthonyross/.gemspec_template
source 'https://rubygems.org'

group :development do
  gem "bundler", "~> 1.7"
  gem "rake", "~> 10.0"
  gem "pry", ">= 0"
end
like image 89
Anthony Avatar answered Nov 15 '22 10:11

Anthony


bundle init

Generates a Gemfile into the current working directory

$ bundle init [--gemspec=FILE]

Options:

--gemspec: Use the specified .gemspec to create the Gemfile Init generates a default Gemfile in the current working directory. When adding a Gemfile to a gem with a gemspec, the --gemspec option will automatically add each dependency listed in the gemspec file to the newly created Gemfile.

All that bundle init does is to generate a Gemfile from a template, a gemspec.

If you want to have a default gem list, just define a gemspec template as use it as your 'default'.

And then just use it like

$ bundle init --gemspec=~/.default

You can even define an alias for it

#note the lack of a space in the alias name
$ alias bundleinit='bundle init --gemspec=~/.default' 

And then use it like

$ bundleinit
like image 26
Mihai Dinculescu Avatar answered Nov 15 '22 08:11

Mihai Dinculescu