diff options
author | John Keiser <john@johnkeiser.com> | 2016-04-15 12:10:40 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2016-04-18 14:21:02 -0700 |
commit | ea2593fe58037739c5c2ada517e3d988dec290ff (patch) | |
tree | 55142ceaef43080201a8d9e54f79868368241f60 /tasks/bin | |
parent | bbc1d6ebccbd7f22fea72a9c2b76ef19f3b5beb3 (diff) | |
download | chef-ea2593fe58037739c5c2ada517e3d988dec290ff.tar.gz |
Make gemfile_util capable of copying groups over
Diffstat (limited to 'tasks/bin')
-rw-r--r-- | tasks/bin/bundle-platform.bat | 2 | ||||
-rwxr-xr-x | tasks/bin/create-override-gemfile | 110 | ||||
-rwxr-xr-x | tasks/bin/run_external_test | 11 |
3 files changed, 114 insertions, 9 deletions
diff --git a/tasks/bin/bundle-platform.bat b/tasks/bin/bundle-platform.bat new file mode 100644 index 0000000000..d193eb0c05 --- /dev/null +++ b/tasks/bin/bundle-platform.bat @@ -0,0 +1,2 @@ +@ECHO OFF +ruby "%~dpn0" %* diff --git a/tasks/bin/create-override-gemfile b/tasks/bin/create-override-gemfile new file mode 100755 index 0000000000..b67da025d2 --- /dev/null +++ b/tasks/bin/create-override-gemfile @@ -0,0 +1,110 @@ +#!/usr/bin/env ruby + +require "rubygems" +require "bundler" + +Bundler.with_clean_env do + require_relative "../gemfile_util" + + options = {} + opts = OptionParser.new do |opts| + opts.banner = "Usage: create-override-gemfile [OPTIONS]" + + opts.on("--gemfile GEMFILE", "The Gemfile to read (default: Gemfile).") { |path| options[:gemfile_path] = path } + opts.on("--lockfile GEMFILE", "The lockfile to read (default: <gemfile>.lock).") { |path| options[:lockfile_path] = path } + + opts.on("--group GROUP", "Groups to include (whitelist).") do |group| + options[:groups] ||= [] + options[:groups] << group.to_sym + end + + opts.on("--without GROUP", "Groups to exclude.") do |group| + options[:without_groups] ||= [] + options[:without_groups] << group.to_sym + end + + opts.on("--gem GEM", "Gems to include regardless of groups.") do |name| + options[:gems] ||= [] + options[:gems] << name + end + + opts.on("--relative-to PATH", "A path to prepend to any relative paths in the Gemfile.") do |path| + unless Pathname.new(path).absolute? + puts opts + raise "--relative-to #{path} was not an absolute path!" + end + options[:relative_to] = path + end + + opts.on("--[no-]copy-groups", "Whether to copy groups over from the original Gemfile or not (default: false).") { |val| options[:copy_groups] = val } + + opts.on("--[no-]override", "Whether to emit override: true on each gem line (default: false).") { |val| options[:override] = val } + + opts.on("-h", "--help", "Print this message.") do + puts opts + exit(0) + end + end + + args = opts.parse(ARGV) + + if args.size > 0 + puts opts + raise "Invalid arguments #{args}" + end + + def create_override_gemfile(gemfile_path: "Gemfile", lockfile_path: "#{gemfile_path}.lock", groups: nil, without_groups: nil, gems: [], copy_groups: false, relative_to: ".", override: false) + relative_to = Pathname.new(relative_to).realpath + # Select the gems we want + bundle = GemfileUtil::Bundle.parse(gemfile_path, lockfile_path) + gems_to_include = bundle.select_gems(groups: groups, without_groups: without_groups) + gems.each do |name| + raise "Requested gem #{name} is not in #{gemfile_path}.lock!" if !bundle.gems[name] + gems_to_include[name] ||= bundle.gems[name] + gems_to_include[name][:dependencies].each do |dep| + gems_to_include[name] ||= bundle.gems[dep] + end + end + + # Add the gems to the Gemfile + gem_root = Pathname.new(gemfile_path).dirname.realpath + gems_to_include.sort_by { |name, options| options[:declared_groups].empty? ? 1 : 0 }.each do |name, options| + comment = nil + options = options.dup + version = options.delete(:version) + if copy_groups + # Some dependencies have no groups (are not in the Gemfile--just runtime + # dependencies). If we actually record that they have no groups, they + # will *always* be installed (or perhaps never). We only want them to + # install if their other deps do, so we mark them with the groups of the + # things that brought them in (the gems that depended on them). To do + # this, we just leave :groups intact. + if options[:declared_groups].empty? + options.delete(:declared_groups) + comment = " # Transitive dependency, not actually in original Gemfile" + else + # For other things, we want to copy the actual declared_groups--the + # ones that were in the Gemfile. We want the same --with and --without + # options to include and exclude them as worked with the original + # Gemfile. + options[:groups] = options.delete(:declared_groups) + end + else + options.delete(:groups) + options.delete(:declared_groups) + end + options.delete(:dependencies) + options.delete(:development_dependencies) + options[:override] = true if override + options[:path] = Pathname.new(options[:path]).expand_path(gem_root).relative_path_from(relative_to).to_s if options[:path] + line = "gem #{name.inspect}, #{version.inspect}" + options.each do |name, value| + line << ", #{name}: #{value.inspect}" + end + line << comment if comment + puts line + end + end + + create_override_gemfile(options) +end diff --git a/tasks/bin/run_external_test b/tasks/bin/run_external_test index f1cefb9138..74f76d3229 100755 --- a/tasks/bin/run_external_test +++ b/tasks/bin/run_external_test @@ -20,7 +20,7 @@ TEST_GEMFILE=$TEST_GEM_ROOT/Gemfile MODIFIED_TEST_GEMFILE=$TEST_GEMFILE.externaltest cat <<EOM > $MODIFIED_TEST_GEMFILE require_relative "$PROJECT_ROOT/tasks/gemfile_util" -GemfileUtil.include_locked_gemfile(self, "$PROJECT_ROOT/Gemfile", groups: [:default], gems: ["$TEST_GEM"] + "$TEST_WITH_GEMS".split(/\s+/)) +GemfileUtil.include_locked_gemfile(self, "$PROJECT_ROOT/Gemfile", gems: ["$TEST_GEM"] + "$TEST_WITH_GEMS".split(/\s+/)) $TEST_GEM_OVERRIDES EOM cat $TEST_GEMFILE >> $MODIFIED_TEST_GEMFILE @@ -38,17 +38,10 @@ export BUNDLE_IGNORE_CONFIG=true # Use the top level bundle cache so we don't have to reinstall their packages if [ -n "$PROJECT_BUNDLE_PATH" ]; then export BUNDLE_PATH=$PROJECT_BUNDLE_PATH - export BUNDLE_DISABLE_SHARED_GEMS=1 - export BUNDLE_NO_PRUNE=true fi export BUNDLE_FROZEN= bundle install export BUNDLE_FROZEN=true bundle config - -# Iterate through the remaining arguments as commands -while test ${#} -gt 0; do - bundle exec $1 - shift -done +bundle exec $@ |