Class: Frameit::ConfigParser
- Inherits:
-
Object
- Object
- Frameit::ConfigParser
- Defined in:
- lib/frameit/config_parser.rb
Instance Method Summary collapse
-
#change_paths_to_absolutes!(values) ⇒ Object
Use absolute paths instead of relative.
-
#fetch_value(path) ⇒ Object
Fetches the finished configuration for a given path.
- #load(path) ⇒ Object
- #parse(data) ⇒ Object
-
#validate_values(values) ⇒ Object
Make sure the paths/colors are valid.
Instance Method Details
#change_paths_to_absolutes!(values) ⇒ Object
Use absolute paths instead of relative
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/frameit/config_parser.rb', line 38 def change_paths_to_absolutes!(values) values.each do |key, value| if value.kind_of? Hash change_paths_to_absolutes!(value) # recursive call elsif value.kind_of? Array value.each do |current| change_paths_to_absolutes!(current) if current.kind_of? Hash # recursive call end else if ['font', 'background'].include? key # Change the paths to relative ones # `replace`: to change the content of the string, so it's actually stored if @path # where is the config file. We don't have a config file in tests containing_folder = File.('..', @path) value.replace File.join(containing_folder, value) end end end end end |
#fetch_value(path) ⇒ Object
Fetches the finished configuration for a given path. This will try to look for a specific value and fallback to a default value if nothing was found
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/frameit/config_parser.rb', line 24 def fetch_value(path) specific = @data['data'].find { |a| path.include? a['filter'] } default = @data['default'] values = default.fastlane_deep_merge(specific || {}) change_paths_to_absolutes!(values) validate_values(values) values end |
#load(path) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/frameit/config_parser.rb', line 3 def load(path) return nil unless File.exist?(path) # we are okay with no config at all UI. "Parsing config file '#{path}'" if $verbose @path = path self.parse(File.read(path)) end |
#parse(data) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/frameit/config_parser.rb', line 11 def parse(data) begin @data = JSON.parse(data) rescue => ex UI.error ex. UI.user_error! "Invalid JSON file at path '#{@path}'. Make sure it's a valid JSON file" end self end |
#validate_values(values) ⇒ Object
Make sure the paths/colors are valid
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/frameit/config_parser.rb', line 60 def validate_values(values) values.each do |key, value| if value.kind_of? Hash validate_values(value) # recursive call else if key == 'font' UI.user_error! "Could not find font at path '#{File.(value)}'" unless File.exist? value end if key == 'fonts' UI.user_error! "`fonts` must be an array" unless value.kind_of? Array value.each do |current| UI.user_error! "You must specify a font path" if current.fetch('font', '').length == 0 UI.user_error! "Could not find font at path '#{File.(current.fetch('font'))}'" unless File.exist? current.fetch('font') UI.user_error! "`supported` must be an array" unless current.fetch('supported', []).kind_of? Array end end if key == 'background' UI.user_error! "Could not find background image at path '#{File.(value)}'" unless File.exist? value end if key == 'color' UI.user_error! "Invalid color '#{value}'. Must be valid Hex #123123" unless value.include? "#" end if key == 'padding' UI.user_error! "padding must be type integer or pair of integers of format 'AxB'" unless value.kind_of?(Integer) || value.split('x').length == 2 end end end end |