diaspora/config/bundler_helper.rb
Benjamin Neff 9dfce77a4d
Remove workaround required to load YAML with ruby 2.7 and 3.1
This works now with ruby >= 3.0
2023-06-12 02:51:41 +02:00

42 lines
1.3 KiB
Ruby

# frozen_string_literal: true
require "yaml"
module BundlerHelper
def self.rails_env
@rails_env ||= ENV["RAILS_ENV"] ||
parse_value_from_toml_file("diaspora.toml", "rails_environment") ||
parse_value_from_yaml_file("diaspora.yml", "configuration", "server", "rails_environment") ||
parse_value_from_yaml_file("defaults.yml", "defaults", "server", "rails_environment")
end
def self.database
@adapter ||= parse_value_from_yaml_file("database.yml", rails_env, "adapter")
abort "No database adapter found, please fix your config/database.yml!" unless @adapter
@adapter.sub("mysql2", "mysql")
end
private_class_method def self.parse_value_from_yaml_file(file, *keys)
parse_yaml_file(file).dig(*keys)
end
private_class_method def self.parse_yaml_file(file)
path = File.join(__dir__, file)
return {} unless File.file?(path)
YAML.safe_load_file(path, aliases: true)
end
private_class_method def self.parse_value_from_toml_file(file, key)
path = File.join(__dir__, file)
if File.file?(path)
File.read(path)[/^\s*#{Regexp.escape(key)}\s*=\s*["']([^"']+)["']\s*$/, 1]
elsif !File.file? File.join(__dir__, "diaspora.yml")
warn "WARNING: Configuration file #{path} not found, ensure it's present" # rubocop:disable Rails/Output
end
end
end