gemfile_helper.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. class GemfileHelper
  2. class << self
  3. def rails_env
  4. ENV['RAILS_ENV'] ||
  5. case File.basename($0)
  6. when 'rspec'
  7. 'test'
  8. when 'rake'
  9. 'test' if ARGV.any?(/\Aspec(?:\z|:)/)
  10. end || 'development'
  11. end
  12. def load_dotenv
  13. root = Pathname.new(__dir__).parent
  14. dotenv_dir = (root / 'vendor/gems').glob('dotenv-[0-9]*').last
  15. yield dotenv_dir.to_s if block_given?
  16. return if ENV['ON_HEROKU'] == 'true'
  17. $:.unshift dotenv_dir.join('lib').to_s
  18. require "dotenv"
  19. $:.shift
  20. sanity_check Dotenv.load(
  21. root.join(".env.local"),
  22. root.join(".env.#{rails_env}"),
  23. root.join(".env")
  24. )
  25. end
  26. GEM_NAME = /[A-Za-z0-9.\-_]+/
  27. GEM_OPTIONS = /(.+?)\s*(?:,\s*(.+?))?/
  28. GEM_SEPARATOR = /\s*(?:,|\z)/
  29. GEM_REGULAR_EXPRESSION = /(#{GEM_NAME})(?:\(#{GEM_OPTIONS}\))?#{GEM_SEPARATOR}/
  30. def parse_each_agent_gem(string)
  31. return unless string
  32. string.scan(GEM_REGULAR_EXPRESSION).each do |name, version, args|
  33. if version =~ /\w+:/
  34. args = "#{version},#{args}"
  35. version = nil
  36. end
  37. yield [name, version, parse_gem_args(args)].compact
  38. end
  39. end
  40. private
  41. def parse_gem_args(args)
  42. return nil unless args
  43. args.scan(/(\w+):\s*(.+?)#{GEM_SEPARATOR}/).to_h { |key, value|
  44. [key.to_sym, value]
  45. }
  46. end
  47. def sanity_check(env)
  48. return if ENV['CI'] == 'true' || ENV['APP_SECRET_TOKEN'] || !env.empty?
  49. # .env is not necessary in bundle update/lock; this helps Renovate
  50. return if (File.basename($0) in 'bundle' | 'bundler') && (ARGV.first in 'lock' | 'update')
  51. puts warning
  52. require "shellwords"
  53. puts "command: #{[$0, *ARGV].shelljoin}"
  54. raise "Could not load huginn settings from .env file."
  55. end
  56. def warning
  57. <<~EOF
  58. Could not load huginn settings from .env file.
  59. Make sure to copy the .env.example to .env and change it to match your configuration.
  60. Capistrano 2 users: Make sure shared files are symlinked before bundle runs: before 'bundle:install', 'deploy:symlink_configs'
  61. EOF
  62. end
  63. end
  64. end