Is possible have a git branch dependency, inside mygem.gemspec?
I'm thinking something similar to the following:
gem.add_runtime_dependency 'oauth2', :git => '[email protected]:lgs/oauth2.git'
... but it doesn't work.
Is possible have a git branch dependency, inside mygem.gemspec?
I'm thinking something similar to the following:
gem.add_runtime_dependency 'oauth2', :git => '[email protected]:lgs/oauth2.git'
... but it doesn't work.
This is not possible, and likely never will be because it would be rather heavy-handed for RubyGems to allow gem developers to require that users have a specific version control system installed to access a gem. Gems should be self-contained with a minimal number of dependencies so that people can use them in as wide an array of application as possible.
If you want to do this for your own internal projects, my suggestion would be to use Bundler which supports this quite well.
Gemfile
and the dependency .gemspec
files, adding a local or git reference to a Gem in your Gemfile
will tell bundler where to find it, even if it's referenced in another gem. You just need to make sure you have compatible version specs. –
Mccraw gem 'A', git: '[email protected]:myorganization/A.git'
to gem B's Gemfile, not its .gemspec –
Negatron bundle install
. What do I miss? –
Baton fail
at the top of it and that nothing happens. –
Baton EDIT
According to a commenter, this is no longer true. Prior information retained for historical context.
Duplicating the reference to a gem in Gemfile and .gemspec now appears to raise a warning message in Bundler, so this answer would appear to be no longer true.
Outdated info
This article by Yehuda Katz cleared up similar confusion for me. It says that, for use in development only, it's best to add the git stuff into the gemfile, but that bundler will still use the dependency/version info from the gemspec (seems magical to me, but I trust Yehuda).
gemspec
in there, it also reads from the gemspec. So when you run bundle install
, I assume (but haven't tested) that what happens is that Bundler installs the gem specified in the Gemfile. Since Bundler has already installed it, that gem is available for the gem to require
, regardless of the fact that it didn't come from a gem repository. No magic, just Bundler working as usual. –
Ramshackle I just was trying to figure this problem out as well. And I just came up with the following solution (which I'm not sure if your publishing your gem or have rights to redistribute that oauth2 gem).
In your gem that requires oauth2 gem run this.
git submodule add [email protected]:lgs/oauth2.git lib/oauth2
If you require a different branch than the default
cd lib/oauth2 && git checkout <branchname_or_ref>
cd .. && git add lib/oauth2
git commit -m "adding outh2 submodule"
In your gemspec add this above your require version line
$:.push File.expand_path('../lib/oauth2/lib', __FILE__)
Also you'll need to add all of the oauth2 gem's runtime dependencies to your gemspec. I haven't figured out a way around this yet.
This is what I did, and it works for us because our gem is required via git so I'm not sure if this would work for a rubygems published gem.
gem 'my_gem', git: '[email protected]:me/myrepo', submodules: true
in your host application if you are installing from github. –
Deanery I found a work-around pretty straight forward:
Say your are in a project P
and you want to use the self made gem tools
which itself uses an OS gem oauth2
.
If you made a patch within oauth2
and need that patch in your gem tools
, you won't be able to fix this issue in the gemspec file according to the accepted answer.
However, you can speficy the version you want within your projet P
's Gemfile, and this will be the version used by tools
on runtime:
gem 'oauth2', github: 'lgs/oauth2'
I was facing similar issue and here is what I found. You cannot add git branch directly for some other gem, However you can acheive this another way. You can define a private gem with repository link and branch name in gemfile of you custom gem i.e
gem 'gem_name', '>=0.1.1', git: 'repository_link ', branch: 'brnach_name'
and run bundle install
Now you can mention it in gemspec file, no need to add version as it will already pick from Gemfile.lock
spec.add_runtime_dependency 'sms_service'
Note: Make sure you keep gemspec
at the bottom in Gemfile. So, it will first install necessary gems and than add them as dependency to your gem.
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem 'sms_service', '>=0.1.1', git: 'repository link', branch: 'branch_name'
gemspec
spec.add_runtime..
(above) just caused the wrong gem version to load. Takeaway.. reference the branch u need in Gemfile.. and make sure to add gemspec to bottom of Gemfile. –
Hyperextension © 2022 - 2024 — McMap. All rights reserved.