Class: LicenseFinder::Conan

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

Instance Method Summary collapse

Methods inherited from PackageManager

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

Constructor Details

This class inherits a constructor from LicenseFinder::PackageManager

Instance Method Details

#current_packagesObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/license_finder/package_managers/conan.rb', line 51

def current_packages
  install_command = 'conan install .'
  Dir.chdir(project_path) { Cmd.run(install_command) }

  deps = deps_list(project_path)
  return [] if deps.nil?

  deps.map do |dep|
    name, version = dep['name'].split('/')
    license_file_path = license_file(project_path, name)

    next unless license_file_is_good?(license_file_path)

    url = dep['homepage']
    url = dep['url'] if url.nil?
    ConanPackage.new(name, version, File.open(license_file_path).read, url)
  end.compact
end

#deps_list(project_path) ⇒ Object



45
46
47
48
49
# File 'lib/license_finder/package_managers/conan.rb', line 45

def deps_list(project_path)
  deps = deps_list_conan_v1(project_path)
  deps = deps_list_conan_v2(project_path) if deps.nil? || deps.empty?
  deps
end

#deps_list_conan_v1(project_path) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/license_finder/package_managers/conan.rb', line 24

def deps_list_conan_v1(project_path)
  info_command = 'conan info .'
  info_output, _stderr, _status = Dir.chdir(project_path) { Cmd.run(info_command) }
  return nil if info_output.empty?

  info_parser = ConanInfoParser.new
  info_parser.parse(info_output)
end

#deps_list_conan_v2(project_path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/license_finder/package_managers/conan.rb', line 33

def deps_list_conan_v2(project_path)
  info_command = 'conan graph info .'
  info_output, stderr, _status = Dir.chdir(project_path) { Cmd.run(info_command) }
  if info_output.empty?
    return if stderr.empty?

    info_output = stderr
  end
  info_parser = ConanInfoParserV2.new
  info_parser.parse(info_output)
end

#license_file(project_path, name) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/license_finder/package_managers/conan.rb', line 16

def license_file(project_path, name)
  candidates = Dir.glob("#{project_path}/licenses/#{name}/**/LICENSE*")
  candidates.each do |candidate|
    return candidate if license_file_is_good?(candidate)
  end
  nil
end

#license_file_is_good?(license_file_path) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/license_finder/package_managers/conan.rb', line 12

def license_file_is_good?(license_file_path)
  !license_file_path.nil? && File.file?(license_file_path)
end

#possible_package_pathsObject



8
9
10
# File 'lib/license_finder/package_managers/conan.rb', line 8

def possible_package_paths
  [project_path.join('conanfile.txt'), project_path.join('conanfile.py')]
end