-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.rs
154 lines (109 loc) · 4.99 KB
/
module.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
pub mod build_src {
use std::{fs, io::Error, path::Path};
use crate::{
cmd::rollback,
config::constants,
utils::{git_ignore, out::print_out, unicode_messages::UMessage},
};
pub fn exists_build_src_dir(path: &Path) -> bool {
Path::new(&path)
.join(constants::BUILD_SRC_DIRNAME)
.as_path()
.exists()
}
pub fn create_build_src_module(project_path: &Path) {
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");
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).expect("Error copying build.gradle.kts");
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) -> Result<(), Error> {
let script = include_bytes!("./../../assets/scripts/build.gradle.kts");
let script_destination_path = buid_src_path.join("build.gradle.kts");
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) -> 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");
let versioning_kt = include_bytes!("./../../assets/kotlin/Versioning.kt");
let versioning_kt_destination_path = build_src_kotlin_path.join("Versioning.kt");
if let Err(error) = fs::write(versioning_kt_destination_path, versioning_kt) {
rollback::run(project_path);
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");
let utils_kt_destination_path = build_src_kotlin_path.join("VersioningUtils.kt");
if let Err(error) = fs::write(utils_kt_destination_path, utils_kt) {
rollback::run(project_path);
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)
}
}
}
#[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);
}
}
}
pub mod app {}