Class: RokuBuilder::ConfigManager

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

Overview

Load and validate config files.

Class Method Summary collapse

Class Method Details

.edit_config(config:, options:, logger:) ⇒ Boolean

Edit the roku config

Parameters:

  • config (String)

    path for the roku config

  • options (String)

    options to set in the config

  • device (String)

    which device to use

  • project (String)

    which project to use

  • stage (String)

    which stage to use

Returns:

  • (Boolean)

    success



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/roku_builder/config_manager.rb', line 71

def self.edit_config(config:, options:, logger:)
  config_object = get_config(config: config, logger: logger)
  return false unless config_object
  project = options[:project].to_sym if options[:project]
  project = config_object[:projects][:default] unless options[:project]
  device = options[:device].to_sym if options[:device]
  device = config_object[:devices][:default] unless options[:device]
  stage = options[:stage].to_sym if options[:stage]
  stage = :production unless options[:stage]
  state = {
    project: project,
    device: device,
    stage: stage
  }
  apply_options(config_object: config_object, options: options[:edit_params], state: state)
  config_string = JSON.pretty_generate(config_object)
  file = File.open(config, "w")
  file.write(config_string)
  file.close
  return true
end

.get_config(config:, logger:) ⇒ Hash

Loads the roku config from file

Parameters:

  • config (String)

    path for the roku config

Returns:

  • (Hash)

    roku config object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/roku_builder/config_manager.rb', line 45

def self.get_config(config:, logger:)
  begin
    config = JSON.parse(File.open(config).read, {symbolize_names: true})
    config[:devices][:default] = config[:devices][:default].to_sym
    config[:projects][:default] = config[:projects][:default].to_sym
    config[:projects].each_pair do |key,value|
      next if key == :default
      if value[:stage_method]
        value[:stage_method] = value[:stage_method].to_sym
      end
    end
    config
  rescue JSON::ParserError
    logger.fatal "Config file is not valid JSON"
    nil
  end
end

.load_config(options:, logger:) ⇒ Integer, Hash

Load config file and generate intermeidate configs

Parameters:

  • options (Hash)

    The options hash

  • logger (Logger)

    system logger

Returns:

  • (Integer)

    Return code

  • (Hash)

    Loaded config

  • (Hash)

    Intermeidate configs



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
# File 'lib/roku_builder/config_manager.rb', line 12

def self.load_config(options:, logger:)
  config_file = File.expand_path(options[:config])
  return MISSING_CONFIG unless File.exist?(config_file)
  code = SUCCESS
  config = ConfigManager.get_config(config: config_file, logger: logger)
  return INVALID_CONFIG unless config
  codes = ConfigValidator.validate_config(config: config)
  fatal = false
  warning = false
  codes.each {|a_code|
    if a_code > 0
      logger.fatal "Invalid Config: "+ ConfigValidator.error_codes()[a_code]
      fatal = true
    elsif a_code < 0
      logger.warn "Depricated Config: "+ ConfigValidator.error_codes()[a_code]
      warning = true
    elsif a_code == 0 and options[:validate]
      logger.info "Config Valid"
    end
  }
  return [INVALID_CONFIG, nil, nil] if fatal
  code = DEPRICATED_CONFIG if warning

  parse_code, configs = ConfigParser.parse_config(options: options, config: config, logger: logger)
  unless parse_code == SUCCESS
    return [parse_code, nil, nil]
  end
  [code, config, configs]
end

.update_configs(configs:, options:) ⇒ Hash

Update the intermeidate configs

Parameters:

  • configs (Hash)

    Intermeidate configs hash

  • options (Hash)

    Options hash

Returns:

  • (Hash)

    New intermeidate configs hash



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

def self.update_configs(configs:, options:)
  if options[:build_version]
    configs[:package_config][:app_name_version] = "#{configs[:project_config][:app_name]} - #{configs[:stage]} - #{options[:build_version]}" if configs[:package_config]
    unless options[:outfile]
      pathname = File.join(options[:out_folder], "#{configs[:project_config][:app_name]}_#{configs[:stage]}_#{options[:build_version]}")
      configs[:package_config][:out_file] =  pathname+".pkg" if configs[:package_config]
      configs[:build_config][:outfile]    = pathname+".zip" if configs[:build_config]
      configs[:inspect_config][:pkg] = configs[:package_config][:out_file] if configs[:inspect_config] and configs[:package_config]
    end
  end
  return configs
end