Class: Licensed::Sources::NuGet
- 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
Class Method Summary collapse
Instance Method Summary collapse
- #enabled? ⇒ Boolean
-
#enumerate_dependencies ⇒ Object
Inspect project.assets.json files for package references.
-
#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.
- #nuget_obj_path ⇒ Object
- #project_assets_file ⇒ Object
- #project_assets_file_path ⇒ Object
- #project_assets_json ⇒ Object
-
#reference_keys ⇒ Object
Returns a unique set of the package reference keys used across all target groups.
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_version ⇒ Object
10 11 12 |
# File 'lib/licensed/sources/nuget.rb', line 10 def self.type_and_version ["nuget"] end |
Instance Method Details
#enabled? ⇒ Boolean
186 187 188 |
# File 'lib/licensed/sources/nuget.rb', line 186 def enabled? File.exist?(project_assets_file_path) end |
#enumerate_dependencies ⇒ Object
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_path ⇒ Object
182 183 184 |
# File 'lib/licensed/sources/nuget.rb', line 182 def nuget_obj_path config.dig("nuget", "obj_path") || "" end |
#project_assets_file ⇒ Object
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_path ⇒ Object
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_json ⇒ Object
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 = "Licensed was unable to read the project.assets.json file. Error: #{e.}" raise Licensed::Sources::Source::Error, end |
#reference_keys ⇒ Object
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 |