Using local gem with Bundler - bundler

I created an in-house gem to use with a Rails project that I want to include in my Gemfile.
The gem is hosted in a private repo on Github.com (so :git is not an option) so I am assuming the best direction to include the gem is :path, e.g.
gem 'mygem', :path => '/path/to/gem/dir'
I am curious though:
Where is the ideal place for the gem to be included? (vendor/ ?)
If vendor is the best place, then I don't need to add the gem to my project repository (since vendor/ is ignored by default)
I am using Capistrano to deploy my project; how should Capistrano be aware of the local gem so that it can deploy it as well?

You can go for the vendor location and have the authentication information for the private gem in the gemfile like this:
gem 'foo', :git => 'https://my_username:my_password#github.com/my_github_account/my_repo.git'
However, I am guessing that you will be uncomfortable exposing such sensitive information in the gemfile so you can use a git protocol based url in your gem file like this:
git://github.com/username/repo.git
git protocol based urls are read only so your private repo should be intact.
Also, I am sure you can write a capistrano task to deploy a private gem to vendor directory but that might involve putting in your github username/password in the capistrano task. Also, you can prompt for the username/password during deployment as opposed to putting it in the capistrano task.

Related

How to manage dependencies between my ruby projects?

This sounds like a basic question, but I can't find any answer to it on the Internet.
So I have a git ruby project database_models. It's a gem. I want 3 other project to use it. I've added a dependency on this project to those 3 projects like this:
gem "database_models", :git => "path", :branch => master
Now, I want a a develop branch of those 3 projects to use the develop branch of database_models, and I want a master branch of those 3 projects to use the master branch of database_models, so that my production environment is stable and independent of my development environment.
I can see 4 options of doing this, and I don't like any of them:
Deploy database_models to the server, and update those 3 projects to reference database_models using a path, instead of git
Git submodule
User different versions of database_models gem (1.1, 1.2, 1.3...). I would probably need my own gem server for that, right?
Write some code in a Gemfile that would choose the correct branch based on the environment where "bundle install" is run.
Usually you'd use Bundler with a local path. Your Gemfile points to the Git or Github repo using git or github:
gem 'spree', github: 'spree/spree', branch: 'master'
Note that includes the branch. You can make each of your projects use a different branch of your gem if you want to. You can make each of your projects use a different branch of your gem if you want to. You can also use groups to deploy different versions of your gem depending on the environment:
group :development, :test
gem 'spree', github: 'spree/spree', branch: 'bleedinedge'
end
group :staging, :production
gem 'spree', github: 'spree/spree', branch: 'master'
end
The above will work fine as long as you keep pushing to the Github. But thanks to local config, you can run the following on your command line:
bundle config local.database_modules ~/Projects/gems/spree
That will add a line to your ~/.bundle/config, so when you run bundle in your projects, it will pull it from your local repo.
User different versions of database_models gem (1.1, 1.2, 1.3...). I would probably need my own gem server for that, right?
I would do this and simply have my Gemfile select the correct gem via a tag. Here is an example:
git 'https://github.com/rails/rails.git', tag: 'v5.0.0'
So you can do the same while storing your custom gem on github (no gem server required).
Here is how to use tags with Git.
This should give you the flexibility you need.

Building a Ruby project with a private gem from GitLab

I want to use a hosted private gem on my Ruby project. This gem is hosted on GitLab.
So I need help on what to add in my Ruby Gemfile to "import" this privately hosted gem.
I am able to use private gems from GitHub or Gemfury but need help with GitLab.
To use a private hosted gem on gitlab you need to create an access token it should have api access. Then after you set in your ENV you can add the following to your gemfile:
gem 'mygem', git: "https://oauth2:#{ENV['GITLAB_TOKEN']}#gitlab.com/mygroup/mygem.git"
I would not put my gitlab username and password in my gemfile because then they exist in your source code for everyone whom has access to see them. It is important to note that your oauth token will be printed in your Gemfile.lock if you use this method.
Yes you can add gem from git lab.
You will need to pass username and password in the url part of the gem.
Example:
gem 'gem_name', 'version', :git => "http://<username>:<password>#myprivate_gitlab_host/private_gems/my_great_gem.git"
see here Is it possible to install gem from private gitlab host from Heroku
I think the :git refers to git and not to github, they are NOT the same...
Could be :git => 'github.com' or :git => gitlab.com or :git => myprivgit.com ...

Ruby: How to Install a Plugin in a Git Repository

