forked from vleue/bevy_embedded_assets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
129 lines (119 loc) · 4.39 KB
/
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
use std::{
env,
fs::{self, File},
io::Write,
path::{Path, PathBuf},
};
const ASSET_PATH_VAR: &str = "BEVY_ASSET_PATH";
fn main() {
cargo_emit::rerun_if_env_changed!(ASSET_PATH_VAR);
// Check if env variable is set for the assets folder
if let Some(dir) = env::var(ASSET_PATH_VAR)
.ok()
.map(|v| Path::new(&v).to_path_buf())
.and_then(|path| {
if path.exists() {
Some(path)
} else {
cargo_emit::warning!(
"${} points to an unknown folder: {}",
ASSET_PATH_VAR,
path.to_string_lossy()
);
None
}
})
// Otherwise, search for the target folder and look for an assets folder next to it
.or_else(|| {
env::var("OUT_DIR")
.ok()
.map(|v| Path::new(&v).to_path_buf())
.and_then(|path| {
for ancestor in path.ancestors() {
if let Some(last) = ancestor.file_name() {
if last == "target" {
return ancestor.parent().map(|p| p.join("assets"));
}
}
}
None
})
.and_then(|path| {
if path.exists() {
Some(path)
} else {
cargo_emit::warning!(
"Could not find asset folder from Cargo build directory"
);
None
}
})
})
{
cargo_emit::rerun_if_changed!(dir.to_string_lossy());
cargo_emit::warning!("Asset folder found: {}", dir.to_string_lossy());
let out_dir = env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("include_all_assets.rs");
let mut file = File::create(dest_path).unwrap();
file.write_all(
"/// Generated function that will add all assets to the [`EmbeddedAssetIo`].
#[allow(unused_variables, clippy::non_ascii_literal)] pub fn include_all_assets(embedded: &mut EmbeddedAssetIo){\n"
.as_ref(),
)
.unwrap();
let building_for_wasm = std::env::var("CARGO_CFG_TARGET_ARCH") == Ok("wasm32".to_string());
visit_dirs(&dir)
.iter()
.map(|path| (path, path.strip_prefix(&dir).unwrap()))
.for_each(|(fullpath, path)| {
let mut path = path.to_string_lossy().to_string();
if building_for_wasm {
// building for wasm. replace paths with forward slash in case we're building from windows
path = path.replace('\\', "/");
}
cargo_emit::rerun_if_changed!(fullpath.to_string_lossy());
file.write_all(
format!(
r#"embedded.add_asset(std::path::Path::new({:?}), include_bytes!({:?}));
"#,
path,
fullpath.to_string_lossy()
)
.as_ref(),
)
.unwrap();
});
file.write_all("}".as_ref()).unwrap();
} else if std::env::var("DOCS_RS").is_ok() {
let out_dir = env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("include_all_assets.rs");
let mut file = File::create(dest_path).unwrap();
file.write_all(
"/// Generated function that will add all assets to the [`EmbeddedAssetIo`].
#[allow(unused_variables)] pub fn include_all_assets(embedded: &mut EmbeddedAssetIo){}"
.as_ref(),
)
.unwrap();
} else {
cargo_emit::warning!(
"Could not find asset folder, please specify its path with ${}",
ASSET_PATH_VAR
);
panic!("No asset folder found");
}
}
fn visit_dirs(dir: &Path) -> Vec<PathBuf> {
let mut collected = vec![];
if dir.is_dir() {
for entry in fs::read_dir(dir).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_dir() {
collected.append(&mut visit_dirs(&path));
} else {
collected.push(path);
}
}
}
collected
}