Module: XCAssetsCop::Linter
- Defined in:
- lib/xcassetscop/linter.rb
Class Method Summary collapse
- .file_name_matches_asset_name(contents_json, file_path) ⇒ Object
- .get_file_extension(contents_json) ⇒ Object
- .get_file_name(contents_json) ⇒ Object
- .lint_file(file_path, config) ⇒ Object
- .lint_files(rules) ⇒ Object
- .validate_file_extension(contents_json, expected) ⇒ Object
- .validate_image_scale(contents_json, expected) ⇒ Object
- .validate_preserves_vector_representation(contents_json, expected) ⇒ Object
- .validate_template_rendering_intent(contents_json, expected) ⇒ Object
Class Method Details
.file_name_matches_asset_name(contents_json, file_path) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/xcassetscop/linter.rb', line 26 def self.file_name_matches_asset_name(contents_json, file_path) asset_name = file_path.split('/').select { |str| str.include? '.imageset' }.first.split('.').first file_name = get_file_name(contents_json)&.split('.')&.first return [] if file_name == asset_name ["Expected asset name and file name to be the same, got:\nAsset name: #{asset_name}\nFile name: #{file_name}"] end |
.get_file_extension(contents_json) ⇒ Object
14 15 16 |
# File 'lib/xcassetscop/linter.rb', line 14 def self.get_file_extension(contents_json) get_file_name(contents_json).split('.').last end |
.get_file_name(contents_json) ⇒ Object
10 11 12 |
# File 'lib/xcassetscop/linter.rb', line 10 def self.get_file_name(contents_json) contents_json&.dig('images')&.first&.dig('filename') end |
.lint_file(file_path, config) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/xcassetscop/linter.rb', line 71 def self.lint_file(file_path, config) file = File.read file_path contents_json = JSON.parse file template_rendering_intent = config.template_rendering_intent image_scale = config.image_scale same_file_and_asset_name = config.same_file_and_asset_name file_extension = config.file_extension preserves_vector_representation = config.preserves_vector_representation errors = [] errors += validate_template_rendering_intent(contents_json, template_rendering_intent.to_sym) if template_rendering_intent errors += validate_image_scale(contents_json, image_scale.to_sym) if image_scale errors += validate_preserves_vector_representation(contents_json, file_path) if preserves_vector_representation errors += validate_file_extension(contents_json, file_extension.to_sym) if file_extension errors += file_name_matches_asset_name(contents_json, file_path) if same_file_and_asset_name errors end |
.lint_files(rules) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/xcassetscop/linter.rb', line 92 def self.lint_files(rules) errors = [] rules.each do |rule| errors += rule.paths.map { |path| lint_file(path, rule.config) }.flatten end errors end |
.validate_file_extension(contents_json, expected) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/xcassetscop/linter.rb', line 18 def self.validate_file_extension(contents_json, expected) file_extension = get_file_extension(contents_json) return [] if expected.to_sym == file_extension.to_sym file_name = get_file_name(contents_json) ["Expected #{file_name} type to be #{expected}, got #{file_extension} instead"] end |
.validate_image_scale(contents_json, expected) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/xcassetscop/linter.rb', line 34 def self.validate_image_scale(contents_json, expected) case contents_json&.dig('images')&.size when 1 image_scale = :single when 3 image_scale = :individual when 4 image_scale = :individual_and_single else raise StandardError, "Couldn't figure out the image scale" end return [] if image_scale == expected file_name = get_file_name contents_json ["Expected #{file_name} scale to be '#{expected}', got '#{image_scale}' instead"] end |
.validate_preserves_vector_representation(contents_json, expected) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/xcassetscop/linter.rb', line 59 def self.validate_preserves_vector_representation(contents_json, expected) preserves_vector_representation = contents_json&.dig('properties')&.dig('preserves-vector-representation') || false file_extension = get_file_extension(contents_json) file_name = get_file_name(contents_json) return ["#{file_name} should be a PDF file if you want to preserve vector data"] if (file_extension.to_sym != :pdf) && expected return ["Expected #{file_name} to#{' NOT' unless expected} preserve vector representation"] unless preserves_vector_representation == expected [] end |
.validate_template_rendering_intent(contents_json, expected) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/xcassetscop/linter.rb', line 51 def self.validate_template_rendering_intent(contents_json, expected) template_rendering_intent = contents_json&.dig('properties')&.sdig('template-rendering-intent')&.to_sym || :default return [] if template_rendering_intent == expected file_name = get_file_name contents_json ["Expected #{file_name} to be rendered as '#{expected}', got '#{template_rendering_intent}' instead"] end |