Class: LicenseFinder::Nuget

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

Defined Under Namespace

Classes: Assembly, Dependency

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PackageManager

#active?, #command_exists?, #current_packages_with_relations, #detected_package_path, #initialize, #prepare, #project_root?, takes_priority_over

Constructor Details

This class inherits a constructor from LicenseFinder::PackageManager

Class Method Details

.nuspec_license_urls(specfile_content) ⇒ Object



112
113
114
115
116
117
# File 'lib/license_finder/package_managers/nuget.rb', line 112

def self.nuspec_license_urls(specfile_content)
  xml = REXML::Document.new(specfile_content)
  REXML::XPath.match(xml, '//metadata//licenseUrl')
              .map(&:get_text)
              .map(&:to_s)
end

Instance Method Details

#assembliesObject



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

def assemblies
  Dir.glob(project_path.join('**', 'packages.config'), File::FNM_DOTMATCH).map do |d|
    path = Pathname.new(d).dirname
    name = path.basename.to_s
    Assembly.new path, name
  end
end

#current_packagesObject



51
52
53
54
55
56
57
58
59
# File 'lib/license_finder/package_managers/nuget.rb', line 51

def current_packages
  dependencies.each_with_object({}) do |dep, memo|
    licenses = license_urls(dep)
    path = Dir.glob("#{Dir.home}/.nuget/packages/#{dep.name.downcase}/#{dep.version}").first

    memo[dep.name] ||= NugetPackage.new(dep.name, dep.version, spec_licenses: licenses, install_path: path)
    memo[dep.name].groups << dep.assembly unless memo[dep.name].groups.include? dep.assembly
  end.values
end

#dependenciesObject



72
73
74
# File 'lib/license_finder/package_managers/nuget.rb', line 72

def dependencies
  assemblies.flat_map(&:dependencies)
end

#installed?(logger = Core.default_logger) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
# File 'lib/license_finder/package_managers/nuget.rb', line 96

def installed?(logger = Core.default_logger)
  _stdout, _stderr, status = Cmd.run(nuget_check)
  if status.success?
    logger.debug self.class, 'is installed', color: :green
  else
    logger.info self.class, 'is not installed', color: :red
  end
  status.success?
end

#license_urls(dep) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/license_finder/package_managers/nuget.rb', line 61

def license_urls(dep)
  files = Dir["**/#{dep.name}.#{dep.version}.nupkg"]
  return nil if files.empty?

  file = files.first
  Zip::File.open file do |zipfile|
    content = zipfile.read(dep.name + '.nuspec')
    Nuget.nuspec_license_urls(content)
  end
end

#nuget_binaryObject



76
77
78
79
80
81
82
83
84
# File 'lib/license_finder/package_managers/nuget.rb', line 76

def nuget_binary
  legacy_vcproj = Dir['**/*.vcproj'].any?

  if legacy_vcproj
    '/usr/local/bin/nugetv3.5.0.exe'
  else
    '/usr/local/bin/nuget.exe'
  end
end

#nuget_checkObject



106
107
108
109
110
# File 'lib/license_finder/package_managers/nuget.rb', line 106

def nuget_check
  return 'where nuget' if LicenseFinder::Platform.windows?

  "which mono && ls #{nuget_binary}"
end

#package_management_commandObject



86
87
88
89
90
# File 'lib/license_finder/package_managers/nuget.rb', line 86

def package_management_command
  return 'nuget' if LicenseFinder::Platform.windows?

  "mono #{nuget_binary}"
end

#possible_package_pathsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/license_finder/package_managers/nuget.rb', line 28

def possible_package_paths
  path = project_path.join('vendor/*.nupkg')
  nuget_dir = Dir[path].map { |pkg| File.dirname(pkg) }.uniq

  # Presence of a .sln is a good indicator for a dotnet solution
  # cf.: https://docs.microsoft.com/en-us/nuget/tools/cli-ref-restore#remarks
  path = project_path.join('*.sln')
  solution_file = Dir[path].first

  possible_paths = [project_path.join('packages.config'), project_path.join('.nuget')]
  possible_paths.unshift(Pathname(solution_file)) unless solution_file.nil?
  possible_paths.unshift(Pathname(nuget_dir.first)) unless nuget_dir.empty?
  possible_paths
end

#prepare_commandObject



92
93
94
# File 'lib/license_finder/package_managers/nuget.rb', line 92

def prepare_command
  "#{package_management_command} restore"
end