spm_swift_package/
structure.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::fs;
use std::io::Write;

use crate::content::*;

pub struct Structure;

impl Structure {
    pub fn create_project(project_name: &str) {
        let path = format!("{}/Sources/{}", project_name, project_name);
        fs::create_dir_all(&path).expect("Error creating project");

        let content = Content::project_swift_content();

        let mut file = fs::File::create(format!(
            "{}/Sources/{}/{}.swift",
            project_name, project_name, project_name
        )).expect("Error writing to file");

        file.write_all(content.as_bytes()).expect("Error creating file");
    }

    pub fn create_test_folder(project_name: &str) {
        let path = format!("{}/Tests/{}Tests", project_name, project_name);
        fs::create_dir_all(&path).expect("Error creating test folder");

        let content = Content::test_content(project_name);

        let mut file = fs::File::create(format!(
            "{}/Tests/{}Tests/{}Tests.swift",
            project_name, project_name, project_name
        )).expect("Error creating file");

        file.write_all(content.as_bytes()).expect("Error writing to file");
    }

    pub fn create_package_swift(project_name: &str) {
        let content = Content::package_swift_content(project_name);
        Self::base_root_project(project_name, "Package.swift", content);
    }

    pub fn create_changelog(project_name: &str) {
        let content = Content::changelog_content();
        Self::base_root_project(project_name, "CHANGELOG.md", content);
    }

    pub fn create_readme(project_name: &str) {
        let content = Content::readme_content(project_name);
        Self::base_root_project(project_name, "README.md", content);
    }

    pub fn create_spi(project_name: &str) {
        let content = Content::spi_content(project_name);
        Self::base_root_project(project_name, ".spi.yml", content);
    }

    fn base_root_project(project_name: &str, name_file: &str, content: String) {
        let path = project_name.to_string();
        fs::create_dir_all(&path).expect("Error creating test folder");

        let mut file =
            fs::File::create(format!("{}/{}", project_name, name_file))
                .expect("Error creating file");

        file.write_all(content.as_bytes()).expect("Error writing to file");
    }
}