From aa5c9cfde38f19ae8795ec3b9ef6c64510fc0212 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Thu, 20 Nov 2014 12:12:43 +0000 Subject: rubygems: Support .gemspec files in subdirectories of the git repo This is cool because now you can import Ruby on Rails. --- baserockimport/exts/importer_bundler_extensions.rb | 38 +++++++++++++++------- 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'baserockimport') diff --git a/baserockimport/exts/importer_bundler_extensions.rb b/baserockimport/exts/importer_bundler_extensions.rb index 0e81cf5..88c82e2 100644 --- a/baserockimport/exts/importer_bundler_extensions.rb +++ b/baserockimport/exts/importer_bundler_extensions.rb @@ -28,7 +28,25 @@ end module Importer module BundlerExtensions + def locate_gemspec(gem_name, path) + target = "#{gem_name}.gemspec" + matches = Dir["#{path}/#{Bundler::Source::Path::DEFAULT_GLOB}"].select do |filename| + File.basename(filename) == target + end + if matches.empty? + error "Did not find any files matching #{target} within #{path}." + exit 1 + elsif matches.length > 1 + error "Multiple files matching #{target} found within #{path}. It's " \ + "not clear which one to use!" + exit 1 + end + matches[0] + end + def create_bundler_definition_for_gemspec(gem_name, path) + gemspec_file = locate_gemspec(gem_name, path) + # Using the real Gemfile doesn't get great results, because people can put # lots of stuff in there that is handy for developers to have but # irrelevant if you just want to produce a .gem. Also, there is only one @@ -40,14 +58,8 @@ module Importer # chosen .gemspec. If present, the Gemfile.lock will be honoured. fake_gemfile = Bundler::Dsl.new fake_gemfile.source('https://rubygems.org') - begin - fake_gemfile.gemspec({:name => gem_name, - :path => path}) - rescue Bundler::InvalidOption - error "Did not find #{gem_name}.gemspec in current directory." - exit 1 - end - + fake_gemfile.gemspec({:name => gem_name, + :path => File.dirname(gemspec_file)}) fake_gemfile.to_definition('Gemfile.lock', true) end @@ -61,11 +73,13 @@ module Importer found[0] end + def directory_is_within(path, expected_subpath) + File.realpath(expected_subpath).start_with?(File.realpath(path)) + end + def spec_is_from_current_source_tree(spec, source_dir) - Dir.chdir(source_dir) do - spec.source.instance_of? Bundler::Source::Path and - File.identical?(spec.source.path, '.') - end + spec.source.instance_of? Bundler::Source::Path and + directory_is_within(source_dir, spec.source.path) end def validate_spec(spec, source_dir_name, expected_version) -- cgit v1.2.1