From 6d34bec0e69d652a011d57f4c7cce799a97daabc Mon Sep 17 00:00:00 2001 From: Glenn Date: Sun, 26 Mar 2023 11:41:40 +0200 Subject: [PATCH] feat: Organize for subcommands --- src/builder/cli.cr | 10 +++++++++- src/builder/run.cr | 35 ++++++++++++++++------------------- src/cli.cr | 19 ++++++++++--------- src/planner/cli.cr | 2 +- src/scaffolder/cli.cr | 9 ++++++++- src/scaffolder/run.cr | 0 6 files changed, 44 insertions(+), 31 deletions(-) create mode 100644 src/scaffolder/run.cr diff --git a/src/builder/cli.cr b/src/builder/cli.cr index 715bf45..23a3d77 100644 --- a/src/builder/cli.cr +++ b/src/builder/cli.cr @@ -3,7 +3,7 @@ require "./config" module DocMachine::Builder class Cli - def self.add_options(opts, parent_config) + def self.add_options(opts, args, parent_config, command) config = Config.new(parent_config) opts.on("build", "Build content and produce deliverables") do @@ -27,7 +27,15 @@ module DocMachine::Builder opts.on("-t", "--tty", "Enable TTY mode (needed for shell)") do config.enable_tty = true end + + command << ->() : Nil do + app = DocMachine::Builder::Run.new(config) + app.prepare + app.start + app.wait + end end + end end end diff --git a/src/builder/run.cr b/src/builder/run.cr index 5c6a211..1700062 100644 --- a/src/builder/run.cr +++ b/src/builder/run.cr @@ -6,10 +6,7 @@ module DocMachine class Run def initialize(@config : DocMachine::Builder::Config) - @basedir = config[:data_dir]? ? config[:data_dir] : Dir.current - @basehash = Digest::SHA256.hexdigest(@basedir)[0..6] - @action = config[:action]? ? config[:action] : "watch" - # @verbosity = config[:verbose]? ? config[:verbose] : 0 + @basehash = Digest::SHA256.hexdigest(@config.data_dir)[0..6] @docker_name = "docmachine-#{@basehash}" @docker_image = "glenux/docmachine:latest" @docker_opts = [] of String @@ -21,9 +18,9 @@ module DocMachine # create directories # setup permissions def prepare() - puts "basedir = #{@basedir}" + puts "basedir = #{@config.data_dir}" puts "docker_image = #{@docker_image}" - puts "action = #{@action}" + puts "action = #{@config.action}" docker_cid = %x{docker ps -f "name=#{@docker_name}" -q}.strip @@ -45,21 +42,21 @@ module DocMachine docker_opts << "run" docker_opts << "-i" # add tty support - docker_opts << "-t" if @enable_tty + 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", "#{@basedir}/docs:/app/docs"] - docker_opts.concat ["-v", "#{@basedir}/slides:/app/slides"] - docker_opts.concat ["-v", "#{@basedir}/images:/app/images"] - docker_opts.concat ["-v", "#{@basedir}/_build:/app/_build"] + 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?("#{@basedir}/.marp/theme.scss") - docker_opt_marp_theme = ["-v", "#{@basedir}/.marp:/app/.marp"] + 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 @@ -67,14 +64,14 @@ module DocMachine end ## Detect Mkdocs configuration - old format (full) - if File.exists?("#{@basedir}/mkdocs.yml") + 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?("#{@basedir}/mkdocs-patch.yml") - docker_opt_mkdocs_config = ["-v", "#{@basedir}/mkdocs-patch.yml:/app/mkdocs-patch.yml"] + 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 @@ -82,7 +79,7 @@ module DocMachine end ## Detect slides - if Dir.exists?("#{@basedir}/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})" @@ -91,7 +88,7 @@ module DocMachine end ## Detect docs - if Dir.exists?("#{@basedir}/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})" @@ -100,7 +97,7 @@ module DocMachine end docker_opts << @docker_image - docker_opts << @action + docker_opts << @config.action puts docker_opts.inspect.colorize(:yellow) @process = Process.new("docker", docker_opts, output: STDOUT, error: STDERR) diff --git a/src/cli.cr b/src/cli.cr index 93e940c..052cc65 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -13,8 +13,9 @@ module DocMachine def initialize end - def start(argv) + def start(args) config = Config.new + commands = [] of Proc(Nil) parser = OptionParser.new do |opts| opts.banner = [ @@ -35,17 +36,17 @@ module DocMachine opts.separator "" opts.separator "Commands:" - DocMachine::Builder::Cli.add_options(opts, config) - DocMachine::Scaffolder::Cli.add_options(opts, config) - DocMachine::Planner::Cli.add_options(opts, config) + 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) end - parser.parse(ARGV) + parser.parse(args) - builder = DocMachine::Builder::Run.new(config) - builder.prepare - builder.start - builder.wait + commands.each do |command| + puts "running #{command}" + command.call() + end end end end diff --git a/src/planner/cli.cr b/src/planner/cli.cr index 5edaa1a..c82b47b 100644 --- a/src/planner/cli.cr +++ b/src/planner/cli.cr @@ -4,7 +4,7 @@ require "./config" module DocMachine module Planner class Cli - def self.add_options(opts, parent_config) + def self.add_options(opts, args, parent_config, command) config = Config.new(parent_config) opts.on("content", "Generate content and structure") do diff --git a/src/scaffolder/cli.cr b/src/scaffolder/cli.cr index f3113e7..a049bf0 100644 --- a/src/scaffolder/cli.cr +++ b/src/scaffolder/cli.cr @@ -3,12 +3,19 @@ require "./config" module DocMachine module Scaffolder class Cli - def self.add_options(opts, parent_config) + def self.add_options(opts, args, parent_config, command) config = Config.new(parent_config) opts.on("scaffold", "Scaffold directory") do opts.banner = "Usage: #{PROGRAM_NAME} scaffold [options]" end + + command << ->() : Nil do + # app = DocMachine::Scaffolder::Run.new(config) + # app.prepare + # app.start + # app.wait + end end end end diff --git a/src/scaffolder/run.cr b/src/scaffolder/run.cr new file mode 100644 index 0000000..e69de29