Class: Bibliothecary::Parsers::Nuget
- Inherits:
-
Object
- Object
- Bibliothecary::Parsers::Nuget
- Extended by:
- MultiParsers::JSONRuntime
- Includes:
- Analyser
- Defined in:
- lib/bibliothecary/parsers/nuget.rb
Class Method Summary collapse
- .mapping ⇒ Object
- .parse_csproj(file_contents, options: {}) ⇒ Object
- .parse_nuspec(file_contents, options: {}) ⇒ Object
- .parse_packages_config(file_contents, options: {}) ⇒ Object
- .parse_packages_lock_json(file_contents, options: {}) ⇒ Object
- .parse_paket_lock(file_contents, options: {}) ⇒ Object
- .parse_project_assets_json(file_contents, options: {}) ⇒ Object
- .parse_project_lock_json(file_contents, options: {}) ⇒ Object
Methods included from MultiParsers::JSONRuntime
Methods included from Analyser
create_analysis, create_error_analysis, included
Class Method Details
.mapping ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/bibliothecary/parsers/nuget.rb', line 12 def self.mapping { match_filename("Project.json") => { kind: "manifest", parser: :parse_json_runtime_manifest, }, match_filename("Project.lock.json") => { kind: "lockfile", parser: :parse_project_lock_json, }, match_filename("packages.lock.json") => { kind: "lockfile", parser: :parse_packages_lock_json, }, match_filename("packages.config") => { kind: "manifest", parser: :parse_packages_config, }, match_extension(".nuspec") => { kind: "manifest", parser: :parse_nuspec, }, match_extension(".csproj") => { kind: "manifest", parser: :parse_csproj, }, match_filename("paket.lock") => { kind: "lockfile", parser: :parse_paket_lock, }, match_filename("project.assets.json") => { kind: "lockfile", parser: :parse_project_assets_json, }, } end |
.parse_csproj(file_contents, options: {}) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/bibliothecary/parsers/nuget.rb', line 115 def self.parse_csproj(file_contents, options: {}) manifest = Ox.parse file_contents # The dotnet samples repo has examples with both of these cases, so both need to be handled: project = if manifest.locate("Project").any? # 1) If there's an <?xml> tag, we need to pick out the "Project" element manifest.locate("Project").first else # 2) If there's no <?xml> tag, the root element is "Project" manifest end packages = project .locate("ItemGroup/PackageReference") .select { |dep| dep.respond_to? "Include" } .map do |dependency| requirement = (dependency.Version if dependency.respond_to? "Version") if requirement.is_a?(Ox::Element) requirement = dependency.nodes.detect { |n| n.value == "Version" }&.text end type = if (dependency.nodes.first&.nodes&.include?("all") && dependency.nodes.first.value.include?("PrivateAssets")) || dependency.attributes[:PrivateAssets] == "All" "development" else "runtime" end Dependency.new( name: dependency.Include, requirement: requirement, type: type, source: .fetch(:filename, nil), platform: platform_name ) end packages += project .locate("ItemGroup/Reference") .select { |dep| dep.respond_to? "Include" } .map do |dependency| vals = *dependency.Include.split(",").map(&:strip) # Skip <Reference> dependencies that only have the name value. Reasoning: # Builtin assemblies like "System.Web" or "Microsoft.CSharp" can be required from the framework or by # downloading via Nuget, and we only want to report on packages that are downloaded from Nuget. We are # pretty sure that if they don't have a version in <Reference> then they're likely from the framework # itself, which means they won't show up in the lockfile and we want to omit them. # Note: if we omit a false positive here it should still show up in the lockfile, and it should be # safer guess like this since <Reference> is an older standard. # Note: this strategy could also skip on-disk 3rd-party packages with a <HintPath> but no version in <Reference> next nil if vals.size == 1 name = vals.shift vals = vals.to_h { |r| r.split("=", 2) } Dependency.new( name: name, requirement: vals["Version"] || "*", type: "runtime", source: .fetch(:filename, nil), platform: platform_name ) end .compact dependencies = packages.uniq(&:name) ParserResult.new(dependencies: dependencies) rescue StandardError ParserResult.new(dependencies: []) end |
.parse_nuspec(file_contents, options: {}) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/bibliothecary/parsers/nuget.rb', line 186 def self.parse_nuspec(file_contents, options: {}) manifest = Ox.parse file_contents dependencies = manifest.package..dependencies.locate("dependency").map do |dependency| Dependency.new( name: dependency.id, requirement: dependency.attributes[:version], type: dependency.respond_to?("developmentDependency") && dependency.developmentDependency == "true" ? "development" : "runtime", source: .fetch(:filename, nil), platform: platform_name ) end ParserResult.new(dependencies: dependencies) rescue StandardError ParserResult.new(dependencies: []) end |
.parse_packages_config(file_contents, options: {}) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/bibliothecary/parsers/nuget.rb', line 99 def self.parse_packages_config(file_contents, options: {}) manifest = Ox.parse file_contents dependencies = manifest.packages.locate("package").map do |dependency| Dependency.new( name: dependency.id, requirement: (dependency.version if dependency.respond_to? "version"), type: dependency.respond_to?("developmentDependency") && dependency.developmentDependency == "true" ? "development" : "runtime", source: .fetch(:filename, nil), platform: platform_name ) end ParserResult.new(dependencies: dependencies) rescue StandardError ParserResult.new(dependencies: []) end |
.parse_packages_lock_json(file_contents, options: {}) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/bibliothecary/parsers/nuget.rb', line 68 def self.parse_packages_lock_json(file_contents, options: {}) manifest = JSON.parse file_contents frameworks = {} manifest.fetch("dependencies", []).each do |framework, deps| frameworks[framework] = deps .reject { |_name, details| details["type"] == "Project" } # Projects do not have versions .map do |name, details| Dependency.new( name: name, # 'resolved' has been set in all examples so far # so fallback to requested is pure paranoia requirement: details.fetch("resolved", details.fetch("requested", "*")), type: "runtime", source: .fetch(:filename, nil), platform: platform_name ) end end unless frameworks.empty? # we should really return multiple manifests, but bibliothecary doesn't # do that yet so at least pick deterministically. # Note, frameworks can be empty, so remove empty ones and then return the last sorted item if any frameworks.delete_if { |_k, v| v.empty? } return ParserResult.new(dependencies: frameworks[frameworks.keys.max]) unless frameworks.empty? end ParserResult.new(dependencies: []) end |
.parse_paket_lock(file_contents, options: {}) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/bibliothecary/parsers/nuget.rb', line 202 def self.parse_paket_lock(file_contents, options: {}) lines = file_contents.split("\n") package_version_re = /\s+(?<name>\S+)\s\((?<version>\d+\.\d+[.\d+[.\d+]*]*)\)/ packages = lines.select { |line| package_version_re.match(line) }.map { |line| package_version_re.match(line) }.map do |match| Dependency.new( name: match[:name].strip, requirement: match[:version], type: "runtime", source: .fetch(:filename, nil), platform: platform_name ) end # we only have to enforce uniqueness by name because paket ensures that there is only the single version globally in the project dependencies = packages.uniq(&:name) ParserResult.new(dependencies: dependencies) end |
.parse_project_assets_json(file_contents, options: {}) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/bibliothecary/parsers/nuget.rb', line 219 def self.parse_project_assets_json(file_contents, options: {}) manifest = JSON.parse file_contents frameworks = {} manifest.fetch("targets", []).each do |framework, deps| frameworks[framework] = deps .select { |_name, details| details["type"] == "package" } .map do |name, _details| name_split = name.split("/") Dependency.new( name: name_split[0], requirement: name_split[1], type: "runtime", source: .fetch(:filename, nil), platform: platform_name ) end end unless frameworks.empty? # we should really return multiple manifests, but bibliothecary doesn't # do that yet so at least pick deterministically. # Note, frameworks can be empty, so remove empty ones and then return the last sorted item if any frameworks.delete_if { |_k, v| v.empty? } return ParserResult.new(dependencies: frameworks[frameworks.keys.max]) unless frameworks.empty? end ParserResult.new(dependencies: []) end |
.parse_project_lock_json(file_contents, options: {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bibliothecary/parsers/nuget.rb', line 53 def self.parse_project_lock_json(file_contents, options: {}) manifest = JSON.parse file_contents dependencies = manifest.fetch("libraries", []).map do |name, _requirement| dep = name.split("/") Dependency.new( name: dep[0], requirement: dep[1], type: "runtime", source: .fetch(:filename, nil), platform: platform_name ) end ParserResult.new(dependencies: dependencies) end |