diff options
-rw-r--r-- | cli/build.rs | 47 | ||||
-rw-r--r-- | core/build_util.rs | 44 | ||||
-rw-r--r-- | core/lib.rs | 1 | ||||
-rw-r--r-- | runtime/build.rs | 68 |
4 files changed, 100 insertions, 60 deletions
diff --git a/cli/build.rs b/cli/build.rs index 84c74683d..45221281f 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -1,7 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -use deno_core::build_util::create_snapshot; -use deno_core::build_util::get_js_files; use deno_core::error::custom_error; use deno_core::json_op_sync; use deno_core::serde_json; @@ -15,6 +13,35 @@ use std::env; use std::path::Path; use std::path::PathBuf; +// TODO(bartlomieju): this module contains a lot of duplicated +// logic with `runtime/build.rs`, factor out to `deno_core`. +fn create_snapshot( + mut js_runtime: JsRuntime, + snapshot_path: &Path, + files: Vec<PathBuf>, +) { + // TODO(nayeemrmn): https://github.com/rust-lang/cargo/issues/3946 to get the + // workspace root. + let display_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap(); + for file in files { + println!("cargo:rerun-if-changed={}", file.display()); + let display_path = file.strip_prefix(display_root).unwrap(); + let display_path_str = display_path.display().to_string(); + js_runtime + .execute( + &("deno:".to_string() + &display_path_str.replace('\\', "/")), + &std::fs::read_to_string(&file).unwrap(), + ) + .unwrap(); + } + + let snapshot = js_runtime.snapshot(); + let snapshot_slice: &[u8] = &*snapshot; + println!("Snapshot size: {}", snapshot_slice.len()); + std::fs::write(&snapshot_path, snapshot_slice).unwrap(); + println!("Snapshot written to: {} ", snapshot_path.display()); +} + #[derive(Debug, Deserialize)] struct LoadArgs { /// The fully qualified specifier that should be loaded. @@ -231,7 +258,7 @@ fn main() { // Main snapshot let compiler_snapshot_path = o.join("COMPILER_SNAPSHOT.bin"); - let js_files = get_js_files(&c.join("tsc")); + let js_files = get_js_files("tsc"); create_compiler_snapshot(&compiler_snapshot_path, js_files, &c); #[cfg(target_os = "windows")] @@ -245,3 +272,17 @@ fn main() { res.compile().unwrap(); } } + +fn get_js_files(d: &str) -> Vec<PathBuf> { + let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); + let mut js_files = std::fs::read_dir(d) + .unwrap() + .map(|dir_entry| { + let file = dir_entry.unwrap(); + manifest_dir.join(file.path()) + }) + .filter(|path| path.extension().unwrap_or_default() == "js") + .collect::<Vec<PathBuf>>(); + js_files.sort(); + js_files +} diff --git a/core/build_util.rs b/core/build_util.rs deleted file mode 100644 index 04a0f870b..000000000 --- a/core/build_util.rs +++ /dev/null @@ -1,44 +0,0 @@ -//! Helper module used in cli/build.rs and runtime/build.rs -use crate::JsRuntime; -use std::path::Path; -use std::path::PathBuf; - -pub fn create_snapshot( - mut js_runtime: JsRuntime, - snapshot_path: &Path, - files: Vec<PathBuf>, -) { - // TODO(nayeemrmn): https://github.com/rust-lang/cargo/issues/3946 to get the - // workspace root. - let display_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap(); - for file in files { - println!("cargo:rerun-if-changed={}", file.display()); - let display_path = file.strip_prefix(display_root).unwrap(); - let display_path_str = display_path.display().to_string(); - js_runtime - .execute( - &("deno:".to_string() + &display_path_str.replace('\\', "/")), - &std::fs::read_to_string(&file).unwrap(), - ) - .unwrap(); - } - - let snapshot = js_runtime.snapshot(); - let snapshot_slice: &[u8] = &*snapshot; - println!("Snapshot size: {}", snapshot_slice.len()); - std::fs::write(&snapshot_path, snapshot_slice).unwrap(); - println!("Snapshot written to: {} ", snapshot_path.display()); -} - -pub fn get_js_files(d: &Path) -> Vec<PathBuf> { - let mut js_files = std::fs::read_dir(d) - .unwrap() - .map(|dir_entry| { - let file = dir_entry.unwrap(); - d.join(file.path()) - }) - .filter(|path| path.extension().unwrap_or_default() == "js") - .collect::<Vec<PathBuf>>(); - js_files.sort(); - js_files -} diff --git a/core/lib.rs b/core/lib.rs index 0f7c23c26..5846ad99d 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -8,7 +8,6 @@ extern crate log; mod async_cancel; mod async_cell; mod bindings; -pub mod build_util; pub mod error; mod flags; mod gotham_state; diff --git a/runtime/build.rs b/runtime/build.rs index 3b8be4136..78f17f61f 100644 --- a/runtime/build.rs +++ b/runtime/build.rs @@ -1,12 +1,51 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -use deno_core::build_util::create_snapshot; -use deno_core::build_util::get_js_files; use deno_core::JsRuntime; use deno_core::RuntimeOptions; use std::env; +use std::path::Path; use std::path::PathBuf; +// TODO(bartlomieju): this module contains a lot of duplicated +// logic with `cli/build.rs`, factor out to `deno_core`. +fn create_snapshot( + mut js_runtime: JsRuntime, + snapshot_path: &Path, + files: Vec<PathBuf>, +) { + deno_web::init(&mut js_runtime); + deno_fetch::init(&mut js_runtime); + deno_crypto::init(&mut js_runtime); + // TODO(nayeemrmn): https://github.com/rust-lang/cargo/issues/3946 to get the + // workspace root. + let display_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap(); + for file in files { + println!("cargo:rerun-if-changed={}", file.display()); + let display_path = file.strip_prefix(display_root).unwrap(); + let display_path_str = display_path.display().to_string(); + js_runtime + .execute( + &("deno:".to_string() + &display_path_str.replace('\\', "/")), + &std::fs::read_to_string(&file).unwrap(), + ) + .unwrap(); + } + + let snapshot = js_runtime.snapshot(); + let snapshot_slice: &[u8] = &*snapshot; + println!("Snapshot size: {}", snapshot_slice.len()); + std::fs::write(&snapshot_path, snapshot_slice).unwrap(); + println!("Snapshot written to: {} ", snapshot_path.display()); +} + +fn create_runtime_snapshot(snapshot_path: &Path, files: Vec<PathBuf>) { + let js_runtime = JsRuntime::new(RuntimeOptions { + will_snapshot: true, + ..Default::default() + }); + create_snapshot(js_runtime, snapshot_path, files); +} + fn main() { // Skip building from docs.rs. if env::var_os("DOCS_RS").is_some() { @@ -23,15 +62,20 @@ fn main() { // Main snapshot let runtime_snapshot_path = o.join("CLI_SNAPSHOT.bin"); - let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); - let js_files = get_js_files(&c.join("rt")); + let js_files = get_js_files("rt"); + create_runtime_snapshot(&runtime_snapshot_path, js_files); +} - let mut js_runtime = JsRuntime::new(RuntimeOptions { - will_snapshot: true, - ..Default::default() - }); - deno_web::init(&mut js_runtime); - deno_fetch::init(&mut js_runtime); - deno_crypto::init(&mut js_runtime); - create_snapshot(js_runtime, &runtime_snapshot_path, js_files); +fn get_js_files(d: &str) -> Vec<PathBuf> { + let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); + let mut js_files = std::fs::read_dir(d) + .unwrap() + .map(|dir_entry| { + let file = dir_entry.unwrap(); + manifest_dir.join(file.path()) + }) + .filter(|path| path.extension().unwrap_or_default() == "js") + .collect::<Vec<PathBuf>>(); + js_files.sort(); + js_files } |