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



168
169
170
# File 'lib/roku_builder/config_parser.rb', line 168

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

#convert_to_source_filesObject



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

def convert_to_source_files
  unless @parsed[:project][:source_files]
    @parsed[:project][:source_files] = @parsed[:project][:files] + @parsed[:project][:folders]
    @parsed[:project].delete(:files)
    @parsed[:project].delete(:folders)
  end
end

#current_projectObject



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

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)


109
110
111
# File 'lib/roku_builder/config_parser.rb', line 109

def file_defined?(type)
  user_file(type).end_with?(".zip") or user_file(type).end_with?(".pkg") or user_file(type).end_with?(".jpg")
end

#get_global_key_configObject

Raises:



192
193
194
195
196
197
198
199
# File 'lib/roku_builder/config_parser.rb', line 192

def get_global_key_config
  raise ParseError, "No Keys Configured" unless @config[:keys]
  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



81
82
83
84
85
86
87
# File 'lib/roku_builder/config_parser.rb', line 81

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



211
212
213
214
215
216
# File 'lib/roku_builder/config_parser.rb', line 211

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)


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

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
33
34
# File 'lib/roku_builder/config_parser.rb', line 22

def parse_config
  process_in_argument
  setup_devices
  setup_api_keys
  setup_project
  setup_in_out_file
  setup_project_config
  setup_stage_config
  setup_key_config
  setup_root_dir
  setup_input_mappings
  setup_console_log
end

#process_in_argumentObject



36
37
38
# File 'lib/roku_builder/config_parser.rb', line 36

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

#project_requiredObject



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

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

#set_default_outfileObject



122
123
124
125
126
# File 'lib/roku_builder/config_parser.rb', line 122

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

#set_project_directoryObject



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

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_api_keysObject



45
46
47
# File 'lib/roku_builder/config_parser.rb', line 45

def setup_api_keys
  @parsed[:api_keys] = @config[:api_keys]
end

#setup_console_logObject



222
223
224
# File 'lib/roku_builder/config_parser.rb', line 222

def setup_console_log
  @parsed[:console_log] = @config[:console_log]
end

#setup_devicesObject



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

def setup_devices
  @parsed[:device_default] = @config[:devices][:default]
  @parsed[:devices] = @config[:devices].select{|key, value| :default != key}
end

#setup_file_and_folder(type) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/roku_builder/config_parser.rb', line 113

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

#setup_in_out_fileObject



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/roku_builder/config_parser.rb', line 89

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

#setup_input_mappingsObject



218
219
220
# File 'lib/roku_builder/config_parser.rb', line 218

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

#setup_key_configObject



182
183
184
185
186
187
188
189
190
# File 'lib/roku_builder/config_parser.rb', line 182

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

#setup_projectObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/roku_builder/config_parser.rb', line 49

def setup_project
  if project_required and not @options[:project]
    project = current_project
    if project
      @options[:project] = project
    elsif @config[:projects][:default]
      @options[:project] = @config[:projects][:default]
    end
    raise ArgumentError, "A project is required for that command" unless @options[:project]
  end
end

#setup_project_configObject



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

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
    convert_to_source_files
    check_for_working
  end
end

#setup_root_dirObject



207
208
209
# File 'lib/roku_builder/config_parser.rb', line 207

def setup_root_dir
  @parsed[:root_dir] = get_root_dir
end

#setup_stage_configObject



173
174
175
176
177
178
179
180
# File 'lib/roku_builder/config_parser.rb', line 173

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:



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

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,
    source_files: nil,
    stage_method: :current
  }
end

#test_key_fileObject



201
202
203
204
205
# File 'lib/roku_builder/config_parser.rb', line 201

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

#user_file(type) ⇒ Object



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

def user_file(type)
  file = @options[type]
  file ||= @config[type]
  file
end