1
- use std:: fs:: remove_dir_all;
2
- use std:: fs:: { copy, create_dir_all} ;
1
+ use std:: fs:: { create_dir_all, remove_dir_all} ;
3
2
use std:: path:: { Path , PathBuf } ;
4
3
use std:: process;
5
4
6
- use tempfile :: TempDir ;
5
+ use argh :: FromArgs ;
7
6
use walkdir:: WalkDir ;
8
7
9
- use argh:: FromArgs ;
10
8
#[ derive( Debug , FromArgs ) ]
11
9
#[ argh( subcommand, name = "compile" ) ]
12
10
/// Compile
@@ -26,16 +24,14 @@ pub struct CompileCmd {
26
24
27
25
impl CompileCmd {
28
26
pub fn run ( & self ) {
29
- let tmp_ibc = TempDir :: new ( ) . unwrap ( ) ;
30
- Self :: compile_ibc_protos ( & self . ibc , tmp_ibc. as_ref ( ) ) ;
31
-
32
- let tmp_lcp = TempDir :: new ( ) . unwrap ( ) ;
33
- Self :: compile_lcp_protos ( & self . ibc , tmp_lcp. as_ref ( ) , & self . descriptor ) ;
34
-
35
- Self :: copy_generated_files ( tmp_lcp. as_ref ( ) , tmp_ibc. as_ref ( ) , & self . out ) ;
27
+ Self :: compile_protos ( & self . ibc , self . out . as_ref ( ) , & self . descriptor ) ;
36
28
}
37
29
38
- fn compile_lcp_protos ( ibc_dir : & Path , out_dir : & Path , descriptor_path : & Path ) {
30
+ fn compile_protos ( ibc_dir : & Path , out_dir : & Path , descriptor_path : & Path ) {
31
+ // Remove old compiled files
32
+ remove_dir_all ( & out_dir) . unwrap_or_default ( ) ;
33
+ create_dir_all ( & out_dir) . unwrap ( ) ;
34
+
39
35
println ! (
40
36
"[info ] Compiling LCP .proto files to Rust into '{}'..." ,
41
37
out_dir. display( )
@@ -45,90 +41,15 @@ impl CompileCmd {
45
41
46
42
// Paths
47
43
let proto_paths = [
48
- format ! (
49
- "{}/../proto/definitions" ,
50
- root
51
- ) ,
52
- ] ;
53
-
54
- let proto_includes_paths = [
55
44
format ! ( "{}/../proto/definitions" , root) ,
56
- format ! ( "{}/proto" , ibc_dir. display( ) ) ,
57
- format ! ( "{}/third_party/proto" , ibc_dir. display( ) ) ,
58
- ] ;
59
-
60
- // List available proto files
61
- let mut protos: Vec < PathBuf > = vec ! [ ] ;
62
- for proto_path in & proto_paths {
63
- println ! ( "Looking for proto files in {:?}" , proto_path) ;
64
- protos. append (
65
- & mut WalkDir :: new ( proto_path)
66
- . into_iter ( )
67
- . filter_map ( |e| e. ok ( ) )
68
- . filter ( |e| {
69
- e. file_type ( ) . is_file ( )
70
- && e. path ( ) . extension ( ) . is_some ( )
71
- && e. path ( ) . extension ( ) . unwrap ( ) == "proto"
72
- } )
73
- . map ( |e| e. into_path ( ) )
74
- . collect ( ) ,
75
- ) ;
76
- }
77
-
78
- println ! ( "Found the following protos:" ) ;
79
- // Show which protos will be compiled
80
- for proto in & protos {
81
- println ! ( "\t -> {:?}" , proto) ;
82
- }
83
- println ! ( "[info ] Compiling.." ) ;
84
-
85
- // List available paths for dependencies
86
- let includes: Vec < PathBuf > = proto_includes_paths. iter ( ) . map ( PathBuf :: from) . collect ( ) ;
87
- let attrs_serde = r#"#[derive(::serde::Serialize, ::serde::Deserialize)]"# ;
88
- let compilation = tonic_build:: configure ( )
89
- . build_client ( true )
90
- . compile_well_known_types ( true )
91
- . client_mod_attribute ( "." , r#"#[cfg(feature = "client")]"# )
92
- . server_mod_attribute ( "." , r#"#[cfg(feature = "server")]"# )
93
- . build_server ( true )
94
- . out_dir ( out_dir)
95
- . extern_path ( ".tendermint" , "::tendermint_proto" )
96
- . type_attribute ( ".cosmos.upgrade.v1beta1" , attrs_serde)
97
- . type_attribute ( ".cosmos.base.v1beta1" , attrs_serde)
98
- . type_attribute ( ".cosmos.base.query.v1beta1" , attrs_serde)
99
- . type_attribute ( ".cosmos.bank.v1beta1" , attrs_serde)
100
- . type_attribute ( ".lcp.service.enclave.v1" , attrs_serde)
101
- . type_attribute ( ".lcp.service.elc.v1" , attrs_serde)
102
- . file_descriptor_set_path ( descriptor_path)
103
- . compile ( & protos, & includes) ;
104
-
105
- match compilation {
106
- Ok ( _) => {
107
- println ! ( "Successfully compiled proto files" ) ;
108
- }
109
- Err ( e) => {
110
- println ! ( "Failed to compile:{:?}" , e. to_string( ) ) ;
111
- process:: exit ( 1 ) ;
112
- }
113
- }
114
- }
115
-
116
- fn compile_ibc_protos ( ibc_dir : & Path , out_dir : & Path ) {
117
- println ! (
118
- "[info ] Compiling IBC .proto files to Rust into '{}'..." ,
119
- out_dir. display( )
120
- ) ;
121
-
122
- // Paths
123
- let proto_paths = [
124
- // ibc-go proto files
125
45
format ! (
126
46
"{}/proto/ibc/core/client/v1/client.proto" ,
127
47
ibc_dir. display( )
128
48
) ,
129
49
] ;
130
50
131
51
let proto_includes_paths = [
52
+ format ! ( "{}/../proto/definitions" , root) ,
132
53
format ! ( "{}/proto" , ibc_dir. display( ) ) ,
133
54
format ! ( "{}/third_party/proto" , ibc_dir. display( ) ) ,
134
55
] ;
@@ -160,85 +81,28 @@ impl CompileCmd {
160
81
161
82
// List available paths for dependencies
162
83
let includes: Vec < PathBuf > = proto_includes_paths. iter ( ) . map ( PathBuf :: from) . collect ( ) ;
163
-
164
84
let attrs_serde = r#"#[derive(::serde::Serialize, ::serde::Deserialize)]"# ;
165
85
let attrs_jsonschema =
166
86
r#"#[cfg_attr(feature = "json-schema", derive(::schemars::JsonSchema))]"# ;
167
87
let attrs_ord = "#[derive(Eq, PartialOrd, Ord)]" ;
168
- let attrs_eq = "#[derive(Eq)]" ;
169
88
let attrs_serde_default = r#"#[serde(default)]"# ;
170
- let attrs_serde_base64 = r#"#[serde(with = "crate::base64")]"# ;
171
- let attrs_jsonschema_str =
172
- r#"#[cfg_attr(feature = "json-schema", schemars(with = "String"))]"# ;
173
-
174
89
let compilation = tonic_build:: configure ( )
175
90
. build_client ( true )
176
91
. compile_well_known_types ( true )
177
92
. client_mod_attribute ( "." , r#"#[cfg(feature = "client")]"# )
178
- . build_server ( false )
93
+ . server_mod_attribute ( "." , r#"#[cfg(feature = "server")]"# )
94
+ . build_server ( true )
179
95
. out_dir ( out_dir)
180
- . extern_path ( ".tendermint" , "::tendermint_proto" )
181
96
. type_attribute ( ".google.protobuf.Any" , attrs_serde)
182
97
. type_attribute ( ".google.protobuf.Timestamp" , attrs_serde)
183
98
. type_attribute ( ".google.protobuf.Duration" , attrs_serde)
184
99
. type_attribute ( ".ibc.core.client.v1" , attrs_serde)
185
100
. type_attribute ( ".ibc.core.client.v1.Height" , attrs_ord)
186
101
. type_attribute ( ".ibc.core.client.v1.Height" , attrs_jsonschema)
187
102
. field_attribute ( ".ibc.core.client.v1.Height" , attrs_serde_default)
188
- . type_attribute ( ".ibc.core.commitment.v1" , attrs_serde)
189
- . type_attribute ( ".ibc.core.commitment.v1.MerkleRoot" , attrs_jsonschema)
190
- . field_attribute (
191
- ".ibc.core.commitment.v1.MerkleRoot.hash" ,
192
- attrs_serde_base64,
193
- )
194
- . field_attribute (
195
- ".ibc.core.commitment.v1.MerkleRoot.hash" ,
196
- attrs_jsonschema_str,
197
- )
198
- . type_attribute ( ".ibc.core.commitment.v1.MerklePrefix" , attrs_jsonschema)
199
- . field_attribute (
200
- ".ibc.core.commitment.v1.MerklePrefix.key_prefix" ,
201
- attrs_serde_base64,
202
- )
203
- . field_attribute (
204
- ".ibc.core.commitment.v1.MerklePrefix.key_prefix" ,
205
- attrs_jsonschema_str,
206
- )
207
- . type_attribute ( ".ibc.core.channel.v1" , attrs_serde)
208
- . type_attribute ( ".ibc.core.channel.v1.Channel" , attrs_jsonschema)
209
- . type_attribute ( ".ibc.core.channel.v1.Counterparty" , attrs_jsonschema)
210
- . type_attribute ( ".ibc.core.connection.v1" , attrs_serde)
211
- . type_attribute ( ".ibc.core.connection.v1.ConnectionEnd" , attrs_jsonschema)
212
- . type_attribute ( ".ibc.core.connection.v1.Counterparty" , attrs_jsonschema)
213
- . type_attribute ( ".ibc.core.connection.v1.Version" , attrs_jsonschema)
214
- . type_attribute ( ".ibc.core.types.v1" , attrs_serde)
215
- . type_attribute ( ".ibc.applications.transfer.v1" , attrs_serde)
216
- . type_attribute ( ".ibc.applications.transfer.v2" , attrs_serde)
217
- . type_attribute (
218
- ".ibc.applications.interchain_accounts.controller.v1" ,
219
- attrs_serde,
220
- )
221
- . type_attribute ( ".ics23" , attrs_serde)
222
- . type_attribute ( ".ics23.LeafOp" , attrs_eq)
223
- . type_attribute ( ".ics23.LeafOp" , attrs_jsonschema)
224
- . field_attribute ( ".ics23.LeafOp.prehash_key" , attrs_serde_default)
225
- . field_attribute ( ".ics23.LeafOp.prefix" , attrs_serde_base64)
226
- . field_attribute ( ".ics23.LeafOp.prefix" , attrs_jsonschema_str)
227
- . type_attribute ( ".ics23.InnerOp" , attrs_jsonschema)
228
- . field_attribute ( ".ics23.InnerOp.prefix" , attrs_serde_base64)
229
- . field_attribute ( ".ics23.InnerOp.prefix" , attrs_jsonschema_str)
230
- . field_attribute ( ".ics23.InnerOp.suffix" , attrs_serde_base64)
231
- . field_attribute ( ".ics23.InnerOp.suffix" , attrs_jsonschema_str)
232
- . type_attribute ( ".ics23.InnerOp" , attrs_eq)
233
- . type_attribute ( ".ics23.ProofSpec" , attrs_eq)
234
- . type_attribute ( ".ics23.ProofSpec" , attrs_jsonschema)
235
- . field_attribute ( ".ics23.ProofSpec.max_depth" , attrs_serde_default)
236
- . field_attribute ( ".ics23.ProofSpec.min_depth" , attrs_serde_default)
237
- . type_attribute ( ".ics23.InnerSpec" , attrs_eq)
238
- . type_attribute ( ".ics23.InnerSpec" , attrs_jsonschema)
239
- . field_attribute ( ".ics23.InnerSpec.empty_child" , attrs_serde_default)
240
- . field_attribute ( ".ics23.InnerSpec.empty_child" , attrs_serde_base64)
241
- . field_attribute ( ".ics23.InnerSpec.empty_child" , attrs_jsonschema_str)
103
+ . type_attribute ( ".lcp.service.enclave.v1" , attrs_serde)
104
+ . type_attribute ( ".lcp.service.elc.v1" , attrs_serde)
105
+ . file_descriptor_set_path ( descriptor_path)
242
106
. compile ( & protos, & includes) ;
243
107
244
108
match compilation {
@@ -251,86 +115,4 @@ impl CompileCmd {
251
115
}
252
116
}
253
117
}
254
-
255
- fn copy_generated_files ( from_dir_lcp : & Path , from_dir_ibc : & Path , to_dir : & Path ) {
256
- println ! (
257
- "[info ] Copying generated files into '{}'..." ,
258
- to_dir. display( )
259
- ) ;
260
-
261
- // Remove old compiled files
262
- remove_dir_all ( & to_dir) . unwrap_or_default ( ) ;
263
- create_dir_all ( & to_dir) . unwrap ( ) ;
264
-
265
- // Copy new compiled files (prost does not use folder structures)
266
- // Copy the SDK files first
267
- let errors_sdk = WalkDir :: new ( from_dir_lcp)
268
- . into_iter ( )
269
- . filter_map ( |e| e. ok ( ) )
270
- . filter ( |e| e. file_type ( ) . is_file ( ) )
271
- . map ( |e| {
272
- copy (
273
- e. path ( ) ,
274
- format ! (
275
- "{}/{}" ,
276
- to_dir. display( ) ,
277
- & e. file_name( ) . to_os_string( ) . to_str( ) . unwrap( )
278
- ) ,
279
- )
280
- } )
281
- . filter_map ( |e| e. err ( ) )
282
- . collect :: < Vec < _ > > ( ) ;
283
-
284
- if !errors_sdk. is_empty ( ) {
285
- for e in errors_sdk {
286
- println ! ( "[error] Error while copying SDK-compiled file: {}" , e) ;
287
- }
288
-
289
- panic ! ( "[error] Aborted." ) ;
290
- }
291
-
292
- // Copy the IBC-go files second, double-checking if anything is overwritten
293
- let errors_ibc = WalkDir :: new ( from_dir_ibc)
294
- . into_iter ( )
295
- . filter_map ( |e| e. ok ( ) )
296
- . filter ( |e| e. file_type ( ) . is_file ( ) )
297
- . map ( |e| {
298
- let generated_fname = e. file_name ( ) . to_owned ( ) . into_string ( ) . unwrap ( ) ;
299
- let prefix = & generated_fname[ 0 ..6 ] ;
300
-
301
- let target_fname = format ! (
302
- "{}/{}" ,
303
- to_dir. display( ) ,
304
- generated_fname,
305
- ) ;
306
-
307
- // If it's a cosmos-relevant file and it exists, we should not overwrite it.
308
- if Path :: new ( & target_fname) . exists ( ) && prefix. eq ( "cosmos" ) {
309
- let original_cosmos_file = std:: fs:: read ( target_fname. clone ( ) ) . unwrap ( ) ;
310
- let new_cosmos_file = std:: fs:: read ( e. path ( ) ) . unwrap ( ) ;
311
- if original_cosmos_file != new_cosmos_file {
312
- println ! (
313
- "[warn ] Cosmos-related file exists already {}! Ignoring the one generated from IBC-go {:?}" ,
314
- target_fname, e. path( )
315
- ) ;
316
- }
317
- Ok ( 0 )
318
- } else {
319
- copy (
320
- e. path ( ) ,
321
- target_fname,
322
- )
323
- }
324
- } )
325
- . filter_map ( |e| e. err ( ) )
326
- . collect :: < Vec < _ > > ( ) ;
327
-
328
- if !errors_ibc. is_empty ( ) {
329
- for e in errors_ibc {
330
- println ! ( "[error] Error while copying IBC-go compiled file: {}" , e) ;
331
- }
332
-
333
- panic ! ( "[error] Aborted." ) ;
334
- }
335
- }
336
118
}
0 commit comments