From 2e027a1c6a7d3c31c0db2051fc0527e9cd9c91e3 Mon Sep 17 00:00:00 2001 From: joaolfp Date: Fri, 31 Oct 2025 14:33:40 -0300 Subject: [PATCH] refactor: adjustment project file code --- src/domain/file/project_file.rs | 167 ++++++++++++++++++-------------- 1 file changed, 95 insertions(+), 72 deletions(-) diff --git a/src/domain/file/project_file.rs b/src/domain/file/project_file.rs index db22c36..a49e8fe 100644 --- a/src/domain/file/project_file.rs +++ b/src/domain/file/project_file.rs @@ -1,78 +1,101 @@ -use std::fs; -use std::io::Write; +use std::{ + fs, + io::Write, + path::{Path, PathBuf}, +}; use crate::domain::file::project_templates::*; +type Result = std::result::Result; + +fn display>(p: P) -> String { + p.as_ref().display().to_string() +} + pub struct ProjectFile; impl ProjectFile { - pub fn create_project(project_name: &str) -> Result<(), String> { - let path = format!("{}/Sources/{}", project_name, project_name); - Self::create_dir(&path)?; - - let content = ProjectTemplates::project_swift_content(); - let file_path = format!( - "{}/Sources/{}/{}.swift", - project_name, project_name, project_name - ); - Self::write_file(&file_path, &content)?; - Ok(()) - } - - pub fn create_test_folder(project_name: &str) -> Result<(), String> { - let path = format!("{}/Tests/{}Tests", project_name, project_name); - Self::create_dir(&path)?; - - let content = ProjectTemplates::test_content(project_name); - let file_path = format!( - "{}/Tests/{}Tests/{}Tests.swift", - project_name, project_name, project_name - ); - Self::write_file(&file_path, &content)?; - Ok(()) - } - - pub fn create_package(project_name: &str, platform: &str, version: &str, is_plugin: bool) -> Result<(), String> { - let content = ProjectTemplates::package_swift_content(project_name, platform, version, is_plugin); - Self::base_root_project(project_name, "Package.swift", content) - } - - pub fn create_changelog(project_name: &str) -> Result<(), String> { - let content = ProjectTemplates::changelog_content(); - Self::base_root_project(project_name, "CHANGELOG.md", content) - } - - pub fn create_readme(project_name: &str) -> Result<(), String> { - let content = ProjectTemplates::readme_content(project_name); - Self::base_root_project(project_name, "README.md", content) - } - - pub fn create_spi(project_name: &str) -> Result<(), String> { - let content = ProjectTemplates::spi_content(project_name); - Self::base_root_project(project_name, ".spi.yml", content) - } - - pub fn create_swiftlint(project_name: &str) -> Result<(), String> { - let content = ProjectTemplates::swiftlint_content(); - Self::base_root_project(project_name, ".swiftlint.yml", content) - } - - fn base_root_project(project_name: &str, name_file: &str, content: String) -> Result<(), String> { - let path = project_name.to_string(); - Self::create_dir(&path)?; - let file_path = format!("{}/{}", project_name, name_file); - Self::write_file(&file_path, &content) - } - - fn create_dir(path: &str) -> Result<(), String> { - fs::create_dir_all(path).map_err(|e| format!("Error creating directory '{}': {}", path, e)) - } - - fn write_file(path: &str, content: &str) -> Result<(), String> { - let mut file = - fs::File::create(path).map_err(|e| format!("Error creating file '{}': {}", path, e))?; - file - .write_all(content.as_bytes()) - .map_err(|e| format!("Error writing to file '{}': {}", path, e)) - } -} + pub fn create_project(project_name: &str) -> Result<()> { + let module_dir = Self::module_dir(project_name); + Self::create_dir(&module_dir)?; + + let content = ProjectTemplates::project_swift_content(); + let file_path = module_dir.join(format!("{name}.swift", name = project_name)); + Self::write_file(&file_path, &content) + } + + pub fn create_test_folder(project_name: &str) -> Result<()> { + let tests_dir = Self::tests_dir(project_name); + Self::create_dir(&tests_dir)?; + + let content = ProjectTemplates::test_content(project_name); + let file_path = tests_dir.join(format!("{name}Tests.swift", name = project_name)); + Self::write_file(&file_path, &content) + } + + pub fn create_package( + project_name: &str, + platform: &str, + version: &str, + is_plugin: bool, + ) -> Result<()> { + let content = ProjectTemplates::package_swift_content( + project_name, + platform, + version, + is_plugin, + ); + Self::create_root_file(project_name, "Package.swift", content) + } + + pub fn create_changelog(project_name: &str) -> Result<()> { + let content = ProjectTemplates::changelog_content(); + Self::create_root_file(project_name, "CHANGELOG.md", content) + } + + pub fn create_readme(project_name: &str) -> Result<()> { + let content = ProjectTemplates::readme_content(project_name); + Self::create_root_file(project_name, "README.md", content) + } + + pub fn create_spi(project_name: &str) -> Result<()> { + let content = ProjectTemplates::spi_content(project_name); + Self::create_root_file(project_name, ".spi.yml", content) + } + + pub fn create_swiftlint(project_name: &str) -> Result<()> { + let content = ProjectTemplates::swiftlint_content(); + Self::create_root_file(project_name, ".swiftlint.yml", content) + } + + // Private + + fn module_dir(project_name: &str) -> PathBuf { + Path::new(project_name).join("Sources").join(project_name) + } + + fn tests_dir(project_name: &str) -> PathBuf { + Path::new(project_name) + .join("Tests") + .join(format!("{name}Tests", name = project_name)) + } + + fn create_root_file(project_name: &str, filename: &str, content: String) -> Result<()> { + let root = Path::new(project_name); + Self::create_dir(root)?; + let file_path = root.join(filename); + Self::write_file(&file_path, &content) + } + + fn create_dir>(path: P) -> Result<()> { + fs::create_dir_all(&path) + .map_err(|e| format!("Error creating directory '{}': {}", display(path), e)) + } + + fn write_file>(path: P, content: &str) -> Result<()> { + let mut file = fs::File::create(&path) + .map_err(|e| format!("Error creating file '{}': {}", display(&path), e))?; + file.write_all(content.as_bytes()) + .map_err(|e| format!("Error writing to file '{}': {}", display(&path), e)) + } +} \ No newline at end of file