class Rundoc::CLI
Public Instance Methods
build(path: )
click to toggle source
# File lib/rundoc/cli.rb, line 3 def build(path: ) @path = Pathname.new(path).expand_path raise "#{@path} does not exist" unless File.exist?(@path) raise "Expecting #{@path} to be a rundoc markdown file" unless File.file?(@path) @working_dir = Pathname.new(File.expand_path("../", @path)) dot_env_path = File.expand_path("../.env", @path) if File.exist?(dot_env_path) require 'dotenv' Dotenv.load(dot_env_path) ENV['AWS_ACCESS_KEY_ID'] ||= ENV['BUCKETEER_AWS_ACCESS_KEY_ID'] ENV['AWS_REGION'] ||= ENV['BUCKETEER_AWS_REGION'] ENV['AWS_SECRET_ACCESS_KEY'] ||= ENV['BUCKETEER_AWS_SECRET_ACCESS_KEY'] ENV['AWS_BUCKET_NAME'] ||= ENV['BUCKETEER_BUCKET_NAME'] end source_contents = File.read(@path) tmp_dir = @working_dir.join("tmp") FileUtils.remove_entry_secure(tmp_dir) if tmp_dir.exist? tmp_dir.mkdir banner = <<~HEREDOC <!-- STOP This file was generated by a rundoc script, do not modify it. Instead modify the rundoc script and re-run it. Command: #{ $0 } #{$*.join(' ')} STOP --> HEREDOC puts "== Running your docs" Dir.chdir(tmp_dir) do @output = Rundoc::Parser.new(source_contents, document_path: @path).to_md Rundoc.sanitize(@output) @output = "#{banner}\n#{@output}" end puts "== Done, run was successful" project_name = if Rundoc.project_root Rundoc.project_root.split('/').last else 'project' end project_dir = @working_dir.join(project_name) FileUtils.remove_entry_secure(project_dir) if project_dir.exist? cp_root = if Rundoc.project_root tmp_dir.join(Rundoc.project_root, ".") else tmp_dir.join(".") end FileUtils.cp_r(cp_root, project_dir) FileUtils.remove_entry_secure(tmp_dir) if tmp_dir.exist? source_path = project_dir.join("README.md") puts "== Done, writing original source to #{source_path}" File.open(source_path, "w") { |f| f.write @output } puts "== Copying source" source_path = project_dir.join("coppied-#{@path.to_s.split('/').last}") File.open(source_path, "w") { |f| f.write source_contents } Dir.chdir(project_dir) do Rundoc.run_after_build end ensure Rundoc::CodeCommand::Background::ProcessSpawn.tasks.each do |name, task| next unless task.alive? puts "Warning background task is still running, cleaning up: name: #{name}" task.stop end end