lib/fontcustom/options.rb in fontcustom-1.3.0.beta vs lib/fontcustom/options.rb in fontcustom-1.3.0.beta2

- old
+ new

@@ -17,10 +17,11 @@ overwrite_examples set_config_path load_config merge_options clean_font_name + clean_css_selector set_manifest_path set_input_paths set_output_paths set_template_paths end @@ -90,10 +91,18 @@ def clean_font_name @options[:font_name] = @options[:font_name].strip.gsub(/\W/, "-") end + def clean_css_selector + unless @options[:css_selector].include? "{{glyph}}" + raise Fontcustom::Error, + "CSS selector `#{@options[:css_selector]}` should contain the \"{{glyph}}\" placeholder." + end + @options[:css_selector] = @options[:css_selector].strip.gsub(/[^\{\}\w]/, "-") + end + def set_manifest_path @options[:manifest] = if ! @options[:manifest].nil? expand_path @options[:manifest] elsif @options[:config] File.join File.dirname(@options[:config]), ".fontcustom-manifest.json" @@ -105,39 +114,25 @@ def set_input_paths if @options[:input].is_a? Hash @options[:input] = symbolize_hash(@options[:input]) if @options[:input].has_key? :vectors @options[:input][:vectors] = expand_path @options[:input][:vectors] - unless File.directory? @options[:input][:vectors] - raise Fontcustom::Error, - "INPUT[:vectors] (`#{relative_to_root(@options[:input][:vectors])}`) should be "\ - "a directory. Check `#{relative_to_root(@options[:config])}` or your CLI options." - end + check_input @options[:input][:vectors] else - raise Fontcustom::Error, - "INPUT should have a :vectors key. Check `#{relative_to_root(@options[:config])}` "\ - "or your CLI options." + raise Fontcustom::Error, + "Input paths (assigned as a hash) should have a :vectors key. Check your options." end if @options[:input].has_key? :templates @options[:input][:templates] = expand_path @options[:input][:templates] - unless File.directory? @options[:input][:templates] - raise Fontcustom::Error, - "INPUT[:templates] (`#{relative_to_root(@options[:input][:templates])}`) "\ - "should be a directory. Check `#{relative_to_root(@options[:config])}` or "\ - "your CLI options." - end + check_input @options[:input][:templates] else @options[:input][:templates] = @options[:input][:vectors] end else input = @options[:input] ? expand_path(@options[:input]) : @options[:project_root] - unless File.directory? input - raise Fontcustom::Error, - "INPUT (`#{relative_to_root(input)}`) should be a directory. Check "\ - "`#{relative_to_root(@options[:config])}` or your CLI options." - end + check_input input @options[:input] = { :vectors => input, :templates => input } end if Dir[File.join(@options[:input][:vectors], "*.svg")].empty? raise Fontcustom::Error, "`#{relative_to_root(@options[:input][:vectors])}` doesn't contain any SVGs." @@ -146,33 +141,30 @@ def set_output_paths if @options[:output].is_a? Hash @options[:output] = symbolize_hash(@options[:output]) unless @options[:output].has_key? :fonts - raise Fontcustom::Error, - "OUTPUT should have a :fonts key. Check `#{relative_to_root(@options[:config])}` "\ - "or your CLI options." + raise Fontcustom::Error, + "Output paths (assigned as a hash) should have a :fonts key. Check your options." end @options[:output].each do |key, val| @options[:output][key] = expand_path val if File.exists?(val) && ! File.directory?(val) - raise Fontcustom::Error, - "OUTPUT[:#{key.to_s}] (`#{relative_to_root(@options[:output][key])}`) should be "\ - "a directory. Check `#{relative_to_root(@options[:config])}` or your CLI options." + raise Fontcustom::Error, + "Output `#{relative_to_root(@options[:output][key])}` exists but isn't a directory. Check your options." end end @options[:output][:css] ||= @options[:output][:fonts] @options[:output][:preview] ||= @options[:output][:fonts] else if @options[:output].is_a? String output = expand_path @options[:output] if File.exists?(output) && ! File.directory?(output) - raise Fontcustom::Error, - "OUTPUT (`#{relative_to_root(output)}`) should be a directory. Check "\ - "`#{relative_to_root(@options[:config])}` or your CLI options." + raise Fontcustom::Error, + "Output `#{relative_to_root(output)}` exists but isn't a directory. Check your options." end else output = File.join @options[:project_root], @options[:font_name] say_message :debug, "Generated files will be saved to `#{relative_to_root(output)}/`." if @options[:debug] end @@ -212,16 +204,24 @@ when "bootstrap-ie7-scss" File.join template_path, "_fontcustom-bootstrap-ie7.scss" else template = File.expand_path File.join(@options[:input][:templates], template) unless template[0] == "/" unless File.exists? template - config = @options[:config] ? " `#{relative_to_root(@options[:config])}` or" : "" raise Fontcustom::Error, - "Custom template `#{relative_to_root(template)}` doesn't exist. "\ - "Check#{config} your CLI options." + "Custom template `#{relative_to_root(template)}` doesn't exist. Check your options." end template end + end + end + + def check_input(dir) + if ! File.exists? dir + raise Fontcustom::Error, + "Input `#{relative_to_root(dir)}` doesn't exist. Check your options." + elsif ! File.directory? dir + raise Fontcustom::Error, + "Input `#{relative_to_root(dir)}` isn't a directory. Check your options." end end end end