feat: Add support for scaffold
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Glenn Y. Rolland 2023-03-27 16:01:20 +02:00
parent 6d34bec0e6
commit b8c887682b
8 changed files with 140 additions and 26 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/bin
/lib

30
shard.lock Normal file
View file

@ -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

View file

@ -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:

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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