Skip to content

Commit 8156bf7

Browse files
committed
refactor(cli/sdk): move all preps to sdk (wip)
1 parent 371d257 commit 8156bf7

File tree

6 files changed

+8
-196
lines changed

6 files changed

+8
-196
lines changed

meta-cli/src/cli/codegen.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ impl Action for Deno {
5757
);
5858

5959
let loader_pool = LoaderPool::new(config, 1)
60-
.with_postprocessor(postprocess::DenoModules::default().codegen(true))
61-
.with_postprocessor(postprocess::PythonModules::default())
62-
.with_postprocessor(postprocess::WasmdegeModules::default());
60+
.with_postprocessor(postprocess::DenoModules::default().codegen(true));
6361

6462
let loader = loader_pool.get_loader().await?;
6563

meta-cli/src/deploy/actors/loader.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tokio::sync::{mpsc, oneshot};
1313

1414
use crate::config::Config;
1515
use crate::typegraph::loader::LoaderPool;
16-
use crate::typegraph::postprocess::{self, DenoModules, EmbedPrismaMigrations};
16+
use crate::typegraph::postprocess::{DenoModules, EmbedPrismaMigrations};
1717
use crate::utils::plural_suffix;
1818

1919
use super::console::{Console, ConsoleActor};
@@ -120,10 +120,6 @@ impl LoaderActor {
120120
pool = pool.with_postprocessor(deno.clone());
121121
}
122122

123-
pool = pool
124-
.with_postprocessor(postprocess::PythonModules::default())
125-
.with_postprocessor(postprocess::WasmdegeModules::default());
126-
127123
if let Some(prisma) = &postprocess_options.prisma {
128124
pool = pool.with_postprocessor(
129125
prisma

meta-cli/src/typegraph/loader/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ impl LoaderPool {
4242
pub fn new(config: Arc<Config>, max_parallel_loads: usize) -> Self {
4343
Self {
4444
config,
45-
postprocessors: vec![
46-
postprocess::Validator.into(),
47-
postprocess::ReformatScripts.into(),
48-
],
45+
postprocessors: vec![postprocess::Validator.into()],
4946
semaphore: Semaphore::new(max_parallel_loads),
5047
}
5148
}

meta-cli/src/typegraph/postprocess.rs

Lines changed: 1 addition & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,11 @@ use std::sync::{Arc, RwLock};
55

66
use crate::config::Config;
77

8-
use anyhow::Context;
98
use anyhow::{bail, Result};
109
use colored::Colorize;
11-
use common::archive::archive_entries;
12-
use common::typegraph::utils::{map_from_object, object_from_map};
1310
use common::typegraph::validator::validate_typegraph;
14-
use common::typegraph::{Materializer, Typegraph};
15-
use ignore::WalkBuilder;
11+
use common::typegraph::Typegraph;
1612
use log::error;
17-
use std::path::Path;
18-
use typescript::parser::transform_script;
1913

2014
pub trait PostProcessor {
2115
fn postprocess(&self, tg: &mut Typegraph, config: &Config) -> Result<()>;
@@ -35,14 +29,6 @@ where
3529
#[derive(Clone)]
3630
pub struct PostProcessorWrapper(Arc<RwLock<Box<dyn PostProcessor + Sync + Send>>>);
3731

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-
4632
impl<T> From<T> for PostProcessorWrapper
4733
where
4834
T: PostProcessor + Send + Sync + 'static,
@@ -63,60 +49,9 @@ pub fn apply_all<'a>(
6349
Ok(())
6450
}
6551

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-
11452
pub use deno_rt::DenoModules;
115-
pub use deno_rt::ReformatScripts;
11653
pub use prisma_rt::EmbedPrismaMigrations;
11754
pub use prisma_rt::EmbeddedPrismaMigrationOptionsPatch;
118-
pub use python_rt::PythonModules;
119-
pub use wasmedge_rt::WasmdegeModules;
12055

12156
pub struct Validator;
12257
impl PostProcessor for Validator {
@@ -139,46 +74,8 @@ impl PostProcessor for Validator {
13974
}
14075

14176
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-
14977
use super::*;
15078

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-
18279
#[derive(Default, Debug, Clone)]
18380
pub struct DenoModules {
18481
codegen: bool,
@@ -196,82 +93,6 @@ pub mod deno_rt {
19693
if self.codegen {
19794
crate::codegen::deno::codegen(tg, tg.path.as_ref().unwrap())?;
19895
}
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-
}
27596
Ok(())
27697
}
27798
}

typegraph/node/sdk/src/typegraph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export function typegraph(
169169
builder(g);
170170

171171
const tgJson = core.finalizeTypegraph(
172-
disableAutoSerialization ? "resolve-artifacts" : "simple",
172+
/*disableAutoSerialization ?*/ "resolve-artifacts", /* : "simple"*/
173173
);
174174
if (!disableAutoSerialization) {
175175
console.log(tgJson);

typegraph/python/typegraph/graph/typegraph.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ def decorator(builder: Callable[[Graph], None]) -> TypegraphOutput:
172172

173173
tg_json = core.finalize_typegraph(
174174
store,
175-
TypegraphFinalizeMode.RESOLVE_ARTIFACTS
176-
if disable_auto_serialization
177-
else TypegraphFinalizeMode.SIMPLE,
175+
TypegraphFinalizeMode.RESOLVE_ARTIFACTS,
176+
# if disable_auto_serialization
177+
# else TypegraphFinalizeMode.SIMPLE,
178178
)
179179

180180
if isinstance(tg_json, Err):

0 commit comments

Comments
 (0)