From 7cc2b57b8dd052ff5986c663865174b247c0ea8c Mon Sep 17 00:00:00 2001 From: Nick Wolf Date: Wed, 21 Oct 2020 16:21:07 +1100 Subject: [PATCH] Refactor railsrc file location to be xdg compliant (#39411) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refactor railsrc file location to be xdg compliant The XDG Base Directory Specification (which is currently used by FOSS projects such as Git, Tmux, Pry, Rspec) provides a default location for various file formats, including config/rc files. This comment refactors app_generator.rb to load railsrc from XDG_CONFIG_HOME if both XDG_CONFIG_HOME is set and rails/railrc exists within the XDG_CONFIG_HOME location. To maintain backwards compatibility it defaults back to ~/.railsrc if either XDG_CONFIG_HOME is not set or there is no rails/railsrc. * Refactor default_rc_file based on jonathanhefner PR feedback * Update generators/rails/app/USAGE to explain railsrc XDG compliance * Refactor default_rc_file based on sinsoku PR feedback * Add test for ARGVScrubber.default_rc_file method fileutils is required because tmpdir does not allow you to create a new temp directory with a set title 'inside' another temporary directory, a file is created for railsrc because tempfile creates a custom and unique name preventing the specification of a file called 'railsrc' Currently this test is failing because the ARGVScrubber class seems to be loading directly from the gem (which still has the previous code) rather than from the file itself. (loading the code in a pry session and using show-method ARGVScrubber.default_rc_file reveals it is coming from gems/railties-6.0.3.3/lib/rails/generators/rails/app/app_generator.rb:536 instead of railties/lib/rails/generators/rails/app/app_generator.rb * Switch to double quote strings * Add file deletion for argv_scrubber test * Refactor test_xdg_config_no_custom_rc with jonathanhefner feedback * Add Changelog for railsrc xdg-specification compliance * Apply backtick and whitespace refactoring suggestsions from jonathanhefner * Apply jonathanhefner suggested Changelog refactoring [Rafael Mendonça França + Nick Wolf + Jonathan Hefner] --- railties/CHANGELOG.md | 11 +++++++++++ railties/lib/rails/generators/rails/app/USAGE | 3 ++- .../rails/generators/rails/app/app_generator.rb | 8 +++++++- railties/test/generators/argv_scrubber_test.rb | 15 +++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 2595f83070..2243771f49 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,14 @@ +* Make railsrc file location xdg-specification compliant + + `rails new` will now look for the default `railsrc` file at + `$XDG_CONFIG_HOME/rails/railsrc` (or `~/.config/rails/railsrc` if + `XDG_CONFIG_HOME` is not set). If this file does not exist, `rails new` + will fall back to `~/.railsrc`. + + The fallback behaviour means this does not cause any breaking changes. + + *Nick Wolf* + * Deprecate `config.active_support.use_sha1_digests` `config.active_support.use_sha1_digests` is deprecated. It is replaced with `config.active_support.hash_digest_class` which allows setting the desired Digest instead. The Rails version defaults have been updated to use this new method as well so the behavior there is unchanged. diff --git a/railties/lib/rails/generators/rails/app/USAGE b/railties/lib/rails/generators/rails/app/USAGE index 28df6ebf44..ea2285761f 100644 --- a/railties/lib/rails/generators/rails/app/USAGE +++ b/railties/lib/rails/generators/rails/app/USAGE @@ -3,7 +3,8 @@ Description: directory structure and configuration at the path you specify. You can specify extra command-line arguments to be used every time - 'rails new' runs in the .railsrc configuration file in your home directory. + 'rails new' runs in the .railsrc configuration file in your home directory, + or in $XDG_CONFIG_HOME/rails/railsrc if XDG_CONFIG_HOME is set. Note that the arguments specified in the .railsrc file don't affect the defaults values shown above in this help message. diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index 4ccba0d474..2c20b1c6cb 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -606,7 +606,13 @@ module Rails end def self.default_rc_file - File.expand_path("~/.railsrc") + xdg_config_home = ENV["XDG_CONFIG_HOME"].presence || "~/.config" + xdg_railsrc = File.expand_path("rails/railsrc", xdg_config_home) + if File.exist?(xdg_railsrc) + xdg_railsrc + else + File.expand_path("~/.railsrc") + end end private diff --git a/railties/test/generators/argv_scrubber_test.rb b/railties/test/generators/argv_scrubber_test.rb index 9ef61dc978..f72cb353e0 100644 --- a/railties/test/generators/argv_scrubber_test.rb +++ b/railties/test/generators/argv_scrubber_test.rb @@ -4,6 +4,8 @@ require "active_support/test_case" require "active_support/testing/autorun" require "rails/generators/rails/app/app_generator" require "tempfile" +require "fileutils" +require "env_helpers" module Rails module Generators @@ -12,6 +14,8 @@ module Rails # current behavior of the ARGVScrubber, they do not mean that the class # *must* act this way, I just want to prevent regressions. + include EnvHelpers + def test_version ["-v", "--version"].each do |str| scrubber = ARGVScrubber.new [str] @@ -56,6 +60,17 @@ module Rails assert_equal [], args end + def test_default_rc_file_with_xdg_config_home + Dir.mktmpdir do |dir| + rc_file = File.join(dir, "rails/railsrc") + FileUtils.mkdir_p(File.dirname(rc_file)) + FileUtils.touch(rc_file) + switch_env("XDG_CONFIG_HOME", dir) do + assert_equal rc_file, ARGVScrubber.default_rc_file + end + end + end + def test_new_homedir_rc file = Tempfile.new "myrcfile" file.puts "--hello-world"