diff --git a/bin/review-compile b/bin/review-compile index d0ba754e3..5fcc77d74 100755 --- a/bin/review-compile +++ b/bin/review-compile @@ -82,6 +82,7 @@ def _main book = ReVIEW::Book::Base.load(@basedir) book.config = @config # needs only at the first time ARGV.each do |item| + error("file not found: #{item}") unless File.exist?(item) chap_name = File.basename(item, '.*') chap = book.chapter(chap_name) compiler = ReVIEW::Compiler.new(load_strategy_class(@target, @check_only)) diff --git a/lib/review/book/base.rb b/lib/review/book/base.rb index 497d7bf09..87adb9629 100644 --- a/lib/review/book/base.rb +++ b/lib/review/book/base.rb @@ -185,6 +185,9 @@ def catalog catalogfile_path = "#{basedir}/#{config['catalogfile']}" @catalog = File.open(catalogfile_path, 'r:BOM|utf-8') { |f| Catalog.new(f) } if File.file? catalogfile_path + if @catalog + @catalog.validate!(basedir) + end @catalog end @@ -247,10 +250,14 @@ def bib_exist? end def prefaces - return mkpart_from_namelist(catalog.predef.split("\n")) if catalog + if catalog + return mkpart_from_namelist(catalog.predef.split("\n")) + end begin - mkpart_from_namelistfile("#{@basedir}/#{config['predef_file']}") if File.file?("#{@basedir}/#{config['predef_file']}") + if File.file?("#{@basedir}/#{config['predef_file']}") + mkpart_from_namelistfile("#{@basedir}/#{config['predef_file']}") + end rescue FileNotFound => err raise FileNotFound, "preface #{err.message}" end diff --git a/lib/review/catalog.rb b/lib/review/catalog.rb index da6047599..c888664fb 100644 --- a/lib/review/catalog.rb +++ b/lib/review/catalog.rb @@ -48,5 +48,35 @@ def postdef return '' unless @yaml['POSTDEF'] @yaml['POSTDEF'].join("\n") end + + def validate!(basedir) + filenames = [] + if predef.present? + filenames.concat(predef.split(/\n/)) + end + parts_with_chaps.each do |chap| + if chap.is_a?(Hash) + chap.each_key do |part| + if File.extname(part) == '.re' + filenames.push(part) + end + end + filenames.concat(chap.values.flatten) + else + filenames.push(chap) + end + end + if appendix.present? + filenames.concat(appendix.split(/\n/)) + end + if postdef.present? + filenames.concat(postdef.split(/\n/)) + end + filenames.each do |filename| + unless File.exist?(File.join(basedir, filename)) + raise FileNotFound, "file not found in catalog.yml: #{basedir}/#{filename}" + end + end + end end end diff --git a/lib/review/epubmaker.rb b/lib/review/epubmaker.rb index 6115463f8..556ea3656 100644 --- a/lib/review/epubmaker.rb +++ b/lib/review/epubmaker.rb @@ -140,6 +140,9 @@ def produce(yamlfile, bookname = nil) log('Call ePUB producer.') @producer.produce("#{bookname}.epub", basetmpdir, epubtmpdir) log('Finished.') + rescue ApplicationError => e + raise if @config['debug'] + error(e.message) ensure FileUtils.remove_entry_secure(basetmpdir) unless @config['debug'] end diff --git a/lib/review/pdfmaker.rb b/lib/review/pdfmaker.rb index ac8c289ac..0df3e8bfd 100644 --- a/lib/review/pdfmaker.rb +++ b/lib/review/pdfmaker.rb @@ -126,7 +126,13 @@ def execute(*args) rescue ReVIEW::ConfigError => e warn e.message end - generate_pdf(yamlfile) + + begin + generate_pdf(yamlfile) + rescue ApplicationError => e + raise if @config['debug'] + error(e.message) + end end def make_input_files(book, yamlfile) diff --git a/lib/review/textmaker.rb b/lib/review/textmaker.rb index 0d8a2f25c..a74ba02b0 100644 --- a/lib/review/textmaker.rb +++ b/lib/review/textmaker.rb @@ -83,7 +83,12 @@ def execute(*args) # YAML configs will be overridden by command line options. @config.deep_merge!(cmd_config) I18n.setup(@config['language']) - generate_text_files(yamlfile) + begin + generate_text_files(yamlfile) + rescue ApplicationError => e + raise if @config['debug'] + error(e.message) + end end def generate_text_files(yamlfile) diff --git a/lib/review/webmaker.rb b/lib/review/webmaker.rb index 2d733ec92..ddb3fe421 100644 --- a/lib/review/webmaker.rb +++ b/lib/review/webmaker.rb @@ -90,7 +90,12 @@ def execute(*args) @config.deep_merge!(cmd_config) @config['htmlext'] = 'html' I18n.setup(@config['language']) - generate_html_files(yamlfile) + begin + generate_html_files(yamlfile) + rescue ApplicationError => e + raise if @config['debug'] + error(e.message) + end end def generate_html_files(yamlfile) diff --git a/test/book_test_helper.rb b/test/book_test_helper.rb index 010d14048..f36e79a20 100644 --- a/test/book_test_helper.rb +++ b/test/book_test_helper.rb @@ -1,4 +1,5 @@ require 'test_helper' +require 'book_test_helper' require 'review/book' require 'stringio' diff --git a/test/test_catalog.rb b/test/test_catalog.rb index 6083e748d..1981d8ea5 100644 --- a/test/test_catalog.rb +++ b/test/test_catalog.rb @@ -3,6 +3,7 @@ class CatalogTest < Test::Unit::TestCase include ReVIEW + include BookTestHelper def test_predef sut = Catalog.new(yaml) @@ -90,6 +91,50 @@ def test_from_object assert_equal(exp.chomp, sut.chaps) end + def test_validate + mktmpbookdir do |dir, _book, _files| + %w[pre01.re pre02.re ch01.re ch02.re post01.re post02.re back01.re back02.re].each do |file| + FileUtils.touch(file) + end + cat = Catalog.new(yaml_hash) + cat.validate!(dir) + end + end + + def test_validate_with_parts + mktmpbookdir do |dir, _book, _files| + %w[ch01.re part1.re ch02.re ch03.re part2.re ch04.re ch05.re].each do |file| + FileUtils.touch(file) + end + cat = Catalog.new(yaml_with_parts) + cat.validate!(dir) + end + end + + def test_validate_fail_ch02 + assert_raise FileNotFound do + mktmpbookdir do |dir, _book, _files| + %w[pre01.re pre02.re ch01.re].each do |file| + FileUtils.touch(file) + end + cat = Catalog.new(yaml_hash) + cat.validate!(dir) + end + end + end + + def test_validate_fail_back02 + assert_raise FileNotFound do + mktmpbookdir do |dir, _book, _files| + %w[pre01.re pre02.re ch01.re ch02.re post01.re post02.re back01.re back03.re].each do |file| + FileUtils.touch(file) + end + cat = Catalog.new(yaml_hash) + cat.validate!(dir) + end + end + end + private def yaml