-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.rs
321 lines (269 loc) · 10.2 KB
/
main.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright (c) 2020-2022, Cosmos Rust authors (licensed under the Apache License Version 2.0)
// Modifications Copyright (c) 2022-present, Crypto.com (licensed under the Apache License Version 2.0)
use regex::Regex;
use std::{
env,
ffi::OsStr,
fs::{self, create_dir_all, remove_dir_all},
io,
path::{Path, PathBuf},
process,
sync::atomic::{self, AtomicBool},
};
use walkdir::WalkDir;
/// Suppress log messages
// TODO(tarcieri): use a logger for this
static QUIET: AtomicBool = AtomicBool::new(false);
/// The directory generated cosmos-sdk proto files go into in this repo
const PROTO_DIR: &str = "../proto/src/prost/";
const CHAIN_MAIN_REV: &str = "v3.3.3";
const CHAIN_MAIN_DIR: &str = "../third_party/chain-main";
const LUNA_CLASSIC_REV: &str = "v0.5.18";
const LUNA_CLASSIC_DIR: &str = "../third_party/luna_classic";
/// A temporary directory for proto building
const TMP_BUILD_DIR: &str = "/tmp/tmp-protobuf/";
// Patch strings used by `copy_and_patch`
/// Protos belonging to these Protobuf packages will be excluded
/// (i.e. because they are sourced from `tendermint-proto`)
const EXCLUDED_PROTO_PACKAGES: &[&str] = &["gogoproto", "google", "tendermint", "cosmos"];
/// Regex for locating instances of `tendermint-proto` in prost/tonic build output
const TENDERMINT_PROTO_REGEX: &str = "(super::)+tendermint";
/// Attribute preceeding a Tonic client definition
const TONIC_CLIENT_ATTRIBUTE: &str = "#[doc = r\" Generated client implementations.\"]";
/// Attributes to add to gRPC clients
const GRPC_CLIENT_ATTRIBUTES: &[&str] = &[
"#[cfg(feature = \"grpc\")]",
"#[cfg_attr(docsrs, doc(cfg(feature = \"grpc\")))]",
TONIC_CLIENT_ATTRIBUTE,
];
/// Log info to the console (if `QUIET` is disabled)
// TODO(tarcieri): use a logger for this
macro_rules! info {
($msg:expr) => {
if !is_quiet() {
println!("[info] {}", $msg)
}
};
($fmt:expr, $($arg:tt)+) => {
info!(&format!($fmt, $($arg)+))
};
}
fn main() {
if is_github() {
set_quiet();
}
let tmp_build_dir: PathBuf = TMP_BUILD_DIR.parse().unwrap();
let proto_dir: PathBuf = PROTO_DIR.parse().unwrap();
if tmp_build_dir.exists() {
fs::remove_dir_all(tmp_build_dir.clone()).unwrap();
}
fs::create_dir(tmp_build_dir.clone()).unwrap();
update_submodules();
output_commit_versions(&tmp_build_dir);
compile_chain_main_protos_and_services(&tmp_build_dir);
compile_luna_classic_protos_and_services(&tmp_build_dir);
copy_generated_files(&tmp_build_dir, &proto_dir);
}
fn is_quiet() -> bool {
QUIET.load(atomic::Ordering::Relaxed)
}
fn set_quiet() {
QUIET.store(true, atomic::Ordering::Relaxed);
}
/// Parse `--github` flag passed to `proto-build` on the eponymous GitHub Actions job.
/// Disables `info`-level log messages, instead outputting only a commit message.
fn is_github() -> bool {
env::args().any(|arg| arg == "--github")
}
fn run_git(args: impl IntoIterator<Item = impl AsRef<OsStr>>) {
let stdout = if is_quiet() {
process::Stdio::null()
} else {
process::Stdio::inherit()
};
let exit_status = process::Command::new("git")
.args(args)
.stdout(stdout)
.status()
.expect("git exit status missing");
if !exit_status.success() {
panic!("git exited with error code: {:?}", exit_status.code());
}
}
fn update_submodules() {
info!("Updating chain-main submodule...");
run_git([
"-C",
CHAIN_MAIN_DIR,
"submodule",
"update",
"--init",
"--recursive",
]);
run_git(["-C", CHAIN_MAIN_DIR, "fetch"]);
run_git(["-C", CHAIN_MAIN_DIR, "reset", "--hard", CHAIN_MAIN_REV]);
info!("Updating luna_classic submodule...");
run_git([
"-C",
LUNA_CLASSIC_DIR,
"submodule",
"update",
"--init",
"--recursive",
]);
run_git(["-C", LUNA_CLASSIC_DIR, "fetch"]);
run_git(["-C", LUNA_CLASSIC_DIR, "reset", "--hard", LUNA_CLASSIC_REV]);
}
fn output_commit_versions(out_dir: &Path) {
let path = out_dir.join("CHAIN_MAIN_COMMIT");
fs::write(path, CHAIN_MAIN_REV).unwrap();
let path = out_dir.join("LUNA_CLASSIC_COMMIT");
fs::write(path, LUNA_CLASSIC_REV).unwrap();
}
fn compile_chain_main_protos_and_services(out_dir: &Path) {
info!(
"Compiling chain_main .proto files to Rust into '{}'...",
out_dir.display()
);
let root = env!("CARGO_MANIFEST_DIR");
let sdk_dir = Path::new(CHAIN_MAIN_DIR);
let proto_includes_paths = [
format!("{}/../proto", root),
format!("{}/proto", sdk_dir.display()),
format!("{}/third_party/cosmos-sdk/proto", sdk_dir.display()),
format!(
"{}/third_party/cosmos-sdk/third_party/proto",
sdk_dir.display()
),
];
// Paths
let proto_paths = [
format!("{}/../proto/definitions/mock", root),
format!("{}/proto/chainmain", sdk_dir.display()),
format!("{}/proto/nft", sdk_dir.display()),
format!("{}/proto/supply", sdk_dir.display()),
];
// List available proto files
let mut protos: Vec<PathBuf> = vec![];
collect_protos(&proto_paths, &mut protos);
// List available paths for dependencies
let includes: Vec<PathBuf> = proto_includes_paths.iter().map(PathBuf::from).collect();
// Compile all of the proto files, along with grpc service clients
info!("Compiling proto definitions and clients for GRPC services!");
tonic_build::configure()
.build_client(true)
.build_server(false)
.out_dir(out_dir)
.extern_path(".tendermint", "::tendermint_proto")
.type_attribute("BaseNFT", "#[derive(Serialize, Deserialize)]")
.type_attribute("Denom", "#[derive(Serialize, Deserialize)]")
.type_attribute("Collection", "#[derive(Serialize, Deserialize)]")
.type_attribute("IDCollection", "#[derive(Serialize, Deserialize)]")
.type_attribute("Owner", "#[derive(Serialize, Deserialize)]")
.compile(&protos, &includes)
.unwrap();
info!("=> Done!");
}
fn compile_luna_classic_protos_and_services(out_dir: &Path) {
info!(
"Compiling luna_classic .proto files to Rust into '{}'...",
out_dir.display()
);
let root = env!("CARGO_MANIFEST_DIR");
let sdk_dir = Path::new(LUNA_CLASSIC_DIR);
let proto_includes_paths = [
format!("{}/../proto", root),
format!("{}/proto", sdk_dir.display()),
format!("{}/third_party/proto", sdk_dir.display()),
];
// Paths
let proto_paths = [
format!("{}/../proto/definitions/mock", root),
format!("{}/proto/terra/market", sdk_dir.display()),
format!("{}/proto/terra/oracle", sdk_dir.display()),
format!("{}/proto/terra/treasury", sdk_dir.display()),
format!("{}/proto/terra/tx", sdk_dir.display()),
format!("{}/proto/terra/vesting", sdk_dir.display()),
format!("{}/proto/terra/wasm", sdk_dir.display()),
];
// List available proto files
let mut protos: Vec<PathBuf> = vec![];
collect_protos(&proto_paths, &mut protos);
// List available paths for dependencies
let includes: Vec<PathBuf> = proto_includes_paths.iter().map(PathBuf::from).collect();
// Compile all of the proto files, along with grpc service clients
info!("Compiling proto definitions and clients for GRPC services!");
tonic_build::configure()
.build_client(true)
.build_server(false)
.out_dir(out_dir)
.compile(&protos, &includes)
.unwrap();
info!("=> Done!");
}
/// collect_protos walks every path in `proto_paths` and recursively locates all .proto
/// files in each path's subdirectories, adding the full path of each file to `protos`
///
/// Any errors encountered will cause failure for the path provided to WalkDir::new()
fn collect_protos(proto_paths: &[String], protos: &mut Vec<PathBuf>) {
for proto_path in proto_paths {
protos.append(
&mut WalkDir::new(proto_path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| {
e.file_type().is_file()
&& e.path().extension().is_some()
&& e.path().extension().unwrap() == "proto"
})
.map(|e| e.into_path())
.collect(),
);
}
}
fn copy_generated_files(from_dir: &Path, to_dir: &Path) {
info!("Copying generated files into '{}'...", to_dir.display());
// Remove old compiled files
remove_dir_all(to_dir).unwrap_or_default();
create_dir_all(to_dir).unwrap();
let mut filenames = Vec::new();
// Copy new compiled files (prost does not use folder structures)
let errors = WalkDir::new(from_dir)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.map(|e| {
let filename = e.file_name().to_os_string().to_str().unwrap().to_string();
filenames.push(filename.clone());
copy_and_patch(e.path(), format!("{}/{}", to_dir.display(), &filename))
})
.filter_map(|e| e.err())
.collect::<Vec<_>>();
if !errors.is_empty() {
for e in errors {
eprintln!("[error] Error while copying compiled file: {}", e);
}
panic!("[error] Aborted.");
}
}
fn copy_and_patch(src: impl AsRef<Path>, dest: impl AsRef<Path>) -> io::Result<()> {
// Skip proto files belonging to `EXCLUDED_PROTO_PACKAGES`
for package in EXCLUDED_PROTO_PACKAGES {
if let Some(filename) = src.as_ref().file_name().and_then(OsStr::to_str) {
if filename.starts_with(&format!("{}.", package)) {
return Ok(());
}
}
}
let contents = fs::read_to_string(src)?;
// `prost-build` output references types from `tendermint-proto` crate
// relative paths, which we need to munge into `tendermint_proto` in
// order to leverage types from the upstream crate.
let contents = Regex::new(TENDERMINT_PROTO_REGEX)
.unwrap()
.replace_all(&contents, "tendermint_proto");
// Patch each service definition with a feature attribute
let patched_contents =
contents.replace(TONIC_CLIENT_ATTRIBUTE, &GRPC_CLIENT_ATTRIBUTES.join("\n"));
fs::write(dest, patched_contents)
}