class Licensed::Sources::Gradle

Constants

DEFAULT_CONFIGURATIONS
GRADLE_LICENSES_CSV_NAME
GRADLE_LICENSES_PATH

Public Instance Methods

enabled?() click to toggle source
# File lib/licensed/sources/gradle.rb, line 44
def enabled?
  !executable.to_s.empty? && File.exist?(config.pwd.join("build.gradle"))
end
enumerate_dependencies() click to toggle source
# File lib/licensed/sources/gradle.rb, line 48
def enumerate_dependencies
  JSON.parse(gradle_runner.run("printDependencies")).map do |package|
    name = "#{package['group']}:#{package['name']}"
    Dependency.new(
      name: name,
      version: package["version"],
      path: config.pwd,
      url: package_url(name: name, version: package["version"]),
      metadata: {
        "type" => Gradle.type,
      }
    )
  end
end

Private Instance Methods

configurations() click to toggle source

Returns the configurations to include in license generation. Defaults to [“runtime”, “runtimeClasspath”]

# File lib/licensed/sources/gradle.rb, line 81
def configurations
  @configurations ||= begin
    if configurations = config.dig("gradle", "configurations")
      Array(configurations)
    else
      DEFAULT_CONFIGURATIONS
    end
  end
end
csv_key(name:, version:) click to toggle source

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

# File lib/licensed/sources/gradle.rb, line 100
def csv_key(name:, version:)
  "#{name}-#{version}"
end
executable() click to toggle source
# File lib/licensed/sources/gradle.rb, line 65
def executable
  return @executable if defined?(@executable)

  @executable = begin
    return gradlew if File.executable?(gradlew)

    "gradle" if Licensed::Shell.tool_available?("gradle")
  end
end
gradle_runner() click to toggle source
# File lib/licensed/sources/gradle.rb, line 75
def gradle_runner
  @gradle_runner ||= Runner.new(configurations, executable)
end
gradlew() click to toggle source

Returns the path to the Gradle wrapper.

# File lib/licensed/sources/gradle.rb, line 92
def gradlew
  @gradlew ||= begin
    gradlew = config.dig("gradle", "gradlew")
    config.root.join(gradlew || "gradlew").to_s
  end
end
load_csv() click to toggle source
# File lib/licensed/sources/gradle.rb, line 112
def load_csv
  begin
    # create the CSV file including dependency license urls using the gradle plugin
    gradle_licenses_dir = File.join(config.root, GRADLE_LICENSES_PATH)
    gradle_runner.run("generateLicenseReport")

    # parse the CSV report for dependency license urls
    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
package_url(name:, version:) click to toggle source
# File lib/licensed/sources/gradle.rb, line 104
def package_url(name:, version:)
  # load and memoize the license report CSV
  @urls ||= load_csv

  # uniquely identify a name and version in the obtained CSV content
  @urls["#{name}-#{version}"]
end
url_for(dependency) click to toggle source

Returns the cached url for the given dependency

# File lib/licensed/sources/gradle.rb, line 130
def url_for(dependency)
  @csv[csv_key(name: dependency.name, version: dependency.version)]
end