Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jdickey/9214861 to your computer and use it in GitHub Desktop.
Save jdickey/9214861 to your computer and use it in GitHub Desktop.
Solving a third-party Gem dependency when building a Gem

So I built a dummy Rails-plugin gem...

rails plugin new foo --skip-bundle -O --full

and filled in my .gemspec as below, including the line

s.add_dependency "rails-assets-sugar"

and added

source "https://rails-assets.org"

to the Gemfile.

Running rake build gave the error

Could not find gem 'rails-assets-sugar (>= 0) ruby', which is required by gem 'foo (>= 0) ruby', in any of the sources.

After hours, never mind how many, spent gnashing my teeth and poring through interminable DuckDuckGo searches, I gave up and went on IRC freenode, Thanks to dwradcliffe, with head-scratching assistance from ddd, I found The Answer™.

Step 1: Add the Gem as a dependency in the .gemspec, as normal

s.add_dependency 'rails-assets-sugar'

Step 2: Add the rails-assets repo to the top of the Gemfile

source "https://rails-assets.org"

Step 3: The Secret Sauce: Add the Gem you want as a normal gem dependency in the Gemfile before the gemspec line

gem "rails-assets-sugar"

Step 4: Rebundle

bundle 

Step 5: Build your frakkin' Gem!

rake build

Step 6: Enjoy the endorphin rush from no longer beating your head against the wall after a solid day of doing so.

Thanks, guys!

$:.push File.expand_path("../lib", __FILE__)
# Maintain your gem's version:
require "foo/version"
# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "foo"
s.version = Foo::VERSION
s.authors = ["Jeff Dickey"]
s.email = ["[email protected]"]
s.homepage = "https://github.com/TheProlog/"
s.summary = "Example of asset bundling."
s.description = "Demonstrates bundling CoffeeScript/JavaScript assets and uses a third-party repo for a dependent Gem."
# TODO: Remove config from s.files?
s.files = Dir["{app,config,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]
s.add_dependency "railties", "~> 3.2.17"
s.add_dependency "rails-assets-sugar"
end
source "https://rubygems.org"
source 'https://rails-assets.org'
gem 'rails-assets-sugar'
# Declare your gem's dependencies in foo.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec
# jquery-rails is used by the dummy application
gem "jquery-rails"
# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.
# To use debugger
# gem 'debugger'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment