diff options
| author | Charlotte Koch <charlotte@magentastripe.com> | 2024-06-09 15:34:28 -0700 |
|---|---|---|
| committer | Charlotte Koch <charlotte@magentastripe.com> | 2024-06-09 15:34:28 -0700 |
| commit | 5ee36725a963de52b219069d7ae592110a994ac8 (patch) | |
| tree | 6f96a2e1775ce7cb2266f0dfcfd8a01993faacf3 /bin | |
| parent | 4ea0ee9a6d5e77ae9220625cffcdd7c3d6ea825f (diff) | |
Add willora(1) which can generate a project directory for you
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/willora | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/bin/willora b/bin/willora new file mode 100755 index 0000000..9b3415d --- /dev/null +++ b/bin/willora @@ -0,0 +1,149 @@ +#!/usr/bin/env ruby +# +# WilloraPDF command line tool +# Charlotte Koch <charlotte@magentastripe.com> +# + +require 'erb' +require 'fileutils' + +########## ########## ########## + +class Willora + VERSION = "0.0.0a" + DEFAULT_TITLE = "My Cool Book" + DEFAULT_AUTHOR = "Allen Smithee" + + attr_reader :errormsg + + def initialize(**opts) + @opts = opts + @errormsg = nil + end + + def main! + puts "WilloraPDF v#{VERSION}" + + rv = 0 + + case @opts[:command] + when :new + rv = self.new_project + else + @errormsg = "unknown command: #{@opts[:command].inspect}" + rv = 1 + end + + return rv + end + + def new_project + # Make sure the WilloraPDF base files seem OK. + _willorabase = File.join(__dir__, "..") + _templatedir = File.join(_willorabase, "templates") + + #epubassetsdir = File.join(_willorabase, "epub-assets") + fontsdir = File.join(_willorabase, "fonts") + libdir = File.join(_willorabase, "lib") + mkdir = File.join(_willorabase, "mk") + scriptdir = File.join(_willorabase, "script") + themedir = File.join(_willorabase, "themes") + + alldirs = [fontsdir, libdir, mkdir, themedir, scriptdir] + + alldirs.each do |dir| + if not File.directory?(dir) + @errormsg = "cannot find required directory, WilloraPDF installation broken? (#{dir})" + return 1 + end + end + + # Fill in some gaps before we continue. + if !@opts[:nickname] + @errormsg = "New project requires a nickname" + return 1 + end + + if @opts[:nickname].split(/\s+/).length > 1 + @errormsg = "Nickname cannot have any spaces" + return 1 + end + + @opts[:title] = DEFAULT_TITLE if !@opts[:title] + @opts[:author] = DEFAULT_AUTHOR if !@opts[:author] + + # Create the new project directory and copy the files over. + realdestbase = File.join(@opts[:destination], @opts[:nickname]) + FileUtils.mkdir_p(realdestbase, :verbose => true) + + alldirs.each do |dir| + basename = File.basename(dir) + realdest = File.join(realdestbase, basename) + puts "#{basename} => #{realdest}" + FileUtils.copy_entry(dir, realdest) + end + + # Fill it up with some new templatized files. + templates = { + "Makefile.erb" => "", + + "CHAP01.adoc.erb" => "adoc", + "frontmatter.adoc.erb" => "adoc", + "part01.adoc.erb" => "adoc", + } + + templates.each do |template, destname| + destdir = File.join(realdestbase, destname) + if not File.directory?(destdir) + FileUtils.mkdir_p(destdir, :verbose => true) + end + templatefile = File.join(_templatedir, template) + result = ERB.new(File.read(templatefile)).result(binding) + newfile = File.join(destdir, File.basename(template, ".erb")) + puts "GENERATE #{newfile}" + File.open(newfile, "w") do |fp| + fp.puts(result) + end + end + + # Leftovers. + converter_class_orig = File.join(_willorabase, "lib", "willora_pdf_converter.rb") + converter_class_new = File.join(realdestbase, "lib", "this_pdf_converter.rb") + puts "#{converter_class_orig} => #{converter_class_new}" + FileUtils.copy_entry(converter_class_orig, converter_class_new) + + puts "New project #{@opts[:nickname].inspect} is ready: #{realdestbase}" + return 0 + end +end + +########## ########## ########## + +if $0 == __FILE__ + require 'optparse' + + opts = {} + + # Parse command-line parameters. + parser = OptionParser.new do |op| + op.on("-h", "--help") { opts[:command] = :help } + op.on("-n", "--new") { opts[:command] = :new } + op.on("--title TITLE") { |title| opts[:title] = title } + op.on("--nickname NAME") { |nick| opts[:nickname] = nick } + op.on("--author AUTHOR") { |author| opts[:author] = author } + op.on("--publisher PUBLISHER") { |publisher| opts[:publisher] = publisher } + op.on("--destination DIR") { |dir| opts[:destination] = File.expand_path(dir) } + end + parser.parse!(ARGV) + + if opts[:command] == :help + puts(parser.to_s) + exit 0 + end + + # Do it. + w = Willora.new(**opts) + rv = w.main! + $stderr.puts("FATAL: #{w.errormsg}") if w.errormsg + exit rv +end |
