Skip to content

Commit 3404cb0

Browse files
authored
refactor: Date and commit hash in --version flag (#1311)
* refactor: Date and commit hash in --version flag The `--version` flag now emits the date and hash of the latest commit in addition to the package version as specified in the `Cargo.toml` file. * fix: Read environment data at compile time * fix: Adjust CLI unit tests
1 parent 5a61935 commit 3404cb0

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

compiler/plc_driver/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "plc_driver"
33
version = "0.1.0"
44
edition = "2021"
5+
build = "build.rs"
56

67
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
78

compiler/plc_driver/build.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use core::str;
2+
use std::process::Command;
3+
4+
fn main() {
5+
let commit_date = Command::new("git").args(["log", "-1", "--format=%cd"]).output();
6+
let commit_hash = Command::new("git").args(["rev-parse", "--short", "HEAD"]).output();
7+
let package_version = env!("CARGO_PKG_VERSION");
8+
9+
if let (Ok(date), Ok(hash)) = (commit_date, commit_hash) {
10+
let date_str = str::from_utf8(&date.stdout).expect("invalid stdout output?").trim();
11+
let hash_str = str::from_utf8(&hash.stdout).expect("invalid stdout output?").trim();
12+
13+
let build_info = format!("{package_version} ({date_str}, {hash_str})");
14+
println!("cargo:rustc-env=RUSTY_BUILD_INFO={build_info}");
15+
}
16+
}

compiler/plc_driver/src/cli.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ pub type ParameterError = clap::Error;
1313
#[clap(
1414
group = ArgGroup::new("format"),
1515
about = "IEC61131-3 Structured Text compiler powered by Rust & LLVM ",
16-
version,
1716
)]
18-
#[clap(propagate_version = true)]
1917
#[clap(subcommand_negates_reqs = true)]
2018
#[clap(subcommand_precedence_over_arg = true)]
2119
pub struct CompileParameters {
@@ -30,6 +28,9 @@ pub struct CompileParameters {
3028
)]
3129
pub output_ast: bool,
3230

31+
#[clap(long = "version", group = "format", global = true)]
32+
pub build_info: bool,
33+
3334
#[clap(
3435
long = "ir",
3536
group = "format",
@@ -79,7 +80,7 @@ pub struct CompileParameters {
7980
#[clap(
8081
name = "input-files",
8182
help = "Read input from <input-files>, may be a glob expression like 'src/**/*' or a sequence of files",
82-
required = true,
83+
// required = true,
8384
min_values = 1
8485
)]
8586
// having a vec allows bash to resolve *.st itself
@@ -493,14 +494,6 @@ mod cli_tests {
493494
($($x:expr),*) => (&["plc", $($x),*]);
494495
}
495496

496-
#[test]
497-
fn missing_parameters_results_in_error() {
498-
// no arguments
499-
expect_argument_error(vec_of_strings![], ErrorKind::MissingRequiredArgument);
500-
// no input file
501-
expect_argument_error(vec_of_strings!["--ir"], ErrorKind::MissingRequiredArgument);
502-
}
503-
504497
#[test]
505498
fn multiple_output_formats_results_in_error() {
506499
expect_argument_error(vec_of_strings!["input.st", "--ir", "--shared"], ErrorKind::ArgumentConflict);
@@ -742,8 +735,8 @@ mod cli_tests {
742735
#[test]
743736
fn cli_supports_version() {
744737
match CompileParameters::parse(vec_of_strings!("input.st", "--version")) {
745-
Ok(_) => panic!("expected version output, but found OK"),
746-
Err(e) => assert_eq!(e.kind(), ErrorKind::DisplayVersion),
738+
Ok(version) => assert!(version.build_info),
739+
_ => panic!("expected the build info flag to be true"),
747740
}
748741
}
749742

compiler/plc_driver/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ pub fn compile_with_options(compile_options: CompilationContext) -> Result<()> {
207207
return print_config_options(&project, &diagnostician, options);
208208
}
209209

210+
if compile_parameters.build_info {
211+
println!("{}", option_env!("RUSTY_BUILD_INFO").unwrap_or("version information unavailable"));
212+
std::process::exit(0);
213+
}
214+
210215
if let Some(SubCommands::Explain { error }) = &compile_parameters.commands {
211216
//Explain the given error
212217
println!("{}", diagnostician.explain(error));

0 commit comments

Comments
 (0)