From c1bc3d017c090074df9e9a1f439667d782b58e36 Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Sun, 21 Oct 2018 18:58:39 +0900 Subject: [PATCH 1/9] implement updater review-update. --- bin/review-update | 19 ++ lib/review/i18n.rb | 8 + lib/review/update.rb | 423 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 450 insertions(+) create mode 100755 bin/review-update create mode 100644 lib/review/update.rb diff --git a/bin/review-update b/bin/review-update new file mode 100755 index 000000000..9a25e8aba --- /dev/null +++ b/bin/review-update @@ -0,0 +1,19 @@ +#!/usr/bin/env ruby +# +# Copyright (c) 2018 Kenshi Muto +# +# This program is free software. +# You can distribute or modify this program under the terms of +# the GNU LGPL, Lesser General Public License version 2.1. +# For details of the GNU LGPL, see the file "COPYING". + +require 'pathname' + +bindir = Pathname.new(__FILE__).realpath.dirname +$LOAD_PATH.unshift((bindir + '../lib').realpath) + +require 'review/update' + +if File.basename($PROGRAM_NAME) == File.basename(__FILE__) + ReVIEW::Update.execute(*ARGV) +end diff --git a/lib/review/i18n.rb b/lib/review/i18n.rb index 4b255c868..a8d98b4e6 100644 --- a/lib/review/i18n.rb +++ b/lib/review/i18n.rb @@ -59,6 +59,10 @@ def self.get(word, locale = nil) @i18n.get(word, locale) end + def self.set(word, str) + @i18n.set(word, str) + end + attr_accessor :locale def initialize(locale = nil) @@ -112,6 +116,10 @@ def get(word, locale = nil) @store[locale][word] end + def set(word, str) + @store[@locale][word] = str + end + def t(str, args = nil) frmt = @store[@locale][str].dup frmt.gsub!('%%', '##') diff --git a/lib/review/update.rb b/lib/review/update.rb new file mode 100644 index 000000000..1f9f008d9 --- /dev/null +++ b/lib/review/update.rb @@ -0,0 +1,423 @@ +# +# Copyright (c) 2018 Kenshi Muto +# +# This program is free software. +# You can distribute or modify this program under the terms of +# the GNU LGPL, Lesser General Public License version 2.1. +# For details of the GNU LGPL, see the file "COPYING". + +require 'fileutils' +require 'optparse' +require 'review' +require 'review/i18n' +require 'yaml' +require 'digest' + +module ReVIEW + class Update + def self.execute(*args) + new.execute(*args) + end + + # should be + TARGET_VERSION = '3.0' + EPUB_VERSION = '3' + HTML_VERSION = '5' + TEX_DOCUMENTCLASS = ['review-jsbook', 'review-jlreq'] + TEX_DOCUMENTCLASS_BAD = ['jsbook', nil] + TEX_DOCUMENTCLASS_OPTS = 'cameraready=print,paper=a5' + TEX_COMMAND = 'uplatex' + TEX_OPTIONS = '-interaction=nonstopmode -file-line-error' + + def initialize + @template = nil + @specified_template = nil + @force = nil + @logger = ReVIEW.logger + @review_dir = File.dirname(File.expand_path('..', __dir__)) + @config_ymls = [] + @locale_ymls = [] + @catalog_ymls = [] + @tex_ymls = [] + @epub_ymls = [] + + @backup = true + @ownlayout_tex = nil + end + + def execute(*args) + parse_options(args) + dir = Dir.pwd + + parse_ymls(dir) + check_old_catalogs(dir) + + show_version + + if @config_ymls.empty? + @logger.error _("!! No *.yml file with 'review_version' was found. Aborted. !!") + exit 1 + end + + check_own_files(dir) + update_version + update_rakefile + update_epub_version + update_tex_parameters + if @template + update_tex_stys(@template, dir) + end + end + + def _(message, args=[]) + unless I18n.get(message) + I18n.set(message, message) # just copy + end + I18n.t(message, args) + end + + def confirm(message, args = [], default=true) + if @force + print _(message, args) + if default + puts ' yes' + return true + else + puts 'no' + return nil + end + end + + loop do + print _(message, args) + if default + print ' [y]/n ' + else + print ' y/[n] ' + end + case gets.chomp.downcase + when 'yes', 'y' + return true + when 'no', 'n' + return nil + when '' + default + end + end + end + + def rewrite_yml(yml, key, val) + content = File.read(yml) + content.gsub!(/^#{key}:.*$/, "#{key}: #{val}") + if @backup + FileUtils.mv yml, "#{yml}-old" + end + File.open(yml, 'w') { |f| f.write(content) } + end + + def parse_options(args) + opts = OptionParser.new + opts.version = ReVIEW::VERSION + opts.banner = "Usage: #{File.basename($PROGRAM_NAME)} [option]" + opts.on('-h', '--help', 'print this message and quit.') do + puts opts.help + exit 0 + end + opts.on('--latex-template name', 'specify LaTeX template name. (default: review-jsbook)') do |tname| + @specified_template = tname + end + opts.on('-y', '--yes', 'override files without asking.') do + @force = true + end + + begin + opts.parse!(args) + rescue OptionParser::ParseError => err + @logger.error err.message + $stderr.puts opts.help + exit 1 + end + + if @specified_template + tdir = File.join(@review_dir, 'templates/latex', @specified_template) + unless File.exist?(tdir) + @logger.error "!! #{tdir} not found. Aborted. !!" + exit 1 + end + end + end + + def parse_ymls(dir) + language = 'ja' + + Dir.glob(File.join(dir, '*.yml')) do |yml| + begin + config = YAML.load_file(yml) + if config['language'].present? + language = config['language'] + end + + if config['review_version'].present? + @config_ymls.push(yml) + end + if config['texdocumentclass'].present? || + config['texcommand'].present? || + config['texoptions'].present? || + config['dvicommand'].present? || + config['dvioptions'].present? || + config['pdfmaker'].present? + @tex_ymls.push(yml) + end + if config['epubmaker'].present? || config['epubversion'].present? || + config['htmlversion'].present? + @epub_ymls.push(yml) + end + if config['locale'].present? + @locale_ymls.push(yml) + end + if config['PREDEF'].present? || config['CHAPS'].present? || + config['APPENDIX'].present? || config['POSTDEF'].present? + @catalog_ymls.push(yml) + end + rescue Psych::SyntaxError + puts "!! #{yml} is broken. Ignored. !!" + end + end + I18n.setup(language) + + @config_ymls.uniq! + @locale_ymls.uniq! + @catalog_ymls.uniq! + @tex_ymls.uniq! + @epub_ymls.uniq! + end + + def check_old_catalogs(dir) + files = Dir.glob(File.join(dir, '*')).map do |fname| + if %w(PREDEF CHAPS POSTDEF PART).include?(File.basename(fname)) + File.basename(fname) + else + nil + end + end.compact + + unless files.empty? + @logger.error _("!! %s file(s) is obsoleted. Run 'review-catalog-converter' to convert to 'catalog.yml' and remove old files. Aborted. !!", files.join(', ')) + exit 1 + end + end + + def show_version + puts _("** review-update updates your project to %s **", ReVIEW::VERSION) + end + + def check_own_files(dir) + if File.exist?(File.join(dir, 'layouts/layout.tex.erb')) + unless confirm('** There is custom layouts/layout.tex.erb file. Updating may break to make PDF until you fix layout.tex.erb. Do you really proceed to update? **', [], nil) + exit 1 + end + end + + if File.exist?(File.join(dir, 'review-ext.rb')) + puts _('** There is review-ext.rb file. You need to update it by yourself. **') + end + end + + def update_version + @config_ymls.each do |yml| + config = YAML.load_file(yml) + if config['review_version'].to_f < TARGET_VERSION.to_f + if confirm("%s: Update 'review_version' to '%s'?", [File.basename(yml), TARGET_VERSION]) + rewrite_yml(yml, 'review_version', TARGET_VERSION) + end + end + end + end + + def update_rakefile(dir) + taskdir = File.join(dir, 'lib/tasks') + unless File.exist?(taskdir) + FileUtils.mkdir_p taskdir + end + + masterrakefile = File.join(@review_dir, 'samples/sample-book/src/Rakefile') + if Digest::SHA256.hexdigest(File.join(dir, 'Rakefile')) != Digest::SHA256.hexdigest(masterrakefile) + if confirm('%s will be overridden with Re:VIEW version (%s). Do you really proceed?', ['Rakefile', masterrakefile]) + FileUtils.mv File.join(dir, 'Rakefile'), File.join(dir, 'Rakefile-old') + FileUtils.cp materrakefile, File.join(dir, 'Rakefile') + end + end + + masterrakefile = File.join(@review_dir, 'samples/sample-book/src/lib/tasks/review.rake') + if Digest::SHA256.hexdigest(File.join(taskdir, 'review.rake')) != Digest::SHA256.hexdigest(masterrakefile) + if confirm('%s will be overridden with Re:VIEW version (%s). Do you really proceed?', ['lib/tasks/review.rake', masterrakefile]) + FileUtils.mv File.join(taskdir, 'review.rake'), File.join(taskdir, 'review.rake-old') + FileUtils.cp materrakefile, File.join(taskdir, 'review.rake') + end + end + end + + def update_epub_version + @epub_ymls.each do |yml| + config = YAML.load_file(yml) + if config['epubversion'].present? && config['epubversion'].to_f < EPUB_VERSION.to_f + if confirm("%s: Update 'epubversion' to '%s'?", [File.basename(yml), EPUB_VERSION]) + rewrite_yml(yml, 'epubversion', EPUB_VERSION) + end + end + if config['htmlversion'].present? && config['htmlversion'].to_f < HTML_VERSION.to_f + if confirm("%s: Update 'htmlversion' to '%s'?", [File.basename(yml), HTML_VERSION]) + rewrite_yml(yml, 'htmlversion', HTML_VERSION) + end + end + end + end + + def update_tex_parameters + @tex_ymls.each do |yml| + config = YAML.load_file(yml) + if config['texdocumentclass'] + if TEX_DOCUMENTCLASS.include?(config['texdocumentclass'][0]) + if config['texdocumentclass'][0] != @specified_template + # want to use other template? + # FIXME + end + else + @template = config['texdocumentclass'][0] + end + + if TEX_DOCUMENTCLASS_BAD.include?(config['texdocumentclass'][0]) + cno = TEX_DOCUMENTCLASS_BAD.index(config['texdocumentclass'][0]) + + if @specified_template != TEX_DOCUMENTCLASS[cno] + # not default, manually selected + unless confirm("%s: 'texdocumentclass' uses the old class '%s'. By default it is migrated to '%s', but you specify '%s'. Do you really migrate 'texdocumentclass' to '%s'?", [File.basename(yml), TEX_DOCUMENTCLASS_BAD[cno], TEX_DOCUMENTCLASS[cno], @specified_template, @specified_template]) + @template = nil + next + end + @template = @specified_template + else + # default migration + @template = TEX_DOCUMENTCLASS[cno] + unless confirm("%s: 'texdocumentclass' uses the old class '%s'. By default it is migrated to '%s'. Do you really migrate 'texdocumentclass' to '%s'?", [File.basename(yml), TEX_DOCUMENTCLASS_BAD[cno], @template, @template]) + @template = nil + next + end + end + + flag, modified_opts = convert_documentclass_opts(yml, @template, config['texdocumentclass'][1]) + if flag # successfully converted + puts _("%s: previous 'texdocumentclass' option '%s' is safely replaced with '%s'.", [File.basename(yml), config['texdocumentclass'][1], modified_opts]) + else # something wrong + unless confirm("%s: previous 'texdocumentclass' option '%s' couldn't be converted fully. '%s' is suggested. Do you really proceed?", [File.basename(yml), config['texdocumentclass'][1], modified_opts], nil) + @template = nil + next + end + end + + rewrite_yml(yml, 'texdocumentclass', %Q(["#{@template}", "#{modfied_opts}"])) + else + @template = nil + @logger.error _("%s: ** 'texdocumentclass' specifies '%s'. Because this is unknown class for this tool, you need to update it by yourself if it won't work. **", [File.basename(yml), config['texdocumentclass']]) + end + end + end + end + + def convert_documentclass_opts(yml, cls, prev_opts) + # XXX: at this time, review-jsbook and review-jlreq uses same parameters + opts = [] + flag = true + case cls + when 'review-jsbook' # at this time, it ignores keyval + prev_opts.split(/\s*,\s*/).each do |v| + case v + when 'a4j', 'a5j', 'b4j', 'b5j', 'a3paper', 'a4paper', 'a5paper', 'a6paper', 'b4paper', 'b5paper', 'b6paper', 'letterpaper', 'legalpaper', 'executivepaper' + opts << "paper=#{v.sub('j', '').sub('paper', '')}" + when /[\d.]+ptj/ # not cared... + q = sprintf('%.2f', v.sub('pt', '').to_f * 1.4056) + opts << "Q=#{q}" + when /[\d.]+pt/ + q = sprintf('%.2f', v.sub('pt', '').to_f * 1.4056) + opts << "Q=#{q}" + when /[\d.]+Q/ + opts << "Q=#{v.sub('Q', '')}" + when 'landscape', 'oneside', 'twoside', 'vartwoside', 'onecolumn', 'twocolumn', 'titlepage', 'notitlepage', 'openright', 'openany', 'leqno', 'fleqn', 'disablejfam', 'draft', 'final', 'mingoth', 'winjis', 'jis', 'papersize', 'english', 'report', 'jslogo', 'nojslogo' + # pass-through + opts << v + when 'uplatex', 'nomag', 'usemag', 'nomag*', 'tombow', 'tombo', 'mentuke', 'autodetect-engine' + # can be ignored + else + flag = nil + end + end + opts << 'cameraready=print' + opts << 'cover=false' + when 'review-jlreq' + # at this time, only think about jsbook->jlreq + prev_opts.split(/\s*,\s*/).each do |v| + case v + when 'a4j', 'a5j', 'b4j', 'b5j', 'a3paper', 'a4paper', 'a5paper', 'a6paper', 'b4paper', 'b5paper', 'b6paper', 'letterpaper', 'legalpaper', 'executivepaper' + opts << "paper=#{v.sub('j', '').sub('paper', '')}" + when /[\d.]+ptj/ # not cared... + opts << "fontsize=#{v.sub('j', '')}" + when /[\d.]+pt/ + opts << "fontsize=#{v}" + when /[\d.]+Q/ + opts << "fontsize=#{v}" + when 'landscape', 'oneside', 'twoside', 'onecolumn', 'twocolumn', 'titlepage', 'notitlepage', 'openright', 'openany', 'leqno', 'fleqn', 'draft', 'final', 'report' + # pass-through + opts << v + when 'uplatex', 'nomag', 'usemag', 'nomag*', 'tombow', 'tombo', 'mentuke', 'autodetect-engine' + # can be ignored + else + # 'vartwoside', 'disablejfam', 'mingoth', 'winjis', 'jis', 'papersize', 'english', 'jslogo', 'nojslogo' + flag = nil + end + end + opts << 'cameraready=print' + opts << 'cover=false' + else + flag = nil + @logger.error _("%s: ** '%s' is unknown class. Ignored. **", [File.basename(yml), cls]) + end + return flag, opts.join(',') + end + + def update_tex_stys(template, dir) + texmacrodir = File.join(dir, 'sty') + unless File.exist?(texmacrodir) + FileUtils.mkdir texmacrodir + end + + tdir = File.join(@review_dir, 'templates/latex', template) + Dir.glob(File.join(tdir, '*.*')).each do |fname| + if fname == 'review-custom.sty' + next + end + unless File.exist?(File.join(texmacrodir, fname)) + # just copy + FileUtils.cp File.join(tdir, fname), texmacrodir + next + end + + if Digest::SHA256.hexdigest(File.join(tdir, fname)) == Digest::SHA256.hexdigest(File.join(texmacrodir, fname)) + # same + next + end + + if confirm("%s will be overridden with Re:VIEW version (%s). Do you really proceed?", [File.join('sty', fname), File.join(tdir, fname)]) + FileUtils.mv File.join(texmacrodir, fname), File.join(texmacrodir, "#{fname}-old") + FileUtils.cp File.join(tdir, fname), texmacrodir + end + end + + if template == 'review-jsbook' + # provide gentombow from vendor/. current version is 2018/08/30 v0.9j + unless File.exist?(File.join(texmacrodir, 'gentombow09j.sty')) + FileUtils.cp File.join(@review_dir, 'vendor/gentombow/gentombow.sty'), File.join(texmacrodir, 'gentombow09j.sty') + end + end + end + end +end From e573e93ff26a6c909498a4a82b4382ed2d47fc3e Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Sun, 21 Oct 2018 19:51:26 +0900 Subject: [PATCH 2/9] fix for rubocop --- lib/review/update.rb | 122 ++++++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 54 deletions(-) diff --git a/lib/review/update.rb b/lib/review/update.rb index 1f9f008d9..fc6eb79af 100644 --- a/lib/review/update.rb +++ b/lib/review/update.rb @@ -69,14 +69,14 @@ def execute(*args) end end - def _(message, args=[]) + def _(message, args = []) unless I18n.get(message) I18n.set(message, message) # just copy end I18n.t(message, args) end - def confirm(message, args = [], default=true) + def confirm(message, args = [], default = true) if @force print _(message, args) if default @@ -194,10 +194,10 @@ def parse_ymls(dir) def check_old_catalogs(dir) files = Dir.glob(File.join(dir, '*')).map do |fname| - if %w(PREDEF CHAPS POSTDEF PART).include?(File.basename(fname)) + if %w[PREDEF CHAPS POSTDEF PART].include?(File.basename(fname)) File.basename(fname) else - nil + return nil end end.compact @@ -208,7 +208,7 @@ def check_old_catalogs(dir) end def show_version - puts _("** review-update updates your project to %s **", ReVIEW::VERSION) + puts _('** review-update updates your project to %s **', ReVIEW::VERSION) end def check_own_files(dir) @@ -226,10 +226,12 @@ def check_own_files(dir) def update_version @config_ymls.each do |yml| config = YAML.load_file(yml) - if config['review_version'].to_f < TARGET_VERSION.to_f - if confirm("%s: Update 'review_version' to '%s'?", [File.basename(yml), TARGET_VERSION]) - rewrite_yml(yml, 'review_version', TARGET_VERSION) - end + if config['review_version'].to_f >= TARGET_VERSION.to_f + next + end + + if confirm("%s: Update 'review_version' to '%s'?", [File.basename(yml), TARGET_VERSION]) + rewrite_yml(yml, 'review_version', TARGET_VERSION) end end end @@ -265,10 +267,11 @@ def update_epub_version rewrite_yml(yml, 'epubversion', EPUB_VERSION) end end - if config['htmlversion'].present? && config['htmlversion'].to_f < HTML_VERSION.to_f - if confirm("%s: Update 'htmlversion' to '%s'?", [File.basename(yml), HTML_VERSION]) - rewrite_yml(yml, 'htmlversion', HTML_VERSION) - end + if !config['htmlversion'].present? || config['htmlversion'].to_f >= HTML_VERSION.to_f + next + end + if confirm("%s: Update 'htmlversion' to '%s'?", [File.basename(yml), HTML_VERSION]) + rewrite_yml(yml, 'htmlversion', HTML_VERSION) end end end @@ -276,50 +279,55 @@ def update_epub_version def update_tex_parameters @tex_ymls.each do |yml| config = YAML.load_file(yml) - if config['texdocumentclass'] - if TEX_DOCUMENTCLASS.include?(config['texdocumentclass'][0]) - if config['texdocumentclass'][0] != @specified_template - # want to use other template? - # FIXME - end - else - @template = config['texdocumentclass'][0] + unless config['texdocumentclass'] + next + end + + if TEX_DOCUMENTCLASS.include?(config['texdocumentclass'][0]) + if config['texdocumentclass'][0] != @specified_template + # want to use other template? + # FIXME end + else + @template = config['texdocumentclass'][0] + end - if TEX_DOCUMENTCLASS_BAD.include?(config['texdocumentclass'][0]) - cno = TEX_DOCUMENTCLASS_BAD.index(config['texdocumentclass'][0]) - - if @specified_template != TEX_DOCUMENTCLASS[cno] - # not default, manually selected - unless confirm("%s: 'texdocumentclass' uses the old class '%s'. By default it is migrated to '%s', but you specify '%s'. Do you really migrate 'texdocumentclass' to '%s'?", [File.basename(yml), TEX_DOCUMENTCLASS_BAD[cno], TEX_DOCUMENTCLASS[cno], @specified_template, @specified_template]) - @template = nil - next - end - @template = @specified_template - else - # default migration - @template = TEX_DOCUMENTCLASS[cno] - unless confirm("%s: 'texdocumentclass' uses the old class '%s'. By default it is migrated to '%s'. Do you really migrate 'texdocumentclass' to '%s'?", [File.basename(yml), TEX_DOCUMENTCLASS_BAD[cno], @template, @template]) - @template = nil - next - end + if TEX_DOCUMENTCLASS_BAD.include?(config['texdocumentclass'][0]) + cno = TEX_DOCUMENTCLASS_BAD.index(config['texdocumentclass'][0]) + + if @specified_template != TEX_DOCUMENTCLASS[cno] + # not default, manually selected + unless confirm("%s: 'texdocumentclass' uses the old class '%s'. By default it is migrated to '%s', but you specify '%s'. Do you really migrate 'texdocumentclass' to '%s'?", + [File.basename(yml), TEX_DOCUMENTCLASS_BAD[cno], + TEX_DOCUMENTCLASS[cno], + @specified_template, @specified_template]) + @template = nil + next end - - flag, modified_opts = convert_documentclass_opts(yml, @template, config['texdocumentclass'][1]) - if flag # successfully converted - puts _("%s: previous 'texdocumentclass' option '%s' is safely replaced with '%s'.", [File.basename(yml), config['texdocumentclass'][1], modified_opts]) - else # something wrong - unless confirm("%s: previous 'texdocumentclass' option '%s' couldn't be converted fully. '%s' is suggested. Do you really proceed?", [File.basename(yml), config['texdocumentclass'][1], modified_opts], nil) - @template = nil - next - end + @template = @specified_template + else + # default migration + @template = TEX_DOCUMENTCLASS[cno] + unless confirm("%s: 'texdocumentclass' uses the old class '%s'. By default it is migrated to '%s'. Do you really migrate 'texdocumentclass' to '%s'?", [File.basename(yml), TEX_DOCUMENTCLASS_BAD[cno], @template, @template]) + @template = nil + next end + end - rewrite_yml(yml, 'texdocumentclass', %Q(["#{@template}", "#{modfied_opts}"])) - else - @template = nil - @logger.error _("%s: ** 'texdocumentclass' specifies '%s'. Because this is unknown class for this tool, you need to update it by yourself if it won't work. **", [File.basename(yml), config['texdocumentclass']]) + flag, modified_opts = convert_documentclass_opts(yml, @template, config['texdocumentclass'][1]) + if flag # successfully converted + puts _("%s: previous 'texdocumentclass' option '%s' is safely replaced with '%s'.", [File.basename(yml), config['texdocumentclass'][1], modified_opts]) + else # something wrong + unless confirm("%s: previous 'texdocumentclass' option '%s' couldn't be converted fully. '%s' is suggested. Do you really proceed?", [File.basename(yml), config['texdocumentclass'][1], modified_opts], nil) + @template = nil + next + end end + + rewrite_yml(yml, 'texdocumentclass', %Q(["#{@template}", "#{modfied_opts}"])) + else + @template = nil + @logger.error _("%s: ** 'texdocumentclass' specifies '%s'. Because this is unknown class for this tool, you need to update it by yourself if it won't work. **", [File.basename(yml), config['texdocumentclass']]) end end end @@ -342,11 +350,16 @@ def convert_documentclass_opts(yml, cls, prev_opts) opts << "Q=#{q}" when /[\d.]+Q/ opts << "Q=#{v.sub('Q', '')}" - when 'landscape', 'oneside', 'twoside', 'vartwoside', 'onecolumn', 'twocolumn', 'titlepage', 'notitlepage', 'openright', 'openany', 'leqno', 'fleqn', 'disablejfam', 'draft', 'final', 'mingoth', 'winjis', 'jis', 'papersize', 'english', 'report', 'jslogo', 'nojslogo' + when 'landscape', 'oneside', 'twoside', 'vartwoside', 'onecolumn', + 'twocolumn', 'titlepage', 'notitlepage', 'openright', + 'openany', 'leqno', 'fleqn', 'disablejfam', 'draft', 'final', + 'mingoth', 'winjis', 'jis', 'papersize', 'english', 'report', + 'jslogo', 'nojslogo' # pass-through opts << v when 'uplatex', 'nomag', 'usemag', 'nomag*', 'tombow', 'tombo', 'mentuke', 'autodetect-engine' # can be ignored + next else flag = nil end @@ -365,11 +378,12 @@ def convert_documentclass_opts(yml, cls, prev_opts) opts << "fontsize=#{v}" when /[\d.]+Q/ opts << "fontsize=#{v}" - when 'landscape', 'oneside', 'twoside', 'onecolumn', 'twocolumn', 'titlepage', 'notitlepage', 'openright', 'openany', 'leqno', 'fleqn', 'draft', 'final', 'report' + when 'landscape', 'oneside', 'twoside', 'onecolumn', 'twocolumn', 'titlepage', 'notitlepage', 'openright', 'openany', 'leqno', 'fleqn', 'draft', 'final', 'report' # pass-through opts << v when 'uplatex', 'nomag', 'usemag', 'nomag*', 'tombow', 'tombo', 'mentuke', 'autodetect-engine' # can be ignored + next else # 'vartwoside', 'disablejfam', 'mingoth', 'winjis', 'jis', 'papersize', 'english', 'jslogo', 'nojslogo' flag = nil @@ -406,7 +420,7 @@ def update_tex_stys(template, dir) next end - if confirm("%s will be overridden with Re:VIEW version (%s). Do you really proceed?", [File.join('sty', fname), File.join(tdir, fname)]) + if confirm('%s will be overridden with Re:VIEW version (%s). Do you really proceed?', [File.join('sty', fname), File.join(tdir, fname)]) FileUtils.mv File.join(texmacrodir, fname), File.join(texmacrodir, "#{fname}-old") FileUtils.cp File.join(tdir, fname), texmacrodir end From 724b02692932f58ba7af0d2d525b1adfc98e712e Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Sun, 21 Oct 2018 21:39:58 +0900 Subject: [PATCH 3/9] update texcommand, dvicommand --- lib/review/update.rb | 122 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 97 insertions(+), 25 deletions(-) diff --git a/lib/review/update.rb b/lib/review/update.rb index fc6eb79af..f12f24f80 100644 --- a/lib/review/update.rb +++ b/lib/review/update.rb @@ -28,6 +28,8 @@ def self.execute(*args) TEX_DOCUMENTCLASS_OPTS = 'cameraready=print,paper=a5' TEX_COMMAND = 'uplatex' TEX_OPTIONS = '-interaction=nonstopmode -file-line-error' + DVI_COMMAND = 'dvipdfmx' + DVI_OPTIONS = '-d 5 -z 9' def initialize @template = nil @@ -61,12 +63,16 @@ def execute(*args) check_own_files(dir) update_version - update_rakefile + update_rakefile(dir) update_epub_version update_tex_parameters if @template update_tex_stys(@template, dir) end + update_tex_command + update_dvi_command + + puts _('Finished.') end def _(message, args = []) @@ -101,7 +107,7 @@ def confirm(message, args = [], default = true) when 'no', 'n' return nil when '' - default + return default end end end @@ -180,7 +186,7 @@ def parse_ymls(dir) @catalog_ymls.push(yml) end rescue Psych::SyntaxError - puts "!! #{yml} is broken. Ignored. !!" + @logger.error "!! #{yml} is broken. Ignored. !!" end end I18n.setup(language) @@ -242,20 +248,31 @@ def update_rakefile(dir) FileUtils.mkdir_p taskdir end - masterrakefile = File.join(@review_dir, 'samples/sample-book/src/Rakefile') - if Digest::SHA256.hexdigest(File.join(dir, 'Rakefile')) != Digest::SHA256.hexdigest(masterrakefile) - if confirm('%s will be overridden with Re:VIEW version (%s). Do you really proceed?', ['Rakefile', masterrakefile]) - FileUtils.mv File.join(dir, 'Rakefile'), File.join(dir, 'Rakefile-old') - FileUtils.cp materrakefile, File.join(dir, 'Rakefile') + master_rakefile = File.join(@review_dir, 'samples/sample-book/src/Rakefile') + + target_rakefile = File.join(dir, 'Rakefile') + if File.exist?(target_rakefile) + if Digest::SHA256.hexdigest(File.read(target_rakefile)) != Digest::SHA256.hexdigest(File.read(master_rakefile)) + if confirm('%s will be overridden with Re:VIEW version (%s). Do you really proceed?', ['Rakefile', master_rakefile]) + FileUtils.mv target_rakefile, "#{target_rakefile}-old" + FileUtils.cp master_rakefile, target_rakefile + end end + else + FileUtils.cp master_rakefile, target_rakefile end - masterrakefile = File.join(@review_dir, 'samples/sample-book/src/lib/tasks/review.rake') - if Digest::SHA256.hexdigest(File.join(taskdir, 'review.rake')) != Digest::SHA256.hexdigest(masterrakefile) - if confirm('%s will be overridden with Re:VIEW version (%s). Do you really proceed?', ['lib/tasks/review.rake', masterrakefile]) - FileUtils.mv File.join(taskdir, 'review.rake'), File.join(taskdir, 'review.rake-old') - FileUtils.cp materrakefile, File.join(taskdir, 'review.rake') + master_rakefile = File.join(@review_dir, 'samples/sample-book/src/lib/tasks/review.rake') + target_rakefile = File.join(taskdir, 'review.rake') + if File.exist?(target_rakefile) + if Digest::SHA256.hexdigest(File.read(target_rakefile)) != Digest::SHA256.hexdigest(File.read(master_rakefile)) + if confirm('%s will be overridden with Re:VIEW version (%s). Do you really proceed?', ['lib/tasks/review.rake', master_rakefile]) + FileUtils.mv target_rakefile, "#{target_rakefile}-old" + FileUtils.cp master_rakefile, target_rakefile + end end + else + FileUtils.cp master_rakefile, target_rakefile end end @@ -284,10 +301,12 @@ def update_tex_parameters end if TEX_DOCUMENTCLASS.include?(config['texdocumentclass'][0]) - if config['texdocumentclass'][0] != @specified_template + if @specified_template.present? && config['texdocumentclass'][0] != @specified_template # want to use other template? - # FIXME + @logger.error _("%s: !! 'texdocumentclass' uses new class '%s' already, but you specified '%s'. This tool can't handle such migration. Ignored. !!", [File.basename(yml), config['texdocumentclass'][0], @specified_template]) + @template = nil end + next else @template = config['texdocumentclass'][0] end @@ -295,7 +314,7 @@ def update_tex_parameters if TEX_DOCUMENTCLASS_BAD.include?(config['texdocumentclass'][0]) cno = TEX_DOCUMENTCLASS_BAD.index(config['texdocumentclass'][0]) - if @specified_template != TEX_DOCUMENTCLASS[cno] + if @specified_template && @specified_template != TEX_DOCUMENTCLASS[cno] # not default, manually selected unless confirm("%s: 'texdocumentclass' uses the old class '%s'. By default it is migrated to '%s', but you specify '%s'. Do you really migrate 'texdocumentclass' to '%s'?", [File.basename(yml), TEX_DOCUMENTCLASS_BAD[cno], @@ -324,7 +343,7 @@ def update_tex_parameters end end - rewrite_yml(yml, 'texdocumentclass', %Q(["#{@template}", "#{modfied_opts}"])) + rewrite_yml(yml, 'texdocumentclass', %Q(["#{@template}", "#{modified_opts}"])) else @template = nil @logger.error _("%s: ** 'texdocumentclass' specifies '%s'. Because this is unknown class for this tool, you need to update it by yourself if it won't work. **", [File.basename(yml), config['texdocumentclass']]) @@ -405,24 +424,27 @@ def update_tex_stys(template, dir) end tdir = File.join(@review_dir, 'templates/latex', template) - Dir.glob(File.join(tdir, '*.*')).each do |fname| - if fname == 'review-custom.sty' + Dir.glob(File.join(tdir, '*.*')).each do |master_styfile| + if Dir.basename(master_styfile) == 'review-custom.sty' next end - unless File.exist?(File.join(texmacrodir, fname)) + + target_styfile = File.join(texmacrodir, Dir.basename(master_styfile)) + + unless File.exist?(target_styfile) # just copy - FileUtils.cp File.join(tdir, fname), texmacrodir + FileUtils.cp master_styfile, target_styfile next end - if Digest::SHA256.hexdigest(File.join(tdir, fname)) == Digest::SHA256.hexdigest(File.join(texmacrodir, fname)) + if Digest::SHA256.hexdigest(File.read(target_styfile)) == Digest::SHA256.hexdigest(File.read(master_styfile)) # same next end - if confirm('%s will be overridden with Re:VIEW version (%s). Do you really proceed?', [File.join('sty', fname), File.join(tdir, fname)]) - FileUtils.mv File.join(texmacrodir, fname), File.join(texmacrodir, "#{fname}-old") - FileUtils.cp File.join(tdir, fname), texmacrodir + if confirm('%s will be overridden with Re:VIEW version (%s). Do you really proceed?', [target_styfile, master_styfile]) + FileUtils.mv target_styfile, "#{target_styfile}-old" + FileUtils.cp master_styfile, target_styfile end end @@ -433,5 +455,55 @@ def update_tex_stys(template, dir) end end end + + def update_tex_command + @tex_ymls.each do |yml| + config = YAML.load_file(yml) + if !config['texcommand'] || config['texcommand'] !~ /\s+\-/ + next + end + + # option should be moved to texoptions + cmd, opts = config['texcommand'].split(/\s+\-/, 2) + opts = "-#{opts}" + + unless confirm("%s: 'texcommand' has options ('%s'). Move it to 'texoptions'?", [File.basename(yml), opts]) + next + end + + if config['texoptions'].present? + config['texoptions'] += " #{opts}" + rewrite_yml(yml, 'texcommand', cmd) + rewrite_yml(yml, 'texoptions', config['texoptions']) + else + rewrite_yml(yml, 'texcommand', %Q(#{cmd}\ntexoptions: "#{TEX_OPTIONS} #{opts}")) + end + end + end + + def update_dvi_command + @tex_ymls.each do |yml| + config = YAML.load_file(yml) + if !config['dvicommand'] || config['dvicommand'] !~ /\s+\-/ + next + end + + # option should be moved to dvioptions + cmd, opts = config['dvicommand'].split(/\s+\-/, 2) + opts = "-#{opts}" + + unless confirm("%s: 'dvicommand' has options ('%s'). Move it to 'dvioptions'?", [File.basename(yml), opts]) + next + end + + if config['dvioptions'].present? + config['dvioptions'] += " #{opts}" + rewrite_yml(yml, 'dvicommand', cmd) + rewrite_yml(yml, 'dvioptions', config['dvioptions']) + else + rewrite_yml(yml, 'dvicommand', %Q(#{cmd}\ndvioptions: "#{DVI_OPTIONS} #{opts}")) + end + end + end end end From c339eecc49ed9b158dd499167a3178eab18e0d09 Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Mon, 22 Oct 2018 00:21:45 +0900 Subject: [PATCH 4/9] making some test case --- lib/review/update.rb | 46 ++++++++++----- test/test_update.rb | 130 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+), 14 deletions(-) create mode 100644 test/test_update.rb diff --git a/lib/review/update.rb b/lib/review/update.rb index f12f24f80..0d127380f 100644 --- a/lib/review/update.rb +++ b/lib/review/update.rb @@ -31,6 +31,9 @@ def self.execute(*args) DVI_COMMAND = 'dvipdfmx' DVI_OPTIONS = '-d 5 -z 9' + attr_reader :config_ymls, :locale_ymls, :catalog_ymls, :tex_ymls, :epub_ymls + attr_accessor :force, :specified_template + def initialize @template = nil @specified_template = nil @@ -44,7 +47,6 @@ def initialize @epub_ymls = [] @backup = true - @ownlayout_tex = nil end def execute(*args) @@ -58,13 +60,14 @@ def execute(*args) if @config_ymls.empty? @logger.error _("!! No *.yml file with 'review_version' was found. Aborted. !!") - exit 1 + raise ApplicationError end check_own_files(dir) update_version update_rakefile(dir) update_epub_version + update_locale update_tex_parameters if @template update_tex_stys(@template, dir) @@ -73,6 +76,8 @@ def execute(*args) update_dvi_command puts _('Finished.') + rescue ApplicationError + exit 1 end def _(message, args = []) @@ -84,12 +89,12 @@ def _(message, args = []) def confirm(message, args = [], default = true) if @force - print _(message, args) + @logger.info _(message, args) if default - puts ' yes' + @logger.info ' yes' return true else - puts 'no' + @logger.info 'no' return nil end end @@ -114,7 +119,7 @@ def confirm(message, args = [], default = true) def rewrite_yml(yml, key, val) content = File.read(yml) - content.gsub!(/^#{key}:.*$/, "#{key}: #{val}") + content.gsub!(/^(\s*)#{key}:.*$/, '\1' + "#{key}: #{val}") if @backup FileUtils.mv yml, "#{yml}-old" end @@ -141,14 +146,14 @@ def parse_options(args) rescue OptionParser::ParseError => err @logger.error err.message $stderr.puts opts.help - exit 1 + raise ApplicationError end if @specified_template tdir = File.join(@review_dir, 'templates/latex', @specified_template) unless File.exist?(tdir) @logger.error "!! #{tdir} not found. Aborted. !!" - exit 1 + raise ApplicationError end end end @@ -156,7 +161,7 @@ def parse_options(args) def parse_ymls(dir) language = 'ja' - Dir.glob(File.join(dir, '*.yml')) do |yml| + Dir.glob(File.join(dir, '*.yml')).sort.each do |yml| begin config = YAML.load_file(yml) if config['language'].present? @@ -209,7 +214,7 @@ def check_old_catalogs(dir) unless files.empty? @logger.error _("!! %s file(s) is obsoleted. Run 'review-catalog-converter' to convert to 'catalog.yml' and remove old files. Aborted. !!", files.join(', ')) - exit 1 + raise ApplicationError end end @@ -220,12 +225,12 @@ def show_version def check_own_files(dir) if File.exist?(File.join(dir, 'layouts/layout.tex.erb')) unless confirm('** There is custom layouts/layout.tex.erb file. Updating may break to make PDF until you fix layout.tex.erb. Do you really proceed to update? **', [], nil) - exit 1 + raise ApplicationError end end if File.exist?(File.join(dir, 'review-ext.rb')) - puts _('** There is review-ext.rb file. You need to update it by yourself. **') + @logger.info _('** There is review-ext.rb file. You need to update it by yourself. **') end end @@ -280,19 +285,32 @@ def update_epub_version @epub_ymls.each do |yml| config = YAML.load_file(yml) if config['epubversion'].present? && config['epubversion'].to_f < EPUB_VERSION.to_f - if confirm("%s: Update 'epubversion' to '%s'?", [File.basename(yml), EPUB_VERSION]) + if confirm("%s: Update 'epubversion' to '%s' from '%s'?", [File.basename(yml), EPUB_VERSION, config['epubversion']]) rewrite_yml(yml, 'epubversion', EPUB_VERSION) end end if !config['htmlversion'].present? || config['htmlversion'].to_f >= HTML_VERSION.to_f next end - if confirm("%s: Update 'htmlversion' to '%s'?", [File.basename(yml), HTML_VERSION]) + if confirm("%s: Update 'htmlversion' to '%s' from '%s'?", [File.basename(yml), HTML_VERSION, config['htmlversion']]) rewrite_yml(yml, 'htmlversion', HTML_VERSION) end end end + def update_locale + @locale_ymls.each do |yml| + config = YAML.load_file(yml) + if !config['chapter_quote'].present? || config['chapter_quote'].scan('%s') != 1 + next + end + v = config['chapter_quote'].sub('%s', '%s %s') + if confirm("%s: 'chapter_quote' now takes 2 values. Update '%s' to '%s'?", [File.basename(yml), config['chapter_quote'], v]) + rewrite_yml(yml, 'chapter_quote', v) + end + end + end + def update_tex_parameters @tex_ymls.each do |yml| config = YAML.load_file(yml) diff --git a/test/test_update.rb b/test/test_update.rb new file mode 100644 index 000000000..6f374faf0 --- /dev/null +++ b/test/test_update.rb @@ -0,0 +1,130 @@ +require 'test_helper' +require 'review/update' +require 'tmpdir' +require 'fileutils' + +class UpdateTest < Test::Unit::TestCase + include ReVIEW + + def setup + @tmpdir = Dir.mktmpdir + @u = Update.new + @u.force = true + I18n.setup('en') + end + + def teardown + FileUtils.rm_rf @tmpdir + end + + def test_broken_yml + File.write(File.join(@tmpdir, 'test.yml'), "invalid: [,]\n") + io = StringIO.new + @u.instance_eval{ @logger = ReVIEW::Logger.new(io) } + @u.parse_ymls(@tmpdir) + assert_match(/test\.yml is broken\. Ignored\./, io.string) + end + + def test_yml_variation + File.write(File.join(@tmpdir, 'config.yml'), "review_version: 2.0\nlanguage: en\n") + File.write(File.join(@tmpdir, 'tex1.yml'), %Q(review_version: 2.0\ntexdocumentclass: ["jsbook", "uplatex,twoside"]\n)) + File.write(File.join(@tmpdir, 'epub1.yml'), "htmlversion: 4\nepubversion: 2\n") + File.write(File.join(@tmpdir, 'locale.yml'), "locale: ja\n") + File.write(File.join(@tmpdir, 'locale2.yml'), "locale: en\n") + File.write(File.join(@tmpdir, 'catalog.yml'), "PREDEF:\n - pr01.re\n") + File.write(File.join(@tmpdir, 'catalog2.yml'), "CHAPS:\n - ch01.re\n") + File.write(File.join(@tmpdir, 'catalog3.yml'), "APPENDIX:\n - app01.re\n") + + @u.parse_ymls(@tmpdir) + + assert_equal 2, @u.config_ymls.size + assert_equal 2, @u.locale_ymls.size + assert_equal 3, @u.catalog_ymls.size + assert_equal 1, @u.tex_ymls.size + assert_equal 1, @u.epub_ymls.size + end + + def test_check_own_files_layout + Dir.mkdir(File.join(@tmpdir, 'layouts')) + File.write(File.join(@tmpdir, 'layouts/layout.tex.erb'), '') + + io = StringIO.new + @u.instance_eval{ @logger = ReVIEW::Logger.new(io) } + assert_raise(ApplicationError) { @u.check_own_files(@tmpdir) } + assert_match(%r{There is custom layouts/layout\.tex\.erb file}, io.string) + end + + def test_check_own_files_reviewext + File.write(File.join(@tmpdir, 'review-ext.rb'), '') + + io = StringIO.new + @u.instance_eval{ @logger = ReVIEW::Logger.new(io) } + @u.check_own_files(@tmpdir) + assert_match(/There is review\-ext\.rb file/, io.string) + end + + def test_update_version_2 + File.write(File.join(@tmpdir, 'config.yml'), "review_version: 2.0\n") + + io = StringIO.new + @u.instance_eval{ @logger = ReVIEW::Logger.new(io) } + @u.parse_ymls(@tmpdir) + @u.update_version + assert_match(/Update 'review_version' to '3.0'/, io.string) + assert_equal 'review_version: 3.0', File.read(File.join(@tmpdir, 'config.yml')).match(/review_version:.*$/).to_s + end + + def test_update_version_3 + File.write(File.join(@tmpdir, 'config.yml'), "review_version: 3.0\n") + + io = StringIO.new + @u.instance_eval{ @logger = ReVIEW::Logger.new(io) } + @u.parse_ymls(@tmpdir) + @u.update_version + assert_equal '', io.string + assert_equal 'review_version: 3.0', File.read(File.join(@tmpdir, 'config.yml')).match(/review_version:.*$/).to_s + end + + def test_update_version_99 + File.write(File.join(@tmpdir, 'config.yml'), "review_version: 99.0\n") + + io = StringIO.new + @u.instance_eval{ @logger = ReVIEW::Logger.new(io) } + @u.parse_ymls(@tmpdir) + @u.update_version + assert_equal '', io.string + assert_equal 'review_version: 99.0', File.read(File.join(@tmpdir, 'config.yml')).match(/review_version:.*$/).to_s + end + + def test_update_rakefile + io = StringIO.new + @u.instance_eval{ @logger = ReVIEW::Logger.new(io) } + @u.update_rakefile(@tmpdir) + assert_equal '', io.string + assert_equal true, File.exist?(File.join(@tmpdir, 'Rakefile')) + assert_equal true, File.exist?(File.join(@tmpdir, 'lib/tasks/review.rake')) + end + + def test_update_rakefile_same + io = StringIO.new + @u.instance_eval{ @logger = ReVIEW::Logger.new(io) } + @u.update_rakefile(@tmpdir) + @u.update_rakefile(@tmpdir) + assert_equal '', io.string + assert_equal true, File.exist?(File.join(@tmpdir, 'Rakefile')) + assert_equal true, File.exist?(File.join(@tmpdir, 'lib/tasks/review.rake')) + end + + def test_update_rakefile_different + File.write(File.join(@tmpdir, 'Rakefile'), '') + FileUtils.mkdir_p File.join(@tmpdir, 'lib/tasks') + File.write(File.join(@tmpdir, 'lib/tasks/review.rake'), '') + io = StringIO.new + @u.instance_eval{ @logger = ReVIEW::Logger.new(io) } + @u.update_rakefile(@tmpdir) + assert_match(/Rakefile will be overridden with/, io.string) + assert_match(%r{lib/tasks/review\.rake will be overridden}, io.string) + assert_equal true, File.exist?(File.join(@tmpdir, 'Rakefile')) + assert_equal true, File.exist?(File.join(@tmpdir, 'lib/tasks/review.rake')) + end +end From c230820eeb28f20d0b0e8c557892384f7a01f120 Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Mon, 22 Oct 2018 09:48:16 +0900 Subject: [PATCH 5/9] test updating stys --- lib/review/update.rb | 13 ++- test/test_update.rb | 231 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 234 insertions(+), 10 deletions(-) diff --git a/lib/review/update.rb b/lib/review/update.rb index 0d127380f..34a87652c 100644 --- a/lib/review/update.rb +++ b/lib/review/update.rb @@ -301,7 +301,7 @@ def update_epub_version def update_locale @locale_ymls.each do |yml| config = YAML.load_file(yml) - if !config['chapter_quote'].present? || config['chapter_quote'].scan('%s') != 1 + if !config['chapter_quote'].present? || config['chapter_quote'].scan('%s').size != 1 next end v = config['chapter_quote'].sub('%s', '%s %s') @@ -353,7 +353,7 @@ def update_tex_parameters flag, modified_opts = convert_documentclass_opts(yml, @template, config['texdocumentclass'][1]) if flag # successfully converted - puts _("%s: previous 'texdocumentclass' option '%s' is safely replaced with '%s'.", [File.basename(yml), config['texdocumentclass'][1], modified_opts]) + @logger.info _("%s: previous 'texdocumentclass' option '%s' is safely replaced with '%s'.", [File.basename(yml), config['texdocumentclass'][1], modified_opts]) else # something wrong unless confirm("%s: previous 'texdocumentclass' option '%s' couldn't be converted fully. '%s' is suggested. Do you really proceed?", [File.basename(yml), config['texdocumentclass'][1], modified_opts], nil) @template = nil @@ -443,17 +443,16 @@ def update_tex_stys(template, dir) tdir = File.join(@review_dir, 'templates/latex', template) Dir.glob(File.join(tdir, '*.*')).each do |master_styfile| - if Dir.basename(master_styfile) == 'review-custom.sty' - next - end - - target_styfile = File.join(texmacrodir, Dir.basename(master_styfile)) + target_styfile = File.join(texmacrodir, File.basename(master_styfile)) unless File.exist?(target_styfile) # just copy FileUtils.cp master_styfile, target_styfile next end + if File.basename(target_styfile) == 'review-custom.sty' + next + end if Digest::SHA256.hexdigest(File.read(target_styfile)) == Digest::SHA256.hexdigest(File.read(master_styfile)) # same diff --git a/test/test_update.rb b/test/test_update.rb index 6f374faf0..ce8f45b21 100644 --- a/test/test_update.rb +++ b/test/test_update.rb @@ -25,6 +25,19 @@ def test_broken_yml assert_match(/test\.yml is broken\. Ignored\./, io.string) end + def test_rewrite_yml + File.write(File.join(@tmpdir, 'test.yml'), "key: foo1\n key: foo2\n\t\t key: foo3\nakey: foo3\nkeya: foo4\n") + @u.rewrite_yml(File.join(@tmpdir, 'test.yml'), 'key', 'val') + cont = < Date: Mon, 22 Oct 2018 10:19:13 +0900 Subject: [PATCH 6/9] test texcommand/dvicommand update --- lib/review/update.rb | 13 ++++--- test/test_update.rb | 82 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 7 deletions(-) diff --git a/lib/review/update.rb b/lib/review/update.rb index 34a87652c..3c9d5cb40 100644 --- a/lib/review/update.rb +++ b/lib/review/update.rb @@ -479,7 +479,6 @@ def update_tex_command if !config['texcommand'] || config['texcommand'] !~ /\s+\-/ next end - # option should be moved to texoptions cmd, opts = config['texcommand'].split(/\s+\-/, 2) opts = "-#{opts}" @@ -490,10 +489,10 @@ def update_tex_command if config['texoptions'].present? config['texoptions'] += " #{opts}" - rewrite_yml(yml, 'texcommand', cmd) - rewrite_yml(yml, 'texoptions', config['texoptions']) + rewrite_yml(yml, 'texcommand', %Q("#{cmd}")) + rewrite_yml(yml, 'texoptions', %Q("#{config['texoptions']}")) else - rewrite_yml(yml, 'texcommand', %Q(#{cmd}\ntexoptions: "#{TEX_OPTIONS} #{opts}")) + rewrite_yml(yml, 'texcommand', %Q("#{cmd}"\ntexoptions: "#{TEX_OPTIONS} #{opts}")) end end end @@ -515,10 +514,10 @@ def update_dvi_command if config['dvioptions'].present? config['dvioptions'] += " #{opts}" - rewrite_yml(yml, 'dvicommand', cmd) - rewrite_yml(yml, 'dvioptions', config['dvioptions']) + rewrite_yml(yml, 'dvicommand', %Q("#{cmd}")) + rewrite_yml(yml, 'dvioptions', %Q("#{config['dvioptions']}")) else - rewrite_yml(yml, 'dvicommand', %Q(#{cmd}\ndvioptions: "#{DVI_OPTIONS} #{opts}")) + rewrite_yml(yml, 'dvicommand', %Q("#{cmd}"\ndvioptions: "#{DVI_OPTIONS} #{opts}")) end end end diff --git a/test/test_update.rb b/test/test_update.rb index ce8f45b21..a51c7add3 100644 --- a/test/test_update.rb +++ b/test/test_update.rb @@ -352,4 +352,86 @@ def test_update_stys_modified assert_match(/review\-base\.sty will be overridden/, io.string) assert_equal cont, File.read(File.join(@tmpdir, 'sty/review-base.sty')) end + + def test_update_tex_command + File.write(File.join(@tmpdir, 'config.yml'), %Q(texcommand: "/Program Files/up-latex --shell-escape -v"\n)) + io = StringIO.new + @u.instance_eval{ @logger = ReVIEW::Logger.new(io) } + @u.parse_ymls(@tmpdir) + @u.update_tex_command + assert_match(/has options/, io.string) + cont = < Date: Mon, 22 Oct 2018 10:33:13 +0900 Subject: [PATCH 7/9] test old catalogs --- lib/review/update.rb | 3 --- test/test_update.rb | 13 +++++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/review/update.rb b/lib/review/update.rb index 3c9d5cb40..3ed694925 100644 --- a/lib/review/update.rb +++ b/lib/review/update.rb @@ -137,9 +137,6 @@ def parse_options(args) opts.on('--latex-template name', 'specify LaTeX template name. (default: review-jsbook)') do |tname| @specified_template = tname end - opts.on('-y', '--yes', 'override files without asking.') do - @force = true - end begin opts.parse!(args) diff --git a/test/test_update.rb b/test/test_update.rb index a51c7add3..c12281f09 100644 --- a/test/test_update.rb +++ b/test/test_update.rb @@ -57,6 +57,19 @@ def test_yml_variation assert_equal 1, @u.epub_ymls.size end + def test_check_old_catalogs + %w[PREDEF CHAPS POSTDEF PART].each do |fname| + File.write(File.join(@tmpdir, fname), '') + + io = StringIO.new + @u.instance_eval{ @logger = ReVIEW::Logger.new(io) } + assert_raise(ApplicationError) { @u.check_old_catalogs(@tmpdir) } + assert_match(/review\-catalog\-converter/, io.string) + + File.unlink(File.join(@tmpdir, fname)) + end + end + def test_check_own_files_layout Dir.mkdir(File.join(@tmpdir, 'layouts')) File.write(File.join(@tmpdir, 'layouts/layout.tex.erb'), '') From 09af72c8c96572d588bec6fc656fe4c76051905f Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Mon, 22 Oct 2018 11:52:27 +0900 Subject: [PATCH 8/9] localize --- bin/review | 2 ++ lib/review/i18n.yml | 18 ++++++++++++++++++ lib/review/update.rb | 8 ++++---- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/bin/review b/bin/review index 7b7ed4a20..725201bf8 100755 --- a/bin/review +++ b/bin/review @@ -25,10 +25,12 @@ usage: review [] Re:VIEW commands are: init : generate Re:VIEW project directory. + update : update Re:VIEW project to newest environment. preproc : preprocess Re:VIEW document file. compile : convert Re:VIEW document file. epubmaker : build EPUB. webmaker : build web pages. + epub2html : convert EPUB to single HTML. pdfmaker : build PDF with LaTeX. textmaker : build text files. vol : show volume of Re:VIEW document. diff --git a/lib/review/i18n.yml b/lib/review/i18n.yml index f7726ed29..7813f50ca 100644 --- a/lib/review/i18n.yml +++ b/lib/review/i18n.yml @@ -62,6 +62,24 @@ ja: advtitle: "広告" profiletitle: "著者紹介" backcovertitle: "裏表紙" + "!! No *.yml file with 'review_version' was found. Aborted. !!": "!! 'review_version' を持つ *.yml ファイルが見つかりませんでした。中止します。 !!" + "!! %s file(s) is obsoleted. Run 'review-catalog-converter' to convert to 'catalog.yml' and remove old files. Aborted. !!": "!! %s ファイルはもう使われません。'review-catalog-converter' を実行して 'catalog.yml' に変換し、古いファイルは削除してください。中止します。 !!" + "** review-update updates your project to %s **": "** review-update はプロジェクトを %s に更新します **" + "** There is custom layouts/layout.tex.erb file. Updating may break to make PDF until you fix layout.tex.erb. Do you really proceed to update? **": "** 固有の layouts/layout.tex.erb ファイルが存在します。プロジェクトを更新した後、このファイルを修正しないと、PDF の生成に問題が生じるかもしれません。本当に進めますか? **" + "** There is review-ext.rb file. You need to update it by yourself. **": "** review-ext.rb ファイルが存在します。このファイルはご自身で更新してください。 **" + "%s: Update '%s' to '%s'?": "%s: '%s' を '%s' に更新しますか?" + "%s will be overridden with Re:VIEW version (%s). Do you really proceed?": "%s は Re:VIEW バージョンのもの (%s) で置き換えられます。本当に進めますか?" + "%s: 'chapter_quote' now takes 2 values. Update '%s' to '%s'?": "%s: 'chapter_quote' は2つの値を取るようになりました。'%s' を '%s' に更新しますか?" + "%s: !! 'texdocumentclass' uses new class '%s' already, but you specified '%s'. This tool can't handle such migration. Ignored. !!": "%s: !! 'texdocumentclass' は新クラス '%s' をすでに使うようになっていますが、'%s' が指定されました。このツールはそのような移行には対応していません。無視します。 !!" + "%s: 'texdocumentclass' uses the old class '%s'. By default it is migrated to '%s', but you specify '%s'. Do you really migrate 'texdocumentclass' to '%s'?": "%s: 'texdocumentclass' は旧クラス '%s' を使っています。デフォルトの移行先は '%s' ですが、'%s' が指定されました。本当に 'texdocumentclass' を '%s' に移行しますか?" + "%s: 'texdocumentclass' uses the old class '%s'. By default it is migrated to '%s'. Do you really migrate 'texdocumentclass' to '%s'?": "%s: 'texdocumentclass' は旧クラス '%s' を使っています。デフォルトの移行先は '%s' です。本当に 'texdocumentclass' を '%s' に移行しますか?" + "%s: previous 'texdocumentclass' option '%s' is safely replaced with '%s'.": "%s: 'texdocumentclass' の古いオプション '%s' は '%s' に安全に置き換えられました。" + "%s: previous 'texdocumentclass' option '%s' couldn't be converted fully. '%s' is suggested. Do you really proceed?": "%s: 'texdocumentclass' の古いオプション '%s' は完全には置き換えできませんでした。'%s' を提案します。本当に進めますか?" + "%s: ** 'texdocumentclass' specifies '%s'. Because this is unknown class for this tool, you need to update it by yourself if it won't work. **": "%s: ** 'texdocumentclass' はクラス '%s' を指定しています。このツールには未知のクラスなので、もし動作しないようであればご自身で更新する必要があります。 **" + "%s: ** '%s' is unknown class. Ignored. **": "%s: ** '%s' は未知のクラスです。無視します。 **" + "%s: 'texcommand' has options ('%s'). Move it to 'texoptions'?": "%s: 'texcommand' にオプションがあります ('%s')。'texoptions' に移動しますか?" + "%s: 'dvicommand' has options ('%s'). Move it to 'dvioptions'?": "%s: 'dvicommand' にオプションがあります ('%s')。'dvioptions' に移動しますか?" + "Finished.": "完了しました。" en: image: "Figure " diff --git a/lib/review/update.rb b/lib/review/update.rb index 3ed694925..83f6eec85 100644 --- a/lib/review/update.rb +++ b/lib/review/update.rb @@ -156,7 +156,7 @@ def parse_options(args) end def parse_ymls(dir) - language = 'ja' + language = 'en' Dir.glob(File.join(dir, '*.yml')).sort.each do |yml| begin @@ -238,7 +238,7 @@ def update_version next end - if confirm("%s: Update 'review_version' to '%s'?", [File.basename(yml), TARGET_VERSION]) + if confirm("%s: Update '%s' to '%s'?", [File.basename(yml), 'review_version', TARGET_VERSION]) rewrite_yml(yml, 'review_version', TARGET_VERSION) end end @@ -282,14 +282,14 @@ def update_epub_version @epub_ymls.each do |yml| config = YAML.load_file(yml) if config['epubversion'].present? && config['epubversion'].to_f < EPUB_VERSION.to_f - if confirm("%s: Update 'epubversion' to '%s' from '%s'?", [File.basename(yml), EPUB_VERSION, config['epubversion']]) + if confirm("%s: Update '%s' to '%s' from '%s'?", [File.basename(yml), 'epubversion', EPUB_VERSION, config['epubversion']]) rewrite_yml(yml, 'epubversion', EPUB_VERSION) end end if !config['htmlversion'].present? || config['htmlversion'].to_f >= HTML_VERSION.to_f next end - if confirm("%s: Update 'htmlversion' to '%s' from '%s'?", [File.basename(yml), HTML_VERSION, config['htmlversion']]) + if confirm("%s: Update '%s' to '%s' from '%s'?", [File.basename(yml), 'htmlversion', HTML_VERSION, config['htmlversion']]) rewrite_yml(yml, 'htmlversion', HTML_VERSION) end end From 3ff5d8bf6bb339772d3ef1c620eb1b22f491230a Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Tue, 23 Oct 2018 18:25:27 +0900 Subject: [PATCH 9/9] refactoring. confirm also when it is upper version --- lib/review/update.rb | 35 ++++++++++++++++++++--------------- test/test_update.rb | 2 +- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/review/update.rb b/lib/review/update.rb index 3ed694925..43e9129c7 100644 --- a/lib/review/update.rb +++ b/lib/review/update.rb @@ -59,7 +59,7 @@ def execute(*args) show_version if @config_ymls.empty? - @logger.error _("!! No *.yml file with 'review_version' was found. Aborted. !!") + @logger.error t("!! No *.yml file with 'review_version' was found. Aborted. !!") raise ApplicationError end @@ -75,12 +75,12 @@ def execute(*args) update_tex_command update_dvi_command - puts _('Finished.') + puts t('Finished.') rescue ApplicationError exit 1 end - def _(message, args = []) + def t(message, args = []) unless I18n.get(message) I18n.set(message, message) # just copy end @@ -89,7 +89,7 @@ def _(message, args = []) def confirm(message, args = [], default = true) if @force - @logger.info _(message, args) + @logger.info t(message, args) if default @logger.info ' yes' return true @@ -100,7 +100,7 @@ def confirm(message, args = [], default = true) end loop do - print _(message, args) + print t(message, args) if default print ' [y]/n ' else @@ -123,7 +123,7 @@ def rewrite_yml(yml, key, val) if @backup FileUtils.mv yml, "#{yml}-old" end - File.open(yml, 'w') { |f| f.write(content) } + File.write(yml, content) end def parse_options(args) @@ -210,13 +210,13 @@ def check_old_catalogs(dir) end.compact unless files.empty? - @logger.error _("!! %s file(s) is obsoleted. Run 'review-catalog-converter' to convert to 'catalog.yml' and remove old files. Aborted. !!", files.join(', ')) + @logger.error t("!! %s file(s) is obsoleted. Run 'review-catalog-converter' to convert to 'catalog.yml' and remove old files. Aborted. !!", files.join(', ')) raise ApplicationError end end def show_version - puts _('** review-update updates your project to %s **', ReVIEW::VERSION) + puts t('** review-update updates your project to %s **', ReVIEW::VERSION) end def check_own_files(dir) @@ -227,18 +227,23 @@ def check_own_files(dir) end if File.exist?(File.join(dir, 'review-ext.rb')) - @logger.info _('** There is review-ext.rb file. You need to update it by yourself. **') + @logger.info t('** There is review-ext.rb file. You need to update it by yourself. **') end end def update_version @config_ymls.each do |yml| config = YAML.load_file(yml) - if config['review_version'].to_f >= TARGET_VERSION.to_f + if config['review_version'].to_f == TARGET_VERSION.to_f next end - if confirm("%s: Update 'review_version' to '%s'?", [File.basename(yml), TARGET_VERSION]) + flag = true + if config['review_version'].to_f > TARGET_VERSION.to_f + flag = nil + end + + if confirm("%s: Update 'review_version' to '%s'?", [File.basename(yml), TARGET_VERSION], flag) rewrite_yml(yml, 'review_version', TARGET_VERSION) end end @@ -318,7 +323,7 @@ def update_tex_parameters if TEX_DOCUMENTCLASS.include?(config['texdocumentclass'][0]) if @specified_template.present? && config['texdocumentclass'][0] != @specified_template # want to use other template? - @logger.error _("%s: !! 'texdocumentclass' uses new class '%s' already, but you specified '%s'. This tool can't handle such migration. Ignored. !!", [File.basename(yml), config['texdocumentclass'][0], @specified_template]) + @logger.error t("%s: !! 'texdocumentclass' uses new class '%s' already, but you specified '%s'. This tool can't handle such migration. Ignored. !!", [File.basename(yml), config['texdocumentclass'][0], @specified_template]) @template = nil end next @@ -350,7 +355,7 @@ def update_tex_parameters flag, modified_opts = convert_documentclass_opts(yml, @template, config['texdocumentclass'][1]) if flag # successfully converted - @logger.info _("%s: previous 'texdocumentclass' option '%s' is safely replaced with '%s'.", [File.basename(yml), config['texdocumentclass'][1], modified_opts]) + @logger.info t("%s: previous 'texdocumentclass' option '%s' is safely replaced with '%s'.", [File.basename(yml), config['texdocumentclass'][1], modified_opts]) else # something wrong unless confirm("%s: previous 'texdocumentclass' option '%s' couldn't be converted fully. '%s' is suggested. Do you really proceed?", [File.basename(yml), config['texdocumentclass'][1], modified_opts], nil) @template = nil @@ -361,7 +366,7 @@ def update_tex_parameters rewrite_yml(yml, 'texdocumentclass', %Q(["#{@template}", "#{modified_opts}"])) else @template = nil - @logger.error _("%s: ** 'texdocumentclass' specifies '%s'. Because this is unknown class for this tool, you need to update it by yourself if it won't work. **", [File.basename(yml), config['texdocumentclass']]) + @logger.error t("%s: ** 'texdocumentclass' specifies '%s'. Because this is unknown class for this tool, you need to update it by yourself if it won't work. **", [File.basename(yml), config['texdocumentclass']]) end end end @@ -427,7 +432,7 @@ def convert_documentclass_opts(yml, cls, prev_opts) opts << 'cover=false' else flag = nil - @logger.error _("%s: ** '%s' is unknown class. Ignored. **", [File.basename(yml), cls]) + @logger.error t("%s: ** '%s' is unknown class. Ignored. **", [File.basename(yml), cls]) end return flag, opts.join(',') end diff --git a/test/test_update.rb b/test/test_update.rb index c12281f09..190b4f367 100644 --- a/test/test_update.rb +++ b/test/test_update.rb @@ -118,7 +118,7 @@ def test_update_version_newer @u.instance_eval{ @logger = ReVIEW::Logger.new(io) } @u.parse_ymls(@tmpdir) @u.update_version - assert_equal '', io.string + assert_match(/Update 'review_version' to '3.0'/, io.string) assert_equal 'review_version: 99.0', File.read(File.join(@tmpdir, 'config.yml')).match(/review_version:.*$/).to_s end