Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): Initial run command for WASM #19

Merged
merged 7 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/build-native.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jobs:
with:
toolchain: stable

- name: Install wasm-pack
run: npm install -g wasm-pack

- name: Install Dependencies
if: matrix.os == 'ubuntu-latest'
run: |
Expand All @@ -53,6 +56,12 @@ jobs:
run: cargo build
working-directory: runtime/functor-runtime-desktop

# Building the web bundle is necessary for the CLI,
# since the cli includes the web bundle for dev server
- name: Build runtime wasm bundle
run: wasm-pack build --target web
working-directory: runtime/functor-runtime-web

- name: Build CLI
run: cargo build
working-directory: cli
Expand Down
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ colored = "2.1.0"
serde_json = "1.0.117"
tokio = { version = "1", features = ["full"] }
libloading = "0.8.3"
warp = "0.3.7"
1 change: 1 addition & 0 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod build;
pub mod develop;
pub mod run;
21 changes: 21 additions & 0 deletions cli/src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use colored::*;
use std::env;
use std::io::{self, BufRead, Error};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command as TokioCommand;

use crate::util::WasmDevServer;
use crate::Environment;

pub async fn execute(working_directory: &str, environment: &Environment) -> Result<(), Error> {
let cwd_path = Path::new(working_directory);
let build_wasm_path = Path::new(&"build-wasm");
let build_wasm_wd = cwd_path.join(build_wasm_path);

match environment {
Environment::Native => panic!("not yet implemented"),
Environment::Wasm => WasmDevServer::start(build_wasm_wd.to_str().unwrap()).await,
}
}
7 changes: 7 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ enum Command {
#[arg(value_enum)]
environment: Option<Environment>,
},
Run {
#[arg(value_enum)]
environment: Option<Environment>,
},
Develop,
}

Expand Down Expand Up @@ -68,6 +72,9 @@ async fn main() -> tokio::io::Result<()> {
commands::build::execute(&working_directory_str, &Environment::default(environment))
.await
}
Command::Run { environment } => {
commands::run::execute(&working_directory_str, &Environment::default(environment)).await
}
Command::Develop => commands::develop::execute(&working_directory_str).await,
};
println!("Done");
Expand Down
3 changes: 3 additions & 0 deletions cli/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
mod shell_command;
pub use shell_command::*;

mod wasm_dev_server;
pub use wasm_dev_server::*;
53 changes: 53 additions & 0 deletions cli/src/util/wasm_dev_server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::io;

use warp::{http::Response, Filter};

pub struct WasmDevServer;

const INDEX_HTML: &[u8] = include_bytes!("../../../runtime/functor-runtime-web/index.html");
const WASM_FILE: &[u8] =
include_bytes!("../../../runtime/functor-runtime-web/pkg/functor_runtime_web_bg.wasm");
const JS_FILE_1: &[u8] =
include_bytes!("../../../runtime/functor-runtime-web/pkg/functor_runtime_web.js");

impl WasmDevServer {
pub async fn start(working_directory: &str) -> Result<(), io::Error> {
let wd = working_directory.to_owned();

println!("Starting dev server in: {}", wd);

// Define routes for each file
let route_index = warp::path::end()
.map(move || {
Response::builder()
.header("Content-Type", "text/html")
.body(INDEX_HTML.to_vec())
})
.boxed();

let route_js1 = warp::path!("pkg" / "functor_runtime_web.js")
.map(|| {
Response::builder()
.header("Content-Type", "application/javascript")
.body(JS_FILE_1.to_vec())
})
.boxed();
let route_wasm = warp::path!("pkg" / "functor_runtime_web_bg.wasm")
.map(|| {
Response::builder()
.header("Content-Type", "application/wasm")
.body(WASM_FILE.to_vec())
})
.boxed();

// Route to serve files from the specified working directory
let route_filesystem = warp::fs::dir(wd);

// Combine all routes
let static_routes = route_index.or(route_js1).or(route_wasm);
let routes = static_routes.or(route_filesystem);

warp::serve(routes).run(([127, 0, 0, 1], 8080)).await;
Ok(())
}
}
Loading