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, id, #initialize, #project_root?, takes_priority_over

Constructor Details

This class inherits a constructor from LicenseFinder::PackageManager

Class Method Details

.nuspec_license_urls(specfile_content) ⇒ Object



152
153
154
155
156
157
# File 'lib/license_finder/package_managers/nuget.rb', line 152

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
60
61
62
63
# File 'lib/license_finder/package_managers/nuget.rb', line 51

def current_packages
  dependencies.each_with_object({}) do |dep, memo|
    licenses = license_urls(dep)
    licenses&.map! do |license|
      license.gsub('https://licenses.nuget.org/', '')
    end

    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



77
78
79
# File 'lib/license_finder/package_managers/nuget.rb', line 77

def dependencies
  assemblies.flat_map(&:dependencies)
end

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

Returns:

  • (Boolean)


136
137
138
139
140
141
142
143
144
# File 'lib/license_finder/package_managers/nuget.rb', line 136

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



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/license_finder/package_managers/nuget.rb', line 65

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



81
82
83
84
85
86
87
88
89
# File 'lib/license_finder/package_managers/nuget.rb', line 81

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



146
147
148
149
150
# File 'lib/license_finder/package_managers/nuget.rb', line 146

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

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

#package_management_commandObject



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

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

#prepareObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/license_finder/package_managers/nuget.rb', line 97

def prepare
  Dir.chdir(project_path) do
    cmd = prepare_command
    stdout, stderr, status = Cmd.run(cmd)
    return if status.success?

    log_errors stderr

    if stderr.include?('-PackagesDirectory')
      logger.info cmd, 'trying fallback prepare command', color: :magenta

      cmd = "#{cmd} -PackagesDirectory /#{Dir.home}/.nuget/packages"
      stdout, stderr, status = Cmd.run(cmd)
      return if status.success?

      log_errors_with_cmd(cmd, stderr)
    end

    error_message = "Prepare command '#{cmd}' failed\n#{stderr}"
    error_message += "\n#{stdout}\n" if !stdout.nil? && !stdout.empty?
    raise error_message unless @prepare_no_fail
  end
end

#prepare_commandObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/license_finder/package_managers/nuget.rb', line 121

def prepare_command
  cmd = package_management_command
  sln_files = Dir['*.sln']
  cmds = []
  if sln_files.count > 1
    sln_files.each do |sln|
      cmds << "#{cmd} restore #{sln}"
    end
  else
    cmds << "#{cmd} restore"
  end

  cmds.join(' && ')
end