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



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

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

#convert_to_source_filesObject



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

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



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

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)


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

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:



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

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



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

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



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

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)


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

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
35
# 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
  setup_deeplinks
end

#process_in_argumentObject



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

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

#project_requiredObject



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

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

#set_default_outfileObject



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

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

#set_project_directoryObject



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

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



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

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

#setup_console_logObject



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

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


227
228
229
# File 'lib/roku_builder/config_parser.rb', line 227

def setup_deeplinks
  @parsed[:deeplinks] = @config[:deeplinks]
end

#setup_devicesObject



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

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



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

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



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

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



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

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

#setup_key_configObject



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

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



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

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



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

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



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

def setup_root_dir
  @parsed[:root_dir] = get_root_dir
end

#setup_stage_configObject



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

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:



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

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



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

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



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

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