From c069c2db00f066f071f054732336de7f554d661f Mon Sep 17 00:00:00 2001 From: Cameron Martin Date: Mon, 24 Jun 2024 21:34:41 +0100 Subject: [PATCH] Set version from Cargo.toml (#40) This uses a genrule to read the Cargo.toml and set `CARGO_PKG_VERSION`. This is picked up by clap to set the version when asked via `--version`. Fixes #39 --- BUILD | 12 ++++++++++++ MODULE.bazel | 4 ++++ MODULE.bazel.lock | 27 ++++++++++++++++++++++----- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/BUILD b/BUILD index efbd76a..56ab6b3 100644 --- a/BUILD +++ b/BUILD @@ -7,6 +7,7 @@ rust_binary( "//src/builtin:builtin.pb", "//src/builtin:default_build_language.pb", ], + rustc_env_files = [":generate_rustc_env_file"], deps = [ "//src/builtin:build_proto_rust", "//src/builtin:builtin_proto_rust", @@ -23,6 +24,17 @@ rust_binary( ], ) +genrule( + name = "generate_rustc_env_file", + srcs = [ + "Cargo.toml", + "src/main.rs", + ], + outs = ["rustc_env_file"], + cmd = "echo \"CARGO_PKG_VERSION=$$($(location @rust_host_tools//:cargo) read-manifest | jq -r .version)\" > $@", + tools = ["@rust_host_tools//:cargo"], +) + rust_test( name = "unit_tests", crate = ":bazel-lsp", diff --git a/MODULE.bazel b/MODULE.bazel index 0653b00..37cdf0a 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -8,6 +8,10 @@ rust.toolchain( use_repo(rust, "rust_toolchains") register_toolchains("@rust_toolchains//:all") +rust_host_tools = use_extension("@rules_rust//rust:extensions.bzl", "rust_host_tools") + +use_repo(rust_host_tools, "rust_host_tools") + crate = use_extension( "@rules_rust//crate_universe:extension.bzl", "crate", diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 1e434eb..7b4ecb8 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -1,6 +1,6 @@ { "lockFileVersion": 6, - "moduleFileHash": "98dabca0e21165df3a944a9abe144e134687550f974feadb4b141d9e0cef7b52", + "moduleFileHash": "20bc37500b4e20315450ab03aed9ee88c643cf6588c549abee29937a5bdd927d", "flags": { "cmdRegistries": [ "https://bcr.bazel.build/" @@ -60,13 +60,30 @@ "hasDevUseExtension": false, "hasNonDevUseExtension": true }, + { + "extensionBzlFile": "@rules_rust//rust:extensions.bzl", + "extensionName": "rust_host_tools", + "usingModule": "", + "location": { + "file": "@@//:MODULE.bazel", + "line": 11, + "column": 32 + }, + "imports": { + "rust_host_tools": "rust_host_tools" + }, + "devImports": [], + "tags": [], + "hasDevUseExtension": false, + "hasNonDevUseExtension": true + }, { "extensionBzlFile": "@rules_rust//crate_universe:extension.bzl", "extensionName": "crate", "usingModule": "", "location": { "file": "@@//:MODULE.bazel", - "line": 11, + "line": 15, "column": 22 }, "imports": { @@ -86,7 +103,7 @@ "devDependency": false, "location": { "file": "@@//:MODULE.bazel", - "line": 15, + "line": 19, "column": 17 } }, @@ -101,7 +118,7 @@ "devDependency": false, "location": { "file": "@@//:MODULE.bazel", - "line": 22, + "line": 26, "column": 17 } }, @@ -116,7 +133,7 @@ "devDependency": false, "location": { "file": "@@//:MODULE.bazel", - "line": 27, + "line": 31, "column": 17 } }