forked from cross-rs/cross
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.rs
More file actions
69 lines (61 loc) · 2 KB
/
Copy pathcodegen.rs
File metadata and controls
69 lines (61 loc) · 2 KB
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
use clap::Args;
use eyre::Context;
use std::fmt::Write;
use crate::util::{get_cargo_workspace, get_matrix};
#[derive(Args, Debug)]
pub struct Codegen {}
pub fn codegen(Codegen { .. }: Codegen) -> cross::Result<()> {
let path = get_cargo_workspace().join("src/docker/provided_images.rs");
std::fs::write(path, docker_images()).wrap_err("when writing src/docker/provided_images.rs")?;
Ok(())
}
pub fn docker_images() -> String {
let mut images = String::from(
r#"#![doc = "*** AUTO-GENERATED, do not touch. Run `cargo xtask codegen` to update ***"]
use super::{ImagePlatform, ProvidedImage};
#[rustfmt::skip]
pub static PROVIDED_IMAGES: &[ProvidedImage] = &["#,
);
for image_target in get_matrix()
.iter()
.filter(|i| i.builds_image() && i.to_image_target().is_toolchain_image() && !i.disabled)
{
write!(
&mut images,
r#"
ProvidedImage {{
name: "{name}",
platforms: &[{platform}],
sub: {sub}
}},"#,
name = image_target.target.clone(),
platform = &image_target
.platforms()
.iter()
.map(|p| {
format!(
"ImagePlatform::{}",
p.replace('-', "_").to_ascii_uppercase()
)
})
.collect::<Vec<_>>()
.join(", "),
sub = if let Some(sub) = &image_target.sub {
format!(r#"Some("{}")"#, sub)
} else {
"None".to_string()
}
)
.expect("writing to string should not fail")
}
images.push_str("\n];\n");
images
}
#[cfg(test)]
#[test]
pub fn ensure_correct_codegen() -> cross::Result<()> {
let provided_images = crate::util::get_cargo_workspace().join("src/docker/provided_images.rs");
let content = cross::file::read(provided_images)?;
assert_eq!(content.replace("\r\n", "\n"), docker_images());
Ok(())
}