class Ninja::Writer

helper for generating ninja files

Attributes

out[R]
rule_suffix[RW]

Public Class Methods

new(out) click to toggle source
# File lib/ninja.rb, line 19
def initialize(out)
  @out = out
end

Public Instance Methods

build(outputs, rule, inputs, options = {}) click to toggle source
# File lib/ninja.rb, line 74
def build(outputs, rule, inputs, options = {})
  out_outputs = Array(outputs)
  out_outputs = out_outputs.map { |path| escape_path(path) } if options[:esc_out]
  all_inputs = Array(inputs)
  all_inputs = all_inputs.map { |path| escape_path(path) } if options[:esc_in]

  if options[:implicit]
    implicit = Array(options[:implicit])
    implicit.map! { |path| escape_path(path) } if options[:esc_implicit]
    unless implicit.empty?
      all_inputs << '| '
      all_inputs += implicit
    end
  end

  if options[:order_only]
    order_only = Array(options[:order_only])
    order_only.map! { |path| escape_path(path) } if options[:esc_order_only]
    unless order_only.empty?
      all_inputs << '|| '
      all_inputs += order_only
    end
  end

  @out.puts("build #{out_outputs.join(' ')}: " \
            "#{([wrap_rule(rule)] + all_inputs).join(' ')}")

  return unless options[:variables]

  options[:variables].each do |key, value|
    variable(key, value, 1)
  end
end
comment(text) click to toggle source
# File lib/ninja.rb, line 27
def comment(text)
  @out.puts(text.split(/[#\n]/).map { |line| "# #{line.strip}" }.join(''))
end
default(paths) click to toggle source
# File lib/ninja.rb, line 116
def default(paths)
  p = Array(paths).compact
  @out.puts("default #{p.join(' ')}") unless p.empty?
end
include(path) click to toggle source
# File lib/ninja.rb, line 108
def include(path)
  @out.puts("include #{path}\n")
end
newline() click to toggle source
# File lib/ninja.rb, line 23
def newline
  @out.puts
end
phony(name, paths) click to toggle source
# File lib/ninja.rb, line 121
def phony(name, paths)
  p = Array(paths).compact
  @out.puts("build #{name}: phony #{p.join(' ')}") unless p.empty?
end
pool(name, depth) click to toggle source
# File lib/ninja.rb, line 46
def pool(name, depth)
  @out.puts("pool #{name}")
  variable('depth', depth, 1)
end
rule(name, command, options = {}) click to toggle source
# File lib/ninja.rb, line 59
def rule(name, command, options = {})
  @out.puts("rule #{wrap_rule(name)}")
  # TODO: check options
  variable('command', command, 1)
  variable('description', options[:description], 1)
  variable('depfile', options[:depfile], 1)
  variable('generator', '1', 1) if options[:generator]
  variable('pool', options[:pool], 1)
  variable('restat', '1', 1) if options[:restat]
  variable('rspfile', options[:rspfile], 1)
  variable('rspfile_content', options[:rspfile_content], 1)
  variable('deps', options[:deps], 1)
  @out.puts
end
subninja(path) click to toggle source
# File lib/ninja.rb, line 112
def subninja(path)
  @out.puts("subninja #{path}\n")
end
variable(key, value, indent = 0) click to toggle source
# File lib/ninja.rb, line 31
def variable(key, value, indent = 0)
  return if value.nil?

  val = if value.is_a?(Array)
          value.map(&:to_s).find_all { |v| !v.empty? }.join(' ')
        else
          value
        end

  return if val.nil?

  @out.write('  ' * indent)
  @out.puts("#{key} = #{val}")
end
wrap_rule(name) click to toggle source
# File lib/ninja.rb, line 51
def wrap_rule(name)
  if @rule_suffix
    "#{name}__#{@rule_suffix}"
  else
    name
  end
end