-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
170 lines (127 loc) · 4.14 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std::{env, fs, process::Command};
use normpath::{BasePathBuf, PathExt};
use zip_extensions::*;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
fn main() -> Result<()> {
run()?;
Ok(())
}
fn run() -> Result<()> {
let task = env::args().nth(1);
match task.as_deref() {
Some("dist") => dist()?,
_ => print_help(),
}
Ok(())
}
fn print_help() {
eprintln!(
"\
TASKS:
dist Builds the binary and zips them with the files in `resources`
"
)
}
fn dist() -> Result<()> {
if dist_dir().exists() {
fs::remove_dir_all(dist_dir())?;
}
fs::create_dir_all(dist_dir())?;
dist_binary()?;
Ok(())
}
#[cfg(target_os = "windows")]
fn dist_binary() -> Result<()> {
let cargo = env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let status = Command::new(cargo)
.current_dir(project_root())
.args(&["build", "--release"])
.status()?;
if !status.success() {
Err("cargo build failed")?;
}
let dst = project_root().join("target/release/crosshair-switcher.exe");
fs::copy(&dst, dist_dir().join("crosshair-switcher.exe"))?;
let from_scripts_dir = project_root().join("resources/scripts");
let from_materials_dir = project_root().join("resources/materials");
let to_scripts_dir = dist_dir().join("scripts");
let to_materials_dir = dist_dir().join("materials");
let status = Command::new("xcopy.exe")
.current_dir(project_root())
.args(&[from_scripts_dir, to_scripts_dir])
.args(&["/E", "/H", "/I"])
.status()?;
if !status.success() {
Err("copying scripts dir failed")?;
}
let status = Command::new("xcopy.exe")
.current_dir(project_root())
.args(&[from_materials_dir, to_materials_dir])
.args(&["/E", "/H", "/I"])
.status()?;
if !status.success() {
Err("copying materials dir failed")?;
}
let archive_file = dist_dir().parent()?.unwrap().join("crosshair-switcher-windows.zip");
let source_dir = dist_dir();
zip_create_from_directory(
&archive_file.as_path().to_owned(),
&source_dir.as_path().to_owned(),
)?;
fs::copy(&archive_file, dist_dir().join("crosshair-switcher-windows.zip"))?;
fs::remove_file(&archive_file)?;
Ok(())
}
#[cfg(target_os = "linux")]
fn dist_binary() -> Result<()> {
let cargo = env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let status = Command::new(cargo)
.current_dir(project_root())
.args(&["build", "--release"])
.status()?;
if !status.success() {
Err("cargo build failed")?;
}
let dst = project_root().join("target/release/crosshair-switcher");
fs::copy(&dst, dist_dir().join("crosshair-switcher"))?;
let from_scripts_dir = project_root().join("resources/scripts");
let from_materials_dir = project_root().join("resources/materials");
let to_scripts_dir = dist_dir().join("scripts");
let to_materials_dir = dist_dir().join("materials");
let status = Command::new("cp")
.current_dir(project_root())
.arg("-R")
.args(&[from_scripts_dir, to_scripts_dir])
.status()?;
if !status.success() {
Err("copying scripts dir failed")?;
}
let status = Command::new("cp")
.current_dir(project_root())
.arg("-R")
.args(&[from_materials_dir, to_materials_dir])
.status()?;
if !status.success() {
Err("copying materials dir failed")?;
}
let archive_file = dist_dir().parent()?.unwrap().join("crosshair-switcher-linux.zip");
let source_dir = dist_dir();
zip_create_from_directory(
&archive_file.as_path().to_owned(),
&source_dir.as_path().to_owned(),
)?;
fs::copy(&archive_file, dist_dir().join("crosshair-switcher-linux.zip"))?;
fs::remove_file(&archive_file)?;
Ok(())
}
fn project_root() -> BasePathBuf {
std::path::Path::new(&env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.to_path_buf()
.normalize()
.unwrap()
}
fn dist_dir() -> BasePathBuf {
project_root().join("target/dist")
}