Skip to content

Commit

Permalink
Fix screenshot location
Browse files Browse the repository at this point in the history
The location of the screenshot directory is a relative path, which changes when `:::>> $ cd <value>` is called.

This change uses dependency injection to guarantee the location of the screenshots. Later we can let the user control the screenshot location via CLI flag.
  • Loading branch information
schneems committed Jul 9, 2024
1 parent ea36656 commit 13fbe5e
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 23 deletions.
18 changes: 16 additions & 2 deletions lib/rundoc/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@ module Rundoc
class CLI
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))
tmp_dir = @working_dir.join("tmp")
screenshots_path = if Rundoc.project_root
tmp_dir.join(Rundoc.project_root, "screenshots")
else
tmp_dir.join("screenshots")
end


tmp_dir.join("screenshots")

dot_env_path = File.expand_path("../.env", @path)
if File.exist?(dot_env_path)
Expand All @@ -17,7 +28,6 @@ def build(path:)
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
Expand All @@ -33,7 +43,11 @@ def build(path:)

puts "== Running your docs"
Dir.chdir(tmp_dir) do
@output = Rundoc::Parser.new(source_contents, document_path: @path).to_md
@output = Rundoc::Parser.new(
source_contents,
document_path: @path,
screenshots_path: screenshots_path
).to_md
Rundoc.sanitize(@output)
@output = "#{banner}\n#{@output}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rundoc/code_command/rundoc/require.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def call(env = {})
document_path = @path.expand_path(current_path)

puts "rundoc.require: Start executing #{@path.to_s.inspect}"
output = Rundoc::Parser.new(document_path.read, document_path: document_path.to_s).to_md
output = Rundoc::Parser.new(document_path.read, document_path: document_path.to_s, screenshots_path: env[:screenshots_path]).to_md
puts "rundoc.require: Done executing #{@path.to_s.inspect}, putting contents into document"

env[:replace] << output
Expand Down
7 changes: 4 additions & 3 deletions lib/rundoc/code_command/website/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ def safe_eval(code)
raise e
end

def screenshot(upload: false)
def screenshot(upload: false, screenshots_path: )
@driver.resize_window_to(@driver.current_window_handle, @width, @height)
FileUtils.mkdir_p("tmp/rundoc_screenshots")
FileUtils.mkdir_p(screenshots_path)
file_name = self.class.next_screenshot_name
file_path = "tmp/rundoc_screenshots/#{file_name}"
file_path = screenshots_path.join(file_name)
session.save_screenshot(file_path)
puts("Screenshot saved to #{file_path}")

return file_path unless upload

Expand Down
2 changes: 1 addition & 1 deletion lib/rundoc/code_command/website/screenshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def to_md(env = {})

def call(env = {})
puts "Taking screenshot: #{@driver.current_url}"
filename = @driver.screenshot(upload: @upload)
filename = @driver.screenshot(upload: @upload, screenshots_path: env[:screenshots_path])
env[:replace] = "![Screenshot of #{@driver.current_url}](#{filename})"
""
end
Expand Down
2 changes: 2 additions & 0 deletions lib/rundoc/code_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def initialize(match, options = {})
@commands = []
@stack = []
@keyword = options[:keyword] or raise "keyword is required"
@screenshots_path = options[:screenshots_path]
@document_path = options[:document_path]
@fence = match[:fence]
@lang = match[:lang]
Expand All @@ -49,6 +50,7 @@ def render
env[:before] = []
env[:after] = []
env[:document_path] = @document_path
env[:screenshots_path] = @screenshots_path

@stack.each do |s|
unless s.respond_to?(:call)
Expand Down
5 changes: 3 additions & 2 deletions lib/rundoc/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class Parser

attr_reader :contents, :keyword, :stack

def initialize(contents, keyword: DEFAULT_KEYWORD, document_path: nil)
def initialize(contents, keyword: DEFAULT_KEYWORD, document_path: nil, screenshots_path: )
@document_path = document_path
@screenshots_path = screenshots_path
@contents = contents
@original = contents.dup
@keyword = keyword
Expand Down Expand Up @@ -41,7 +42,7 @@ def partition
@stack << head unless head.empty?
unless code.empty?
match = code.match(CODEBLOCK_REGEX)
@stack << CodeSection.new(match, keyword: keyword, document_path: @document_path)
@stack << CodeSection.new(match, keyword: keyword, document_path: @document_path, screenshots_path: @screenshots_path)
end
@contents = tail
end
Expand Down
9 changes: 0 additions & 9 deletions test/fixtures/cnb/ruby/rundoc.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
```
:::-- rundoc.configure
Rundoc.configure do |config|
config.filter_sensitive(
"tmp/rundoc_screenshots/screenshot_1.png" =>
"assets/images/ruby-getting-started-screenshot.png"
)
end
```

```
:::>> rundoc.require "./intro.md"
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/cnb/shared/install_pack.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
We assume you have [docker installed](https://docs.docker.com/engine/install/) and a working copy [of git](https://github.com/git-guides/install-git). Next, you will need to install the CLI tool for building CNBs, [pack CLI](https://buildpacks.io/docs/for-platform-operators/how-to/integrate-ci/pack/). If you're on a Mac you can install it via Homebrew:

```
:::>- $ brew install buildpacks/tap/pack
:::-> print.text
$ brew install buildpacks/tap/pack
```

Ensure that `pack` is installed correctly:
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/cnb/shared/pack_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Now build an image named `my-image-name` by executing the heroku builder against
```
$ pack build my-image-name --path .
:::-- $ docker rmi -f my-image-name
:::-- $ pack build my-image-name --path . &> build_output.txt
:::-- $ pack build my-image-name --path . 2>&1 | tee build_output.txt
:::-> $ cat build_output.txt
```

Expand Down
6 changes: 3 additions & 3 deletions test/rundoc/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_parse_bash
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
expected = "sup\n\n```\n$ mkdir foo\n$ ls\nfoo\n```\n\nyo\n"
parsed = Rundoc::Parser.new(contents)
parsed = Rundoc::Parser.new(contents, screenshots_path: Pathname("/dev/null"))
actual = parsed.to_md.gsub(Rundoc::CodeSection::AUTOGEN_WARNING, "")
assert_equal expected, actual
end
Expand All @@ -38,7 +38,7 @@ def test_parse_write_commands
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
expected = "sup\n\nIn file `foo/code.rb` write:\n\n```\na = 1 + 1\nb = a * 2\n```\nyo\n"
parsed = Rundoc::Parser.new(contents)
parsed = Rundoc::Parser.new(contents, screenshots_path: Pathname("/dev/null"))
actual = parsed.to_md.gsub(Rundoc::CodeSection::AUTOGEN_WARNING, "")
assert_equal expected, actual
end
Expand All @@ -56,7 +56,7 @@ def test_parse_write_commands
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
expected = "\nIn file `foo/newb.rb` write:\n\n```\nputs 'hello world'\n$ cat foo/newb.rb\nputs 'hello world'\n```\n"
parsed = Rundoc::Parser.new(contents)
parsed = Rundoc::Parser.new(contents, screenshots_path: Pathname("/dev/null"))
actual = parsed.to_md.gsub(Rundoc::CodeSection::AUTOGEN_WARNING, "")
assert_equal expected, actual
end
Expand Down

0 comments on commit 13fbe5e

Please sign in to comment.