Skip to content

Commit

Permalink
More project build changes
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianReeves committed May 30, 2024
1 parent ec7e8c1 commit 82a5910
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 154 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ zig-cache/
zig-out/

# Build Artifacts
.out/
out/
dist/
target/
Expand Down
5 changes: 4 additions & 1 deletion .moon/toolchain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ bun:
node:
packageManager: bun

rust: {}
rust:
bins:
- 'cargo-make@0.37.12'
- 'cargo-nextest'
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ serde = "1.0.203"
serde_json = "1.0.117"
serde_v8 = "0.192.0"
tokio = "1.10.0"


30 changes: 30 additions & 0 deletions apps/cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "morphir"
version.workspace = true
description = "Morphir CLI"
homepage.workspace = true
keywords = ["morphir", "finos"]
authors.workspace = true
license.workspace = true
edition.workspace = true

[badges]
maintnance = { status = "actively-developer" }

[[bin]]
name = "morphir"
path = "src/main.rs"

[lib]
name = "morphir"
path = "src/lib.rs"

[features]

[dependencies]
#boa_engine.workspace = true
tokio.workspace = true

[build-dependencies]
#git-download = "0.1.1"
download_git = "0.0.2"
23 changes: 23 additions & 0 deletions apps/cli/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use download_git;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// download_git::download(
// "https://github.com/twbs/bootstrap.git:main",
// download_git::DownloadOptions {
// target_files: Some(vec!["dist".to_string(), "README.md".to_string()]),
// dest_path: String::from(TEST_FOLDER),
// },
// )?;
// git_download::repo("https://github.com/akiradeveloper/lol")
// // Tag name can be used.
// .branch_name("v0.9.1")
// // Can be saved in a different name.
// .add_file("lol-core/proto/lol-core.proto", "proto/lol.proto")
// .exec()?;

// tonic_build::configure()
// .build_server(false)
// .compile(&["lol.proto"], &["proto"])?;

Ok(())
}
Empty file added apps/cli/src/lib.rs
Empty file.
4 changes: 4 additions & 0 deletions apps/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[tokio::main]
async fn main() -> MainResult {
Ok(())
}
3 changes: 2 additions & 1 deletion crates/morphir-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ path = "src/lib.rs"

[dependencies]
boa_engine.workspace = true
tokio.workspace = true

[build-dependencies]
deno_core.workspace = true
deno_core.workspace = true
152 changes: 2 additions & 150 deletions crates/morphir-runtime/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,151 +1,3 @@
use std::env;
use deno_ast::MediaType;
use deno_ast::ParseParams;
use deno_ast::SourceTextInfo;
use deno_core::error::AnyError;
use deno_core::futures::FutureExt;
use deno_core::op2;
use deno_core::Extension;
use deno_core::Snapshot;
use std::rc::Rc;

#[op2(async)]
#[string]
async fn op_read_file(#[string] path: String) -> Result<String, AnyError> {
let contents = tokio::fs::read_to_string(path).await?;
Ok(contents)
}

#[op2(async)]
#[string]
async fn op_write_file(#[string] path: String, #[string] contents: String) -> Result<(), AnyError> {
tokio::fs::write(path, contents).await?;
Ok(())
}

#[op2(async)]
#[string]
async fn op_fetch(#[string] url: String) -> Result<String, AnyError> {
let body = reqwest::get(url).await?.text().await?;
Ok(body)
}

#[op2(async)]
async fn op_set_timeout(delay: f64) -> Result<(), AnyError> {
tokio::time::sleep(std::time::Duration::from_millis(delay as u64)).await;
Ok(())
}

#[op2(fast)]
fn op_remove_file(#[string] path: String) -> Result<(), AnyError> {
std::fs::remove_file(path)?;
Ok(())
}

struct TsModuleLoader;

impl deno_core::ModuleLoader for TsModuleLoader {
fn resolve(
&self,
specifier: &str,
referrer: &str,
_kind: deno_core::ResolutionKind,
) -> Result<deno_core::ModuleSpecifier, AnyError> {
deno_core::resolve_import(specifier, referrer).map_err(|e| e.into())
}

fn load(
&self,
module_specifier: &deno_core::ModuleSpecifier,
_maybe_referrer: Option<&reqwest::Url>,
_is_dyn_import: bool,
) -> std::pin::Pin<Box<deno_core::ModuleSourceFuture>> {
let module_specifier = module_specifier.clone();
async move {
let path = module_specifier.to_file_path().unwrap();

let media_type = MediaType::from_path(&path);
let (module_type, should_transpile) = match MediaType::from_path(&path) {
MediaType::JavaScript | MediaType::Mjs | MediaType::Cjs => {
(deno_core::ModuleType::JavaScript, false)
}
MediaType::Jsx => (deno_core::ModuleType::JavaScript, true),
MediaType::TypeScript
| MediaType::Mts
| MediaType::Cts
| MediaType::Dts
| MediaType::Dmts
| MediaType::Dcts
| MediaType::Tsx => (deno_core::ModuleType::JavaScript, true),
MediaType::Json => (deno_core::ModuleType::Json, false),
_ => panic!("Unknown extension {:?}", path.extension()),
};

let code = std::fs::read_to_string(&path)?;
let code = if should_transpile {
let parsed = deno_ast::parse_module(ParseParams {
specifier: module_specifier.to_string(),
text_info: SourceTextInfo::from_string(code),
media_type,
capture_tokens: false,
scope_analysis: false,
maybe_syntax: None,
})?;
parsed.transpile(&Default::default())?.text
} else {
code
};
let module = deno_core::ModuleSource::new(
module_type,
deno_core::ModuleCode::from(code),
&module_specifier
);
Ok(module)
}
.boxed_local()
}
}

