Skip to content

Commit

Permalink
Merge pull request #27 from fluentci-io/feat/cache-remote-wasm-modules
Browse files Browse the repository at this point in the history
feat: cache remote wasm modules
  • Loading branch information
tsirysndr authored May 14, 2024
2 parents 1a697c1 + 03dc5c6 commit 1195434
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MPL-2.0"
name = "fluentci-engine"
readme = "../../README.md"
repository = "https://github.com/fluentci-io/fluentci-engine"
version = "0.4.0"
version = "0.4.1"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -23,4 +23,5 @@ fluentci-shared = {path = "../shared", version = "0.2.0"}
get-port = "4.0.0"
md5 = "0.7.0"
regex = "1.10.3"
sha256 = "1.5.0"
tokio = {version = "1.36.0", features = ["tokio-macros", "macros", "rt", "rt-multi-thread"]}
40 changes: 36 additions & 4 deletions crates/cli/src/cmd/call.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use std::sync::{mpsc, Arc, Mutex};
use std::{
fs,
process::{Command, Stdio},
sync::{mpsc, Arc, Mutex},
};

use anyhow::Error;
use extism::{Manifest, PluginBuilder, UserData, Wasm, PTR};
use fluentci_core::deps::Graph;
use fluentci_ext::runner::Runner;
use fluentci_ext::{pkgx::Pkgx, runner::Runner, Extension};
use fluentci_shared::{
cache::*,
devbox::*,
Expand All @@ -23,7 +28,7 @@ use fluentci_shared::{
state::State,
};

pub fn call(module: &str, command: &str) {
pub fn call(module: &str, command: &str) -> Result<(), Error> {
match fluentci_core::init_tracer() {
Ok(_) => {}
Err(e) => {
Expand All @@ -48,7 +53,7 @@ pub fn call(module: &str, command: &str) {
});

let module = match module.starts_with("http") {
true => Wasm::url(module),
true => download_module(module)?,
false => Wasm::file(module),
};

Expand Down Expand Up @@ -134,5 +139,32 @@ pub fn call(module: &str, command: &str) {
println!("{}", err);
std::process::exit(1);
}
};
Ok(())
}

pub fn download_module(url: &str) -> Result<Wasm, Error> {
let filename = format!("{}.wasm", sha256::digest(url).to_string());
let work_dir = format!("{}/.fluentci/cache", std::env::var("HOME").unwrap());

if fs::metadata(format!("{}/{}", work_dir, filename)).is_ok() {
return Ok(Wasm::file(format!("{}/{}", work_dir, filename)));
}

Pkgx::default().setup()?;

let cmd = format!("pkgx curl -s {} -o {}", url, filename);
fs::create_dir_all(&work_dir)?;

let mut child = Command::new("bash")
.arg("-c")
.arg(cmd)
.current_dir(&work_dir)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()?;

child.wait()?;

Ok(Wasm::file(format!("{}/{}", work_dir, filename)))
}
2 changes: 1 addition & 1 deletion crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async fn main() -> Result<(), Error> {
let module = args.value_of("module").unwrap();
let command = args.values_of("command").unwrap();
let command = command.collect::<Vec<_>>().join(" ");
call(module, &command);
call(module, &command)?;
}
_ => cli().print_help()?,
};
Expand Down

0 comments on commit 1195434

Please sign in to comment.