class Pith::Output

Attributes

error[R]
input[R]
path[R]

Public Class Methods

for(input, path) click to toggle source
# File lib/pith/output.rb, line 9
def self.for(input, path)
  new(input, path)
end
new(input, path) click to toggle source
# File lib/pith/output.rb, line 13
def initialize(input, path)
  @input = input
  @path = path
end

Public Instance Methods

build() click to toggle source

Generate output for this template

# File lib/pith/output.rb, line 32
def build
  return false if @generated
  logger.info("--> #{path}")
  @dependencies = Set.new
  file.parent.mkpath
  if input.template?
    evaluate_template
  else
    copy_resource
  end
  @generated = true
end
delete() click to toggle source
# File lib/pith/output.rb, line 52
def delete
  invalidate
  if file.exist?
    logger.info("--X #{path}")
    FileUtils.rm_f(file)
  end
end
file() click to toggle source
# File lib/pith/output.rb, line 26
def file
  @file ||= project.output_dir + path
end
invalidate() click to toggle source
# File lib/pith/output.rb, line 60
def invalidate
  if @generated
    @dependencies.each do |d|
      d.remove_observer(self)
    end
    @dependencies = nil
    @generated = nil
  end
end
project() click to toggle source
# File lib/pith/output.rb, line 22
def project
  input.project
end
record_dependency_on(*inputs) click to toggle source
# File lib/pith/output.rb, line 45
def record_dependency_on(*inputs)
  inputs.each do |input|
    @dependencies << input
    input.add_observer(self, :invalidate)
  end
end

Private Instance Methods

copy_resource() click to toggle source
# File lib/pith/output.rb, line 72
def copy_resource
  FileUtils.copy(input.file, file)
  record_dependency_on(input)
end
evaluate_template() click to toggle source
# File lib/pith/output.rb, line 77
def evaluate_template
  render_context = RenderContext.new(self)
  file.open("w") do |out|
    begin
      @error = nil
      out.puts(render_context.render(input))
    rescue StandardError, SyntaxError => e
      @error = e
      logger.warn exception_summary(e, :max_backtrace => 5)
      out.puts "<pre>"
      out.puts exception_summary(e)
    end
  end
  record_dependency_on(project.config_provider)
end
exception_summary(e, options = {}) click to toggle source
# File lib/pith/output.rb, line 97
def exception_summary(e, options = {})
  max_backtrace = options[:max_backtrace] || 999
  trimmed_backtrace = e.backtrace[0, max_backtrace]
  (["#{e.class}: #{e.message}"] + trimmed_backtrace).join("\n    ") + "\n"
end
logger() click to toggle source
# File lib/pith/output.rb, line 93
def logger
  project.logger
end