class Puppet_X::Binford2k::Itemize::Runner

Attributes

results[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/puppet_x/binford2k/itemize/runner.rb, line 7
def initialize(options = {})
  Puppet::Util::Log.newdestination(:console)

  @paths     = expand(Array(options[:manifests]))
  @options   = options
  @results   = {}

  if options[:manifests].size == 1
    root = options[:manifests].first
    path = [File.expand_path("#{root}/metadata.json"),
            File.expand_path("#{root}/../metadata.json")].select { |p| File.exist? p }.first

    if path
      @metadata  = JSON.parse(File.read(File.expand_path("#{path}/../metadata.json")))
      @namespace = @metadata['name'].split(/[-\/]/).last

      # we can only use the module name part of this, not the user namespace
      @dependencies = @metadata['dependencies'].map do |dep|
        dep['name'].split(/[-\/]/).last
      end
      # inject a few mocked dependencies that we can assume will always exist
      @dependencies << @namespace
      @dependencies << 'puppet_enterprise'
    else
      # what error to display here?
    end
  end
end

Public Instance Methods

dump!() click to toggle source
# File lib/puppet_x/binford2k/itemize/runner.rb, line 72
def dump!
  require 'json'
  puts JSON.pretty_generate(@results)
end
expand(paths) click to toggle source
# File lib/puppet_x/binford2k/itemize/runner.rb, line 36
def expand(paths)
  paths.map do |path|
    path = File.expand_path(path)

    if File.file? path
      path
    elsif File.directory? path
      Dir.glob("#{path}/**/*.pp")
    else
      raise "Path '#{path}' doesn't appear to be a file or directory"
    end
  end.flatten
end
run!() click to toggle source
# File lib/puppet_x/binford2k/itemize/runner.rb, line 50
def run!
  @paths.each do |path|
    Puppet.debug "Itemizing #{path}"
    parser = Puppet_X::Binford2k::Itemize::Parser.new(path, @options).parse!
    parser.results.each do |kind, counts|
      @results[kind] ||= {}

      counts.each do |name, count|
        segments = name.split('::')
        if @dependencies and segments.size > 1
          Puppet.warn_once(:dependency, name, "Undeclared module dependancy: #{name}", :default, :default) unless @dependencies.include? segments.first
        end
        next if @options[:external] and segments.first == @namespace

        @results[kind][name] ||= 0
        @results[kind][name]  += count
      end
    end
  end
  self
end