aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib
diff options
context:
space:
mode:
authory-yagi <yuuji.yaginuma@gmail.com>2019-07-21 08:24:19 +0900
committerGitHub <noreply@github.com>2019-07-21 08:24:19 +0900
commita390001bbe482619e3a00186d23421e93f8e8c39 (patch)
tree10b55714758cad1ae10530771b2b0d5062d8e7a8 /railties/lib
parent400b2103540e1dfb370eab29a891687d778ad357 (diff)
parent9147b284b6fa66af6b2af493c2e7e88d449c2247 (diff)
downloadrails-a390001bbe482619e3a00186d23421e93f8e8c39.tar.gz
rails-a390001bbe482619e3a00186d23421e93f8e8c39.tar.bz2
rails-a390001bbe482619e3a00186d23421e93f8e8c39.zip
Merge pull request #35285 from masakazutakewaka/fix_railtie_add_newline_to_gemfile_insertion
Add a newline at the end of a Gemfile when it doesn't end with a newline
Diffstat (limited to 'railties/lib')
-rw-r--r--railties/lib/rails/generators/actions.rb22
1 files changed, 14 insertions, 8 deletions
diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb
index 406a5b8fc7..c3a43d5398 100644
--- a/railties/lib/rails/generators/actions.rb
+++ b/railties/lib/rails/generators/actions.rb
@@ -40,8 +40,7 @@ module Rails
in_root do
str = "gem #{parts.join(", ")}"
str = indentation + str
- str = "\n" + str
- append_file "Gemfile", str, verbose: false
+ append_file_with_newline "Gemfile", str, verbose: false
end
end
@@ -58,9 +57,9 @@ module Rails
log :gemfile, "group #{str}"
in_root do
- append_file "Gemfile", "\ngroup #{str} do", force: true
+ append_file_with_newline "Gemfile", "group #{str} do", force: true
with_indentation(&block)
- append_file "Gemfile", "\nend\n", force: true
+ append_file_with_newline "Gemfile", "end", force: true
end
end
@@ -71,9 +70,9 @@ module Rails
log :github, "github #{str}"
in_root do
- append_file "Gemfile", "\n#{indentation}github #{str} do", force: true
+ append_file_with_newline "Gemfile", "#{indentation}github #{str} do", force: true
with_indentation(&block)
- append_file "Gemfile", "\n#{indentation}end", force: true
+ append_file_with_newline "Gemfile", "#{indentation}end", force: true
end
end
@@ -91,9 +90,9 @@ module Rails
in_root do
if block
- append_file "Gemfile", "\nsource #{quote(source)} do", force: true
+ append_file_with_newline "Gemfile", "source #{quote(source)} do", force: true
with_indentation(&block)
- append_file "Gemfile", "\nend\n", force: true
+ append_file_with_newline "Gemfile", "end", force: true
else
prepend_file "Gemfile", "source #{quote(source)}\n", verbose: false
end
@@ -344,6 +343,13 @@ module Rails
ensure
@indentation -= 1
end
+
+ # Append string to a file with a newline if necessary
+ def append_file_with_newline(path, str, options = {})
+ gsub_file path, /\n?\z/, options do |match|
+ match.end_with?("\n") ? "" : "\n#{str}\n"
+ end
+ end
end
end
end