Skip to content

Commit 88ee786

Browse files
authored
refactor: adjustment project file code (#168)
1 parent 3696585 commit 88ee786

File tree

1 file changed

+95
-72
lines changed

1 file changed

+95
-72
lines changed

src/domain/file/project_file.rs

Lines changed: 95 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,101 @@
1-
use std::fs;
2-
use std::io::Write;
1+
use std::{
2+
fs,
3+
io::Write,
4+
path::{Path, PathBuf},
5+
};
36

47
use crate::domain::file::project_templates::*;
58

9+
type Result<T> = std::result::Result<T, String>;
10+
11+
fn display<P: AsRef<Path>>(p: P) -> String {
12+
p.as_ref().display().to_string()
13+
}
14+
615
pub struct ProjectFile;
716

817
impl ProjectFile {
9-
pub fn create_project(project_name: &str) -> Result<(), String> {
10-
let path = format!("{}/Sources/{}", project_name, project_name);
11-
Self::create_dir(&path)?;
12-
13-
let content = ProjectTemplates::project_swift_content();
14-
let file_path = format!(
15-
"{}/Sources/{}/{}.swift",
16-
project_name, project_name, project_name
17-
);
18-
Self::write_file(&file_path, &content)?;
19-
Ok(())
20-
}
21-
22-
pub fn create_test_folder(project_name: &str) -> Result<(), String> {
23-
let path = format!("{}/Tests/{}Tests", project_name, project_name);
24-
Self::create_dir(&path)?;
25-
26-
let content = ProjectTemplates::test_content(project_name);
27-
let file_path = format!(
28-
"{}/Tests/{}Tests/{}Tests.swift",
29-
project_name, project_name, project_name
30-
);
31-
Self::write_file(&file_path, &content)?;
32-
Ok(())
33-
}
34-
35-
pub fn create_package(project_name: &str, platform: &str, version: &str, is_plugin: bool) -> Result<(), String> {
36-
let content = ProjectTemplates::package_swift_content(project_name, platform, version, is_plugin);
37-
Self::base_root_project(project_name, "Package.swift", content)
38-
}
39-
40-
pub fn create_changelog(project_name: &str) -> Result<(), String> {
41-
let content = ProjectTemplates::changelog_content();
42-
Self::base_root_project(project_name, "CHANGELOG.md", content)
43-
}
44-
45-
pub fn create_readme(project_name: &str) -> Result<(), String> {
46-
let content = ProjectTemplates::readme_content(project_name);
47-
Self::base_root_project(project_name, "README.md", content)
48-
}
49-
50-
pub fn create_spi(project_name: &str) -> Result<(), String> {
51-
let content = ProjectTemplates::spi_content(project_name);
52-
Self::base_root_project(project_name, ".spi.yml", content)
53-
}
54-
55-
pub fn create_swiftlint(project_name: &str) -> Result<(), String> {
56-
let content = ProjectTemplates::swiftlint_content();
57-
Self::base_root_project(project_name, ".swiftlint.yml", content)
58-
}
59-
60-
fn base_root_project(project_name: &str, name_file: &str, content: String) -> Result<(), String> {
61-
let path = project_name.to_string();
62-
Self::create_dir(&path)?;
63-
let file_path = format!("{}/{}", project_name, name_file);
64-
Self::write_file(&file_path, &content)
65-
}
66-
67-
fn create_dir(path: &str) -> Result<(), String> {
68-
fs::create_dir_all(path).map_err(|e| format!("Error creating directory '{}': {}", path, e))
69-
}
70-
71-
fn write_file(path: &str, content: &str) -> Result<(), String> {
72-
let mut file =
73-
fs::File::create(path).map_err(|e| format!("Error creating file '{}': {}", path, e))?;
74-
file
75-
.write_all(content.as_bytes())
76-
.map_err(|e| format!("Error writing to file '{}': {}", path, e))
77-
}
78-
}
18+
pub fn create_project(project_name: &str) -> Result<()> {
19+
let module_dir = Self::module_dir(project_name);
20+
Self::create_dir(&module_dir)?;
21+
22+
let content = ProjectTemplates::project_swift_content();
23+
let file_path = module_dir.join(format!("{name}.swift", name = project_name));
24+
Self::write_file(&file_path, &content)
25+
}
26+
27+
pub fn create_test_folder(project_name: &str) -> Result<()> {
28+
let tests_dir = Self::tests_dir(project_name);
29+
Self::create_dir(&tests_dir)?;
30+
31+
let content = ProjectTemplates::test_content(project_name);
32+
let file_path = tests_dir.join(format!("{name}Tests.swift", name = project_name));
33+
Self::write_file(&file_path, &content)
34+
}
35+
36+
pub fn create_package(
37+
project_name: &str,
38+
platform: &str,
39+
version: &str,
40+
is_plugin: bool,
41+
) -> Result<()> {
42+
let content = ProjectTemplates::package_swift_content(
43+
project_name,
44+
platform,
45+
version,
46+
is_plugin,
47+
);
48+
Self::create_root_file(project_name, "Package.swift", content)
49+
}
50+
51+
pub fn create_changelog(project_name: &str) -> Result<()> {
52+
let content = ProjectTemplates::changelog_content();
53+
Self::create_root_file(project_name, "CHANGELOG.md", content)
54+
}
55+
56+
pub fn create_readme(project_name: &str) -> Result<()> {
57+
let content = ProjectTemplates::readme_content(project_name);
58+
Self::create_root_file(project_name, "README.md", content)
59+
}
60+
61+
pub fn create_spi(project_name: &str) -> Result<()> {
62+
let content = ProjectTemplates::spi_content(project_name);
63+
Self::create_root_file(project_name, ".spi.yml", content)
64+
}
65+
66+
pub fn create_swiftlint(project_name: &str) -> Result<()> {
67+
let content = ProjectTemplates::swiftlint_content();
68+
Self::create_root_file(project_name, ".swiftlint.yml", content)
69+
}
70+
71+
// Private
72+
73+
fn module_dir(project_name: &str) -> PathBuf {
74+
Path::new(project_name).join("Sources").join(project_name)
75+
}
76+
77+
fn tests_dir(project_name: &str) -> PathBuf {
78+
Path::new(project_name)
79+
.join("Tests")
80+
.join(format!("{name}Tests", name = project_name))
81+
}
82+
83+
fn create_root_file(project_name: &str, filename: &str, content: String) -> Result<()> {
84+
let root = Path::new(project_name);
85+
Self::create_dir(root)?;
86+
let file_path = root.join(filename);
87+
Self::write_file(&file_path, &content)
88+
}
89+
90+
fn create_dir<P: AsRef<Path>>(path: P) -> Result<()> {
91+
fs::create_dir_all(&path)
92+
.map_err(|e| format!("Error creating directory '{}': {}", display(path), e))
93+
}
94+
95+
fn write_file<P: AsRef<Path>>(path: P, content: &str) -> Result<()> {
96+
let mut file = fs::File::create(&path)
97+
.map_err(|e| format!("Error creating file '{}': {}", display(&path), e))?;
98+
file.write_all(content.as_bytes())
99+
.map_err(|e| format!("Error writing to file '{}': {}", display(&path), e))
100+
}
101+
}

0 commit comments

Comments
 (0)