Class: Licensed::Sources::Gradle::Dependency

Inherits:
Dependency
  • Object
show all
Defined in:
lib/licensed/sources/gradle.rb

Constant Summary collapse

GRADLE_LICENSES_CSV_NAME =
"licenses.csv".freeze

Constants inherited from Dependency

Dependency::LEGAL_FILES_PATTERN

Instance Attribute Summary

Attributes inherited from Dependency

#errors, #name, #path, #version

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Dependency

#errors?, #license_contents, #license_key, #notice_contents, #record

Constructor Details

#initialize(name:, version:, path:, executable:, configurations:, metadata: {}) ⇒ Dependency

Returns a new instance of Dependency.



56
57
58
59
60
# File 'lib/licensed/sources/gradle.rb', line 56

def initialize(name:, version:, path:, executable:, configurations:, metadata: {})
  @configurations = configurations
  @executable = executable
  super(name: name, version: version, path: path, metadata: )
end

Class Method Details

.csv_key(name:, version:) ⇒ Object

Returns a key to uniquely identify a name and version in the obtained CSV content



21
22
23
# File 'lib/licensed/sources/gradle.rb', line 21

def csv_key(name:, version:)
  "#{name}-#{version}"
end

.load_csv(path, executable, configurations) ⇒ Object

Loads and caches license report CSV data as a hash of :name-:version => :url pairs

executable - The gradle executable to run to generate the license report configurations - The gradle configurations to generate license report for

Returns a hash of dependency identifiers to their license content URL



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/licensed/sources/gradle.rb', line 31

def load_csv(path, executable, configurations)
  @csv ||= begin
    gradle_licenses_dir = File.join(path, GRADLE_LICENSES_PATH)
    Licensed::Sources::Gradle.gradle_command("generateLicenseReport", path: path, executable: executable, configurations: configurations)
    CSV.foreach(File.join(gradle_licenses_dir, GRADLE_LICENSES_CSV_NAME), headers: true).each_with_object({}) do |row, hsh|
      name, _, version = row["artifact"].rpartition(":")
      key = csv_key(name: name, version: version)
      hsh[key] = row["moduleLicenseUrl"]
    end
  ensure
    FileUtils.rm_rf(gradle_licenses_dir)
  end
end

.retrieve_license(url) ⇒ Object

Cache and return the results of getting the license content.



51
52
53
# File 'lib/licensed/sources/gradle.rb', line 51

def retrieve_license(url)
  (@licenses ||= {})[url] ||= Net::HTTP.get(URI(url))
end

.url_for(dependency) ⇒ Object

Returns the cached url for the given dependency



46
47
48
# File 'lib/licensed/sources/gradle.rb', line 46

def url_for(dependency)
  @csv[csv_key(name: dependency.name, version: dependency.version)]
end

Instance Method Details

#exist?Boolean

Returns whether the dependency content exists

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/licensed/sources/gradle.rb', line 63

def exist?
  # shouldn't force network connections just to check if content exists
  # only check that the path is not empty
  !path.to_s.empty?
end

#project_filesObject

Returns a Licensee::ProjectFiles::LicenseFile for the dependency



70
71
72
73
74
75
76
77
78
79
# File 'lib/licensed/sources/gradle.rb', line 70

def project_files
  self.class.load_csv(path, @executable, @configurations)
  url = self.class.url_for(self)

  return [] if url.nil?

  license_data = self.class.retrieve_license(url)

  Array(Licensee::ProjectFiles::LicenseFile.new(license_data, { uri: url }))
end