#!/usr/bin/env ruby # # Copyright (c) 2013-2014 Masanori Kado # # 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 'pathname' bindir = Pathname.new(__FILE__).realpath.dirname $LOAD_PATH.unshift((bindir + '../lib').realpath) require 'review/version' def main opts = OptionParser.new opts.version = ReVIEW::VERSION opts.banner = "Usage: #{File.basename($0)} [option] dirname" opts.on('-h', '--help', 'print this message and quit.') { puts opts.help exit 0 } opts.on('-f', '--force', 'generate files (except *.re) if directory has already existed.') { @force = true } opts.on('-l', '--locale', 'generate locale.yml file.') { @locale = true } begin opts.parse! rescue OptionParser::ParseError => err $stderr.puts err.message $stderr.puts opts.help exit 1 end if ARGV.empty? $stderr.puts opts.help exit 1 end initdir = File.expand_path(ARGV.shift) @review_dir = File.dirname(File.expand_path(__FILE__ + "./../")) generate_dir(initdir) do |dir| generate_catalog_file(dir) generate_sample(dir) generate_images_dir(dir) generate_cover_image(dir) generate_layout(dir) generate_style(dir) generate_texmacro(dir) generate_config(dir) generate_locale(dir) if @locale generate_rakefile(dir) end end def generate_dir(dir) if File.exist?(dir) && !@force puts "#{dir} already exists." exit end FileUtils.mkdir_p dir yield dir end def generate_sample(dir) if !@force File.open("#{dir}/#{File.basename(dir)}.re", "w") do |file| file.write("= ") end end end def generate_layout(dir) FileUtils.mkdir_p dir + '/layouts' File.open("#{dir}/layouts/layout.html.erb", "w") do |file| file.write <<-EOS <%= title %> <%= body %> EOS end end def generate_catalog_file(dir) File.open(dir + "/catalog.yml", "w") do |file| file.write <<-EOS PREDEF: CHAPS: - #{File.basename(dir)}.re APPENDIX: POSTDEF: EOS end end def generate_images_dir(dir) FileUtils.mkdir_p dir + '/images' end def generate_cover_image(dir) FileUtils.cp @review_dir + "/test/sample-book/src/images/cover.jpg", dir + '/images/' end def generate_config(dir) FileUtils.cp @review_dir + "/test/sample-book/src/config.yml", dir end def generate_style(dir) FileUtils.cp @review_dir + "/test/sample-book/src/style.css", dir end def generate_texmacro(dir) texmacrodir = dir + '/sty' FileUtils.mkdir_p texmacrodir FileUtils.cp @review_dir + "/test/sample-book/src/sty/reviewmacro.sty", texmacrodir end def generate_rakefile(dir) FileUtils.cp @review_dir + "/test/sample-book/src/Rakefile", dir end def generate_locale(dir) FileUtils.cp @review_dir + '/lib/review/i18n.yml', dir + '/locale.yml' end if File.basename($0) == File.basename(__FILE__) main() end