1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #33743 from steakknife/steakknife/improve-template-generator-actions

add github to template actions, template actions minor refactor
This commit is contained in:
Rafael França 2018-08-31 16:56:00 -05:00 committed by GitHub
commit f3fe56c732
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 16 deletions

View file

@ -7,7 +7,7 @@ module Rails
module Actions module Actions
def initialize(*) # :nodoc: def initialize(*) # :nodoc:
super super
@in_group = nil @indentation = 0
@after_bundle_callbacks = [] @after_bundle_callbacks = []
end end
@ -36,13 +36,11 @@ module Rails
log :gemfile, message log :gemfile, message
options.each do |option, value| parts << quote(options) unless options.empty?
parts << "#{option}: #{quote(value)}"
end
in_root do in_root do
str = "gem #{parts.join(", ")}" str = "gem #{parts.join(", ")}"
str = " " + str if @in_group str = indentation + str
str = "\n" + str str = "\n" + str
append_file "Gemfile", str, verbose: false append_file "Gemfile", str, verbose: false
end end
@ -54,20 +52,32 @@ module Rails
# gem "rspec-rails" # gem "rspec-rails"
# end # end
def gem_group(*names, &block) def gem_group(*names, &block)
name = names.map(&:inspect).join(", ") options = names.extract_options!
log :gemfile, "group #{name}" str = names.map(&:inspect)
str << quote(options) unless options.empty?
str = str.join(", ")
log :gemfile, "group #{str}"
in_root do in_root do
append_file "Gemfile", "\ngroup #{name} do", force: true append_file "Gemfile", "\ngroup #{str} do", force: true
with_indentation(&block)
@in_group = true
instance_eval(&block)
@in_group = false
append_file "Gemfile", "\nend\n", force: true append_file "Gemfile", "\nend\n", force: true
end end
end end
def github(repo, options = {}, &block)
str = [quote(repo)]
str << quote(options) unless options.empty?
str = str.join(", ")
log :github, "github #{str}"
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
# Add the given source to +Gemfile+ # Add the given source to +Gemfile+
# #
# If block is given, gem entries in block are wrapped into the source group. # If block is given, gem entries in block are wrapped into the source group.
@ -83,9 +93,7 @@ module Rails
in_root do in_root do
if block if block
append_file "Gemfile", "\nsource #{quote(source)} do", force: true append_file "Gemfile", "\nsource #{quote(source)} do", force: true
@in_group = true with_indentation(&block)
instance_eval(&block)
@in_group = false
append_file "Gemfile", "\nend\n", force: true append_file "Gemfile", "\nend\n", force: true
else else
prepend_file "Gemfile", "source #{quote(source)}\n", verbose: false 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. # Surround string with single quotes if there is no quotes.
# Otherwise fall back to double quotes # Otherwise fall back to double quotes
def quote(value) # :doc: 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 return value.inspect unless value.is_a? String
if value.include?("'") if value.include?("'")
@ -334,6 +347,19 @@ module Rails
"#{value.strip.indent(amount)}\n" "#{value.strip.indent(amount)}\n"
end end
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 end
end end

View file

@ -144,6 +144,44 @@ class ActionsTest < Rails::Generators::TestCase
assert_file "Gemfile", /\ngroup :development, :test do\n gem 'rspec-rails'\nend\n\ngroup :test do\n gem 'fakeweb'\nend/ assert_file "Gemfile", /\ngroup :development, :test do\n gem 'rspec-rails'\nend\n\ngroup :test do\n gem 'fakeweb'\nend/
end end
def test_github_should_create_an_indented_block
run_generator
action :github, "user/repo" do
gem "foo"
gem "bar"
gem "baz"
end
assert_file "Gemfile", /\ngithub 'user\/repo' do\n gem 'foo'\n gem 'bar'\n gem 'baz'\nend/
end
def test_github_should_create_an_indented_block_with_options
run_generator
action :github, "user/repo", a: "correct", other: true do
gem "foo"
gem "bar"
gem "baz"
end
assert_file "Gemfile", /\ngithub 'user\/repo', a: 'correct', other: true do\n gem 'foo'\n gem 'bar'\n gem 'baz'\nend/
end
def test_github_should_create_an_indented_block_within_a_group
run_generator
action :gem_group, :magic do
github "user/repo", a: "correct", other: true do
gem "foo"
gem "bar"
gem "baz"
end
end
assert_file "Gemfile", /\ngroup :magic do\n github 'user\/repo', a: 'correct', other: true do\n gem 'foo'\n gem 'bar'\n gem 'baz'\n end\nend\n/
end
def test_environment_should_include_data_in_environment_initializer_block def test_environment_should_include_data_in_environment_initializer_block
run_generator run_generator
autoload_paths = 'config.autoload_paths += %w["#{Rails.root}/app/extras"]' autoload_paths = 'config.autoload_paths += %w["#{Rails.root}/app/extras"]'