@@ -5,17 +5,11 @@ use std::sync::{Arc, RwLock};
5
5
6
6
use crate :: config:: Config ;
7
7
8
- use anyhow:: Context ;
9
8
use anyhow:: { bail, Result } ;
10
9
use colored:: Colorize ;
11
- use common:: archive:: archive_entries;
12
- use common:: typegraph:: utils:: { map_from_object, object_from_map} ;
13
10
use common:: typegraph:: validator:: validate_typegraph;
14
- use common:: typegraph:: { Materializer , Typegraph } ;
15
- use ignore:: WalkBuilder ;
11
+ use common:: typegraph:: Typegraph ;
16
12
use log:: error;
17
- use std:: path:: Path ;
18
- use typescript:: parser:: transform_script;
19
13
20
14
pub trait PostProcessor {
21
15
fn postprocess ( & self , tg : & mut Typegraph , config : & Config ) -> Result < ( ) > ;
35
29
#[ derive( Clone ) ]
36
30
pub struct PostProcessorWrapper ( Arc < RwLock < Box < dyn PostProcessor + Sync + Send > > > ) ;
37
31
38
- impl PostProcessorWrapper {
39
- pub fn generic (
40
- pp : impl Fn ( & mut Typegraph , & Config ) -> Result < ( ) > + Sync + Send + ' static ,
41
- ) -> Self {
42
- PostProcessorWrapper :: from ( GenericPostProcessor ( pp) )
43
- }
44
- }
45
-
46
32
impl < T > From < T > for PostProcessorWrapper
47
33
where
48
34
T : PostProcessor + Send + Sync + ' static ,
@@ -63,60 +49,9 @@ pub fn apply_all<'a>(
63
49
Ok ( ( ) )
64
50
}
65
51
66
- fn compress_and_encode ( main_path : & Path , tg_path : & Path ) -> Result < String > {
67
- // Note: tg_path and main_path are all absolute
68
- // tg_root/tg.py
69
- // tg_root/* <= script location
70
- if main_path. is_relative ( ) {
71
- bail ! (
72
- "script path {:?} is relative, absolute expected" ,
73
- main_path. display( )
74
- ) ;
75
- }
76
-
77
- if tg_path. is_relative ( ) {
78
- bail ! (
79
- "typegraph path {:?} is relative, absolute expected" ,
80
- tg_path. display( )
81
- ) ;
82
- }
83
-
84
- let tg_root = tg_path. parent ( ) . with_context ( || {
85
- format ! (
86
- "invalid state: typegraph path {:?} does not have parent" ,
87
- tg_path. display( )
88
- )
89
- } ) ?;
90
-
91
- let dir_walker = WalkBuilder :: new ( tg_root)
92
- . standard_filters ( true )
93
- // .add_custom_ignore_filename(".DStore")
94
- . sort_by_file_path ( |a, b| a. cmp ( b) )
95
- . build ( ) ;
96
-
97
- let enc_content = match archive_entries ( dir_walker, Some ( tg_root) ) . unwrap ( ) {
98
- Some ( b64) => b64,
99
- None => "" . to_string ( ) ,
100
- } ;
101
-
102
- let file = match main_path. strip_prefix ( tg_root) {
103
- Ok ( ret) => ret,
104
- Err ( _) => bail ! (
105
- "{:?} does not contain script {:?}" ,
106
- tg_root. display( ) ,
107
- main_path. display( ) ,
108
- ) ,
109
- } ;
110
-
111
- Ok ( format ! ( "file:{};base64:{}" , file. display( ) , enc_content) )
112
- }
113
-
114
52
pub use deno_rt:: DenoModules ;
115
- pub use deno_rt:: ReformatScripts ;
116
53
pub use prisma_rt:: EmbedPrismaMigrations ;
117
54
pub use prisma_rt:: EmbeddedPrismaMigrationOptionsPatch ;
118
- pub use python_rt:: PythonModules ;
119
- pub use wasmedge_rt:: WasmdegeModules ;
120
55
121
56
pub struct Validator ;
122
57
impl PostProcessor for Validator {
@@ -139,46 +74,8 @@ impl PostProcessor for Validator {
139
74
}
140
75
141
76
pub mod deno_rt {
142
- use std:: fs;
143
-
144
- use common:: typegraph:: runtimes:: deno:: { FunctionMatData , ModuleMatData } ;
145
- use common:: typegraph:: runtimes:: { KnownRuntime , TGRuntime } ;
146
-
147
- use common:: typegraph:: utils:: { find_runtimes, get_materializers} ;
148
-
149
77
use super :: * ;
150
78
151
- pub struct ReformatScripts ;
152
-
153
- impl From < ReformatScripts > for PostProcessorWrapper {
154
- fn from ( _val : ReformatScripts ) -> Self {
155
- PostProcessorWrapper :: generic ( reformat_scripts)
156
- }
157
- }
158
-
159
- fn reformat_materializer_script ( mat : & mut Materializer ) -> Result < ( ) > {
160
- if mat. name . as_str ( ) == "function" {
161
- let mut mat_data: FunctionMatData = object_from_map ( std:: mem:: take ( & mut mat. data ) ) ?;
162
- // TODO check variable `_my_lambda` exists and is a function expression/lambda
163
- mat_data. script = transform_script ( mat_data. script ) ?;
164
- mat. data = map_from_object ( mat_data) ?;
165
- }
166
- Ok ( ( ) )
167
- }
168
-
169
- fn reformat_scripts ( typegraph : & mut Typegraph , _c : & Config ) -> Result < ( ) > {
170
- for rt_idx in find_runtimes ( typegraph, |rt| {
171
- matches ! ( rt, TGRuntime :: Known ( KnownRuntime :: Deno ( _) ) )
172
- } )
173
- . into_iter ( )
174
- {
175
- for mat_idx in get_materializers ( typegraph, rt_idx as u32 ) {
176
- reformat_materializer_script ( & mut typegraph. materializers [ mat_idx] ) ?;
177
- }
178
- }
179
- Ok ( ( ) )
180
- }
181
-
182
79
#[ derive( Default , Debug , Clone ) ]
183
80
pub struct DenoModules {
184
81
codegen : bool ,
@@ -196,82 +93,6 @@ pub mod deno_rt {
196
93
if self . codegen {
197
94
crate :: codegen:: deno:: codegen ( tg, tg. path . as_ref ( ) . unwrap ( ) ) ?;
198
95
}
199
- for mat in tg. materializers . iter_mut ( ) . filter ( |m| m. name == "module" ) {
200
- let mut mat_data: ModuleMatData = object_from_map ( std:: mem:: take ( & mut mat. data ) ) ?;
201
- log:: debug!( "processing module {:?}" , mat_data. code) ;
202
- let Some ( path) = mat_data. code . strip_prefix ( "file:" ) else {
203
- continue ;
204
- } ;
205
-
206
- // make sure tg_path is absolute
207
- let tg_path = fs:: canonicalize ( tg. path . to_owned ( ) . unwrap ( ) ) . unwrap ( ) ;
208
- let main_path = tg_path. parent ( ) . unwrap ( ) . join ( path) ;
209
- mat_data. code = compress_and_encode ( & main_path, & tg_path) ?;
210
- log:: debug!( "compressed module {:?}" , mat_data. code) ;
211
-
212
- mat. data = map_from_object ( mat_data) ?;
213
- tg. deps . push ( main_path) ;
214
- }
215
- Ok ( ( ) )
216
- }
217
- }
218
- }
219
-
220
- pub mod python_rt {
221
- use super :: * ;
222
- use common:: typegraph:: runtimes:: python:: ModuleMatData ;
223
- use std:: fs;
224
-
225
- #[ derive( Default , Debug ) ]
226
- pub struct PythonModules { }
227
-
228
- impl PostProcessor for PythonModules {
229
- fn postprocess ( & self , tg : & mut Typegraph , _config : & Config ) -> Result < ( ) > {
230
- for mat in tg. materializers . iter_mut ( ) . filter ( |m| m. name == "pymodule" ) {
231
- let mut mat_data: ModuleMatData = object_from_map ( std:: mem:: take ( & mut mat. data ) ) ?;
232
- let path = mat_data
233
- . code
234
- . strip_prefix ( "file:" )
235
- . context ( "\" file:\" prefix is not present" ) ?;
236
-
237
- // make sure tg_path is absolute
238
- let tg_path = fs:: canonicalize ( tg. path . to_owned ( ) . unwrap ( ) ) . unwrap ( ) ;
239
- let main_path = tg_path. parent ( ) . unwrap ( ) . join ( path) ;
240
- mat_data. code = compress_and_encode ( & main_path, & tg_path) ?;
241
-
242
- mat. data = map_from_object ( mat_data) ?;
243
- tg. deps . push ( main_path) ;
244
- }
245
- Ok ( ( ) )
246
- }
247
- }
248
- }
249
-
250
- pub mod wasmedge_rt {
251
- use super :: * ;
252
- use common:: { archive:: encode_to_base_64, typegraph:: runtimes:: wasmedge:: WasiMatData } ;
253
- use std:: fs;
254
-
255
- #[ derive( Default , Debug ) ]
256
- pub struct WasmdegeModules { }
257
-
258
- impl PostProcessor for WasmdegeModules {
259
- fn postprocess ( & self , tg : & mut Typegraph , _config : & Config ) -> Result < ( ) > {
260
- for mat in tg. materializers . iter_mut ( ) . filter ( |m| m. name == "wasi" ) {
261
- let mut mat_data: WasiMatData = object_from_map ( std:: mem:: take ( & mut mat. data ) ) ?;
262
- let path = mat_data
263
- . wasm
264
- . strip_prefix ( "file:" )
265
- . context ( "\" file:\" prefix is not present" ) ?;
266
-
267
- // make sure tg_path is absolute
268
- let tg_path = fs:: canonicalize ( tg. path . to_owned ( ) . unwrap ( ) ) . unwrap ( ) ;
269
- let wasi_path = tg_path. parent ( ) . unwrap ( ) . join ( path) ;
270
- mat_data. wasm = encode_to_base_64 ( & wasi_path) ?;
271
-
272
- mat. data = map_from_object ( mat_data) ?;
273
- tg. deps . push ( wasi_path) ;
274
- }
275
96
Ok ( ( ) )
276
97
}
277
98
}
0 commit comments