Class: LicenseFinder::Pub

Inherits:
PackageManager show all
Defined in:
lib/license_finder/package_managers/pub.rb

Defined Under Namespace

Classes: PubError

Instance Method Summary collapse

Methods inherited from PackageManager

#active?, #command_exists?, #current_packages_with_relations, #detected_package_path, id, #initialize, #installed?, #project_root?, takes_priority_over

Constructor Details

This class inherits a constructor from LicenseFinder::PackageManager

Instance Method Details

#current_packagesObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/license_finder/package_managers/pub.rb', line 10

def current_packages
  unless File.exist?('pubspec.lock')
    raise PubError, "No checked-out Pub packages found.
      Please install your dependencies first."
  end

  if ENV['PUB_CACHE'].nil? || ENV['PUB_CACHE'].eql?('')
    raise PubError, 'While PUB_CACHE environment variable is empty, retrieving package licenses is impossible. Please set the PUB_CACHE env variable (default: ~/.pub)'
  end

  stdout, _stderr, _status = Cmd.run('flutter pub deps --json')
  yaml_deps = JSON.parse(stdout)
  yaml_deps['packages'].map do |dependency|
    package_name = dependency['name']
    subpath = "#{dependency['name']}-#{dependency['version']}"
    package_version = dependency['version']

    project_repo = dependency['source'] == 'git' ? Pathname("#{ENV['PUB_CACHE']}/git/#{dependency['name']}-*/") : Pathname("#{ENV['PUB_CACHE']}/hosted/pub.dartlang.org/#{subpath}")

    homepage = read_repository_home(project_repo)
    homepage = "https://pub.dev/packages/#{package_name}" if homepage.nil? || homepage.empty?
    PubPackage.new(
      package_name,
      package_version,
      license_text(project_repo),
      logger: logger,
      install_path: project_repo,
      homepage: homepage
    )
  end
end

#package_management_commandObject



46
47
48
# File 'lib/license_finder/package_managers/pub.rb', line 46

def package_management_command
  'flutter'
end

#possible_package_pathsObject



42
43
44
# File 'lib/license_finder/package_managers/pub.rb', line 42

def possible_package_paths
  [project_path.join('pubspec.lock')]
end

#prepareObject



54
55
56
57
58
59
60
61
62
# File 'lib/license_finder/package_managers/pub.rb', line 54

def prepare
  prep_cmd = "#{prepare_command} #{production_flag}"
  _stdout, stderr, status = Dir.chdir(project_path) { Cmd.run(prep_cmd) }

  return if status.success?

  log_errors stderr
  raise "Prepare command '#{prep_cmd}' failed" unless @prepare_no_fail
end

#prepare_commandObject



50
51
52
# File 'lib/license_finder/package_managers/pub.rb', line 50

def prepare_command
  'flutter pub get'
end