Class: RbSys::Cargo::Metadata
- Inherits:
-
Object
- Object
- RbSys::Cargo::Metadata
- Defined in:
- lib/rb_sys/cargo/metadata.rb
Overview
Extracts metadata from a Cargo project using ‘cargo metadata`.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.inferred(deps: false) ⇒ RbSys::Cargo::Metadata
Infers the Cargo project’s name from the Cargo.toml file.
-
.new_or_inferred(name, deps: false) ⇒ Object
Initializes a new Cargo::Metadata instance or infers the Cargo project’s name.
Instance Method Summary collapse
-
#edition ⇒ String
Returns the package’s Rust edition.
-
#features ⇒ Array<String>
Returns the package’s features.
-
#id ⇒ String
Returns the package’s id.
-
#initialize(name, deps: false) ⇒ Metadata
constructor
Initializes a new Cargo::Metadata instance.
-
#load! ⇒ RbSys::Cargo::Metadata
Eagerly run ‘cargo metadata`, raising a RbSys::CargoCargoMetadataError` if it fails.
-
#manifest_directory ⇒ String
Returns the path where the Cargo project’s Cargo.toml is located.
-
#manifest_path ⇒ String
Returns the path to the package’s Cargo.toml.
-
#metadata ⇒ Hash
Returns the package’s custom metadata.
-
#packages ⇒ Array<Hash>
Returns the workspace members for the Cargo project.
-
#rb_sys_version ⇒ String
Returns the rb-sys version, if any.
-
#target_directory ⇒ String
Returns the target directory for the Cargo project.
-
#version ⇒ String
Returns the package’s version.
-
#workspace_root ⇒ String
Returns the workspace root for the Cargo project.
Constructor Details
#initialize(name, deps: false) ⇒ Metadata
Initializes a new Cargo::Metadata instance.
35 36 37 38 39 40 41 42 |
# File 'lib/rb_sys/cargo/metadata.rb', line 35 def initialize(name, deps: false) raise ArgumentError, "name must be a String" unless name.is_a?(String) @name = name @cargo_metadata = nil @package_metadata = nil @deps = deps end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/rb_sys/cargo/metadata.rb', line 10 def name @name end |
Class Method Details
.inferred(deps: false) ⇒ RbSys::Cargo::Metadata
Infers the Cargo project’s name from the Cargo.toml file.
16 17 18 19 20 |
# File 'lib/rb_sys/cargo/metadata.rb', line 16 def inferred(deps: false) new(File.read("Cargo.toml").match(/^name = "(.*)"/)[1], deps: deps) rescue new(File.basename(Dir.pwd), deps: deps) end |
.new_or_inferred(name, deps: false) ⇒ Object
Initializes a new Cargo::Metadata instance or infers the Cargo project’s name.
25 26 27 28 29 |
# File 'lib/rb_sys/cargo/metadata.rb', line 25 def new_or_inferred(name, deps: false) new(name, deps: deps).load! rescue CargoMetadataError inferred end |
Instance Method Details
#edition ⇒ String
Returns the package’s Rust edition.
96 97 98 |
# File 'lib/rb_sys/cargo/metadata.rb', line 96 def edition .fetch("edition") end |
#features ⇒ Array<String>
Returns the package’s features.
103 104 105 |
# File 'lib/rb_sys/cargo/metadata.rb', line 103 def features .fetch("features") end |
#id ⇒ String
Returns the package’s id.
89 90 91 |
# File 'lib/rb_sys/cargo/metadata.rb', line 89 def id .fetch("id") end |
#load! ⇒ RbSys::Cargo::Metadata
Eagerly run ‘cargo metadata`, raising a RbSys::CargoCargoMetadataError` if it fails.
126 127 128 129 |
# File 'lib/rb_sys/cargo/metadata.rb', line 126 def load! self end |
#manifest_directory ⇒ String
Returns the path where the Cargo project’s Cargo.toml is located.
47 48 49 |
# File 'lib/rb_sys/cargo/metadata.rb', line 47 def manifest_directory @manifest_directory ||= File.dirname(manifest_path) end |
#manifest_path ⇒ String
Returns the path to the package’s Cargo.toml.
75 76 77 |
# File 'lib/rb_sys/cargo/metadata.rb', line 75 def manifest_path .fetch("manifest_path") end |
#metadata ⇒ Hash
Returns the package’s custom metadata.
110 111 112 |
# File 'lib/rb_sys/cargo/metadata.rb', line 110 def .fetch("metadata") end |
#packages ⇒ Array<Hash>
Returns the workspace members for the Cargo project.
68 69 70 |
# File 'lib/rb_sys/cargo/metadata.rb', line 68 def packages .fetch("packages") end |
#rb_sys_version ⇒ String
Returns the rb-sys version, if any.
117 118 119 120 121 |
# File 'lib/rb_sys/cargo/metadata.rb', line 117 def rb_sys_version pkg = packages.find { |p| p.fetch("name") == "rb-sys" } return unless pkg pkg["version"] end |
#target_directory ⇒ String
Returns the target directory for the Cargo project.
54 55 56 |
# File 'lib/rb_sys/cargo/metadata.rb', line 54 def target_directory .fetch("target_directory") end |
#version ⇒ String
Returns the package’s version.
82 83 84 |
# File 'lib/rb_sys/cargo/metadata.rb', line 82 def version .fetch("version") end |
#workspace_root ⇒ String
Returns the workspace root for the Cargo project.
61 62 63 |
# File 'lib/rb_sys/cargo/metadata.rb', line 61 def workspace_root .fetch("workspace_root") end |