From 594b5f853a4adc644ef8ae5ffa4f680f9c1769a5 Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Sun, 23 Apr 2023 15:59:29 +0200 Subject: [PATCH] refactor: shorten names & use same structure --- src/{builder => build}/cli.cr | 6 +- src/build/config.cr | 11 +++ src/build/run.cr | 122 ++++++++++++++++++++++++ src/builder/config.cr | 13 --- src/builder/run.cr | 125 ------------------------- src/cli.cr | 19 ++-- src/plan/cli.cr | 15 +++ src/{planner => plan}/config.cr | 2 +- src/{planner/runner.cr => plan/run.cr} | 0 src/planner/cli.cr | 17 ---- src/scaffold/cli.cr | 31 ++++++ src/{scaffolder => scaffold}/config.cr | 2 +- src/{scaffolder => scaffold}/run.cr | 4 +- src/{scaffolder => write}/cli.cr | 7 +- src/write/config.cr | 11 +++ src/write/run.cr | 61 ++++++++++++ 16 files changed, 274 insertions(+), 172 deletions(-) rename src/{builder => build}/cli.cr (84%) create mode 100644 src/build/config.cr create mode 100644 src/build/run.cr delete mode 100644 src/builder/config.cr delete mode 100644 src/builder/run.cr create mode 100644 src/plan/cli.cr rename src/{planner => plan}/config.cr (87%) rename src/{planner/runner.cr => plan/run.cr} (100%) delete mode 100644 src/planner/cli.cr create mode 100644 src/scaffold/cli.cr rename src/{scaffolder => scaffold}/config.cr (84%) rename src/{scaffolder => scaffold}/run.cr (93%) rename src/{scaffolder => write}/cli.cr (81%) create mode 100644 src/write/config.cr create mode 100644 src/write/run.cr diff --git a/src/builder/cli.cr b/src/build/cli.cr similarity index 84% rename from src/builder/cli.cr rename to src/build/cli.cr index fcd6d86..8cdb4dc 100644 --- a/src/builder/cli.cr +++ b/src/build/cli.cr @@ -1,12 +1,12 @@ require "./config" -module DocMachine::Builder +module DocMachine::Build class Cli def self.add_options(opts, args, parent_config, commands) config = Config.new(parent_config) - opts.on("build", "Build content and produce deliverables") do + opts.on("build", "Build content and produce HTML & PDF deliverables") do opts.banner = [ "Usage: #{PROGRAM_NAME} build [options]", "", @@ -29,7 +29,7 @@ module DocMachine::Builder end commands << ->() : Nil do - app = DocMachine::Builder::Run.new(config) + app = DocMachine::Build::Run.new(config) app.prepare app.start app.wait diff --git a/src/build/config.cr b/src/build/config.cr new file mode 100644 index 0000000..3c1b2e5 --- /dev/null +++ b/src/build/config.cr @@ -0,0 +1,11 @@ + +module DocMachine::Build + class Config + property data_dir : String = Dir.current + property action : String = "watch" + property enable_tty : Bool = false + + def initialize(@parent : DocMachine::Config) + end + end +end diff --git a/src/build/run.cr b/src/build/run.cr new file mode 100644 index 0000000..a7591a0 --- /dev/null +++ b/src/build/run.cr @@ -0,0 +1,122 @@ + +require "./config" + +module DocMachine::Build + class Run + def initialize(@config : DocMachine::Build::Config) + @basehash = Digest::SHA256.hexdigest(@config.data_dir)[0..6] + @docker_name = "docmachine-#{@basehash}" + @docker_image = "glenux/docmachine:latest" + @docker_opts = [] of String + @process = nil + end + + + # cleanup environment + # create directories + # setup permissions + def prepare() + puts "basedir = #{@config.data_dir}" + puts "docker_image = #{@docker_image}" + puts "action = #{@config.action}" + + docker_cid = %x{docker ps -f "name=#{@docker_name}" -q}.strip + + puts "docker_name: #{@docker_name}" + puts "docker_cid: #{docker_cid}" + + if !docker_cid.empty? + Process.run("docker", ["kill", @docker_name]) + end + end + + def start() + uid = %x{id -u}.strip + gid = %x{id -g}.strip + puts "uid: #{uid}" + puts "cid: #{gid}" + + docker_opts = [] of String + docker_opts << "run" + docker_opts << "-i" + # add tty support + docker_opts << "-t" if @config.enable_tty + # add container name + docker_opts.concat ["--name", @docker_name] + docker_opts << "--rm" + docker_opts << "--shm-size=1gb" + docker_opts.concat ["-e", "EXT_UID=#{uid}"] + docker_opts.concat ["-e", "EXT_GID=#{gid}"] + docker_opts.concat ["-v", "#{@config.data_dir}/docs:/app/docs"] + docker_opts.concat ["-v", "#{@config.data_dir}/slides:/app/slides"] + docker_opts.concat ["-v", "#{@config.data_dir}/images:/app/images"] + docker_opts.concat ["-v", "#{@config.data_dir}/_build:/app/_build"] + + ## Detect Marp SCSS + if File.exists?("#{@config.data_dir}/.marp/theme.scss") + docker_opt_marp_theme = ["-v", "#{@config.data_dir}/.marp:/app/.marp"] + docker_opts.concat docker_opt_marp_theme + puts "Theme: detected Marp files. Adding option to command line (#{docker_opt_marp_theme})" + else + puts "Theme: no theme detected. Using default files" + end + + ## Detect Mkdocs configuration - old format (full) + if File.exists?("#{@config.data_dir}/mkdocs.yml") + puts "Mkdocs: detected mkdocs.yml file. Please rename to mkdocs-patch.yml" + exit 1 + end + + ## Detect Mkdocs configuration - new format (patch) + if File.exists?("#{@config.data_dir}/mkdocs-patch.yml") + docker_opt_mkdocs_config = ["-v", "#{@config.data_dir}/mkdocs-patch.yml:/app/mkdocs-patch.yml"] + docker_opts.concat docker_opt_mkdocs_config + puts "Mkdocs: detected mkdocs-patch.yml file. Adding option to command line (#{docker_opt_mkdocs_config})" + else + puts "Mkdocs: no mkdocs-patch.yml detected. Using default files" + end + + ## Detect slides + if Dir.exists?("#{@config.data_dir}/slides") + docker_opt_marp_port = ["-p", "5200:5200"] + docker_opts.concat docker_opt_marp_port + puts "Slides: detected slides directory. Adding option to command line (#{docker_opt_marp_port})" + else + puts "Slides: no slides directory detected." + end + + ## Detect docs + if Dir.exists?("#{@config.data_dir}/docs") + docker_opt_marp_port = ["-p", "5100:5100"] + docker_opts.concat docker_opt_marp_port + puts "Slides: detected docs directory. Adding option to command line (#{docker_opt_marp_port})" + else + puts "Slides: no slides docs detected." + end + + docker_opts << @docker_image + docker_opts << @config.action + + puts docker_opts.inspect.colorize(:yellow) + @process = Process.new("docker", docker_opts, output: STDOUT, error: STDERR) + end + + def wait() + process = @process + return if process.nil? + + Signal::INT.trap do + STDERR.puts "Received CTRL-C" + process.signal(Signal::KILL) + Process.run("docker", ["kill", @docker_name]) + end + process.wait + end + + def stop() + end + + def docker_opts() + end + end +end diff --git a/src/builder/config.cr b/src/builder/config.cr deleted file mode 100644 index 67787c9..0000000 --- a/src/builder/config.cr +++ /dev/null @@ -1,13 +0,0 @@ - -module DocMachine - module Builder - class Config - property data_dir : String = Dir.current - property action : String = "watch" - property enable_tty : Bool = false - - def initialize(@parent : DocMachine::Config) - end - end - end -end diff --git a/src/builder/run.cr b/src/builder/run.cr deleted file mode 100644 index 1700062..0000000 --- a/src/builder/run.cr +++ /dev/null @@ -1,125 +0,0 @@ - -require "./config" - -module DocMachine - module Builder - - class Run - def initialize(@config : DocMachine::Builder::Config) - @basehash = Digest::SHA256.hexdigest(@config.data_dir)[0..6] - @docker_name = "docmachine-#{@basehash}" - @docker_image = "glenux/docmachine:latest" - @docker_opts = [] of String - @process = nil - end - - - # cleanup environment - # create directories - # setup permissions - def prepare() - puts "basedir = #{@config.data_dir}" - puts "docker_image = #{@docker_image}" - puts "action = #{@config.action}" - - docker_cid = %x{docker ps -f "name=#{@docker_name}" -q}.strip - - puts "docker_name: #{@docker_name}" - puts "docker_cid: #{docker_cid}" - - if !docker_cid.empty? - Process.run("docker", ["kill", @docker_name]) - end - end - - def start() - uid = %x{id -u}.strip - gid = %x{id -g}.strip - puts "uid: #{uid}" - puts "cid: #{gid}" - - docker_opts = [] of String - docker_opts << "run" - docker_opts << "-i" - # add tty support - docker_opts << "-t" if @config.enable_tty - # add container name - docker_opts.concat ["--name", @docker_name] - docker_opts << "--rm" - docker_opts << "--shm-size=1gb" - docker_opts.concat ["-e", "EXT_UID=#{uid}"] - docker_opts.concat ["-e", "EXT_GID=#{gid}"] - docker_opts.concat ["-v", "#{@config.data_dir}/docs:/app/docs"] - docker_opts.concat ["-v", "#{@config.data_dir}/slides:/app/slides"] - docker_opts.concat ["-v", "#{@config.data_dir}/images:/app/images"] - docker_opts.concat ["-v", "#{@config.data_dir}/_build:/app/_build"] - - ## Detect Marp SCSS - if File.exists?("#{@config.data_dir}/.marp/theme.scss") - docker_opt_marp_theme = ["-v", "#{@config.data_dir}/.marp:/app/.marp"] - docker_opts.concat docker_opt_marp_theme - puts "Theme: detected Marp files. Adding option to command line (#{docker_opt_marp_theme})" - else - puts "Theme: no theme detected. Using default files" - end - - ## Detect Mkdocs configuration - old format (full) - if File.exists?("#{@config.data_dir}/mkdocs.yml") - puts "Mkdocs: detected mkdocs.yml file. Please rename to mkdocs-patch.yml" - exit 1 - end - - ## Detect Mkdocs configuration - new format (patch) - if File.exists?("#{@config.data_dir}/mkdocs-patch.yml") - docker_opt_mkdocs_config = ["-v", "#{@config.data_dir}/mkdocs-patch.yml:/app/mkdocs-patch.yml"] - docker_opts.concat docker_opt_mkdocs_config - puts "Mkdocs: detected mkdocs-patch.yml file. Adding option to command line (#{docker_opt_mkdocs_config})" - else - puts "Mkdocs: no mkdocs-patch.yml detected. Using default files" - end - - ## Detect slides - if Dir.exists?("#{@config.data_dir}/slides") - docker_opt_marp_port = ["-p", "5200:5200"] - docker_opts.concat docker_opt_marp_port - puts "Slides: detected slides directory. Adding option to command line (#{docker_opt_marp_port})" - else - puts "Slides: no slides directory detected." - end - - ## Detect docs - if Dir.exists?("#{@config.data_dir}/docs") - docker_opt_marp_port = ["-p", "5100:5100"] - docker_opts.concat docker_opt_marp_port - puts "Slides: detected docs directory. Adding option to command line (#{docker_opt_marp_port})" - else - puts "Slides: no slides docs detected." - end - - docker_opts << @docker_image - docker_opts << @config.action - - puts docker_opts.inspect.colorize(:yellow) - @process = Process.new("docker", docker_opts, output: STDOUT, error: STDERR) - end - - def wait() - process = @process - return if process.nil? - - Signal::INT.trap do - STDERR.puts "Received CTRL-C" - process.signal(Signal::KILL) - Process.run("docker", ["kill", @docker_name]) - end - process.wait - end - - def stop() - end - - def docker_opts() - end - end - end -end diff --git a/src/cli.cr b/src/cli.cr index d6627de..36bfed7 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -3,10 +3,14 @@ require "digest/sha256" require "colorize" require "./config" -require "./builder/cli" -require "./builder/run" -require "./scaffolder/cli" -require "./planner/cli" +require "./build/cli" +require "./build/run" +require "./scaffold/cli" +require "./scaffold/run" +require "./plan/cli" +require "./plan/run" +require "./write/cli" +require "./write/run" module DocMachine class Cli @@ -36,9 +40,10 @@ module DocMachine opts.separator "" opts.separator "Commands:" - DocMachine::Builder::Cli.add_options(opts, args, config, commands) - DocMachine::Scaffolder::Cli.add_options(opts, args, config, commands) - DocMachine::Planner::Cli.add_options(opts, args, config, commands) + DocMachine::Scaffold::Cli.add_options(opts, args, config, commands) + DocMachine::Plan::Cli.add_options(opts, args, config, commands) + DocMachine::Write::Cli.add_options(opts, args, config, commands) + DocMachine::Build::Cli.add_options(opts, args, config, commands) end parser.parse(args) diff --git a/src/plan/cli.cr b/src/plan/cli.cr new file mode 100644 index 0000000..5becba0 --- /dev/null +++ b/src/plan/cli.cr @@ -0,0 +1,15 @@ + +require "./config" + +module DocMachine::Plan + class Cli + def self.add_options(opts, args, parent_config, command) + config = Config.new(parent_config) + + opts.on("plan", "Generate content structure (beta)") do + opts.banner = "Usage: #{PROGRAM_NAME} plan [options]" + opts.on("-t", "--test", "Test") { puts "Test" } + end + end + end +end diff --git a/src/planner/config.cr b/src/plan/config.cr similarity index 87% rename from src/planner/config.cr rename to src/plan/config.cr index 6444962..181d274 100644 --- a/src/planner/config.cr +++ b/src/plan/config.cr @@ -1,6 +1,6 @@ module DocMachine - module Planner + module Plan class Config def initialize(@parent : DocMachine::Config) end diff --git a/src/planner/runner.cr b/src/plan/run.cr similarity index 100% rename from src/planner/runner.cr rename to src/plan/run.cr diff --git a/src/planner/cli.cr b/src/planner/cli.cr deleted file mode 100644 index c82b47b..0000000 --- a/src/planner/cli.cr +++ /dev/null @@ -1,17 +0,0 @@ - -require "./config" - -module DocMachine - module Planner - class Cli - def self.add_options(opts, args, parent_config, command) - config = Config.new(parent_config) - - opts.on("content", "Generate content and structure") do - opts.banner = "Usage: #{PROGRAM_NAME} plan [options]" - opts.on("-t", "--test", "Test") { puts "Test" } - end - end - end - end -end diff --git a/src/scaffold/cli.cr b/src/scaffold/cli.cr new file mode 100644 index 0000000..4e67740 --- /dev/null +++ b/src/scaffold/cli.cr @@ -0,0 +1,31 @@ +require "./config" +require "./run" + +module DocMachine::Scaffold + class Cli + def self.add_options(opts, args, parent_config, commands) + config = Config.new(parent_config) + + opts.on("scaffold", "Scaffold target directory (beta)") do + opts.banner = "Usage: #{PROGRAM_NAME} scaffold [options] TARGET" + + opts.on("-f", "--force", "Don't ask for confirmation") do + config.force = true + end + + commands << ->() : Nil do + if args.size < 1 + STDERR.puts "ERROR: No target given!" + exit 1 + end + config.target_directory = args[0] + + app = DocMachine::Scaffold::Run.new(config) + app.prepare + app.start + app.wait + end + end + end + end +end diff --git a/src/scaffolder/config.cr b/src/scaffold/config.cr similarity index 84% rename from src/scaffolder/config.cr rename to src/scaffold/config.cr index 4b4bb63..4d5db07 100644 --- a/src/scaffolder/config.cr +++ b/src/scaffold/config.cr @@ -1,6 +1,6 @@ -module DocMachine::Scaffolder +module DocMachine::Scaffold class Config property target_directory : String = "." property force : Bool = false diff --git a/src/scaffolder/run.cr b/src/scaffold/run.cr similarity index 93% rename from src/scaffolder/run.cr rename to src/scaffold/run.cr index c3dafa7..8de36e0 100644 --- a/src/scaffolder/run.cr +++ b/src/scaffold/run.cr @@ -8,9 +8,9 @@ require "./config" # Shards require "term-prompt" -module DocMachine::Scaffolder +module DocMachine::Scaffold class Run - private property config : DocMachine::Scaffolder::Config + private property config : DocMachine::Scaffold::Config def initialize(@config) end diff --git a/src/scaffolder/cli.cr b/src/write/cli.cr similarity index 81% rename from src/scaffolder/cli.cr rename to src/write/cli.cr index b803f6b..49cd3b2 100644 --- a/src/scaffolder/cli.cr +++ b/src/write/cli.cr @@ -1,12 +1,12 @@ require "./config" require "./run" -module DocMachine::Scaffolder +module DocMachine::Write class Cli def self.add_options(opts, args, parent_config, commands) config = Config.new(parent_config) - opts.on("scaffold", "Scaffold target directory") do + opts.on("write", "Write content target for plan (beta)") do opts.banner = "Usage: #{PROGRAM_NAME} scaffold [options] TARGET" opts.on("-f", "--force", "Don't ask for confirmation") do @@ -20,7 +20,7 @@ module DocMachine::Scaffolder end config.target_directory = args[0] - app = DocMachine::Scaffolder::Run.new(config) + app = DocMachine::Write::Run.new(config) app.prepare app.start app.wait @@ -29,3 +29,4 @@ module DocMachine::Scaffolder end end end + diff --git a/src/write/config.cr b/src/write/config.cr new file mode 100644 index 0000000..132f6ef --- /dev/null +++ b/src/write/config.cr @@ -0,0 +1,11 @@ + + +module DocMachine::Write + class Config + property target_directory : String = "." + property force : Bool = false + + def initialize(@parent : DocMachine::Config) + end + end +end diff --git a/src/write/run.cr b/src/write/run.cr new file mode 100644 index 0000000..f84e3d4 --- /dev/null +++ b/src/write/run.cr @@ -0,0 +1,61 @@ + +# Core +require "file_utils" + +# Internal +require "./config" + +# Shards +require "term-prompt" + +module DocMachine::Write + class Run + private property config : DocMachine::Write::Config + + def initialize(@config) + end + + # Verify parameters + def prepare() + if ! File.directory? @config.target_directory + STDERR.puts "ERROR: target must be a directory" + exit 1 + end + + puts "Target directory: #{@config.target_directory}" + + if !@config.force + prompt = Term::Prompt.new + confirm = prompt.no?("Are you sure you want to proceed?") + exit 1 if !confirm + end + end + + def start() + puts "== Writeing #{@config.target_directory}" + p = Path.new(@config.target_directory) + cwd = Dir.current + ["docs", "slides", "images"].each do |dir| + p_sub = p.join(dir) + puts "-- creating #{p_sub}" + FileUtils.mkdir_p(p_sub) + end + ["docs", "slides"].each do |dir| + p_sub = p.join(dir) + FileUtils.cd(p_sub) + puts "-- creating link to images in #{p_sub}" + if File.symlink? "images" + FileUtils.rm "images" + end + FileUtils.ln_sf(Path.new("..","images"), Path.new("images")) + FileUtils.cd(cwd) + end + puts "-- creating README.md" + FileUtils.touch("README.md") + end + + # Verify parameters + def wait() + end + end +end