Skip to content

Instantly share code, notes, and snippets.

@kmtr
Created June 22, 2017 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmtr/6e917786c254596cc6fa361f47061c86 to your computer and use it in GitHub Desktop.
Save kmtr/6e917786c254596cc6fa361f47061c86 to your computer and use it in GitHub Desktop.
cargo metadataでプロジェクトの情報を取得する ref: http://qiita.com/kmtr/items/21fe9fd6c25f55169e61
$ cargo init --bin ferris
$ cd ferris
$ ls
Cargo.lock Cargo.toml src
$ cargo metadata --format-version=1
{"packages":[{"name":"ferris","version":"0.1.0","id":"ferris 0.1.0 (path+file:///path/to/ferris)","license":null,"license_file":null,"description":null,"source":null,"dependencies":[],"targets":[{"kind":["bin"],"crate_types":["bin"],"name":"ferris","src_path":"/path/to/ferris/src/main.rs"}],"features":{},"manifest_path":"/path/to/ferris/Cargo.toml"}],"workspace_members":["ferris 0.1.0 (path+file:///path/to/ferris)"],"resolve":{"nodes":[{"id":"ferris 0.1.0 (path+file:///path/to/ferris)","dependencies":[]}],"root":"ferris 0.1.0 (path+file:///path/to/ferris)"},"target_directory":"/path/to/ferris/target","version":1}
$ cargo metadata --format-version=1 | jq .packages[0].name
"ferris"
$ cargo metadata --format-version=1 | jq '.packages | map(select( .name == "ferris" )) | .[0].dependencies'
[]
$ echo 'regex = "0.2.2"' >> Cargo.toml
$ cargo metadata --format-version=1 | jq '.packages | map(select( .name == "ferris" )) | .[0].dependencies'
Updating registry `https://github.com/rust-lang/crates.io-index`
[
{
"name": "regex",
"source": "registry+https://github.com/rust-lang/crates.io-index",
"req": "^0.2.2",
"kind": null,
"optional": false,
"uses_default_features": true,
"features": [],
"target": null
}
]
$ cargo metadata --format-version=1 --no-deps | jq .packages[0].dependencies
[
{
"name": "regex",
"source": "registry+https://github.com/rust-lang/crates.io-index",
"req": "^0.2.2",
"kind": null,
"optional": false,
"uses_default_features": true,
"features": [],
"target": null
}
]
.PHONY: version
version:
@echo $(shell cargo metadata --format-version=1 --no-deps \
| jq .packages[0].version)
$ make version
0.1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment