diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 6193b327..113fc112 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -41,4 +41,4 @@ jobs: run: | cd server ./bt install --dev - ./bt check + ./bt test diff --git a/builder/Cargo.toml b/builder/Cargo.toml index 48bb795d..645043b9 100644 --- a/builder/Cargo.toml +++ b/builder/Cargo.toml @@ -26,3 +26,4 @@ edition = "2021" clap = { version = "4.5.19", features = ["derive"] } cmd_lib = "1.9.5" current_platform = "0.2.0" +regex = "1.11.1" diff --git a/builder/src/main.rs b/builder/src/main.rs index 6831c0b4..f8ca839a 100644 --- a/builder/src/main.rs +++ b/builder/src/main.rs @@ -35,6 +35,7 @@ use std::{ffi::OsStr, fs, path::Path, process::Command}; use clap::{Parser, Subcommand}; use cmd_lib::run_cmd; use current_platform::CURRENT_PLATFORM; +use regex::Regex; // ### Local // @@ -60,10 +61,15 @@ enum Commands { }, /// Update all dependencies. Update, - /// Run lints and compile. - Check, + /// Run lints and tests. + Test, /// Build everything. Build, + /// Change the version for the client, server, and extensions. + ChangeVersion { + /// The new version number, such as "0.1.1". + new_version: String, + }, /// Steps to run before `cargo dist build`. Prerelease, /// Steps to run after `cargo dist build`. This builds and publishes a @@ -228,6 +234,29 @@ fn remove_dir_all_if_exists + std::fmt::Display>( Ok(()) } +fn search_and_replace_file< + P: AsRef + std::fmt::Display, + S1: AsRef + std::fmt::Display, + S2: AsRef, +>( + path: P, + search_regex: S1, + replace_string: S2, +) -> Result<(), Box> { + let file_contents = fs::read_to_string(&path) + .map_err(|err| -> String { format!("Unable to open file {path} for reading: {err}") })?; + let re = Regex::new(search_regex.as_ref()) + .map_err(|err| -> String { format!("Error in search regex {search_regex}: {err}") })?; + let file_contents_replaced = re.replace(&file_contents, replace_string.as_ref()); + assert_ne!( + file_contents, file_contents_replaced, + "No replacements made." + ); + fs::write(&path, file_contents_replaced.as_bytes()) + .map_err(|err| -> String { format!("Error writing to {path}: {err}") })?; + Ok(()) +} + // ## Core routines // // These functions simplify common build-focused development tasks and support @@ -320,7 +349,7 @@ fn run_update() -> Result<(), Box> { Ok(()) } -fn run_check() -> Result<(), Box> { +fn run_test() -> Result<(), Box> { // On Windows, `cargo sort --check` fails since it default to LF, not CRLF, // line endings. Work around this by changing this setting only on Windows. // See the @@ -375,6 +404,27 @@ fn run_build() -> Result<(), Box> { Ok(()) } +fn run_change_version(new_version: &String) -> Result<(), Box> { + let replacement_string = format!("${{1}}{new_version}${{2}}"); + search_and_replace_file( + "Cargo.toml", + r#"(\r?\nversion = ")[\d.]+("\r?\n)"#, + &replacement_string, + )?; + let json_search_regex = r#"(\r?\n "version": ")[\d.]+(",\r?\n)"#; + search_and_replace_file( + "../client/package.json", + json_search_regex, + &replacement_string, + )?; + search_and_replace_file( + "../extensions/VSCode/package.json", + json_search_regex, + &replacement_string, + )?; + Ok(()) +} + fn run_prerelease() -> Result<(), Box> { // Clean out all bundled files before the rebuild. remove_dir_all_if_exists("../client/static/bundled")?; @@ -422,8 +472,9 @@ impl Cli { match &self.command { Commands::Install { dev } => run_install(*dev), Commands::Update => run_update(), - Commands::Check => run_check(), + Commands::Test => run_test(), Commands::Build => run_build(), + Commands::ChangeVersion { new_version } => run_change_version(new_version), Commands::Prerelease => run_prerelease(), Commands::Postrelease { target, .. } => run_postrelease(target), }