diff --git a/src/build/cli.cr b/src/build/cli.cr index 8cdb4dc..b957f60 100644 --- a/src/build/cli.cr +++ b/src/build/cli.cr @@ -16,12 +16,16 @@ module DocMachine::Build opts.separator "" opts.separator "Builder Options:" + opts.on("-a", "--action ACTION", "Action (watch, build, shell, etc.)") do |action| + config.action = action + end + opts.on("-d", "--data-dir DIR", "Content directory") do |dir| config.data_dir = dir end - opts.on("-a", "--action ACTION", "Action (watch, build, shell, etc.)") do |action| - config.action = action + opts.on("-p", "--port PORT", "Set base port to PORT") do |port| + config.port = port.to_i end opts.on("-t", "--tty", "Enable TTY mode (needed for shell)") do diff --git a/src/build/config.cr b/src/build/config.cr index 3c1b2e5..8129259 100644 --- a/src/build/config.cr +++ b/src/build/config.cr @@ -4,6 +4,7 @@ module DocMachine::Build property data_dir : String = Dir.current property action : String = "watch" property enable_tty : Bool = false + property port : Int32 = 5100 def initialize(@parent : DocMachine::Config) end diff --git a/src/build/run.cr b/src/build/run.cr index 0d1c5d0..af91688 100644 --- a/src/build/run.cr +++ b/src/build/run.cr @@ -30,6 +30,17 @@ module DocMachine::Build self._pull_image() end + private def _find_port(port_base) + (port_base..65535).each do |port| + return port if _port_available?(port) + end + raise "No port available" + end + + private def _port_available?(port) + true + end + private def _avoid_duplicates docker_cid = %x{docker ps -f "name=#{@docker_name}" -q}.strip @@ -119,7 +130,7 @@ module DocMachine::Build ## Detect Mkdocs configuration - old format (full) if File.exists?("#{@config.data_dir}/mkdocs.yml") - Log.info { "Mkdocs: detected mkdocs.yml file. Please rename to mkdocs-patch.yml" } + Log.info { "Docs: detected mkdocs.yml file. Please rename to mkdocs-patch.yml" } exit 1 end @@ -127,27 +138,33 @@ module DocMachine::Build 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 - Log.info { "Mkdocs: detected mkdocs-patch.yml file. Adding option to command line (#{docker_opt_mkdocs_config})" } + Log.info { "Docs: detected mkdocs-patch.yml file. Adding option to command line (#{docker_opt_mkdocs_config})" } else - Log.info { "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 - Log.info { "Slides: detected slides directory. Adding option to command line (#{docker_opt_marp_port})" } - else - Log.info { "Slides: no slides directory detected." } + Log.info { "Docs: no mkdocs-patch.yml detected. Using default files" } end ## Detect docs if Dir.exists?("#{@config.data_dir}/docs") - docker_opt_marp_port = ["-p", "5100:5100"] - docker_opts.concat docker_opt_marp_port - Log.info { "Slides: detected docs directory. Adding option to command line (#{docker_opt_marp_port})" } + Log.info { "Docs: detected docs directory." } + mkdocs_port = _find_port(@config.port) + docker_opt_mkdocs_port = ["-p", "#{mkdocs_port}:5100"] + docker_opts.concat docker_opt_mkdocs_port + Log.notice { "Using port #{mkdocs_port} for docs" } + Log.info { "Docs: Adding option to command line (#{docker_opt_marp_port})" } else - Log.info { "Slides: no slides docs detected." } + Log.info { "Docs: no docs detected." } + end + + ## Detect slides + if Dir.exists?("#{@config.data_dir}/slides") + Log.info { "Slides: detected slides directory." } + marp_port = _find_port(@config.port+100) + docker_opt_marp_port = ["-p", "#{marp_port}:5200"] + docker_opts.concat docker_opt_marp_port + Log.info { "Slides: Adding option to command line (#{docker_opt_marp_port})" } + Log.notice { "Slides: Using port #{marp_port} for slides" } + else + Log.info { "Slides: no slides directory detected." } end docker_opts << @docker_image