From 20e3cacbaa1e86b76483fe1d94cfc42907a5727c Mon Sep 17 00:00:00 2001 From: Barry Allard Date: Tue, 28 Aug 2018 20:20:29 -0700 Subject: add github to template actions, light DSL refactor --- railties/lib/rails/generators/actions.rb | 56 +++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 15 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb index ae395708cb..78d2471890 100644 --- a/railties/lib/rails/generators/actions.rb +++ b/railties/lib/rails/generators/actions.rb @@ -7,7 +7,7 @@ module Rails module Actions def initialize(*) # :nodoc: super - @in_group = nil + @indentation = 0 @after_bundle_callbacks = [] end @@ -36,13 +36,11 @@ module Rails log :gemfile, message - options.each do |option, value| - parts << "#{option}: #{quote(value)}" - end + parts << quote(options) unless options.empty? in_root do str = "gem #{parts.join(", ")}" - str = " " + str if @in_group + str = indentation + str str = "\n" + str append_file "Gemfile", str, verbose: false end @@ -54,17 +52,29 @@ module Rails # gem "rspec-rails" # end def gem_group(*names, &block) - name = names.map(&:inspect).join(", ") - log :gemfile, "group #{name}" + options = names.extract_options! + str = names.map(&:inspect) + str << quote(options) unless options.empty? + str = str.join(", ") + log :gemfile, "group #{str}" in_root do - append_file "Gemfile", "\ngroup #{name} do", force: true + append_file "Gemfile", "\ngroup #{str} do", force: true + with_indentation(&block) + append_file "Gemfile", "\nend\n", force: true + end + end - @in_group = true - instance_eval(&block) - @in_group = false + def github(repo, options = {}, &block) + str = [quote(repo)] + str << quote(options) unless options.empty? + str = str.join(", ") + log :github, "github #{str}" - append_file "Gemfile", "\nend\n", force: true + in_root do + append_file "Gemfile", "\n#{indentation}github #{str} do", force: true + with_indentation(&block) + append_file "Gemfile", "\n#{indentation}end", force: true end end @@ -83,9 +93,7 @@ module Rails in_root do if block append_file "Gemfile", "\nsource #{quote(source)} do", force: true - @in_group = true - instance_eval(&block) - @in_group = false + with_indentation(&block) append_file "Gemfile", "\nend\n", force: true else prepend_file "Gemfile", "source #{quote(source)}\n", verbose: false @@ -315,6 +323,11 @@ module Rails # Surround string with single quotes if there is no quotes. # Otherwise fall back to double quotes def quote(value) # :doc: + if value.respond_to? :each_pair + return value.map do |k, v| + "#{k}: #{quote(v)}" + end.join(", ") + end return value.inspect unless value.is_a? String if value.include?("'") @@ -334,6 +347,19 @@ module Rails "#{value.strip.indent(amount)}\n" end end + + # Indent the +Gemfile+ to the depth of @indentation + def indentation # :doc: + " " * @indentation + end + + # Manage +Gemfile+ indentation for a DSL action block + def with_indentation(&block) # :doc: + @indentation += 1 + instance_eval(&block) + ensure + @indentation -= 1 + end end end end -- cgit v1.2.3