-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
150 lines (140 loc) · 4.36 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use std::env;
use std::fs::{self, OpenOptions};
use std::io::{self, prelude::*};
use std::process::Command;
fn replace(path: &str, old: &str, new: &str) -> io::Result<()> {
let contents = fs::read_to_string(path)?;
let new = contents.replace(old, new);
let mut file = OpenOptions::new().write(true).truncate(true).open(path)?;
file.write(new.as_bytes())?;
Ok(())
}
fn main() {
let package_version = env!("CARGO_PKG_VERSION");
let package_name = env!("CARGO_PKG_NAME");
let current_dir = env!("CARGO_MANIFEST_DIR");
eprintln!("Current working directory: {current_dir}");
eprintln!("Downloading the api specification...");
Command::new("wget")
.arg("https://raw.githubusercontent.com/manticoresoftware/openapi/master/manticore.yml")
.arg("-c")
.arg("-P")
.arg(¤t_dir)
.status()
.unwrap();
eprintln!("Generating files from the api specification...");
// https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/rust.md
Command::new("docker")
.arg("run")
.arg("--rm")
.arg("-v")
.arg(&format!("{current_dir}:/local"))
.arg("openapitools/openapi-generator-cli")
.arg("generate")
.arg("-i")
.arg("/local/manticore.yml")
.arg("-g")
.arg("rust")
.arg("-o")
.arg(&format!("/local"))
.arg("--additional-properties")
.arg("library=reqwest")
.arg("--additional-properties")
.arg(&format!("packageName={package_name}"))
.arg("--additional-properties")
.arg(&format!("packageVersion={package_version}"))
.arg("--additional-properties")
.arg("preferUnsignedInt=true")
.arg("--additional-properties")
.arg("supportMiddleware=true")
.status()
.unwrap();
// sudo chown -R ${USER}:${USER} src/apis/ src/models/
replace(
"src/models/search_request.rs",
r#"rename = "source""#,
r#"rename = "_source""#,
)
.unwrap();
replace("src/models/match_filter.rs", r#"Serialize, "#, "").unwrap();
replace(
"src/models/search_response.rs",
r#"struct SearchResponse {"#,
"struct SearchResponse<T = serde_json::Value> {",
)
.unwrap();
replace(
"src/models/search_response.rs",
r#"impl SearchResponse {"#,
"impl<T> SearchResponse<T> {",
)
.unwrap();
replace(
"src/models/search_response.rs",
r#"models::SearchResponseHits>>"#,
"models::SearchResponseHits<T>>>",
)
.unwrap();
replace(
"src/models/search_response.rs",
r#"-> SearchResponse {"#,
"-> SearchResponse<T> {",
)
.unwrap();
replace(
"src/models/search_response_hits.rs",
r#"Vec<serde_json::Value>"#,
"Vec<T>",
)
.unwrap();
replace(
"src/models/search_response_hits.rs",
r#"struct SearchResponseHits {"#,
"struct SearchResponseHits<T = serde_json::Value> {",
)
.unwrap();
replace(
"src/models/search_response_hits.rs",
r#"impl SearchResponseHits {"#,
"impl<T> SearchResponseHits<T> {",
)
.unwrap();
replace(
"src/models/search_response_hits.rs",
r#"-> SearchResponseHits {"#,
"-> SearchResponseHits<T> {",
)
.unwrap();
replace(
"src/apis/utils_api.rs",
r#"raw_response: Option<bool>"#,
r#"raw_response: bool"#,
)
.unwrap();
replace(
"src/apis/utils_api.rs",
r#"let Some(ref local_var_str) = raw_response"#,
r#"raw_response"#,
)
.unwrap();
replace(
"src/apis/utils_api.rs",
r#"("raw_response", &local_var_str.to_string())"#,
r#"("raw_response", &raw_response.to_string())"#,
)
.unwrap();
replace(
"src/apis/utils_api.rs",
r#"local_var_req_builder = local_var_req_builder.json(&body);"#,
r#"local_var_req_builder = if raw_response {
local_var_req_builder.form(&[("query", body), ("mode", "raw")])
} else {
local_var_req_builder.form(&[("query", body)])
};"#,
)
.unwrap();
println!("cargo:rerun-if-changed=Cargo.toml");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=.openapi-generator-ignore");
println!("cargo:rerun-if-changed={current_dir}/manticore.yml");
}