spm_swift_package/
structure.rs

1use std::fs;
2use std::io::Write;
3
4use crate::content::*;
5
6pub struct Structure;
7
8impl Structure {
9    pub fn create_project(project_name: &str) {
10        let path = format!("{}/Sources/{}", project_name, project_name);
11        fs::create_dir_all(&path).expect("Error creating project");
12
13        let content = Content::project_swift_content();
14
15        let mut file = fs::File::create(format!(
16            "{}/Sources/{}/{}.swift",
17            project_name, project_name, project_name
18        )).expect("Error writing to file");
19
20        file.write_all(content.as_bytes()).expect("Error creating file");
21    }
22
23    pub fn create_test_folder(project_name: &str) {
24        let path = format!("{}/Tests/{}Tests", project_name, project_name);
25        fs::create_dir_all(&path).expect("Error creating test folder");
26
27        let content = Content::test_content(project_name);
28
29        let mut file = fs::File::create(format!(
30            "{}/Tests/{}Tests/{}Tests.swift",
31            project_name, project_name, project_name
32        )).expect("Error creating file");
33
34        file.write_all(content.as_bytes()).expect("Error writing to file");
35    }
36
37    pub fn create_package_swift(project_name: &str, platform: &str, version: &str) {
38        let content = Content::package_swift_content(project_name, platform, version);
39        Self::base_root_project(project_name, "Package.swift", content);
40    }
41
42    pub fn create_changelog(project_name: &str) {
43        let content = Content::changelog_content();
44        Self::base_root_project(project_name, "CHANGELOG.md", content);
45    }
46
47    pub fn create_readme(project_name: &str) {
48        let content = Content::readme_content(project_name);
49        Self::base_root_project(project_name, "README.md", content);
50    }
51
52    pub fn create_spi(project_name: &str) {
53        let content = Content::spi_content(project_name);
54        Self::base_root_project(project_name, ".spi.yml", content);
55    }
56
57    pub fn create_swiftlint(project_name: &str) {
58        let content = Content::swiftlint_content();
59        Self::base_root_project(project_name, ".swiftlint.yml", content);
60    }
61
62    pub fn create_mise(project_name: &str, tag: &str) {
63        let content = Content::mise_content(tag);
64        Self::base_root_project(project_name, "mise.toml", content);
65    }
66
67    fn base_root_project(project_name: &str, name_file: &str, content: String) {
68        let path = project_name.to_string();
69        fs::create_dir_all(&path).expect("Error creating test folder");
70
71        let mut file =
72            fs::File::create(format!("{}/{}", project_name, name_file))
73                .expect("Error creating file");
74
75        file.write_all(content.as_bytes()).expect("Error writing to file");
76    }
77}