static RUNTIME_SNAPSHOT: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/MORPHIRJS_SNAPSHOT.bin"));

async fn run_js(file_path: &str) -> Result<(), AnyError> {
let main_module = deno_core::resolve_path(file_path, env::current_dir()?.as_path())?;
let runjs_extension = Extension::builder("morphir")
.ops(vec![
op_read_file::decl(),
op_write_file::decl(),
op_remove_file::decl(),
op_fetch::decl(),
op_set_timeout::decl(),
])
.build();
let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
module_loader: Some(Rc::new(TsModuleLoader)),
startup_snapshot: Some(Snapshot::Static(RUNTIME_SNAPSHOT)),
extensions: vec![runjs_extension],
..Default::default()
});

let mod_id = js_runtime.load_main_module(&main_module, None).await?;
let result = js_runtime.mod_evaluate(mod_id);
js_runtime.run_event_loop(false).await?;
result.await?
}

fn main() {
let args = &env::args().collect::<Vec<String>>()[1..];

if args.is_empty() {
eprintln!("Usage: morphirx <file>");
std::process::exit(1);
}
let file_path = &args[0];

let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
if let Err(error) = runtime.block_on(run_js(file_path)) {
eprintln!("error: {error}");
}
}
println!("Hello, world!");
}
85 changes: 84 additions & 1 deletion moon.yml
Original file line number Diff line number Diff line change
@@ -1 +1,84 @@
language: elm
language: 'rust'
type: 'application'
tags:
- 'elm'

env:
CARGO_TERM_COLOR: 'always'

fileGroups:
elm-sources:
- 'src/**/*.elm'
sources:
- 'apps/cli/src/**/*'
- 'crates/*/src/**/*'
- 'crates/*/Cargo.toml'
- 'Cargo.toml'
tests:
- 'crates/*/benches/**/*'
- 'crates/*/tests/**/*'

tasks:
build:
command: 'cargo build'
inputs:
- '@globs(sources)'
check:
command: 'cargo check --workspace'
inputs:
- '@globs(sources)'
format:
command: 'cargo fmt --all --check'
inputs:
- '@globs(sources)'
- '@globs(tests)'
lint:
command: 'cargo clippy --workspace'
inputs:
- '@globs(sources)'
- '@globs(tests)'
test:
command: 'cargo test --workspace'
inputs:
- '@globs(sources)'
- '@globs(tests)'
ensure-elm-docs-dir:
command: 'mkdir -p @out(0)'
outputs:
- '.out/elm-out/finos/morphir'
check-elm-docs:
command: 'elm make --docs=./.out/elm-out/finos/morphir/docs.json'
inputs:
- '@globs(elm-sources)'
outputs:
- '.out/elm-out/finos/morphir/docs.json'
deps:
- target: "~:ensure-elm-docs-dir"
cargo-clean:
inputs: []
command: 'cargo clean'
elm-clean-outputs:
inputs:
- '.out/elm-out'
command: 'rm -rf @in(0)'
clean:
inputs: []
deps:
- "~:cargo-clean"
- "~:elm-clean-outputs"
purge:
inputs: []
deps:
- "~:clean"
- "~:purge-targets"
- "~:purge-elm-dependencies"
- "~:purge-node-dependencies"
purge-targets:
inputs: []
command: 'rm -rf target'
purge-elm-dependencies:
inputs: []
command: 'rm -rf elm-stuff'
purge-node-dependencies:
inputs: []
command: 'rm -rf node_modules'
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
],
"scripts": {
"build": "moon run :build",
"clean": "moon run clean",
"pruge": "moon run pruge",
"build:verify-elm-package": "zig build verify-elm-package --summary all",
"build:check-elm-docs": "zig build check-elm-docs --summary all",
"check-elm-docs": "moon run check-elm-docs",
"postinstall": "elm-tooling install",
"list-projects": "moon project-graph --json | bunx --bun node-jq '.projects| keys[]'"
},
Expand Down

0 comments on commit 82a5910

Please sign in to comment.