From 5ec47aaf131534af821deb710f779429a79c1b28 Mon Sep 17 00:00:00 2001 From: Jake Romer Date: Sun, 22 Aug 2021 21:49:56 -0400 Subject: [PATCH] Enable passing env var paths to cli -m option Currently, if you add the --template/-m option to a railsrc file, you lose the ability to define the path to your rails template using an environment variable, which you have when passing that option from the command line. This patch adds that ability by shelling out to interpret the passed path. --- railties/lib/rails/generators/app_base.rb | 2 +- .../test/generators/app_generator_test.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index ea2a64fea2..96cf73c25c 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -148,7 +148,7 @@ module Rails when /^https?:\/\// options[:template] when String - File.expand_path(options[:template], Dir.pwd) + File.expand_path(`echo #{options[:template]}`.strip) else options[:template] end diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 4100973e60..4ec3bf9101 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -720,6 +720,25 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"])) end + def test_template_from_url + url = "https://raw.githubusercontent.com/rails/rails/f95c0b7e96/railties/test/fixtures/lib/template.rb" + FileUtils.cd(Rails.root) + assert_match(/It works from file!/, run_generator([destination_root, "-m", url])) + end + + def test_template_from_abs_path + absolute_path = File.expand_path(Rails.root, "fixtures") + FileUtils.cd(Rails.root) + assert_match(/It works from file!/, run_generator([destination_root, "-m", "#{absolute_path}/lib/template.rb"])) + end + + def test_template_from_env_var_path + ENV["FIXTURES_HOME"] = File.expand_path(Rails.root, "fixtures") + FileUtils.cd(Rails.root) + assert_match(/It works from file!/, run_generator([destination_root, "-m", "$FIXTURES_HOME/lib/template.rb"])) + ENV.delete("FIXTURES_HOME") + end + def test_usage_read_from_file assert_called(File, :read, returns: "USAGE FROM FILE") do assert_equal "USAGE FROM FILE", Rails::Generators::AppGenerator.desc