Class: RbSys::Cargo::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/rb_sys/cargo/metadata.rb

Overview

Extracts metadata from a Cargo project using ‘cargo metadata`.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, deps: false) ⇒ Metadata

Initializes a new Cargo::Metadata instance.

Parameters:

  • name (String)

    the name of the Cargo project

Raises:

  • (ArgumentError)


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

#nameObject (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.

Parameters:

  • name (String)

    the name of the Cargo project



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

#editionString

Returns the package’s Rust edition.

Returns:

  • (String)


96
97
98
# File 'lib/rb_sys/cargo/metadata.rb', line 96

def edition
  .fetch("edition")
end

#featuresArray<String>

Returns the package’s features.

Returns:

  • (Array<String>)


103
104
105
# File 'lib/rb_sys/cargo/metadata.rb', line 103

def features
  .fetch("features")
end

#idString

Returns the package’s id.

Returns:

  • (String)


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_directoryString

Returns the path where the Cargo project’s Cargo.toml is located.

Returns:

  • (String)


47
48
49
# File 'lib/rb_sys/cargo/metadata.rb', line 47

def manifest_directory
  @manifest_directory ||= File.dirname(manifest_path)
end

#manifest_pathString

Returns the path to the package’s Cargo.toml.

Returns:

  • (String)


75
76
77
# File 'lib/rb_sys/cargo/metadata.rb', line 75

def manifest_path
  .fetch("manifest_path")
end

#metadataHash

Returns the package’s custom metadata.

Returns:

  • (Hash)


110
111
112
# File 'lib/rb_sys/cargo/metadata.rb', line 110

def 
  .fetch("metadata")
end

#packagesArray<Hash>

Returns the workspace members for the Cargo project.

Returns:

  • (Array<Hash>)


68
69
70
# File 'lib/rb_sys/cargo/metadata.rb', line 68

def packages
  .fetch("packages")
end

#rb_sys_versionString

Returns the rb-sys version, if any.

Returns:

  • (String)


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_directoryString

Returns the target directory for the Cargo project.

Returns:

  • (String)


54
55
56
# File 'lib/rb_sys/cargo/metadata.rb', line 54

def target_directory
  .fetch("target_directory")
end

#versionString

Returns the package’s version.

Returns:

  • (String)


82
83
84
# File 'lib/rb_sys/cargo/metadata.rb', line 82

def version
  .fetch("version")
end

#workspace_rootString

Returns the workspace root for the Cargo project.

Returns:

  • (String)


61
62
63
# File 'lib/rb_sys/cargo/metadata.rb', line 61

def workspace_root
  .fetch("workspace_root")
end