Class: Licensed::Sources::NuGet

Inherits:
Source
  • Object
show all
Defined in:
lib/licensed/sources/nuget.rb

Overview

Only supports ProjectReference (project.assets.json) style restore used in .NET Core. Does not currently support packages.config style restore.

Defined Under Namespace

Classes: NuGetDependency

Instance Attribute Summary

Attributes inherited from Source

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Source

#dependencies, full_type, #ignored?, inherited, #initialize, register_source, require_matched_dependency_version, #source_config, type

Constructor Details

This class inherits a constructor from Licensed::Sources::Source

Class Method Details

.type_and_versionObject



10
11
12
# File 'lib/licensed/sources/nuget.rb', line 10

def self.type_and_version
  ["nuget"]
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/licensed/sources/nuget.rb', line 186

def enabled?
  File.exist?(project_assets_file_path)
end

#enumerate_dependenciesObject

Inspect project.assets.json files for package references. Ideally we’d use ‘dotnet list package` instead, but its output isn’t easily machine readable and doesn’t contain everything we need.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/licensed/sources/nuget.rb', line 193

def enumerate_dependencies
  reference_keys.map do |reference_key|
    package_id_parts = reference_key.partition("/")
    name = package_id_parts[0]
    version = package_id_parts[-1]
    id = "#{name}-#{version}"

    path = full_dependency_path(reference_key)
    error = "Package #{id} path was not found in project.assets.json, or does not exist on disk at any project package folder" if path.nil?

    NuGetDependency.new(
      name: id,
      version: version,
      path: path,
      errors: Array(error),
      metadata: {
        "type" => NuGet.type,
        "name" => name
      }
    )
  end
end

#full_dependency_path(reference_key) ⇒ Object

Returns a dependency’s path, if it exists, in one of the project’s global or fallback package folders



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/licensed/sources/nuget.rb', line 227

def full_dependency_path(reference_key)
  dependency_path = project_assets_json.dig("libraries", reference_key, "path")
  return unless dependency_path

  nuget_package_dirs = [
    project_assets_json.dig("project", "restore", "packagesPath"),
    *Array(project_assets_json.dig("project", "restore", "fallbackFolders"))
  ].compact

  nuget_package_dirs.map { |dir| File.join(dir, dependency_path) }
                    .find { |path| File.directory?(path) }
end

#nuget_obj_pathObject



182
183
184
# File 'lib/licensed/sources/nuget.rb', line 182

def nuget_obj_path
  config.dig("nuget", "obj_path") || ""
end

#project_assets_fileObject



170
171
172
173
# File 'lib/licensed/sources/nuget.rb', line 170

def project_assets_file
  return @project_assets_file if defined?(@project_assets_file)
  @project_assets_file = File.read(project_assets_file_path)
end

#project_assets_file_pathObject



166
167
168
# File 'lib/licensed/sources/nuget.rb', line 166

def project_assets_file_path
  File.join(config.pwd, nuget_obj_path, "project.assets.json")
end

#project_assets_jsonObject



175
176
177
178
179
180
# File 'lib/licensed/sources/nuget.rb', line 175

def project_assets_json
  @project_assets_json ||= JSON.parse(project_assets_file)
rescue JSON::ParserError => e
  message = "Licensed was unable to read the project.assets.json file. Error: #{e.message}"
  raise Licensed::Sources::Source::Error, message
end

#reference_keysObject

Returns a unique set of the package reference keys used across all target groups



217
218
219
220
221
222
223
224
# File 'lib/licensed/sources/nuget.rb', line 217

def reference_keys
  all_reference_keys = project_assets_json["targets"].flat_map do |_, references|
    references.select { |key, reference| reference["type"] == "package" }
              .keys
  end

  Set.new(all_reference_keys)
end