Class: Rangefinder

Inherits:
Object
  • Object
show all
Defined in:
lib/rangefinder.rb,
lib/rangefinder/version.rb

Defined Under Namespace

Classes: Bigquery, Parser

Constant Summary collapse

VERSION =
'0.0.6'

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Rangefinder

Returns a new instance of Rangefinder.



8
9
10
11
12
13
14
15
16
17
# File 'lib/rangefinder.rb', line 8

def initialize(options)
  options[:filenames] ||= [] # don't blow up if we're called with no files

  @options  = options
  @bigquery = Rangefinder::Bigquery.new(options)

  if options[:filenames].size == 1 and File.directory?(options[:filenames].first)
    options[:filenames] = Dir.glob("#{options[:filenames].first}/**/*")
  end
end

Instance Method Details

#analyze(files) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rangefinder.rb', line 45

def analyze(files)
  files.map do |filename|
    case File.extname(filename).downcase
    when '.pp'
      kind, name = Rangefinder::Parser::Puppet.new(filename).evaluate!
    when '.rb'
      kind, name = Rangefinder::Parser::Ruby.new(filename).evaluate!
    else
      $logger.info "Unknown file type: #{filename}"
      next
    end

    if kind.nil? or name.nil?
      $logger.info "Skipping #{filename}"
      next
    end

    @bigquery.find(namespace(filename), kind, name)
  end
end

#debugObject



121
122
123
124
# File 'lib/rangefinder.rb', line 121

def debug
  require 'pry'
  binding.pry
end

#namespace(path) ⇒ Object

get the namespace of the module this file is located in. doing it inside the loop is slower, but it means that we can check files from multiple modules at once.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rangefinder.rb', line 69

def namespace(path)
  segments = Pathname(File.expand_path(path)).each_filename.to_a
  index    = segments.rindex {|item| ['lib','manifests'].include? item}

  return nil if index.nil?

  modroot  = segments[0...index]
  pathname = File.join([File::SEPARATOR, modroot, 'metadata.json'].flatten)
   = JSON.parse(File.read(pathname)) rescue {}
  ['name']
end

#pretty_print(results) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rangefinder.rb', line 90

def pretty_print(results)
  output  = "[#{results[:name]}] is a _#{results[:kind]}_\n"
  output << "==================================\n"

  if results[:exact].empty? and results[:near].empty?
    output << "with no external impact.\n\n"
  end

  unless results[:puppetfile].empty?
    output << "The enclosing module is declared in #{results[:puppetfile]} indexed public Puppetfiles\n\n"
  end

  unless results[:exact].empty?
    output << "Breaking changes to this file WILL impact these modules:\n"
    results[:exact].each do |row|
      output << "  * #{row[:module]} (#{row[:repo]})\n"
    end
    output << "\n"
  end

  unless results[:near].empty?
    output << "Breaking changes to this file MAY impact these modules:\n"
    results[:near].each do |row|
      output << "  * #{row[:module]} (#{row[:repo]})\n"
    end
    output << "\n"
  end

  output
end


81
82
83
84
85
86
87
88
# File 'lib/rangefinder.rb', line 81

def print_line(results)
  sprintf("%-#{@maxlen}s %-12s %-8s %-7s %-12s\n",
          results[:name],
          results[:kind],
          results[:exact].size,
          results[:near].size,
          results[:puppetfile])
end

#render!Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rangefinder.rb', line 19

def render!
  results = analyze(@options[:filenames]).compact
  @maxlen = (results.map {|res| res[:name].length} + [12]).max # Take into account the 'item name' label

  if @options[:render] == :summarize
    printf("%-#{@maxlen}s %-12s %-8s %-7s %-12s\n", 'Item Name', 'Kind', 'Exact', 'Near', 'Puppetfiles')
    puts '=' * (@maxlen+42)
  end

  results.each do |item|
    case @options[:render]
    when :human
      puts pretty_print(item)
    when :summarize
      puts print_line(item)
    when :json
      puts JSON.pretty_generate(item)
    when :yaml
      require 'yaml'
      puts item.to_yaml
    else
      raise "Invalid render type (#{@options[:render]})."
    end
  end
end