From f22d62393b239f79565f4654a1fb0df1b4656ee9 Mon Sep 17 00:00:00 2001 From: Andrew Hlynskyi Date: Wed, 23 Jun 2021 01:02:07 +0300 Subject: [PATCH] fix(cli): actual Rust binding version in generated Cargo.toml --- Cargo.lock | 10 ++++++++++ cli/Cargo.toml | 3 +++ cli/build.rs | 17 +++++++++++++++++ cli/src/generate/binding_files.rs | 5 ++++- cli/src/generate/templates/cargo.toml | 2 +- 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 000e4d56..48ccbce5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -658,6 +658,15 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" +[[package]] +name = "toml" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +dependencies = [ + "serde", +] + [[package]] name = "tree-sitter" version = "0.19.5" @@ -691,6 +700,7 @@ dependencies = [ "smallbitvec", "tempfile", "tiny_http", + "toml", "tree-sitter", "tree-sitter-config", "tree-sitter-highlight", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index e331e349..85e9b4d1 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -74,3 +74,6 @@ features = ["std"] [dev-dependencies] rand = "0.8" tempfile = "3" + +[build-dependencies] +toml = "0.5" diff --git a/cli/build.rs b/cli/build.rs index 322d239b..37ea3b32 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -9,6 +9,12 @@ fn main() { if wasm_files_present() { println!("cargo:rustc-cfg={}", "TREE_SITTER_EMBED_WASM_BINDING"); } + + let rust_binding_version = read_rust_binding_version(); + println!( + "cargo:rustc-env={}={}", + "RUST_BINDING_VERSION", rust_binding_version + ); } fn wasm_files_present() -> bool { @@ -67,3 +73,14 @@ fn read_git_sha() -> Option { None } + +fn read_rust_binding_version() -> String { + let path = "../lib/Cargo.toml"; + let text = fs::read_to_string(path).unwrap(); + let cargo_toml = toml::from_str::(text.as_ref()).unwrap(); + cargo_toml["package"]["version"] + .as_str() + .unwrap() + .trim_matches('"') + .to_string() +} diff --git a/cli/src/generate/binding_files.rs b/cli/src/generate/binding_files.rs index 3ea9b356..1f3ac881 100644 --- a/cli/src/generate/binding_files.rs +++ b/cli/src/generate/binding_files.rs @@ -13,6 +13,8 @@ const PACKAGE_JSON_TEMPLATE: &'static str = include_str!("./templates/package.js const PARSER_NAME_PLACEHOLDER: &'static str = "PARSER_NAME"; const CLI_VERSION_PLACEHOLDER: &'static str = "CLI_VERSION"; const CLI_VERSION: &'static str = env!("CARGO_PKG_VERSION"); +const RUST_BINDING_VERSION: &'static str = env!("RUST_BINDING_VERSION"); +const RUST_BINDING_VERSION_PLACEHOLDER: &'static str = "RUST_BINDING_VERSION"; pub fn generate_binding_files(repo_path: &Path, language_name: &str) -> Result<()> { let bindings_dir = repo_path.join("bindings"); @@ -121,7 +123,8 @@ fn generate_file(path: &Path, template: &str, language_name: &str) -> Result<()> path, template .replace(PARSER_NAME_PLACEHOLDER, language_name) - .replace(CLI_VERSION_PLACEHOLDER, CLI_VERSION), + .replace(CLI_VERSION_PLACEHOLDER, CLI_VERSION) + .replace(RUST_BINDING_VERSION_PLACEHOLDER, RUST_BINDING_VERSION), ) } diff --git a/cli/src/generate/templates/cargo.toml b/cli/src/generate/templates/cargo.toml index aaa6118a..ccb72def 100644 --- a/cli/src/generate/templates/cargo.toml +++ b/cli/src/generate/templates/cargo.toml @@ -20,7 +20,7 @@ include = [ path = "bindings/rust/lib.rs" [dependencies] -tree-sitter = "0.19.3" +tree-sitter = "~RUST_BINDING_VERSION" [build-dependencies] cc = "1.0"