I'm not new to programming, but brand new to Ruby. Everything's working, but I'm still missing a key concept: how do you install a plugin and where/how do you include it in an app?
Example:
I'm trying to use the Facebooker2 plugin: https://github.com/mmangino/facebooker2. In the readme, step 1 is to "Install facebooker2 as a plugin in your rails app." I've run the command git clone https://github.com/mmangino/facebooker2.git to download a read only version of the repository.
Do I then bundle that up using Bundler, or do I need to create a gem file in some way? Do I simply
use gem to install it, or do I need to compile it into a gem?
Any help (terminal commands or otherwise) are extremely helpful.
I looked at the repo and it's set up as a gem. You can simply add
gem 'facebooker2'
to your Gemfile (in the root of your project) and run
bundle install
to download it and add it to your list of installed gems, both in development and in production.
Rails used to include the concept of plugins (added to your /vendor/plugins directory) but that's been dropped in favor of gems.
If you're source is source 'https://rubygems.org' but the gem you need is specific to github and not part of the rubygems.org library, then you can add the git method to your gemfile. You can also select a specific branch version. For example, here I have the gem cancan being pulled from the github repository on the 2.0 branch.
gem "cancan", :git => "git://github.com/ryanb/cancan.git", :branch => "2.0"

Gemfile - separating Production gems from Development gems

So I know that in a Gemfile I can do something like this:
group :development, :test do
gem 'gem1'
gem 'gem2'
end
What I am looking to accomplish is something like this:
group :production do
gem 'gem1'
gem 'gem2'
end
group :development, :test do
gem 'gem1', :path => '/Documents/Code/gem1/'
gem 'gem2', :path => '/Documents/Code/gem2/'
end
So our application uses 2 gems that we also develop locally. In order to improve time while developing on our local machines, we want to be able to point our dev environments to the local copies of the gems - this way it picks up all changes without needing to restart our rails server. Otherwise we would have to rebuild the gem, re-install the gem, and restart rails with every development change in the gem.
However, doing this gives me the following error:
You cannot specify the same gem twice coming from different sources. You specified that gem1 (>= 0) should come from an unspecfied source and source at /Documents/Code/gem1
I have tried even running something like bundle install --without production and I get the same error.
Does anyone know if it IS possible to do what I would like to do?
Thanks!
i think that there is a supported way to do it and some hacks to work around it.
supported way:
use bundle config with the local option as described here: http://bundler.io/v1.3/man/bundle-config.1.html
hacky way:
use env vars and execute bundler before using in production: http://www.cowboycoded.com/2010/08/10/using-2-sources-for-a-gem-in-different-environments-with-bundler/
there is a feature-request for this problem on github with several related issues and lots of comments: https://github.com/carlhuda/bundler/issues/396
the github issue phoet linked to is resolved, and is consistent with the supported way.
I dug around through the docs, you'll need to set the config variable and updated your gemfile to reference the branch as well.
e.g.
edit your Gemfile:
gem <gem_name>, git: <git_url>, branch: <branch>
on command line:
bundle config local.<gem_name> <local_path_to_gem>
I solved this by creating a new Gemfile that's identical to the original except for the target gem's source. Then, in config/boot.rb, I used:
require 'rails'
if Rails.env.development?
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../DevGemfile', __FILE__)
else
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../ProdGemfile', __FILE__)
end

Capistrano bundle install gem from git rails not finding gem?

Forked wicked_pdf and added to Gemfile
gem 'wicked_pdf', '= 0.7.2', :git => 'git://github.com/geoffcorey/wicked_pdf.git'
Capistrano deploy does
bundle install --path vendor/gems --without development
All gems show up in vendor/gems/ruby/1.9.1/gems except wicked_pdf which the repo is cloned to vendor/gems/ruby/1.9.1/bundler/gems.
bundle list will show the wicked_pdf (0.7.2 156782e) but when I fire up the application via Apache/Passenger, Rails 3.1.3 cannot find wicked_pdf.
Is there something else I should be doing as part of the deploy to have the wicked_pdf build the gem and install as a separate task?
I am having exactly the same problem here (but hosting on heroku).
http://gembundler.com/man/bundle-package.1.html
"In Bundler 1.0, the bundle package command only packages .gem files, not gems specified using the :git or :path options. This will likely change in the future."
Have a look at:
Bundler: `bundle package` with a :git source
and maybe use:
http://underpantsgnome.com/2011/01/05/how-to-install-private-gems-on-heroku
to install the gem.
Your problem may be that (a) you're locking it to an exact version "=0.7.2", but you don't specify a commit ID on the git repo. These two things are in conflict. It's possible that the version entry in the gemspec is no longer 0.7.2 at the tip of the branch you're pulling from git.
If you specify a git location for a gem, it's best not to specify a version, but instead the commit ID you want, i.e.:
gem 'wicked_pdf', :git => 'git://github.com/geoffcorey/wicked_pdf.git', :ref => 'commit_id_on_github_you_want'

Resources