Class: Rundoc::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/rundoc/cli.rb

Defined Under Namespace

Modules: DEFAULTS

Instance Attribute Summary collapse

Instance Method Summary collapse

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, with_contents_dir: 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
67
68
# 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,
  with_contents_dir: 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,
    with_contents_dir: with_contents_dir,
    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.expand_path

  @on_failure_dir = if on_failure_dir
    Pathname(on_failure_dir)
  else
    @execution_context.source_dir.join(DEFAULTS::ON_FAILURE_DIR)
  end.expand_path
end

Instance Attribute Details

#after_build_contextObject (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_argsObject (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_cmdObject (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_pathObject (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_contextObject (readonly)

Returns the value of attribute execution_context.



15
16
17
# File 'lib/rundoc/cli.rb', line 15

def execution_context
  @execution_context
end

#forceObject (readonly)

Returns the value of attribute force.



14
15
16
# File 'lib/rundoc/cli.rb', line 14

def force
  @force
end

#ioObject (readonly)

Returns the value of attribute io.



14
15
16
# File 'lib/rundoc/cli.rb', line 14

def io
  @io
end

#on_failure_dirObject (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_dirObject (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

#callObject



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
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/rundoc/cli.rb', line 134

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 "## erring on failure directory #{on_failure_dir}"
    clean_dir(
      dir: on_failure_dir,
      description: "on failure directory"
    )
  end

  if execution_context.with_contents_dir
    io.puts "## Copying contents from #{execution_context.with_contents_dir} to tmp working dir"
    Dir.chdir(execution_context.with_contents_dir) do
      FileUtils.cp_r(
        ".",
        execution_context.output_dir
      )
    end
  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 StandardError, SignalException => e
      io.puts "Received exception: #{e.inspect}, cleaning up before re-raise"
      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



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rundoc/cli.rb', line 120

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

Returns:

  • (Boolean)


70
71
72
# File 'lib/rundoc/cli.rb', line 70

def force?
  force
end

#load_dotenvObject



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rundoc/cli.rb', line 93

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



242
243
244
245
246
247
248
249
250
# File 'lib/rundoc/cli.rb', line 242

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



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rundoc/cli.rb', line 107

def prepend_cli_banner(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