forked from luxtorpeda-dev/luxtorpeda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_build.rs
182 lines (153 loc) · 5.58 KB
/
post_build.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
171
172
173
174
175
176
177
178
179
180
181
182
use std::env;
use std::path::Path;
extern crate fs_extra;
use fs_extra::copy_items;
use std::fs;
use std::process::Command;
extern crate tar;
use xz2::write::XzEncoder;
// These variables are used to generate compatibilitytool.vdf
const TOOL_NAME: &str = "luxtorpeda";
const TOOL_NAME_DEV: &str = "luxtorpeda_dev";
const TOOL_NAME_DISPLAY: &str = "Luxtorpeda";
const TOOL_NAME_DISPLAY_DEV: &str = "Luxtorpeda (dev)";
// Files that should be copied from the root of the repo
const ROOT_FILES: &[&str] = &[
"toolmanifest.vdf",
"LICENSE",
"README.md",
"luxtorpeda.sh"
];
// Files that should be copied from target to the final destination
const FILES: &[&str] = &[
"compatibilitytool.vdf",
"libluxtorpeda.so",
"luxtorpeda.pck",
"luxtorpeda.x86_64",
];
fn main() {
let out_dir = env::var("CRATE_OUT_DIR").unwrap();
let profile = env::var("CRATE_PROFILE").unwrap();
create_target_gdignore(&out_dir);
if profile == "release" {
release_godot_workaround(&out_dir);
}
match env::var("GODOT") {
Ok(godot_path) => build_godot_project(&out_dir, &godot_path, &profile),
Err(_) => {
eprintln!("godot not provided so skipping");
}
};
create_compatibilitytool_vdf(&out_dir, &profile);
copy_root_files(&out_dir);
match env::var("TARGET") {
Ok(target) => {
if !target.is_empty() {
resolve_target(&out_dir, &target);
}
},
Err(_) => {
eprintln!("target not provided so skipping");
}
}
}
fn create_target_gdignore(out_dir: &str) {
std::fs::File::create(Path::new(out_dir).join(".gdignore")).expect("create gdignore failed");
}
fn release_godot_workaround(out_dir: &str) {
println!("release_godot_workaround, copying files");
let options = fs_extra::dir::CopyOptions {
overwrite: true,
..Default::default()
};
let debug_dir = Path::new(out_dir).parent().unwrap().join("debug");
create_target_gdignore(out_dir);
fs::create_dir_all(debug_dir.clone()).expect("failed to create debug dir");
let mut from_paths = Vec::new();
from_paths.push(Path::new(out_dir).join("libluxtorpeda.so"));
copy_items(&from_paths, debug_dir, &options).expect("release_godot_workaround copy failed");
}
fn copy_root_files(out_dir: &str) {
println!("copy_root_files");
let options = fs_extra::dir::CopyOptions {
overwrite: true,
..Default::default()
};
copy_items(&ROOT_FILES, out_dir, &options).expect("copy_root_files copy failed");
}
fn build_godot_project(out_dir: &str, godot_path: &str, profile: &str) {
println!("build_godot_project");
let out_path = Path::new(out_dir).join("luxtorpeda.x86_64").into_os_string().into_string().unwrap();
let build_cmd = Command::new(godot_path)
.args(["--path", ".", &std::format!("--export-{}", profile).to_string(), "Linux/X11", &out_path, "--display-driver", "headless"])
.status()
.expect("failed to execute godot");
if !build_cmd.success() {
panic!("build_godot_project failed");
}
}
fn create_compatibilitytool_vdf(out_dir: &str, profile: &str) {
println!("create_compatibilitytool_vdf");
let template_str = fs::read_to_string("compatibilitytool.template").expect("create_compatibilitytool_vdf read template error");
let output_path = Path::new(out_dir).join("compatibilitytool.vdf");
let display_name = match profile {
"debug" => TOOL_NAME_DISPLAY_DEV,
_ => TOOL_NAME_DISPLAY
};
let tool_name = match profile {
"debug" => TOOL_NAME_DEV,
_ => TOOL_NAME
};
let mut file_str = template_str.replace("%display_name%", display_name);
file_str = file_str.replace("%name%", tool_name);
fs::write(output_path, file_str).expect("create_compatibilitytool_vdf write error");
}
fn resolve_target(out_dir: &str, target: &str) {
println!("resolve_target, target is {}", target);
build_folder(&out_dir, TOOL_NAME);
match target {
"luxtorpeda" => {},
"luxtorpeda.tar.xz" => {
create_archive(TOOL_NAME, &target);
},
"user_install" => {},
"install" => {},
_ => {
eprintln!("resolve_target - Unknown target of {}", target);
}
};
}
fn build_folder(out_dir: &str, folder_path: &str) {
println!("build_folder with {}", folder_path);
fs::create_dir_all(folder_path).expect("build_tool_folder create dir failed");
let options = fs_extra::dir::CopyOptions {
overwrite: true,
copy_inside: true,
..Default::default()
};
let mut files = Vec::new();
for filename in ROOT_FILES.iter() {
files.push(Path::new(out_dir).join(filename));
}
for filename in FILES.iter() {
files.push(Path::new(out_dir).join(filename));
}
copy_items(&files, folder_path, &options).expect("build_tool_folder copy failed");
match env::var("VERSION") {
Ok(version) => {
if !version.is_empty() {
fs::write(Path::new(folder_path).join("version"), version).expect("build_folder version write error");
}
},
Err(_) => {
eprintln!("version not provided so skipping");
}
}
}
fn create_archive(folder_path: &str, archive_name: &str) {
println!("create_archive from folder {}", folder_path);
let tar_xz = std::fs::File::create(archive_name).expect("create_archive file create error");
let enc = XzEncoder::new(tar_xz, 6);
let mut tar = tar::Builder::new(enc);
tar.append_dir_all(TOOL_NAME, folder_path).expect("create_archive append error");
}