Class: Rundoc::CLI
- Inherits:
-
Object
- Object
- Rundoc::CLI
- Defined in:
- lib/rundoc/cli.rb
Defined Under Namespace
Modules: DEFAULTS
Instance Attribute Summary collapse
-
#after_build_context ⇒ Object
readonly
Returns the value of attribute after_build_context.
-
#cli_args ⇒ Object
readonly
Returns the value of attribute cli_args.
-
#cli_cmd ⇒ Object
readonly
Returns the value of attribute cli_cmd.
-
#dotenv_path ⇒ Object
readonly
Returns the value of attribute dotenv_path.
-
#execution_context ⇒ Object
readonly
Returns the value of attribute execution_context.
-
#force ⇒ Object
readonly
Returns the value of attribute force.
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#on_failure_dir ⇒ Object
readonly
Returns the value of attribute on_failure_dir.
-
#on_success_dir ⇒ Object
readonly
Returns the value of attribute on_success_dir.
Instance Method Summary collapse
- #call ⇒ Object
- #check_directories_empty! ⇒ Object
- #force? ⇒ Boolean
-
#initialize(source_path:, io: $stderr, cli_cmd: $0, cli_args: $*, force: false, dotenv_path: nil, on_success_dir: nil, on_failure_dir: nil, output_filename: nil, screenshots_dirname: nil) ⇒ CLI
constructor
A new instance of CLI.
- #load_dotenv ⇒ Object
- #move_dir_contents(from:, to:) ⇒ Object
- #prepend_cli_banner(contents) ⇒ Object
Constructor Details
#initialize(source_path:, io: $stderr, cli_cmd: $0, cli_args: $*, force: false, dotenv_path: nil, on_success_dir: nil, on_failure_dir: nil, output_filename: nil, screenshots_dirname: nil) ⇒ CLI
Returns a new instance of CLI.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rundoc/cli.rb', line 17 def initialize( source_path:, io: $stderr, cli_cmd: $0, cli_args: $*, force: false, dotenv_path: nil, on_success_dir: nil, on_failure_dir: nil, output_filename: nil, screenshots_dirname: nil ) @io = io @force = force @cli_cmd = cli_cmd @cli_args = cli_args screenshots_dirname = check_relative_path(screenshots_dirname || DEFAULTS::SCREENSHOTS_DIR) output_filename = check_relative_path(output_filename || DEFAULTS::OUTPUT_FILENAME) @execution_context = Rundoc::Context::Execution.new( output_dir: Dir.mktmpdir, source_path: source_path, screenshots_dirname: screenshots_dirname ) @after_build_context = Context::AfterBuild.new( output_dir: execution_context.output_dir, screenshots_dir: execution_context.screenshots_dir, output_markdown_path: @execution_context.output_dir.join(output_filename) ) @dotenv_path = if dotenv_path Pathname(dotenv_path) else @execution_context.source_dir.join(DEFAULTS::DOTENV_PATH) end @on_success_dir = if on_success_dir Pathname(on_success_dir) else @execution_context.source_dir.join(DEFAULTS::ON_SUCCESS_DIR) end. @on_failure_dir = if on_failure_dir Pathname(on_failure_dir) else @execution_context.source_dir.join(DEFAULTS::ON_FAILURE_DIR) end. end |
Instance Attribute Details
#after_build_context ⇒ Object (readonly)
Returns the value of attribute after_build_context.
15 16 17 |
# File 'lib/rundoc/cli.rb', line 15 def after_build_context @after_build_context end |
#cli_args ⇒ Object (readonly)
Returns the value of attribute cli_args.
14 15 16 |
# File 'lib/rundoc/cli.rb', line 14 def cli_args @cli_args end |
#cli_cmd ⇒ Object (readonly)
Returns the value of attribute cli_cmd.
14 15 16 |
# File 'lib/rundoc/cli.rb', line 14 def cli_cmd @cli_cmd end |
#dotenv_path ⇒ Object (readonly)
Returns the value of attribute dotenv_path.
14 15 16 |
# File 'lib/rundoc/cli.rb', line 14 def dotenv_path @dotenv_path end |
#execution_context ⇒ Object (readonly)
Returns the value of attribute execution_context.
15 16 17 |
# File 'lib/rundoc/cli.rb', line 15 def execution_context @execution_context end |
#force ⇒ Object (readonly)
Returns the value of attribute force.
14 15 16 |
# File 'lib/rundoc/cli.rb', line 14 def force @force end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
14 15 16 |
# File 'lib/rundoc/cli.rb', line 14 def io @io end |
#on_failure_dir ⇒ Object (readonly)
Returns the value of attribute on_failure_dir.
14 15 16 |
# File 'lib/rundoc/cli.rb', line 14 def on_failure_dir @on_failure_dir end |
#on_success_dir ⇒ Object (readonly)
Returns the value of attribute on_success_dir.
14 15 16 |
# File 'lib/rundoc/cli.rb', line 14 def on_success_dir @on_success_dir end |
Instance Method Details
#call ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/rundoc/cli.rb', line 132 def call io.puts "## Running your docs" load_dotenv check_directories_empty! source_contents = execution_context.source_path.read if on_failure_dir.exist? && !Dir.empty?(on_failure_dir) io.puts "## earing on failure directory #{on_failure_dir}" clean_dir( dir: on_failure_dir, description: "on failure directory" ) end io.puts "## Working dir is #{execution_context.output_dir}" Dir.chdir(execution_context.output_dir) do parser = Rundoc::Parser.new( source_contents, context: execution_context ) output = begin parser.to_md rescue => e on_fail raise e end on_success(output) end ensure # Stop any hanging background tasks Rundoc::CodeCommand::Background::ProcessSpawn.tasks.each do |name, task| next unless task.alive? io.puts "Warning background task is still running, cleaning up: name: #{name}" task.stop end if execution_context.output_dir.exist? clean_dir( dir: execution_context.output_dir, description: "tmp working directory" ) end end |
#check_directories_empty! ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/rundoc/cli.rb', line 118 def check_directories_empty! [on_success_dir, on_failure_dir].each do |dir| dir.mkpath next if Dir.empty?(dir) if force? io.puts "## WARNING: #{dir} is not empty, it may be cleared due to running with the `--force` flag" else raise "## ABORTING: #{dir} is not empty, clear it or re-run with `--force` flag" end end end |
#force? ⇒ Boolean
68 69 70 |
# File 'lib/rundoc/cli.rb', line 68 def force? force end |
#load_dotenv ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rundoc/cli.rb', line 91 def load_dotenv if File.exist?(dotenv_path) io.puts("Found .env file #{dotenv_path}, loading") require "dotenv" Dotenv.load(dotenv_path) ENV["AWS_REGION"] ||= ENV["BUCKETEER_AWS_REGION"] ENV["AWS_BUCKET_NAME"] ||= ENV["BUCKETEER_BUCKET_NAME"] ENV["AWS_ACCESS_KEY_ID"] ||= ENV["BUCKETEER_AWS_ACCESS_KEY_ID"] ENV["AWS_SECRET_ACCESS_KEY"] ||= ENV["BUCKETEER_AWS_SECRET_ACCESS_KEY"] else io.puts("## No .env file found #{dotenv_path}, skipping dotenv loading") end end |
#move_dir_contents(from:, to:) ⇒ Object
227 228 229 230 231 232 233 234 235 |
# File 'lib/rundoc/cli.rb', line 227 def move_dir_contents(from:, to:) io.puts("## Moving contents from #{from} to #{to}") Dir.glob(File.join(from, "{*,.*}")).each do |item| next if item.end_with?(".", "..") FileUtils.mv(item, to) end end |
#prepend_cli_banner(contents) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rundoc/cli.rb', line 105 def (contents) <<~HEREDOC <!-- STOP This file was generated by a rundoc script, do not modify it. Instead modify the rundoc script and re-run it. Command: #{cli_cmd} #{cli_args.join(" ")} STOP --> #{contents} HEREDOC end |