From 675606ee7d8dbec1b9ecf89718630447bfdabfdb Mon Sep 17 00:00:00 2001 From: Brian Dominick Date: Fri, 15 Feb 2019 11:18:00 -0500 Subject: [PATCH] Fix options bug; add ability to skip missing migrate sources (#60) Fix options bug; add ability to skip missing migrate sources The `options:` setting for `migrate` actions was reading an erroneous truthiness to test for the existence of a setting. Fixed this so opt settings other than `inclusive: false` actually work. Added the ability to set an option `missing:` with either `warn`, `skip`, or `exit` as values. If `missing: warn`, the operation skips the migrate step and logs a warning to console. If `missing: exit` (the default, w/ backward compatibility) the operation throws an error and exits. Added example and explanation for new `missing:` setting to README. * Remove erroneous require (from testing) * Rearrange missing handling; add 'skip' option * Bump version to 0.9.5 --- README.adoc | 14 ++++++++++++-- lib/liquidoc.rb | 32 +++++++++++++++++++++++++++----- lib/liquidoc/version.rb | 2 +- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/README.adoc b/README.adoc index 70ffdbe..d934c3d 100644 --- a/README.adoc +++ b/README.adoc @@ -554,14 +554,19 @@ In addition to designating `action: migrate`, migrate operations require just a [source,yaml] .Example config -- Instructing file copies with 'migrate' action ---- +- action: migrate + source: index.adoc + target: _build/ - action: migrate source: assets/images target: _build/img options: inclusive: false - action: migrate - source: index-map.adoc - target: _build/index-map.adoc + source: tmp/{{imported_file}}.adoc + target: _build/{{portal_path}}/{{imported_file}}.adoc + options: + missing: warn ---- The first action step above copies all the files and folders in `assets/images` and adds them to `_build/img`. @@ -570,6 +575,11 @@ When both the source and target paths are directories and inclusive is `true`, t When inclusive is `false`, they copy to `target/`. Individual files must be listed in individual steps, one per step, as in the second step above. + +In case of a missing source directory or file to be migrated, the default behavior is to exit the build operation (`missing: exit`). +This can be overridden and the migrate action skipped when the source is missing. +Setting the option `missing: warn` logs a warning to console, and `missing: skip` will only print a warning under `--verbose` operations. + // end::migrate-operations[] === Render Operations diff --git a/lib/liquidoc.rb b/lib/liquidoc.rb index 035eae4..cf31de6 100755 --- a/lib/liquidoc.rb +++ b/lib/liquidoc.rb @@ -123,8 +123,12 @@ def iterate_build cfg end when "migrate" inclusive = true - inclusive = step.options['inclusive'] if defined?(step.options['inclusive']) - copy_assets(step.source, step.target, inclusive) + missing = "exit" + if step.options + inclusive = step.options['inclusive'] if step.options.has_key?("inclusive") + missing = step.options['missing'] if step.options.has_key?("missing") + end + copy_assets(step.source, step.target, inclusive, missing) when "render" validate_file_input(step.source, "source") if step.source builds = step.builds @@ -252,6 +256,7 @@ def initialize step if (defined?(@step['action'])).nil? raise "ConfigStructError" end + @step['options'] = nil unless defined?(step['options']) validate() end @@ -787,11 +792,25 @@ def regurgidata datasrc, output # === # Copy images and other files into target dir -def copy_assets src, dest, inclusive=true +def copy_assets src, dest, inclusive=true, missing='exit' unless File.file?(src) unless inclusive then src = src + "/." end end - @logger.debug "Copying #{src} to #{dest}" + src_to_dest = "#{src} to #{dest}" + unless (File.file?(src) || File.directory?(src)) + case missing + when "warn" + @logger.warn "Skipping migrate action (#{src_to_dest}); source not found." + return + when "skip" + @logger.debug "Skipping migrate action (#{src_to_dest}); source not found." + return + when "exit" + @logger.error "Unexpected missing source in migrate action (#{src_to_dest})." + raise "MissingSourceExit" + end + end + @logger.debug "Copying #{src_to_dest}" begin FileUtils.mkdir_p(dest) unless File.directory?(dest) if File.directory?(src) @@ -801,7 +820,7 @@ def copy_assets src, dest, inclusive=true end @logger.info "Copied #{src} to #{dest}." rescue Exception => ex - @logger.warn "Problem while copying assets. #{ex.message}" + @logger.error "Problem while copying assets. #{ex.message}" raise end end @@ -905,6 +924,9 @@ def asciidocify doc, build if build.backend == "pdf" @logger.info "Generating PDF. This can take some time..." end + + + Asciidoctor.convert_file( doc.index, to_file: to_file, diff --git a/lib/liquidoc/version.rb b/lib/liquidoc/version.rb index 97d067d..fa5224a 100644 --- a/lib/liquidoc/version.rb +++ b/lib/liquidoc/version.rb @@ -1,3 +1,3 @@ module Liquidoc - VERSION = "0.9.4" + VERSION = "0.9.5" end