From 8410af86602475a8895893b04a295a48a023a924 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Tue, 27 Feb 2024 15:26:56 +0100 Subject: [PATCH] refactor: self review --- proto-compiler/src/functions.rs | 34 +++++++++++++++++++++++++++++++++ proto-compiler/src/lib.rs | 30 +++++++---------------------- 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/proto-compiler/src/functions.rs b/proto-compiler/src/functions.rs index fe183e7..1a72413 100644 --- a/proto-compiler/src/functions.rs +++ b/proto-compiler/src/functions.rs @@ -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) { + Ok(content) => { + println!( + "[info] => Found previously downloaded Tenderdash {}.", + content.trim() + ); + content.eq(commitish) + }, + Err(_) => false, + } +} diff --git a/proto-compiler/src/lib.rs b/proto-compiler/src/lib.rs index 6cdb5db..27f5e58 100644 --- a/proto-compiler/src/lib.rs +++ b/proto-compiler/src/lib.rs @@ -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 @@ -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 @@ -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!"); }