develop #3

Merged
glenux merged 35 commits from develop into master 2024-06-02 07:08:41 +00:00
16 changed files with 274 additions and 172 deletions
Showing only changes of commit 594b5f853a - Show all commits

View file

@ -1,12 +1,12 @@
require "./config" require "./config"
module DocMachine::Builder module DocMachine::Build
class Cli class Cli
def self.add_options(opts, args, parent_config, commands) def self.add_options(opts, args, parent_config, commands)
config = Config.new(parent_config) 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 = [ opts.banner = [
"Usage: #{PROGRAM_NAME} build [options]", "Usage: #{PROGRAM_NAME} build [options]",
"", "",
@ -29,7 +29,7 @@ module DocMachine::Builder
end end
commands << ->() : Nil do commands << ->() : Nil do
app = DocMachine::Builder::Run.new(config) app = DocMachine::Build::Run.new(config)
app.prepare app.prepare
app.start app.start
app.wait app.wait

11
src/build/config.cr Normal file
View file

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

122
src/build/run.cr Normal file
View file

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

View file

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

View file

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

View file

@ -3,10 +3,14 @@ require "digest/sha256"
require "colorize" require "colorize"
require "./config" require "./config"
require "./builder/cli" require "./build/cli"
require "./builder/run" require "./build/run"
require "./scaffolder/cli" require "./scaffold/cli"
require "./planner/cli" require "./scaffold/run"
require "./plan/cli"
require "./plan/run"
require "./write/cli"
require "./write/run"
module DocMachine module DocMachine
class Cli class Cli
@ -36,9 +40,10 @@ module DocMachine
opts.separator "" opts.separator ""
opts.separator "Commands:" opts.separator "Commands:"
DocMachine::Builder::Cli.add_options(opts, args, config, commands) DocMachine::Scaffold::Cli.add_options(opts, args, config, commands)
DocMachine::Scaffolder::Cli.add_options(opts, args, config, commands) DocMachine::Plan::Cli.add_options(opts, args, config, commands)
DocMachine::Planner::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 end
parser.parse(args) parser.parse(args)

15
src/plan/cli.cr Normal file
View file

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

View file

@ -1,6 +1,6 @@
module DocMachine module DocMachine
module Planner module Plan
class Config class Config
def initialize(@parent : DocMachine::Config) def initialize(@parent : DocMachine::Config)
end end

View file

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

31
src/scaffold/cli.cr Normal file
View file

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

View file

@ -1,6 +1,6 @@
module DocMachine::Scaffolder module DocMachine::Scaffold
class Config class Config
property target_directory : String = "." property target_directory : String = "."
property force : Bool = false property force : Bool = false

View file

@ -8,9 +8,9 @@ require "./config"
# Shards # Shards
require "term-prompt" require "term-prompt"
module DocMachine::Scaffolder module DocMachine::Scaffold
class Run class Run
private property config : DocMachine::Scaffolder::Config private property config : DocMachine::Scaffold::Config
def initialize(@config) def initialize(@config)
end end

View file

@ -1,12 +1,12 @@
require "./config" require "./config"
require "./run" require "./run"
module DocMachine::Scaffolder module DocMachine::Write
class Cli class Cli
def self.add_options(opts, args, parent_config, commands) def self.add_options(opts, args, parent_config, commands)
config = Config.new(parent_config) 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.banner = "Usage: #{PROGRAM_NAME} scaffold [options] TARGET"
opts.on("-f", "--force", "Don't ask for confirmation") do opts.on("-f", "--force", "Don't ask for confirmation") do
@ -20,7 +20,7 @@ module DocMachine::Scaffolder
end end
config.target_directory = args[0] config.target_directory = args[0]
app = DocMachine::Scaffolder::Run.new(config) app = DocMachine::Write::Run.new(config)
app.prepare app.prepare
app.start app.start
app.wait app.wait
@ -29,3 +29,4 @@ module DocMachine::Scaffolder
end end
end end
end end

11
src/write/config.cr Normal file
View file

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

61
src/write/run.cr Normal file
View file

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