Sha256: d858b312b487f93af4059841cae4576201b20a04922840db0ceeb828c506f404

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

require "bundler"
require "pathname"
require "fileutils"

module Danger
  class PluginFileResolver
    # Takes an array of files, gems or nothing, then resolves them into
    # paths that should be sent into the documentation parser
    def initialize(references)
      @refs = references
    end

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
danger-2.0.1 lib/danger/plugin_support/plugin_file_resolver.rb
danger-2.0.0 lib/danger/plugin_support/plugin_file_resolver.rb