Class: RokuBuilder::ManifestInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/roku_builder/plugins/manifest_inspector.rb

Instance Method Summary collapse

Constructor Details

#initialize(config:, dir:, raf:) ⇒ ManifestInspector

Returns a new instance of ManifestInspector.



6
7
8
9
10
# File 'lib/roku_builder/plugins/manifest_inspector.rb', line 6

def initialize(config:, dir:, raf:)
  @config = config
  @dir = dir
  @raf_inspector = raf
end

Instance Method Details

#run(inspector_config) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/roku_builder/plugins/manifest_inspector.rb', line 12

def run(inspector_config)
  @warnings = []
  @attributes = {}
  @line_numbers = {}
  @inspector_config = inspector_config
  manifest = File.join(@config.root_dir, "manifest")
  unless File.exist?(manifest)
    add_warning(warning: :packageManifestFile)
  else
    File.open(manifest) do |file|
      current_line = 0
      file.readlines.each do |line|
        current_line += 1
        parts = line.split("=")
        key = parts.shift.to_sym
        if @attributes[key]
          add_warning(warning: :manifestDuplicateAttribute, key: key, line: current_line)
        else
          value = parts.join("=").chomp
          if !value or value == ""
            add_warning(warning: :manifestEmptyValue, key: key, line: current_line)
          else
            @attributes[key] = value
            @line_numbers[key] = current_line
          end
        end
      end
    end
    manifest_attributes.each_pair do |key, attribute_config|
      if @attributes[key]
        if attribute_config[:deprecated]
          add_warning(warning: :manifestDeprecatedAttribute, key: key)
        end
        if attribute_config[:validations]
          attribute_config[:validations].each_pair do |type, value|
            case type
            when :integer
              unless "%0#{@attributes[key].length}i" % @attributes[key].to_i == @attributes[key]
                add_warning(warning: :manifestInvalidValue, key: key)
              end
            when :hex
              unless @attributes[key][0] == "#" and !@attributes[key][1..-1][/\H/]
                add_warning(warning: :manifestInvalidValue, key: key)
              end
            when :float
              unless @attributes[key].to_f.to_s == @attributes[key]
                add_warning(warning: :manifestInvalidValue, key: key)
              end
            when :boolean
              unless ["false", "true"].include? @attributes[key]
                add_warning(warning: :manifestInvalidValue, key: key)
              end
            when :non_negative
              unless @attributes[key].to_f >= 0
                add_warning(warning: :manifestInvalidValue, key: key)
              end
            when :not_equal
              if value.include? @attributes[key]
                add_warning(warning: :manifestInvalidValue, key: key)
              end
            when :equals
              unless value.include? @attributes[key]
                add_warning(warning: :manifestInvalidValue, key: key)
              end
            when :starts_with
              unless @attributes[key].start_with? value
                add_warning(warning: :manifestInvalidValue, key: key)
              end
            else
              raise ImplementationError, "Unknown Validation #{type}"
            end
          end
        end
        if attribute_config[:notify]
          attribute_config[:notify].each do |regexp|
            if /#{regexp}/ =~ @attributes[key]
              add_warning(warning: :manifestHasValue, key: key)
              break
            end
          end
        end
        if attribute_config[:isResource]
          path = File.join(@dir, @attributes[key].gsub("pkg:/", ""))
          unless File.exist?(path)
            mapping = {"{0}": @attributes[key], "{1}": key }
            add_warning(warning: :manifestMissingFile, key: key, mapping: mapping)
          else
            if attribute_config[:resolution]
              size = ImageSize.path(path).size
              target = ImageSize::Size.new(attribute_config[:resolution])
              unless size == target
                mapping = {
                  "{0}": @attributes[key],
                  "{1}": key,
                  "{2}": size,
                  "{3}": target
                }
                add_warning(warning: :manifestIncorrectImageResolution, key: key, mapping: mapping)
              end
            end
          end
        end
      elsif attribute_config[:required]
        add_warning(warning: :manifestMissingAttribute, key: key)
      end
      @raf_inspector.inspect_manifest(attributes: @attributes, line_numbers: @line_numbers)
    end
  end
  @warnings
end