class Scad4r::Runner

Public Class Methods

new(options = {}) click to toggle source
# File lib/scad4r/runner.rb, line 11
def initialize(options = {})
  @options = {format: :stl,
              constants: {},
              parser: PassThrough.new,
              timed: true}.merge(options)
end

Public Instance Methods

run(path, options = {}) click to toggle source
# File lib/scad4r/runner.rb, line 18
def run(path, options = {})
  net_options = @options.merge(options)

  parser = net_options.fetch(:parser)

  # provide a reasonable default
  net_options = {output: output_file(path, net_options)}.merge(net_options)

  io = shell_out(openscad_invocation(path, net_options))

  result_hash = parser.parse io.read

  result_hash.merge({output: net_options.fetch(:output)})
end

Protected Instance Methods

shell_out(cmd) click to toggle source
# File lib/scad4r/runner.rb, line 35
def shell_out(cmd)
  r, w = pipes
  process = ChildProcess.build(*cmd)
  process.io.stdout = process.io.stderr = w
  process.start
  w.close
  process.wait # to do, stream progress

  r
end

Private Instance Methods

openscad_command(options) click to toggle source
# File lib/scad4r/runner.rb, line 56
def openscad_command(options)
  if options.fetch(:timed)
    %w(time openscad)
  else
    "openscad"
  end
end
openscad_invocation(path, options) click to toggle source
# File lib/scad4r/runner.rb, line 52
def openscad_invocation(path, options)
  [*openscad_command(options),*runtime_arguments(path, options)]
end
output_file(path, options) click to toggle source
# File lib/scad4r/runner.rb, line 75
def output_file(path, options)
  extension = options.fetch(:format)
  Pathname.new(path).sub_ext(".#{extension}")
end
pipes() click to toggle source
# File lib/scad4r/runner.rb, line 48
def pipes
  IO.pipe
end
runtime_arguments(path, options) click to toggle source
# File lib/scad4r/runner.rb, line 64
def runtime_arguments(path, options)
  arguments = []

  arguments << "-o#{options.fetch(:output)}"

  arguments.push(*setting_constants(options.fetch(:constants)))

  # input file
  arguments << path.to_s
end
setting_constants(assignments) click to toggle source
# File lib/scad4r/runner.rb, line 80
def setting_constants(assignments)
  assignments.inject([]) do |constants, (name, value)|
    constant = "#{name}="

    case value
    when String
      constant << value.inspect
    when Symbol
      constant << value.to_s.inspect
    else
      constant << value.to_s
    end

    constants << "-D '#{constant}'"
  end
end