Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ jobs:
run: |
cd server
./bt install --dev
./bt check
./bt test
1 change: 1 addition & 0 deletions builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
59 changes: 55 additions & 4 deletions builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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
Expand Down Expand Up @@ -228,6 +234,29 @@ fn remove_dir_all_if_exists<P: AsRef<Path> + std::fmt::Display>(
Ok(())
}

fn search_and_replace_file<
P: AsRef<Path> + std::fmt::Display,
S1: AsRef<str> + std::fmt::Display,
S2: AsRef<str>,
>(
path: P,
search_regex: S1,
replace_string: S2,
) -> Result<(), Box<dyn std::error::Error>> {
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
Expand Down Expand Up @@ -320,7 +349,7 @@ fn run_update() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

fn run_check() -> Result<(), Box<dyn std::error::Error>> {
fn run_test() -> Result<(), Box<dyn std::error::Error>> {
// 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
Expand Down Expand Up @@ -375,6 +404,27 @@ fn run_build() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

fn run_change_version(new_version: &String) -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
// Clean out all bundled files before the rebuild.
remove_dir_all_if_exists("../client/static/bundled")?;
Expand Down Expand Up @@ -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),
}
Expand Down
Loading