class Licensed::Sources::Gradle::Runner

The Gradle::Runner class is a wrapper which provides an interface to run gradle commands with the init script initialized

Public Class Methods

new(configurations, executable) click to toggle source
# File lib/licensed/sources/gradle.rb, line 137
def initialize(configurations, executable)
  @executable = executable
  @init_script = create_init_script(configurations)
end

Public Instance Methods

run(command) click to toggle source
# File lib/licensed/sources/gradle.rb, line 142
def run(command)
  args = [command]
  # The configuration cache is an incubating feature that can be activated manually.
  # The gradle plugin for licenses does not support it so we prevent it to run for gradle version supporting it.
  args << "--no-configuration-cache" if gradle_version >= Gem::Version.new("6.6")
  Licensed::Shell.execute(@executable, "-q", "--init-script", @init_script.path, *args)
end

Private Instance Methods

create_init_script(configurations) click to toggle source
# File lib/licensed/sources/gradle.rb, line 152
        def create_init_script(configurations)
          # we need to create extensions in the event that the user hasn't configured custom configurations
          # to avoid hitting errors where core Gradle configurations are set with canBeResolved=false
          configuration_map = configurations.map { |c| [c, "licensed#{c}"] }.to_h
          configuration_dsl = configuration_map.map { |orig, custom| "#{custom}.extendsFrom(#{orig})" }

          f = Tempfile.new(["init", ".gradle"])
          f.write(
            <<~EOF
                import com.github.jk1.license.render.CsvReportRenderer
                import com.github.jk1.license.filter.LicenseBundleNormalizer
                final configs = #{configuration_map.values.inspect}

                initscript {
                  repositories {
                    maven {
                      url "https://plugins.gradle.org/m2/"
                    }
                  }
                  dependencies {
                    classpath "com.github.jk1:gradle-license-report:#{gradle_version >= Gem::Version.new("7.0") ? "2.0" : "1.17"}"
                  }
                }

                allprojects {
                  configurations {
                    #{configuration_dsl.join("\n") }
                  }

                  apply plugin: com.github.jk1.license.LicenseReportPlugin
                  licenseReport {
                      outputDir = "$rootDir/#{GRADLE_LICENSES_PATH}"
                      configurations = configs
                      renderers = [new CsvReportRenderer()]
                      filters = [new LicenseBundleNormalizer()]
                  }

                  task printDependencies {
                    doLast {
                        def dependencies = []
                        configs.each {
                            configurations[it].resolvedConfiguration.resolvedArtifacts.each { artifact ->
                                def id = artifact.moduleVersion.id
                                dependencies << "{ \\"group\\": \\"${id.group}\\", \\"name\\": \\"${id.name}\\", \\"version\\": \\"${id.version}\\" }"
                            }
                        }
                        println "[${dependencies.join(", ")}]"
                    }
                  }
                }
              EOF
            )
          f.close
          f
        end
gradle_version() click to toggle source

Returns the version of gradle used during execution

# File lib/licensed/sources/gradle.rb, line 209
def gradle_version
  @gradle_version ||= begin
    version = Licensed::Shell.execute(@executable, "--version").scan(/Gradle [\d+]\.[\d+]/).last.split(" ").last
    Gem::Version.new(version)
  end
end