Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds 'Imports' functionality #486

Merged
merged 3 commits into from
Nov 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/wraith/helpers/utilities.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
29 changes: 25 additions & 4 deletions lib/wraith/wraith.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"]
Expand Down
10 changes: 10 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/configs/test_config--imports.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
imports: 'test_config--phantom.yaml'

screen_widths:
- 1337