Module: ProconBypassMan::ButtonsSettingConfiguration::Loader

Defined in:
lib/procon_bypass_man/buttons_setting_configuration/loader.rb

Class Method Summary collapse

Class Method Details

.fallback_setting_if_has_backup(current_setting_path:) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/procon_bypass_man/buttons_setting_configuration/loader.rb', line 55

def self.fallback_setting_if_has_backup(current_setting_path: )
  return unless File.exist?(ProconBypassMan.fallback_setting_path)
  return if current_setting_path.nil?

  FileUtils.copy(
    ProconBypassMan.fallback_setting_path,
    current_setting_path,
  )
  FileUtils.rm_rf(ProconBypassMan.fallback_setting_path)
end

.load(setting_path:) ⇒ ProconBypassMan::ButtonsSettingConfiguration



10
11
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
# File 'lib/procon_bypass_man/buttons_setting_configuration/loader.rb', line 10

def self.load(setting_path: )
   = ProconBypassMan::ButtonsSettingConfiguration::MetadataLoader.load(setting_path: setting_path)
  if(Gem::Version.new(.required_pbm_version) >= Gem::Version.new(ProconBypassMan::VERSION))
    ProconBypassMan::SendErrorCommand.execute(error: '起動中のPBMが設定ファイルのバージョンを満たしていません。設定ファイルが意図した通り動かない可能性があります。PBMのバージョンをあげてください。')
  end

  ProconBypassMan::Procon.reset! # TODO: ここでresetするのは微妙な気がする

  new_instance, yaml =
    begin
      new_instance = ProconBypassMan::ButtonsSettingConfiguration.new
      new_instance.setting_path = setting_path
      yaml = ProconBypassMan::YamlLoader.load(path: setting_path)
      new_instance.instance_eval(yaml["setting"])
      validator = Validator.new(new_instance)
      if validator.valid?
        [new_instance, yaml]
      else
        fallback_setting_if_has_backup(current_setting_path: setting_path)
        raise ProconBypassMan::CouldNotLoadConfigError, validator.errors_to_s
      end
    rescue SyntaxError
      fallback_setting_if_has_backup(current_setting_path: setting_path)
      raise ProconBypassMan::CouldNotLoadConfigError, "Rubyスクリプトのシンタックスエラーです"
    rescue NameError
      fallback_setting_if_has_backup(current_setting_path: setting_path)
      raise ProconBypassMan::CouldNotLoadConfigError, "Rubyスクリプトに未定義の定数・変数があります"
    rescue Psych::SyntaxError
      fallback_setting_if_has_backup(current_setting_path: setting_path)
      raise ProconBypassMan::CouldNotLoadConfigError, "yamlのシンタックスエラーです"
    end

  ProconBypassMan.config.raw_setting = yaml
  ProconBypassMan.buttons_setting_configuration = new_instance
  File.write(ProconBypassMan.digest_path, Digest::MD5.hexdigest(yaml["setting"]))
  FileUtils.rm_rf(ProconBypassMan.fallback_setting_path) # NOTE: 設定ファイルの読み込みに成功したら、バックアップを削除する

  ProconBypassMan.buttons_setting_configuration
end

.reload_settingObject



50
51
52
53
# File 'lib/procon_bypass_man/buttons_setting_configuration/loader.rb', line 50

def self.reload_setting
  ProconBypassMan.ephemeral_config.reset!
  self.load(setting_path: ProconBypassMan.buttons_setting_configuration.setting_path)
end