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!

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'
$:.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

Gems, as you might or might not know, are uncompressed Unix tar files.

[jeffdickey@Jeffs-Prolog-iMac foo]$ tar tvf pkg/foo-0.0.1.gem 
-r--r--r--  0 wheel  wheel     980 Feb 26 02:19 metadata.gz
-r--r--r--  0 wheel  wheel   12218 Feb 26 02:19 data.tar.gz
-r--r--r--  0 wheel  wheel     422 Feb 26 02:19 checksums.yaml.gz

Viewing the contents of data.tar.gz shows everything you'd expect:

[jeffdickey@Jeffs-Prolog-iMac foo]$ tar xf pkg/foo-0.0.1.gem data.tar.gz
[jeffdickey@Jeffs-Prolog-iMac foo]$ tar xzvf data.tar.gz
-rw-r--r--  0 wheel  wheel    1048 Feb 26 02:19 MIT-LICENSE
-rw-r--r--  0 wheel  wheel     191 Feb 26 02:19 README.rdoc
-rw-r--r--  0 wheel  wheel     698 Feb 26 02:19 Rakefile
-rw-r--r--  0 wheel  wheel     567 Feb 26 02:19 app/assets/javascripts/foo/util/gateway.js.coffee
-rw-r--r--  0 wheel  wheel      37 Feb 26 02:19 config/routes.rb
-rw-r--r--  0 wheel  wheel      37 Feb 26 02:19 lib/foo.rb
-rw-r--r--  0 wheel  wheel      54 Feb 26 02:19 lib/foo/engine.rb
-rw-r--r--  0 wheel  wheel      35 Feb 26 02:19 lib/foo/version.rb
-rw-r--r--  0 wheel  wheel      81 Feb 26 02:19 lib/tasks/foo_tasks.rake
-rw-r--r--  0 wheel  wheel    9220 Feb 26 02:19 test/dummy/README.rdoc
-rw-r--r--  0 wheel  wheel     270 Feb 26 02:19 test/dummy/Rakefile
-rw-r--r--  0 wheel  wheel     641 Feb 26 02:19 test/dummy/app/assets/javascripts/application.js
-rw-r--r--  0 wheel  wheel     546 Feb 26 02:19 test/dummy/app/assets/stylesheets/application.css
-rw-r--r--  0 wheel  wheel      80 Feb 26 02:19 test/dummy/app/controllers/application_controller.rb
-rw-r--r--  0 wheel  wheel      29 Feb 26 02:19 test/dummy/app/helpers/application_helper.rb
-rw-r--r--  0 wheel  wheel     232 Feb 26 02:19 test/dummy/app/views/layouts/application.html.erb
-rw-r--r--  0 wheel  wheel     155 Feb 26 02:19 test/dummy/config.ru
-rw-r--r--  0 wheel  wheel    2779 Feb 26 02:19 test/dummy/config/application.rb
-rw-r--r--  0 wheel  wheel     235 Feb 26 02:19 test/dummy/config/boot.rb
-rw-r--r--  0 wheel  wheel     149 Feb 26 02:19 test/dummy/config/environment.rb
-rw-r--r--  0 wheel  wheel    1070 Feb 26 02:19 test/dummy/config/environments/development.rb
-rw-r--r--  0 wheel  wheel    2310 Feb 26 02:19 test/dummy/config/environments/production.rb
-rw-r--r--  0 wheel  wheel    1391 Feb 26 02:19 test/dummy/config/environments/test.rb
-rw-r--r--  0 wheel  wheel     404 Feb 26 02:19 test/dummy/config/initializers/backtrace_silencers.rb
-rw-r--r--  0 wheel  wheel     533 Feb 26 02:19 test/dummy/config/initializers/inflections.rb
-rw-r--r--  0 wheel  wheel     205 Feb 26 02:19 test/dummy/config/initializers/mime_types.rb
-rw-r--r--  0 wheel  wheel     496 Feb 26 02:19 test/dummy/config/initializers/secret_token.rb
-rw-r--r--  0 wheel  wheel     407 Feb 26 02:19 test/dummy/config/initializers/session_store.rb
-rw-r--r--  0 wheel  wheel     341 Feb 26 02:19 test/dummy/config/initializers/wrap_parameters.rb
-rw-r--r--  0 wheel  wheel     214 Feb 26 02:19 test/dummy/config/locales/en.yml
-rw-r--r--  0 wheel  wheel    1788 Feb 26 02:19 test/dummy/config/routes.rb
-rw-r--r--  0 wheel  wheel       0 Feb 26 02:19 test/dummy/log/test.log
-rw-r--r--  0 wheel  wheel     728 Feb 26 02:19 test/dummy/public/404.html
-rw-r--r--  0 wheel  wheel     711 Feb 26 02:19 test/dummy/public/422.html
-rw-r--r--  0 wheel  wheel     643 Feb 26 02:19 test/dummy/public/500.html
-rw-r--r--  0 wheel  wheel       0 Feb 26 02:19 test/dummy/public/favicon.ico
-rwxr-xr-x  0 wheel  wheel     295 Feb 26 02:19 test/dummy/script/rails
-rw-r--r--  0 wheel  wheel     122 Feb 26 02:19 test/foo_test.rb
-rw-r--r--  0 wheel  wheel     134 Feb 26 02:19 test/integration/navigation_test.rb
-rw-r--r--  0 wheel  wheel     469 Feb 26 02:19 test/test_helper.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment