Sha256: c1e5ec7cfa80c338af543a77f0816588bc860beb4249b4bca6f524f892d41d02

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require 'open3'
require 'tempfile'
require 'tmpdir'

module LiquidDiagrams
  module Rendering
    module_function

    def render_with_stdin_stdout(command, content)
      render_with_command(command, :stdout, stdin_data: content)
    end

    def render_with_tempfile(command, content)
      input = Tempfile.new('input')
      output = Tempfile.new(%w[output .svg])

      File.write(input.path, content)

      extra = yield input.path, output.path
      command = "#{command} #{extra}"

      render_with_command(command, output.path)
    # TODO: recue Tempfile.new and File.write error
    ensure
      input.close!
      output.close!
    end

    def render_with_command(command, output = :stdout, **options)
      begin
        stdout, stderr, status = Open3.capture3(command, **options)
      rescue Errno::ENOENT
        raise Errors::CommandNotFoundError, command.split(' ')[0]
      end

      unless status.success?
        raise Errors::RenderingFailedError, <<~MSG
          #{command}: #{stderr.empty? ? stdout : stderr}
        MSG
      end

      output == :stdout ? stdout : File.read(output)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
liquid-diagrams-0.3.0 lib/liquid_diagrams/rendering.rb
liquid-diagrams-0.2.0 lib/liquid_diagrams/rendering.rb
liquid-diagrams-0.1.0 lib/liquid_diagrams/rendering.rb