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

#resolveObject



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/danger/plugin_support/plugin_file_resolver.rb', line 13

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

  # 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") }
        paths = relative_paths.map { |path| File.join(dir, path) }

        spec_paths = gem_names.flat_map { |plugin| Dir.glob("vendor/gems/ruby/*/specifications/#{plugin}*.gemspec").first }
        real_gems = spec_paths.map { |path| Gem::Specification.load path }

         = real_gems.map do |gem|
          {
            name: gem.name,
            gem: gem.name,
            author: gem.authors,
            url: gem.homepage,
            description: gem.summary,
            license: gem.license || "Unknown",
            version: gem.version.to_s
          }
        end
        { paths: paths, gems:  }
      end
    end
  # When empty, imply you want to test the current lib folder as a plugin
  else
    paths = Dir.glob(File.join(".", "lib/**/*.rb")).map { |path| File.expand_path(path) }
    { paths: paths, gems: [] }
  end
end