Class: Licensed::Sources::Cargo

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

Instance Attribute Summary

Attributes inherited from Source

#config

Instance Method Summary collapse

Methods inherited from Source

#dependencies, full_type, #ignored?, inherited, #initialize, type, type_and_version

Constructor Details

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

Instance Method Details

#cargo_metadataObject

Returns parsed JSON metadata returned from the cargo CLI



56
57
58
59
60
61
# File 'lib/licensed/sources/cargo.rb', line 56

def 
  @cargo_metadata ||= JSON.parse()
rescue JSON::ParserError => e
  message = "Licensed was unable to parse the output from 'cargo metadata'. JSON Error: #{e.message}"
  raise Licensed::Sources::Source::Error, message
end

#cargo_metadata_commandObject

Runs a command to get cargo metadata for the current package



64
65
66
67
# File 'lib/licensed/sources/cargo.rb', line 64

def 
  options = Array(config.dig("cargo", "metadata_options")).flat_map(&:split)
  Licensed::Shell.execute("cargo", "metadata", "--format-version=1", *options)
end

#cargo_metadata_packagesObject

Returns a hash of id => package pairs sourced from the “packages” cargo metadata property



44
45
46
47
48
# File 'lib/licensed/sources/cargo.rb', line 44

def 
  @cargo_metadata_packages ||= ["packages"].each_with_object({}) do |package, hsh|
    hsh[package["id"]] = package
  end
end

#cargo_metadata_resolved_node_idsObject

Returns the ids of all resolved nodes used to build the current package



36
37
38
39
40
41
# File 'lib/licensed/sources/cargo.rb', line 36

def 
  .dig("resolve", "nodes")
                .map { |node| node["id"] }
                .reject { |id| .include?(id) }

end

#cargo_metadata_workspace_membersObject

Returns a set of the ids of packages in the current workspace



51
52
53
# File 'lib/licensed/sources/cargo.rb', line 51

def 
  @cargo_metadata_workspace_members ||= Set.new(Array(["workspace_members"]))
end

#enabled?Boolean

Source is enabled when the cargo tool and Cargo.toml manifest file are available

Returns:

  • (Boolean)


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

def enabled?
  return false unless Licensed::Shell.tool_available?("cargo")
  config.pwd.join("Cargo.toml").exist?
end

#enumerate_dependenciesObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/licensed/sources/cargo.rb', line 14

def enumerate_dependencies
  packages.map do |package|
    Dependency.new(
      name: "#{package["name"]}-#{package["version"]}",
      version: package["version"],
      path: File.dirname(package["manifest_path"]),
      metadata: {
        "name" => package["name"],
        "type" => Cargo.type,
        "summary" => package["description"],
        "homepage" => package["homepage"]
      }
    )
  end
end

#packagesObject

Returns the package data for all dependencies used to build the current package



31
32
33
# File 'lib/licensed/sources/cargo.rb', line 31

def packages
  .map { |id| [id] }
end