diff options
Diffstat (limited to 'lib/license/finder/ext/gradle.rb')
| -rw-r--r-- | lib/license/finder/ext/gradle.rb | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/license/finder/ext/gradle.rb b/lib/license/finder/ext/gradle.rb new file mode 100644 index 0000000..2c3ce01 --- /dev/null +++ b/lib/license/finder/ext/gradle.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +module LicenseFinder + class Gradle + def current_packages + return [] unless download_licenses + + Pathname + .glob(project_path.join('**', 'dependency-license.xml')) + .map(&:read) + .flat_map { |xml_file| parse_from(xml_file) }.uniq + end + + def package_management_command + wrapper? ? './gradlew' : 'gradle' + end + + private + + def download_licenses + _stdout, _stderr, status = Dir.chdir(project_path) do + shell.execute([ + @command, + ENV.fetch('GRADLE_CLI_OPTS', '--exclude-task=test'), + 'downloadLicenses' + ], env: { 'TERM' => 'noop' }) + end + + status.success? + end + + def wrapper? + File.exist?(File.join(project_path, 'gradlew')) + end + + def xml_parsing_options + @xml_parsing_options ||= { 'GroupTags' => { 'dependencies' => 'dependency' } } + end + + def parse_from(xml_file) + XmlSimple + .xml_in(xml_file, xml_parsing_options) + .fetch('dependency', []).map { |hash| map_from(hash) } + end + + def map_from(hash) + GradlePackage.new(hash, include_groups: @include_groups) + end + end +end |
