Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Catalog#validate! #957

Merged
merged 7 commits into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/review-compile
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 9 additions & 2 deletions lib/review/book/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions lib/review/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions lib/review/epubmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lib/review/pdfmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion lib/review/textmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion lib/review/webmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/book_test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'test_helper'
require 'book_test_helper'
require 'review/book'

require 'stringio'
Expand Down
45 changes: 45 additions & 0 deletions test/test_catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

class CatalogTest < Test::Unit::TestCase
include ReVIEW
include BookTestHelper

def test_predef
sut = Catalog.new(yaml)
Expand Down Expand Up @@ -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
Expand Down