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.2'

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Rangefinder

Returns a new instance of Rangefinder.



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

def initialize(options)
  @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

#debugObject



92
93
94
95
# File 'lib/rangefinder.rb', line 92

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.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rangefinder.rb', line 53

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rangefinder.rb', line 65

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[: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

#render!Object



17
18
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
44
45
46
47
48
# File 'lib/rangefinder.rb', line 17

def render!
  @options[:filenames].each 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

    results = @bigquery.find(namespace(filename), kind, name)

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