Class: Licensed::Sources::Gradle

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

Defined Under Namespace

Classes: Dependency

Constant Summary collapse

DEFAULT_CONFIGURATIONS =
["runtime", "runtimeClasspath"].freeze
GRADLE_LICENSES_PATH =
".gradle-licenses".freeze

Instance Attribute Summary

Attributes inherited from Source

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Source

#dependencies, #ignored?, inherited, #initialize, type

Constructor Details

This class inherits a constructor from Licensed::Sources::Source

Class Method Details

.add_gradle_license_report_plugins_block(gradle_build_file) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/licensed/sources/gradle.rb', line 125

def self.add_gradle_license_report_plugins_block(gradle_build_file)

  if gradle_build_file.include? "plugins"
    gradle_build_file.gsub(/(?<=plugins)\s+{/, " { id 'com.github.jk1.dependency-license-report' version '1.6'")
  else

    gradle_build_file = " plugins { id 'com.github.jk1.dependency-license-report' version '1.6' }" + gradle_build_file
  end
end

.gradle_command(*args, path:, executable:, configurations:) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/licensed/sources/gradle.rb', line 135

def self.gradle_command(*args, path:, executable:, configurations:)
  gradle_build_file = File.read("build.gradle")

  if !gradle_build_file.include? "dependency-license-report"
    gradle_build_file = Licensed::Sources::Gradle.add_gradle_license_report_plugins_block(gradle_build_file)
  end

  Dir.chdir(path) do
    Tempfile.create(["license-", ".gradle"], path) do |f|
      f.write(gradle_build_file)
      f.write gradle_file(configurations)
      f.close
      Licensed::Shell.execute(executable, "-q", "-b", f.path, *args)
    end
  end
end

.gradle_file(configurations) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/licensed/sources/gradle.rb', line 152

def self.gradle_file(configurations)
  <<~EOF

    import com.github.jk1.license.render.CsvReportRenderer
    import com.github.jk1.license.filter.LicenseBundleNormalizer

    final configs = #{configurations.inspect}

    licenseReport {
        configurations = configs
        outputDir = "$projectDir/#{GRADLE_LICENSES_PATH}"
        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 "[\\n${dependencies.join(", ")}\\n]"
        }
    }
  EOF
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/licensed/sources/gradle.rb', line 82

def enabled?
  !gradle_executable.to_s.empty? && File.exist?(config.pwd.join("build.gradle"))
end

#enumerate_dependenciesObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/licensed/sources/gradle.rb', line 86

def enumerate_dependencies
  JSON.parse(self.class.gradle_command("printDependencies", path: config.pwd, executable: gradle_executable, configurations: configurations)).map do |package|
    name = "#{package["group"]}:#{package["name"]}"
    Dependency.new(
      name: name,
      version: package["version"],
      path: config.pwd,
      executable: gradle_executable,
      configurations: configurations,
      metadata: {
        "type"     => Gradle.type
      }
    )
  end
end