瀏覽代碼

Refactor gemfile_helper by using the latest Ruby features

Akinori MUSHA 10 月之前
父節點
當前提交
85451b6540
共有 1 個文件被更改,包括 24 次插入23 次删除
  1. 24 23
      lib/gemfile_helper.rb

+ 24 - 23
lib/gemfile_helper.rb

@@ -6,36 +6,37 @@ class GemfileHelper
         when 'rspec'
           'test'
         when 'rake'
-          'test' if ARGV.any? { |arg| /\Aspec(?:\z|:)/ === arg }
+          'test' if ARGV.any?(/\Aspec(?:\z|:)/)
         end || 'development'
     end
 
     def load_dotenv
-      dotenv_dir = Dir[File.join(File.dirname(__FILE__), '../vendor/gems/dotenv-[0-9]*')].sort.last
+      root = Pathname.new(__dir__).parent
+      dotenv_dir = (root / 'vendor/gems').glob('dotenv-[0-9]*').last
 
-      yield dotenv_dir if block_given?
+      yield dotenv_dir.to_s if block_given?
 
       return if ENV['ON_HEROKU'] == 'true'
 
-      $:.unshift File.join(dotenv_dir, 'lib')
+      $:.unshift dotenv_dir.join('lib').to_s
       require "dotenv"
       $:.shift
 
-      root = Pathname.new(File.join(File.dirname(__FILE__), '..'))
       sanity_check Dotenv.load(
-                                root.join(".env.local"),
-                                root.join(".env.#{rails_env}"),
-                                root.join(".env")
-                              )
+        root.join(".env.local"),
+        root.join(".env.#{rails_env}"),
+        root.join(".env")
+      )
     end
 
-    GEM_NAME = '[A-Za-z0-9\.\-\_]+'.freeze
-    GEM_OPTIONS = '(.+?)\s*(?:,\s*(.+?)){0,1}'.freeze
-    GEM_SEPARATOR = '\s*(?:,|\z)'.freeze
-    GEM_REGULAR_EXPRESSION = /(#{GEM_NAME})(?:\(#{GEM_OPTIONS}\)){0,1}#{GEM_SEPARATOR}/
+    GEM_NAME = /[A-Za-z0-9.\-_]+/
+    GEM_OPTIONS = /(.+?)\s*(?:,\s*(.+?))?/
+    GEM_SEPARATOR = /\s*(?:,|\z)/
+    GEM_REGULAR_EXPRESSION = /(#{GEM_NAME})(?:\(#{GEM_OPTIONS}\))?#{GEM_SEPARATOR}/
 
     def parse_each_agent_gem(string)
       return unless string
+
       string.scan(GEM_REGULAR_EXPRESSION).each do |name, version, args|
         if version =~ /\w+:/
           args = "#{version},#{args}"
@@ -49,17 +50,17 @@ class GemfileHelper
 
     def parse_gem_args(args)
       return nil unless args
-      options = {}
-      args.scan(/(\w+):\s*(.+?)#{GEM_SEPARATOR}/).each do |key, value|
-        options[key.to_sym] = value
-      end
-      options
+
+      args.scan(/(\w+):\s*(.+?)#{GEM_SEPARATOR}/).to_h { |key, value|
+        [key.to_sym, value]
+      }
     end
 
     def sanity_check(env)
       return if ENV['CI'] == 'true' || ENV['APP_SECRET_TOKEN'] || !env.empty?
       # .env is not necessary in bundle update/lock; this helps Renovate
       return if (File.basename($0) in 'bundle' | 'bundler') && (ARGV.first in 'lock' | 'update')
+
       puts warning
       require "shellwords"
       puts "command: #{[$0, *ARGV].shelljoin}"
@@ -67,13 +68,13 @@ class GemfileHelper
     end
 
     def warning
-      <<-EOF
-Could not load huginn settings from .env file.
+      <<~EOF
+        Could not load huginn settings from .env file.
 
-Make sure to copy the .env.example to .env and change it to match your configuration.
+        Make sure to copy the .env.example to .env and change it to match your configuration.
 
-Capistrano 2 users: Make sure shared files are symlinked before bundle runs: before 'bundle:install', 'deploy:symlink_configs'
-EOF
+        Capistrano 2 users: Make sure shared files are symlinked before bundle runs: before 'bundle:install', 'deploy:symlink_configs'
+      EOF
     end
   end
 end