From b8c887682b5a8a536bb42f88310ef4605b01deb0 Mon Sep 17 00:00:00 2001 From: Glenn Date: Mon, 27 Mar 2023 16:01:20 +0200 Subject: [PATCH] feat: Add support for scaffold --- .gitignore | 1 + shard.lock | 30 ++++++++++++++++++++ shard.yml | 13 ++++++--- src/builder/cli.cr | 4 +-- src/cli.cr | 13 +++++++-- src/scaffolder/cli.cr | 33 ++++++++++++++-------- src/scaffolder/config.cr | 11 ++++---- src/scaffolder/run.cr | 61 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 140 insertions(+), 26 deletions(-) create mode 100644 shard.lock diff --git a/.gitignore b/.gitignore index 4052fca..ea479e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /bin +/lib diff --git a/shard.lock b/shard.lock new file mode 100644 index 0000000..6e040c8 --- /dev/null +++ b/shard.lock @@ -0,0 +1,30 @@ +version: 2.0 +shards: + completion: + git: https://github.com/f/completion.git + version: 0.1.0+git.commit.d8799381b2de14430496199260eca64eb329625f + + cor: + git: https://github.com/watzon/cor.git + version: 0.1.0+git.commit.9c9e51ac6168f3bd4fdc51d679b65de09ef76cac + + ioctl: + git: https://github.com/crystal-posix/ioctl.cr.git + version: 1.0.0 + + term-cursor: + git: https://github.com/crystal-term/cursor.git + version: 0.1.0+git.commit.8805d5f686d153db92cf2ce3333433f8ed3708d0 + + term-prompt: + git: https://github.com/crystal-term/prompt.git + version: 0.1.0+git.commit.bf2b17f885a6c660aea0dda62b0b9da4343ab295 + + term-reader: + git: https://github.com/crystal-term/reader.git + version: 0.1.0+git.commit.cd022d4d4628e5d9de47e669a770ccb7df412863 + + term-screen: + git: https://github.com/crystal-term/screen.git + version: 0.1.0+git.commit.ea51ee8d1f6c286573c41a7e784d31c80af7b9bb + diff --git a/shard.yml b/shard.yml index 4eb42db..fdfff8b 100644 --- a/shard.yml +++ b/shard.yml @@ -12,10 +12,15 @@ targets: docmachine: main: src/main.cr -# dependencies: -# pg: -# github: will/crystal-pg -# version: "~> 0.5" +dependencies: + term-prompt: + github: crystal-term/prompt + +# completion: +# github: f/completion +# pg: +# github: will/crystal-pg +# version: "~> 0.5" # development_dependencies: # webmock: diff --git a/src/builder/cli.cr b/src/builder/cli.cr index 23a3d77..fcd6d86 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, args, parent_config, command) + def self.add_options(opts, args, parent_config, commands) config = Config.new(parent_config) opts.on("build", "Build content and produce deliverables") do @@ -28,7 +28,7 @@ module DocMachine::Builder config.enable_tty = true end - command << ->() : Nil do + commands << ->() : Nil do app = DocMachine::Builder::Run.new(config) app.prepare app.start diff --git a/src/cli.cr b/src/cli.cr index 052cc65..d6627de 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -42,10 +42,17 @@ module DocMachine end parser.parse(args) + puts commands - commands.each do |command| - puts "running #{command}" - command.call() + if commands.size > 0 + commands.each do |command| + # puts "== Running #{command}" + command.call() + end + else + puts parser.to_s + STDOUT.puts "" + STDERR.puts "ERROR: no command defined" end end end diff --git a/src/scaffolder/cli.cr b/src/scaffolder/cli.cr index a049bf0..b803f6b 100644 --- a/src/scaffolder/cli.cr +++ b/src/scaffolder/cli.cr @@ -1,20 +1,29 @@ require "./config" +require "./run" -module DocMachine - module Scaffolder - class Cli - def self.add_options(opts, args, parent_config, command) - config = Config.new(parent_config) +module DocMachine::Scaffolder + class Cli + def self.add_options(opts, args, parent_config, commands) + config = Config.new(parent_config) - opts.on("scaffold", "Scaffold directory") do - opts.banner = "Usage: #{PROGRAM_NAME} scaffold [options]" + opts.on("scaffold", "Scaffold target directory") do + opts.banner = "Usage: #{PROGRAM_NAME} scaffold [options] TARGET" + + opts.on("-f", "--force", "Don't ask for confirmation") do + config.force = true end - command << ->() : Nil do - # app = DocMachine::Scaffolder::Run.new(config) - # app.prepare - # app.start - # app.wait + commands << ->() : Nil do + if args.size < 1 + STDERR.puts "ERROR: No target given!" + exit 1 + end + config.target_directory = args[0] + + app = DocMachine::Scaffolder::Run.new(config) + app.prepare + app.start + app.wait end end end diff --git a/src/scaffolder/config.cr b/src/scaffolder/config.cr index e242c5e..4b4bb63 100644 --- a/src/scaffolder/config.cr +++ b/src/scaffolder/config.cr @@ -1,10 +1,11 @@ -module DocMachine - module Scaffolder - class Config - def initialize(@parent : DocMachine::Config) - end +module DocMachine::Scaffolder + class Config + property target_directory : String = "." + property force : Bool = false + + def initialize(@parent : DocMachine::Config) end end end diff --git a/src/scaffolder/run.cr b/src/scaffolder/run.cr index e69de29..c3dafa7 100644 --- a/src/scaffolder/run.cr +++ b/src/scaffolder/run.cr @@ -0,0 +1,61 @@ + +# Core +require "file_utils" + +# Internal +require "./config" + +# Shards +require "term-prompt" + +module DocMachine::Scaffolder + class Run + private property config : DocMachine::Scaffolder::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 "== Scaffolding #{@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