-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
56 lines (55 loc) · 1.89 KB
/
build.rs
File metadata and controls
56 lines (55 loc) · 1.89 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
use anyhow::Result;
fn main() -> Result<()> {
println!("cargo::rerun-if-changed=../openapi_spec/firecrest_3.1.yaml");
// convert to json in temp dir
let f = std::fs::File::open("../openapi_spec/firecrest_3.1.yaml")?;
let json_value: serde_json::Value = serde_yaml::from_reader(f)?;
let out = tempfile::Builder::new()
.suffix(".json")
.disable_cleanup(true)
.tempfile()?;
let outpath = out.path();
let outpath = dbg!(outpath);
let o = std::fs::File::create(outpath)?;
serde_json::to_writer_pretty(o, &json_value)?;
std::process::Command::new("oas3-gen")
.arg("generate")
.arg("-i")
.arg(outpath.to_str().unwrap())
.arg("-o")
.arg("./src/types.rs")
.spawn()
.expect("oas3-gen failed to run, if it's missing please install with 'cargo install oas3-gen@0.21.1'")
.wait()?;
// we need to add an into clause because the defaults in the openapi spec are weird
let content = std::fs::read_to_string("./src/types.rs")?;
let content = content.replace(
"default(Some(\"gzip\".to_string())",
"default(Some(\"gzip\".to_string().into())",
);
// remove source as it's a temporary file and we don't want to change it every time the build runs, even if
// there are no changes
let content = content
.lines()
.filter(|l| !l.starts_with("//! Source: "))
.collect::<Vec<&str>>()
.join("\n");
let content = format!(
"// @generated by oas3-gen
#![allow(clippy::all)]
#![allow(dead_code)]
{}",
content
);
std::fs::write("./src/types.rs", content)?;
// format file
std::process::Command::new("cargo")
.arg("+nightly")
.arg("fmt")
.arg("--")
.arg("./src/types.rs")
.spawn()
.expect("cargo couldn't format the file")
.wait()?;
Ok(())
}