Class: RokuBuilder::ConfigParser

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options:, config:) ⇒ ConfigParser

Returns a new instance of ConfigParser.



14
15
16
17
18
19
20
# File 'lib/roku_builder/config_parser.rb', line 14

def initialize(options:, config:)
  @logger = Logger.instance
  @options = options
  @config = config
  @parsed = {init_params: {}}
  parse_config
end

Instance Attribute Details

#parsedObject (readonly)

Returns the value of attribute parsed.



7
8
9
# File 'lib/roku_builder/config_parser.rb', line 7

def parsed
  @parsed
end

Class Method Details

.parse(options:, config:) ⇒ Object



9
10
11
12
# File 'lib/roku_builder/config_parser.rb', line 9

def self.parse(options:, config:)
  parser = new(options: options, config: config)
  parser.parsed
end

Instance Method Details

#check_for_workingObject



148
149
150
# File 'lib/roku_builder/config_parser.rb', line 148

def check_for_working
  @parsed[:project][:stage_method] = :working if @options[:working]
end

#current_projectObject



60
61
62
63
64
65
# File 'lib/roku_builder/config_parser.rb', line 60

def current_project
  @config[:projects].each_pair do |key,value|
    return key if is_current_project?(project_config: value)
  end
  nil
end

#file_defined?(type) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/roku_builder/config_parser.rb', line 97

def file_defined?(type)
  @options[type].end_with?(".zip") or @options[type].end_with?(".pkg") or @options[type].end_with?(".jpg")
end

#get_global_key_configObject

Raises:



170
171
172
173
174
175
176
# File 'lib/roku_builder/config_parser.rb', line 170

def get_global_key_config
  raise ParseError, "Unknown Key: #{@parsed[:key]}" unless @config[:keys][@parsed[:key].to_sym]
  @parsed[:key] = @config[:keys][@parsed[:key].to_sym].dup
  if @config[:keys][:key_dir]  and !@parsed[:key][:keyed_pkg].start_with?("./")
    @parsed[:key][:keyed_pkg] = File.join(@config[:keys][:key_dir], @parsed[:key][:keyed_pkg])
  end
end

#get_repo_path(project_config:) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/roku_builder/config_parser.rb', line 75

def get_repo_path(project_config:)
  if @config[:projects][:project_dir] and !Pathname.new(project_config[:directory]).absolute?
    Pathname.new(File.join(@config[:projects][:project_dir], project_config[:directory])).realdirpath
  else
    Pathname.new(project_config[:directory]).realdirpath
  end
end

#get_root_dirObject



188
189
190
191
192
193
# File 'lib/roku_builder/config_parser.rb', line 188

def get_root_dir
  root_dir = @parsed[:project][:directory] if @parsed[:project]
  root_dir = @options[:in] if @options[:in]
  root_dir = Pathname.pwd.to_s if @options[:current]
  root_dir
end

#is_current_project?(project_config:) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
# File 'lib/roku_builder/config_parser.rb', line 67

def is_current_project?(project_config:)
  return false unless project_config.is_a?(Hash)
  repo_path = get_repo_path(project_config: project_config)
  Pathname.pwd.descend do |path_parent|
    return true if path_parent == repo_path
  end
end

#parse_configObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/roku_builder/config_parser.rb', line 22

def parse_config
  process_in_argument
  setup_device
  setup_project
  setup_in_out_file
  setup_project_config
  setup_stage_config
  setup_key_config
  setup_root_dir
  setup_input_mappings
end

#process_in_argumentObject



34
35
36
# File 'lib/roku_builder/config_parser.rb', line 34

def process_in_argument
  @options[:in] = File.expand_path(@options[:in]) if @options[:in]
end

#project_requiredObject



55
56
57
58
# File 'lib/roku_builder/config_parser.rb', line 55

def project_required
  non_project_source = ([:current, :in] & @options.keys).count > 0
  @options.has_source? and not non_project_source
end

#set_default_outfileObject



110
111
112
113
114
# File 'lib/roku_builder/config_parser.rb', line 110

def set_default_outfile
  unless @parsed[:out][:folder]
    @parsed[:out][:folder] = Dir.tmpdir
  end
end

#set_project_directoryObject



139
140
141
142
143
144
145
146
# File 'lib/roku_builder/config_parser.rb', line 139

