Class: Danger::PluginFileResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/danger/plugin_support/plugin_file_resolver.rb

Instance Method Summary collapse

Constructor Details

#initialize(references) ⇒ PluginFileResolver

Takes an array of files, gems or nothing, then resolves them into paths that should be sent into the documentation parser



9
10
11
# File 'lib/danger/plugin_support/plugin_file_resolver.rb', line 9

def initialize(references)
  @refs = references
end

Instance Method Details

#resolve_to_pathsObject



13
14
15
16
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
# File 'lib/danger/plugin_support/plugin_file_resolver.rb', line 13

def resolve_to_paths
  # When given existing paths, map to absolute & existing paths
  if !@refs.nil? and @refs.select { |ref| File.file? ref }.any?
    @refs.select { |ref| File.file? ref }.map { |path| File.expand_path(path) }

  # When given a list of gems
  elsif @refs and @refs.kind_of? Array
    Bundler.with_clean_env do
      # We don't use the block syntax as we want it to persist until the OS cleans it on reboot
      # or whatever, it needs to persist outside this scope.
      dir = Dir.mktmpdir

      Dir.chdir(dir) do
        gem_names = @refs
        gemfile = File.new("Gemfile", "w")
        gemfile.write "source 'https://rubygems.org'"

        gem_names.each do |plugin|
          gemfile.write "\ngem '#{plugin}'"
        end

        gemfile.close
        `bundle install --path vendor/gems`

        # the paths are relative to our current Chdir
        relative_paths = gem_names.flat_map { |plugin| Dir.glob("vendor/gems/ruby/*/gems/#{plugin}*/lib/**/**/**/**.rb") }
        relative_paths.map { |path| File.join(dir, path) }
      end
    end
  # When empty, imply you want to test the current lib folder as a plugin
  else
    Dir.glob(File.join(".", "lib/*.rb")).map { |path| File.expand_path(path) }
  end
end