diff --git a/lib/rundoc/cli.rb b/lib/rundoc/cli.rb index 9e271cb..ac49ad3 100644 --- a/lib/rundoc/cli.rb +++ b/lib/rundoc/cli.rb @@ -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) @@ -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 @@ -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 diff --git a/lib/rundoc/code_command/rundoc/require.rb b/lib/rundoc/code_command/rundoc/require.rb index c83fc2d..4ea62ac 100644 --- a/lib/rundoc/code_command/rundoc/require.rb +++ b/lib/rundoc/code_command/rundoc/require.rb @@ -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 diff --git a/lib/rundoc/code_command/website/driver.rb b/lib/rundoc/code_command/website/driver.rb index 6adae64..1674093 100644 --- a/lib/rundoc/code_command/website/driver.rb +++ b/lib/rundoc/code_command/website/driver.rb @@ -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 diff --git a/lib/rundoc/code_command/website/screenshot.rb b/lib/rundoc/code_command/website/screenshot.rb index 4686abf..927a7fa 100644 --- a/lib/rundoc/code_command/website/screenshot.rb +++ b/lib/rundoc/code_command/website/screenshot.rb @@ -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 diff --git a/lib/rundoc/code_section.rb b/lib/rundoc/code_section.rb index 51449d5..fb18116 100644 --- a/lib/rundoc/code_section.rb +++ b/lib/rundoc/code_section.rb @@ -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] @@ -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) diff --git a/lib/rundoc/parser.rb b/lib/rundoc/parser.rb index db02eca..4778289 100644 --- a/lib/rundoc/parser.rb +++ b/lib/rundoc/parser.rb @@ -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 @@ -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 diff --git a/test/fixtures/cnb/ruby/rundoc.md b/test/fixtures/cnb/ruby/rundoc.md index 700f6e5..92d6c83 100644 --- a/test/fixtures/cnb/ruby/rundoc.md +++ b/test/fixtures/cnb/ruby/rundoc.md @@ -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" diff --git a/test/fixtures/cnb/shared/install_pack.md b/test/fixtures/cnb/shared/install_pack.md index 7d7118f..797eca9 100644 --- a/test/fixtures/cnb/shared/install_pack.md +++ b/test/fixtures/cnb/shared/install_pack.md @@ -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: diff --git a/test/fixtures/cnb/shared/pack_build.md b/test/fixtures/cnb/shared/pack_build.md index c44254e..6f72f62 100644 --- a/test/fixtures/cnb/shared/pack_build.md +++ b/test/fixtures/cnb/shared/pack_build.md @@ -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 ``` diff --git a/test/rundoc/parser_test.rb b/test/rundoc/parser_test.rb index eb91338..81a9d0e 100644 --- a/test/rundoc/parser_test.rb +++ b/test/rundoc/parser_test.rb @@ -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 @@ -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 @@ -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