From b7767ba050d11e447eb01d58dabe9a154f6ed3c7 Mon Sep 17 00:00:00 2001 From: georgegiosue Date: Tue, 16 Apr 2024 13:35:36 -0500 Subject: [PATCH] refactor: buildSrc module to improve error handling and reliability --- src/cmd/cicd.rs | 2 +- src/source/module.rs | 121 +++++++++++++++++++++++++++++++++---------- 2 files changed, 95 insertions(+), 28 deletions(-) diff --git a/src/cmd/cicd.rs b/src/cmd/cicd.rs index e795237..c49cc6e 100644 --- a/src/cmd/cicd.rs +++ b/src/cmd/cicd.rs @@ -25,7 +25,7 @@ pub fn run(project_path: &Path, build_runtime: &AndroidBuildRuntime) { if !exists_build_src_dir(&project_path) { create_build_src_module(&project_path); } else { - copy_kotlin_files(&project_path); + copy_kotlin_files(&project_path).expect("Error creating kotlin files for versioning"); print_out(UMessage::SUCCESS( "buildSrc module is present and prepared for versioning", )); diff --git a/src/source/module.rs b/src/source/module.rs index db69247..f9c9c40 100644 --- a/src/source/module.rs +++ b/src/source/module.rs @@ -1,8 +1,5 @@ pub mod build_src { - use std::{ - fs::{self}, - path::Path, - }; + use std::{fs, io::Error, path::Path}; use crate::{ cmd::rollback, @@ -22,29 +19,34 @@ pub mod build_src { let build_src_kotlin_path = build_src_path.join("src").join("main").join("kotlin"); - create_hierarchy(&build_src_kotlin_path); + create_hierarchy(&build_src_kotlin_path).expect("Could not create buildSrc directory"); let files_to_ignore = vec!["/build"]; git_ignore(&build_src_path, files_to_ignore); - copy_build_script(&build_src_path); + copy_build_script(&build_src_path).expect("Error copying build.gradle.kts"); - copy_kotlin_files(&project_path); + copy_kotlin_files(&project_path).expect("Error creating kotlin files for versioning"); print_out(UMessage::SUCCESS("buildSrc module created successfully")) } - fn copy_build_script(buid_src_path: &Path) { + fn copy_build_script(buid_src_path: &Path) -> Result<(), Error> { let script = include_bytes!("./../../assets/scripts/build.gradle.kts"); let script_destination_path = buid_src_path.join("build.gradle.kts"); - if let Err(error) = std::fs::write(script_destination_path, script) { - panic!("Error copying build.gradle.kts | {}", error) + match std::fs::write(script_destination_path, script) { + Ok(_) => Ok(()), + Err(error) => { + rollback::run(buid_src_path.parent().unwrap()); + + Err(error) + } } } - pub fn copy_kotlin_files(project_path: &Path) { + pub fn copy_kotlin_files(project_path: &Path) -> Result<(), Error> { let build_src_path = Path::new(&project_path).join(constants::BUILD_SRC_DIRNAME); let build_src_kotlin_path = build_src_path.join("src").join("main").join("kotlin"); @@ -54,7 +56,10 @@ pub mod build_src { if let Err(error) = fs::write(versioning_kt_destination_path, versioning_kt) { rollback::run(project_path); - panic!("Could not create Versioning.kt file | {error}") + + let error_message = format!("{}: {}", "Could not create Versioning.kt", error); + + return Err(Error::new(std::io::ErrorKind::Other, error_message)); } let utils_kt = include_bytes!("./../../assets/kotlin/VersioningUtils.kt"); @@ -62,24 +67,86 @@ pub mod build_src { if let Err(error) = fs::write(utils_kt_destination_path, utils_kt) { rollback::run(project_path); - panic!("Could not create VersioningUtils.kt file | {}", error); + + let error_message = + format!("{}: {}", "Could not create VersioningUtils.kt file", error); + + return Err(Error::new(std::io::ErrorKind::Other, error_message)); + } + + Ok(()) + } + + fn create_hierarchy(dir_path: &Path) -> Result<(), Error> { + match fs::create_dir_all(&dir_path) { + Ok(_) => Ok(()), + Err(error) => { + rollback::run( + dir_path + .parent() + .unwrap() + .parent() + .unwrap() + .parent() + .unwrap() + .parent() + .unwrap(), + ); + + Err(error) + } } } - fn create_hierarchy(dir_path: &Path) { - if let Err(error) = fs::create_dir_all(&dir_path) { - rollback::run( - dir_path - .parent() - .unwrap() - .parent() - .unwrap() - .parent() - .unwrap() - .parent() - .unwrap(), - ); - panic!("Could not create buildSrc directory | {error}") + #[cfg(test)] + mod test { + + use crate::{ + build::runtime::AndroidBuildRuntime, utils::replicate_android_project_to_temp, + }; + + use super::*; + + #[test] + fn test_create_hierarchy() { + let kotlin_project_path = replicate_android_project_to_temp(AndroidBuildRuntime::KTS); + + let build_src_path = kotlin_project_path.join("buildSrc"); + + let build_src_kotlin_path = build_src_path.join("src").join("main").join("kotlin"); + + create_hierarchy(&build_src_kotlin_path).unwrap(); + + assert_eq!(build_src_kotlin_path.exists(), true); + + let _ = std::fs::remove_dir_all(kotlin_project_path); + } + + #[test] + fn test_copy_kotlin_files() { + let kotlin_project_path = replicate_android_project_to_temp(AndroidBuildRuntime::KTS); + + let kotlin_files = vec!["Versioning.kt", "VersioningUtils.kt"]; + + let build_src_kotlin_path = kotlin_project_path + .join("buildSrc") + .join("src") + .join("main") + .join("kotlin"); + + create_hierarchy(&build_src_kotlin_path).unwrap(); + + copy_kotlin_files(&kotlin_project_path).unwrap(); + + let read_dir = fs::read_dir(build_src_kotlin_path).unwrap(); + + let kotlin_files_present: bool = read_dir + .map(|asd| asd.unwrap().file_name().to_string_lossy().to_string()) + .all(|file_name| kotlin_files.contains(&file_name.as_str())); + + assert_eq!(kotlin_files_present, true); + + let _ = std::fs::remove_dir_all(kotlin_project_path); } } }