Sha256: b1bed047ae97655163fc944f10a19b1182364f927f64241c267c08ef92e0a806

Contents?: true

Size: 1.96 KB

Versions: 11

Compression:

Stored size: 1.96 KB

Contents

require 'xmlsimple'
require 'with_env'
require 'license_finder/package_utils/gradle_dependency_finder'

module LicenseFinder
  class Gradle < PackageManager
    def initialize(options = {})
      super
      @command = options[:gradle_command] || package_management_command
      @include_groups = options[:gradle_include_groups]
    end

    def current_packages
      WithEnv.with_env('TERM' => 'dumb') do
        command = "#{@command} downloadLicenses"
        _stdout, stderr, status = Dir.chdir(project_path) { Cmd.run(command) }
        raise "Command '#{command}' failed to execute: #{stderr}" unless status.success?

        dependencies = GradleDependencyFinder.new(project_path).dependencies
        packages = dependencies.flat_map do |xml_file|
          options = { 'GroupTags' => { 'dependencies' => 'dependency' } }
          contents = XmlSimple.xml_in(xml_file, options).fetch('dependency', [])
          contents.map do |dep|
            GradlePackage.new(dep, logger: logger, include_groups: @include_groups)
          end
        end
        packages.uniq
      end
    end

    def package_management_command
      if Platform.windows?
        wrapper = 'gradlew.bat'
        gradle = 'gradle.bat'
      else
        wrapper = './gradlew'
        gradle = 'gradle'
      end

      File.exist?(File.join(project_path, wrapper)) ? wrapper : gradle
    end

    private

    def detected_package_path
      alternate_build_file = build_file_from_settings(project_path)
      return alternate_build_file if alternate_build_file

      project_path.join('build.gradle')
    end

    def build_file_from_settings(project_path)
      settings_gradle_path = project_path.join 'settings.gradle'

      return nil unless File.exist? settings_gradle_path

      settings_gradle = File.read settings_gradle_path

      match = /rootProject.buildFileName = ['"](?<build_file>.*)['"]/.match settings_gradle

      return nil unless match

      project_path.join match[:build_file]
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
license_finder-5.4.1 lib/license_finder/package_managers/gradle.rb
license_finder-5.4.0 lib/license_finder/package_managers/gradle.rb
license_finder-5.3.0 lib/license_finder/package_managers/gradle.rb
license_finder-5.2.3 lib/license_finder/package_managers/gradle.rb
license_finder-5.2.1 lib/license_finder/package_managers/gradle.rb
license_finder-5.2.0 lib/license_finder/package_managers/gradle.rb
license_finder-5.1.1 lib/license_finder/package_managers/gradle.rb
license_finder-5.1.0 lib/license_finder/package_managers/gradle.rb
license_finder-5.0.3 lib/license_finder/package_managers/gradle.rb
license_finder-5.0.2 lib/license_finder/package_managers/gradle.rb
license_finder-5.0.0 lib/license_finder/package_managers/gradle.rb