1
0

gemfile_helper.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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? { |arg| /\Aspec(?:\z|:)/ === arg }
  10. end || 'development'
  11. end
  12. def load_dotenv
  13. dotenv_dir = Dir[File.join(File.dirname(__FILE__), '../vendor/gems/dotenv-[0-9]*')].sort.last
  14. yield dotenv_dir if block_given?
  15. return if ENV['ON_HEROKU'] == 'true'
  16. $:.unshift File.join(dotenv_dir, 'lib')
  17. require "dotenv"
  18. $:.shift
  19. root = Pathname.new(File.join(File.dirname(__FILE__), '..'))
  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\.\-\_]+'.freeze
  27. GEM_OPTIONS = '(.+?)\s*(?:,\s*(.+?)){0,1}'.freeze
  28. GEM_SEPARATOR = '\s*(?:,|\z)'.freeze
  29. GEM_REGULAR_EXPRESSION = /(#{GEM_NAME})(?:\(#{GEM_OPTIONS}\)){0,1}#{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. options = {}
  44. args.scan(/(\w+):\s*(.+?)#{GEM_SEPARATOR}/).each do |key, value|
  45. options[key.to_sym] = value
  46. end
  47. options
  48. end
  49. def sanity_check(env)
  50. return if ENV['CI'] == 'true' || !env.empty?
  51. puts warning
  52. raise "Could not load huginn settings from .env file."
  53. end
  54. def warning
  55. <<-EOF
  56. Could not load huginn settings from .env file.
  57. Make sure to copy the .env.example to .env and change it to match your configuration.
  58. Capistrano 2 users: Make sure shared files are symlinked before bundle runs: before 'bundle:install', 'deploy:symlink_configs'
  59. EOF
  60. end
  61. end
  62. end