Ruby: How to Install a Plugin in a Git Repository - ruby

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"

Related

Gem from custom git repository is not available

I tried to modify an existing gem and forked the git repository.
I added some commits and pointed bundler to my GitHub repository.
bundler update does recognise the change and downloads my version of the gem.
Anyway when I try to launch the application which depends on that gem (testkitchen) my changes aren't available. And when I delete the official version of the gem my version is not found/used and the app fails.
The weird thing is that all the official gems are installed to /var/lib/gems and my version goes to ~/.bundler
gem environment also shows up the correct directories
- GEM PATHS:
- /var/lib/gems/2.2.0
- /home/ansible/.bundler/ruby/2.2.0
I'm not using rvm or similar. Am I doing something wrong?
As #matt pointed out I forgot to add bundle exec to my commands.

Ruby gem equivalent of "pip install -e"?

In Python I can install a package from source in "editable" mode using pip install -e. Then I can carry on editing the code, and any changes will be automatically picked by other Python scripts that import library
Is there a comparable workflow for developing Ruby gems? What is the "Ruby way" of using libs as they are being developed rather than, for example, compiling and installing a gem every time I make a change to the source?
There are two common approaches one might use with bundler:
one executes bundle install --path vendor/bundle and does not run bundle update unless everything is tested.
one tells a bundler to use a local version of the gem:
in Gemfile (this is not supported in mymaingem.gemspec due to rubygems maintainence issues): gem 'mycutegem', :git => 'git://github.com/myname/mycutegem', :branch => 'master';
in command line: bundle config local.mycutegem /path_to_local_git/mycutegem.
The first approach will download everything into subfolder of your current project (here it’d be vendor/bundle.) Feel free to modify everything there, it’ll be reflected.
The second approach is likely better. You are to clone the gem from github and instruct bundle to use your local clone of the respective git repository. This approach provides you with an ability to publish the changes to your main gem into the repository. As soon as dependent repo is published as well, the up-to-date version will be retrieven by your gem subscribers, assuming they have not instructed their bundlers to use their locals.
Hope this helps.
Let's assume you have your gem code residing in a folder (say my_project/mygem/lib). You have some Ruby code in my_project that you want using the mygem code.
What I'd do is add mygem/lib to the $LOAD_PATH global variable in the beginning of said files. Kinda like this:
$LOAD_PATH << File.expand_path('lib', './mygem') # Resolve global paths
require 'a_file' # Would require "mygem/lib/a_file.rb"
I am not sure if this is exactly what you want to achieve, but from the description I infer that you want to have a local copy of some gem and reference that gem in your project.
If this is the case, you can (usually) achieve it in two steps:
Clone the gem from VCS (in most cases: git), e.g.
git clone url-of-the-gem-repo
Reference the local copy using Bundler :path, e.g.
gem "some-gem", :path => "/path/to/local/copy"
If the gem is stored at github, an even better way is to first fork it at github and then clone your own copy. Then, if you provide any improvements to the code in the local repo, you can easily share it with the world using a pull request.
I realize this is a 5 year old question, but I found all of these answers unsatisfying. Since I use ruby to develop CLI tools, using bundler for testing is not ideal. I need to be able to execute my test commands anywhere, and get the actual equivalent of pip install --editable.
Here's my solution.
Use RVM to setup a new gemset for development
rvm gemset use tools-dev --create
Install your gems
gem install mygem.gem
Locate the installation directory
gem list tool -d
In the output, it will say installed at ${rvm_gemset_path}. Copy that path. It will also be an environment variable GEM_HOME, but I include this step for completeness and clarity.
Delete the installed copy
rm -Rf ${rvm_gemset_path}/gems/mygem-${version}
Create a symlink to the git repository working directory.
ln -s ${PWD} ${rvm_gemset_path}/gems/mygem-${version}
This bash script will do it in a more automated fashion assuming that you are in the git directory and you've set GEM_NAME as your gem's name.
rvm gemset use --create ${GEM_NAME}-dev
gem build ${GEM_NAME}.gemspec
gem install ${GEM_NAME}*.gem
gem_install_path=$(ruby -e "puts Gem::Specification.find_by_name('${GEM_NAME}').full_gem_path")
mv $gem_install_path "${gem_install_path}.bak"
ln -s $PWD $gem_install_path

How could I install Gems from git repositories?

I need to install gem form git repository. repository contains .gemspec file. In my gem file i have following code:
gem 'echo_server', :git => 'http://127.0.0.1/org/echo_server.git'
When i am running bundle install gems are installer in .bundeler and not showing in gem list.
my question is:
How the gem will be available in system, so that i can use in require ?
There are some similar SOQ but it does't help me.
It is not showing up when you type gem list because it is not being installed like a regular gem. You can require it like you would any other library because Bundler knows about it and will set it up for you. You should see it in your $LOAD_PATH:
$LOAD_PATH.grep(/nameofgem/)
See the Bundler documentation on this for more information.
If instead you want to install it as a regular gem from the Git repository, you can clone the repository and then build and install the generated gem. For example:
gem build echo_server.gemspec
gem install echo_server-X.Y.Z.gem
There is also specific_install.

Ruby bundler: use gem from mercurial repository

As everyone know, we can use gem from git repository:
gem 'nokogiri', :git => 'git://github.com/tenderlove/nokogiri.git', :branch => '1.4'
(example from http://bundler.io/gemfile.html)
But I prefer Mercurial, so is it possible to use gem from Mercurial repo?
Currently no, however there has been some activity to add it. The most recent seems to be Bundler Issue #1219.
The work around I would go with would be to either clone locally and use the file path in the Gemfile. Perhaps using Ryan Bigg's nifty Bundler Local Paths Trick on the development system. Unfortunately this is not a good solution for deploying.
Hopefully the bundler team will merge in support at some point.

Bundler: always use latest revision of git branch in Gemfile

I have a Gemfile with a private git repo in the following format:
gem 'magic_beans', :git => "git#git.example.com:magic_beans.git', :branch => 'super_beans'
When I bundle install, the Gemfile.lock locks it to a specific SHA revision.
Can I get bundler to always check and use the latest SHA commit and/or update the Gemfile.lock? Notice that when I push updates to the super_beans branch I am not modifying the gem version.
Ideally, every time I run bundle it would check upstream git repo for a newer SHA revision of the branch.
This isn't how bundler works.
The point is to allow seamless versioning of dependencies.
(particularly so you know exactly what version of the code is deployed at any given time).
If want the latest version, you should just run.
bundle update magic_beans
This is exactly the same functionality as if you just say
gem "rails"
I'd suggest though, if you have a range of specific things you want to update
then add a custom binary (say an executable file named bundle_update)
#!/usr/bin/env bash
bundle install
bundle update magic_beans
Then just do a ./bundle_update when you want to update these things.
You can run bundle update to update all or specific gems to their latest available version, as stated in the docs.
Would that help?
After searching through the documents I finally found the magic way to do this:
bundle update magic_beans --source magic_beans
That is to update the magic_beans gem only, but not to touch other locked gems. The doc about this is: http://bundler.io/man/bundle-update.1.html
delete .gemlock is what worked for me :/

Resources