diff options
| author | Charlotte Koch <charlotte@magentastripe.com> | 2024-07-16 11:16:07 -0700 |
|---|---|---|
| committer | Charlotte Koch <charlotte@magentastripe.com> | 2024-07-16 11:16:43 -0700 |
| commit | e67326df41041ee6946e20ea8ddda30c22536a2e (patch) | |
| tree | 5e1040039e82e889abedaedbb45752fd0d371d5a /bin | |
| parent | 7a2c426add338b6fd3c62271903caa74118c92a2 (diff) | |
The meat and potatoes of willora(1) are now in a Ruby-ish 'lib/'
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/willora | 186 |
1 files changed, 30 insertions, 156 deletions
diff --git a/bin/willora b/bin/willora index dc272e8..37591b0 100755 --- a/bin/willora +++ b/bin/willora @@ -6,167 +6,41 @@ require 'erb' require 'fileutils' +require 'optparse' -########## ########## ########## +$LOAD_PATH.unshift File.join(__dir__, "..", "lib") +require 'willora/willora' -class Willora - VERSION = "0.0.0a" - DEFAULT_TITLE = "My Cool Book" - DEFAULT_AUTHOR = "Allen Smithee" - DEFAULT_PUBLISHER = "Made-Up Press" +opts = { + :willorabase => File.join(__dir__, ".."), +} - attr_reader :errormsg +command = ARGV.shift - 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__, "..") - _projectfiles = File.join(_willorabase, "project-files") - _templatedir = File.join(_willorabase, "templates") - - #epubassetsdir = File.join(_willorabase, "epub-assets") - fontsdir = File.join(_willorabase, "fonts") - libdir = File.join(_projectfiles, "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 - - if !@opts[:destination] - @errormsg = "Destination directory is required" - return 1 - 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 - - realdestbase = File.join(@opts[:destination], @opts[:nickname]) - - if File.exists?(realdestbase) - @errormsg = "Directory already exists: #{realdestbase.inspect}" - return 1 - end - - @opts[:title] = DEFAULT_TITLE if !@opts[:title] - @opts[:author] = DEFAULT_AUTHOR if !@opts[:author] - @opts[:publisher] = DEFAULT_PUBLISHER if !@opts[:publisher] - - # Create the new project directory and copy the files over. - FileUtils.mkdir_p(realdestbase, :verbose => true) - - alldirs << File.join(_willorabase, "Gemfile") - - 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(libdir, "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 +if ["help", "new"].include?(command) + opts[:command] = command.intern +else + $stderr.puts("FATAL: unknown command #{command.inspect}") + exit 1 end -########## ########## ########## - -if $0 == __FILE__ - require 'optparse' - - opts = {} - - command = ARGV.shift - - if ["help", "new"].include?(command) - opts[:command] = command.intern - else - $stderr.puts("FATAL: unknown command #{command.inspect}") - exit 1 - end - - # Parse command-line parameters. - parser = OptionParser.new do |op| - 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 !command || command == :help - puts(parser.to_s) - exit 0 - end +# Parse command-line parameters. +parser = OptionParser.new do |op| + 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) - # Do it. - w = Willora.new(**opts) - rv = w.main! - $stderr.puts("FATAL: #{w.errormsg}") if w.errormsg - exit rv +if !command || 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 |
