diff --git a/lib/wraith/helpers/utilities.rb b/lib/wraith/helpers/utilities.rb index e98e1aa0..2774de78 100644 --- a/lib/wraith/helpers/utilities.rb +++ b/lib/wraith/helpers/utilities.rb @@ -1,5 +1,11 @@ require "wraith/helpers/custom_exceptions" +def absolute_path_of_dir(filepath) + path_parts = filepath.split('/') + path_to_dir = path_parts.first path_parts.size - 1 + path_to_dir.join('/') +end + def convert_to_absolute(filepath) if !filepath "false" diff --git a/lib/wraith/wraith.rb b/lib/wraith/wraith.rb index a689dbf3..47d8f304 100644 --- a/lib/wraith/wraith.rb +++ b/lib/wraith/wraith.rb @@ -7,11 +7,21 @@ class Wraith::Wraith attr_accessor :config def initialize(config, yaml_passed = false) - @config = yaml_passed ? config : open_config_file(config) + if yaml_passed + @config = config + else + filepath = determine_config_path config + @config = YAML.load_file filepath + end + + if @config['imports'] + @config = apply_imported_config(@config['imports'], @config) + end + logger.level = verbose ? Logger::DEBUG : Logger::INFO end - def open_config_file(config_name) + def determine_config_path(config_name) possible_filenames = [ config_name, "#{config_name}.yml", @@ -22,13 +32,24 @@ def open_config_file(config_name) possible_filenames.each do |filepath| if File.exist?(filepath) - config = File.open filepath - return YAML.load config + @config_dir = absolute_path_of_dir(convert_to_absolute filepath) + return convert_to_absolute filepath end end + fail ConfigFileDoesNotExistError, "unable to find config \"#{config_name}\"" end + def apply_imported_config(config_to_import, config) + path_to_config = "#{@config_dir}/#{config_to_import}" + if File.exist?(path_to_config) + yaml = YAML.load_file path_to_config + return yaml.merge(config) + end + + fail ConfigFileDoesNotExistError, "unable to find referenced imported config \"#{config_name}\"" + end + def directory # Legacy support for those using array configs @config["directory"].is_a?(Array) ? @config["directory"].first : @config["directory"] diff --git a/spec/config_spec.rb b/spec/config_spec.rb index edbb8139..f49176dc 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -16,6 +16,16 @@ it "contains shot options" do expect(wraith.config).to include "directory" => "shots" end + + it "should be able to import other configs" do + config_name = get_path_relative_to __FILE__, "./configs/test_config--imports.yaml" + wraith = Wraith::Wraith.new(config_name) + + # retain the imported config settings + expect(wraith.paths).to eq("home" => "/", "uk_index" => "/uk") + # ...but override the imported config in places + expect(wraith.widths).to eq [1337] + end end describe "When creating a wraith worker" do diff --git a/spec/configs/test_config--imports.yaml b/spec/configs/test_config--imports.yaml new file mode 100644 index 00000000..d5118f90 --- /dev/null +++ b/spec/configs/test_config--imports.yaml @@ -0,0 +1,4 @@ +imports: 'test_config--phantom.yaml' + +screen_widths: + - 1337