Skip to content

Commit

Permalink
refactor: self review
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Feb 27, 2024
1 parent 354ec4e commit 8410af8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
34 changes: 34 additions & 0 deletions proto-compiler/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,37 @@ pub(crate) fn tenderdash_commitish() -> String {
Err(_) => DEFAULT_TENDERDASH_COMMITISH.to_string(),
}
}

/// Save the commitish of last successful download to a file in a state file,
/// located in the `dir` directory and named `download.state`.
pub(crate) fn save_state(dir: &Path, commitish: &str) {
let state_file = PathBuf::from(&dir).join("download.state");

std::fs::write(&state_file, commitish)
.map_err(|e| {
println!(
"[warn] => Failed to write download.state file {}: {}",
state_file.display(),
e
);
})
.ok();
}

/// Check if the state file contains the same commitish as the one we are trying
/// to download. State file should be located in the `dir` and named
/// `download.state`
pub(crate) fn check_state(dir: &Path, commitish: &str) -> bool {
let state_file = PathBuf::from(&dir).join("download.state");

match read_to_string(&state_file) {

Check warning

Code scanning / clippy

the borrowed expression implements the required traits Warning

the borrowed expression implements the required traits

Check warning

Code scanning / clippy

the borrowed expression implements the required traits Warning

the borrowed expression implements the required traits
Ok(content) => {
println!(
"[info] => Found previously downloaded Tenderdash {}.",
content.trim()
);
content.eq(commitish)
},
Err(_) => false,
}
}
30 changes: 7 additions & 23 deletions proto-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use functions::{
mod constants;
use constants::{CUSTOM_FIELD_ATTRIBUTES, CUSTOM_TYPE_ATTRIBUTES, TENDERDASH_REPO};

use crate::functions::{check_state, save_state};

/// Import and compile protobuf definitions for Tenderdash.
///
/// Checkouts tenderdash repository to ../target/tenderdash and generates
Expand Down Expand Up @@ -49,26 +51,18 @@ pub fn proto_compile() {
let commitish = tenderdash_commitish();

// check if this commitish is already downloaded
let download_info_file = PathBuf::from(&prost_out_dir).join("download.state");
let mut download = true;
if let Ok(content) = std::fs::read_to_string(&download_info_file) {
if content.eq(&commitish) {
println!(
" [info] => Tenderdash {} already extracted, skipping download and build",
commitish,
);
download = false;
}
}
let download = !check_state(&prost_out_dir, &commitish);

if download {
println!("[info] => Fetching {TENDERDASH_REPO} at {commitish} into {tenderdash_dir:?}");
println!("[info] => Fetching {TENDERDASH_REPO} at {commitish} into {tenderdash_dir:?}.");
fetch_commitish(
&PathBuf::from(&tenderdash_dir),
&cargo_target_dir,
TENDERDASH_REPO,
&commitish,
); // This panics if it fails.
} else {
println!("[info] => Skipping download.");
}

// We need all files in proto/tendermint/abci, plus .../types/canonical.proto
Expand Down Expand Up @@ -117,19 +111,9 @@ pub fn proto_compile() {

println!("[info] => Removing old structs and copying new structs.");
copy_files(&out_dir, &prost_out_dir); // This panics if it fails.
println!("[info] => Removing old structs and copying new structs.");

generate_tenderdash_lib(&out_dir, &tenderdash_lib_target, &abci_ver, &tenderdash_ver);

std::fs::write(&download_info_file, commitish)
.map_err(|e| {
println!(
"[warn] => Failed to write download.state file {}: {}",
download_info_file.display(),
e
);
})
.ok();

save_state(&prost_out_dir, &commitish);
println!("[info] => Done!");
}

0 comments on commit 8410af8

Please sign in to comment.