From 360f17cdd9d7e3f5035258473f8243f94b0a6b8c Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 18 Aug 2020 12:36:54 +0900 Subject: [PATCH] add ReVIEW::Configure.create --- lib/review/configure.rb | 20 ++++++++++++++++++++ lib/review/epubmaker.rb | 15 +++------------ lib/review/idgxmlmaker.rb | 13 +++---------- lib/review/pdfmaker.rb | 13 +++---------- lib/review/textmaker.rb | 13 +++---------- lib/review/webmaker.rb | 14 ++++---------- 6 files changed, 36 insertions(+), 52 deletions(-) diff --git a/lib/review/configure.rb b/lib/review/configure.rb index e2aaa6495..20e675338 100644 --- a/lib/review/configure.rb +++ b/lib/review/configure.rb @@ -119,6 +119,26 @@ def self.values conf end + def self.create(maker: nil, yamlfile: nil, config: nil) + conf = self.values + conf.maker = maker + + if yamlfile + begin + loader = ReVIEW::YAMLLoader.new + conf.deep_merge!(loader.load_file(yamlfile)) + rescue => e + error "yaml error #{e.message}" + end + end + # YAML configs will be overridden by command line options. + if config + conf.deep_merge!(config) + end + + conf + end + def [](key) maker = self.maker if maker && self.key?(maker) && self.fetch(maker) && self.fetch(maker).key?(key) diff --git a/lib/review/epubmaker.rb b/lib/review/epubmaker.rb index 8718814de..a049e5d4d 100644 --- a/lib/review/epubmaker.rb +++ b/lib/review/epubmaker.rb @@ -52,18 +52,9 @@ def log(msg) end def load_yaml(yamlfile) - loader = ReVIEW::YAMLLoader.new - @config = ReVIEW::Configure.values - begin - @config.deep_merge!(loader.load_file(yamlfile)) - rescue => e - error "yaml error #{e.message}" - end - @producer = Producer.new(@config) @producer.load(yamlfile) @config = @producer.config - @config.maker = 'epubmaker' end def self.execute(*args) @@ -94,13 +85,13 @@ def parse_opts(args) end def execute(*args) - @config = ReVIEW::Configure.values - @config.maker = 'epubmaker' cmd_config, yamlfile, exportfile = parse_opts(args) error "#{yamlfile} not found." unless File.exist?(yamlfile) + @config = ReVIEW::Configure.create(maker: 'epubmaker', + yamlfile: yamlfile, + config: cmd_config) load_yaml(yamlfile) - @config.deep_merge!(cmd_config) update_log_level log("Loaded yaml file (#{yamlfile}).") diff --git a/lib/review/idgxmlmaker.rb b/lib/review/idgxmlmaker.rb index a1bec2ad0..df9a65943 100644 --- a/lib/review/idgxmlmaker.rb +++ b/lib/review/idgxmlmaker.rb @@ -77,19 +77,12 @@ def remove_old_files(path) end def execute(*args) - @config = ReVIEW::Configure.values - @config.maker = 'idgxmlmaker' cmd_config, yamlfile = parse_opts(args) error "#{yamlfile} not found." unless File.exist?(yamlfile) - begin - loader = ReVIEW::YAMLLoader.new - @config.deep_merge!(loader.load_file(yamlfile)) - rescue => e - error "yaml error #{e.message}" - end - # YAML configs will be overridden by command line options. - @config.deep_merge!(cmd_config) + @config = ReVIEW::Configure.create(maker: 'idgxmlmaker', + yamlfile: yamlfile, + config: cmd_config) I18n.setup(@config['language']) begin generate_idgxml_files(yamlfile) diff --git a/lib/review/pdfmaker.rb b/lib/review/pdfmaker.rb index 0b764482d..c57c39041 100644 --- a/lib/review/pdfmaker.rb +++ b/lib/review/pdfmaker.rb @@ -122,20 +122,13 @@ def parse_opts(args) end def execute(*args) - @config = ReVIEW::Configure.values - @config.maker = 'pdfmaker' cmd_config, yamlfile = parse_opts(args) error "#{yamlfile} not found." unless File.exist?(yamlfile) - begin - loader = ReVIEW::YAMLLoader.new - @config.deep_merge!(loader.load_file(yamlfile)) - rescue => e - error "yaml error #{e.message}" - end + @config = ReVIEW::Configure.create(maker: 'pdfmaker', + yamlfile: yamlfile, + config: cmd_config) - # YAML configs will be overridden by command line options. - @config.deep_merge!(cmd_config) I18n.setup(@config['language']) @basedir = File.absolute_path(File.dirname(yamlfile)) diff --git a/lib/review/textmaker.rb b/lib/review/textmaker.rb index 3a712fb0e..1b119c943 100644 --- a/lib/review/textmaker.rb +++ b/lib/review/textmaker.rb @@ -75,20 +75,13 @@ def remove_old_files(path) end def execute(*args) - @config = ReVIEW::Configure.values - @config.maker = 'textmaker' cmd_config, yamlfile = parse_opts(args) error "#{yamlfile} not found." unless File.exist?(yamlfile) - begin - loader = ReVIEW::YAMLLoader.new - @config.deep_merge!(loader.load_file(yamlfile)) - rescue => e - error "yaml error #{e.message}" - end + @config = ReVIEW::Configure.create(maker: 'textmaker', + yamlfile: yamlfile, + config: cmd_config) - # YAML configs will be overridden by command line options. - @config.deep_merge!(cmd_config) I18n.setup(@config['language']) begin generate_text_files(yamlfile) diff --git a/lib/review/webmaker.rb b/lib/review/webmaker.rb index 6a45fc0e8..1172eb54f 100644 --- a/lib/review/webmaker.rb +++ b/lib/review/webmaker.rb @@ -80,19 +80,13 @@ def remove_old_files(path) end def execute(*args) - @config = ReVIEW::Configure.values - @config.maker = 'webmaker' cmd_config, yamlfile = parse_opts(args) error "#{yamlfile} not found." unless File.exist?(yamlfile) - begin - loader = ReVIEW::YAMLLoader.new - @config.deep_merge!(loader.load_file(yamlfile)) - rescue => e - error "yaml error #{e.message}" - end - # YAML configs will be overridden by command line options. - @config.deep_merge!(cmd_config) + @config = ReVIEW::Configure.create(maker: 'webmaker', + yamlfile: yamlfile, + config: cmd_config) + @config['htmlext'] = 'html' I18n.setup(@config['language']) begin