def set_project_directory
  if @config[:projects][:project_dir]  and !Pathname.new(@parsed[:project][:directory]).absolute?
    @parsed[:project][:directory] = File.join(@config[:projects][:project_dir], @parsed[:project][:directory])
  end
  unless Dir.exist?(@parsed[:project][:directory])
    raise ParseError, "Missing project directory: #{@parsed[:project][:directory]}"
  end
end

#setup_deviceObject

Raises:

  • (ArgumentError)


38
39
40
41
42
# File 'lib/roku_builder/config_parser.rb', line 38

def setup_device
  @options[:device] = @config[:devices][:default] unless @options[:device]
  @parsed[:device_config] = @config[:devices][@options[:device].to_sym]
  raise ArgumentError, "Unknown device: #{@options[:device]}" unless @parsed[:device_config]
end

#setup_file_and_folder(type) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/roku_builder/config_parser.rb', line 101

def setup_file_and_folder(type)
  @parsed[type][:folder], @parsed[type][:file] = Pathname.new(@options[type]).split.map{|p| p.to_s}
  if @parsed[type][:folder] == "." and not @options[type].start_with?(".")
    @parsed[type][:folder] = nil
  else
    @parsed[type][:folder] = File.expand_path(@parsed[type][:folder])
  end
end

#setup_in_out_fileObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/roku_builder/config_parser.rb', line 83

def setup_in_out_file
  [:in, :out].each do |type|
    @parsed[type] = {file: nil, folder: nil}
    if @options[type]
      if file_defined?(type)
        setup_file_and_folder(type)
      elsif @options[type]
        @parsed[type][:folder] = File.expand_path(@options[type])
      end
    end
  end
  set_default_outfile
end

#setup_input_mappingsObject



195
196
197
# File 'lib/roku_builder/config_parser.rb', line 195

def setup_input_mappings
  @parsed[:input_mappings] = @config[:input_mappings]
end

#setup_key_configObject



162
163
164
165
166
167
168
# File 'lib/roku_builder/config_parser.rb', line 162

def setup_key_config
  if @parsed[:stage]
    @parsed[:key] = @parsed[:stage][:key]
    get_global_key_config if @parsed[:key].class == String
    test_key_file
  end
end

#setup_projectObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/roku_builder/config_parser.rb', line 44

def setup_project
  if project_required and not @options[:project]
    project = current_project
    if project
      @options[:project] = project
    else
      @options[:project] = @config[:projects][:default]
    end
  end
end

#setup_project_configObject



116
117
118
119
120
121
122
123
124
125
# File 'lib/roku_builder/config_parser.rb', line 116

def setup_project_config
  if @options[:current]
    stub_project_config_for_current
  elsif  project_required
    @parsed[:project] = @config[:projects][@options[:project].to_sym].dup
    raise ParseError, "Unknown Project: #{@options[:project]}" unless @parsed[:project]
    set_project_directory
    check_for_working
  end
end

#setup_root_dirObject



184
185
186
# File 'lib/roku_builder/config_parser.rb', line 184

def setup_root_dir
  @parsed[:root_dir] = get_root_dir
end

#setup_stage_configObject



153
154
155
156
157
158
159
160
# File 'lib/roku_builder/config_parser.rb', line 153

def setup_stage_config
  if project_required
    stage = @options[:stage].to_sym if @options[:stage]
    stage ||= @parsed[:project][:stages].keys[0].to_sym
    raise ParseError, "Unknown Stage: #{stage}" unless @parsed[:project][:stages][stage]
    @parsed[:stage] = @parsed[:project][:stages][stage]
  end
end

#stub_project_config_for_currentObject

Raises:



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/roku_builder/config_parser.rb', line 127

def stub_project_config_for_current
  pwd =  Pathname.pwd.to_s
  manifest = File.join(pwd, "manifest")
  raise ParseError, "Missing Manifest: #{manifest}" unless File.exist?(manifest)
  @parsed[:project] = {
    directory: pwd,
    folders: nil,
    files: nil,
    stage_method: :current
  }
end

#test_key_fileObject



178
179
180
181
182
# File 'lib/roku_builder/config_parser.rb', line 178

def test_key_file
  if @parsed[:key] and not File.exist?(@parsed[:key][:keyed_pkg])
    raise ParseError, "Bad key file: #{@parsed[:key][:keyed_pkg]}"
  end
end