1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 16:42:21 -05:00

build: allow deno_runtime crate to build in docs.rs (#11602)

This commit is contained in:
Bartek Iwańczuk 2021-08-11 18:01:30 +02:00 committed by GitHub
parent cd323612fd
commit 57aea228e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 113 additions and 91 deletions

2
Cargo.lock generated
View file

@ -818,7 +818,7 @@ dependencies = [
[[package]] [[package]]
name = "deno_runtime" name = "deno_runtime"
version = "0.22.0" version = "0.22.2"
dependencies = [ dependencies = [
"atty", "atty",
"deno_broadcast_channel", "deno_broadcast_channel",

View file

@ -2,13 +2,17 @@
[package] [package]
name = "deno_runtime" name = "deno_runtime"
version = "0.22.0" version = "0.22.2"
authors = ["the Deno authors"] authors = ["the Deno authors"]
edition = "2018" edition = "2018"
license = "MIT" license = "MIT"
repository = "https://github.com/denoland/deno" repository = "https://github.com/denoland/deno"
description = "Provides the deno runtime library" description = "Provides the deno runtime library"
[features]
# "fake" feature that allows to generate docs on docs.rs
docsrs = []
[lib] [lib]
name = "deno_runtime" name = "deno_runtime"
path = "lib.rs" path = "lib.rs"
@ -88,3 +92,6 @@ nix = "0.20.0"
[dev-dependencies] [dev-dependencies]
# Used in benchmark # Used in benchmark
test_util = { path = "../test_util" } test_util = { path = "../test_util" }
[package.metadata.docs.rs]
features = ["docsrs"]

View file

@ -1,90 +1,110 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use deno_core::Extension;
use deno_core::JsRuntime;
use deno_core::RuntimeOptions;
use std::env; use std::env;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
// TODO(bartlomieju): this module contains a lot of duplicated // This is a shim that allows to generate documentation on docs.rs
// logic with `cli/build.rs`, factor out to `deno_core`. #[cfg(not(feature = "docsrs"))]
fn create_snapshot( mod not_docs {
mut js_runtime: JsRuntime, use super::*;
snapshot_path: &Path, use deno_core::Extension;
files: Vec<PathBuf>, use deno_core::JsRuntime;
) { use deno_core::RuntimeOptions;
// TODO(nayeemrmn): https://github.com/rust-lang/cargo/issues/3946 to get the
// workspace root. // TODO(bartlomieju): this module contains a lot of duplicated
let display_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap(); // logic with `cli/build.rs`, factor out to `deno_core`.
for file in files { fn create_snapshot(
println!("cargo:rerun-if-changed={}", file.display()); mut js_runtime: JsRuntime,
let display_path = file.strip_prefix(display_root).unwrap(); snapshot_path: &Path,
let display_path_str = display_path.display().to_string(); files: Vec<PathBuf>,
js_runtime ) {
.execute_script( // TODO(nayeemrmn): https://github.com/rust-lang/cargo/issues/3946 to get the
&("deno:".to_string() + &display_path_str.replace('\\', "/")), // workspace root.
&std::fs::read_to_string(&file).unwrap(), let display_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
) for file in files {
.unwrap(); 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_script(
&("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());
} }
let snapshot = js_runtime.snapshot(); fn create_runtime_snapshot(snapshot_path: &Path, files: Vec<PathBuf>) {
let snapshot_slice: &[u8] = &*snapshot; let extensions: Vec<Extension> = vec![
println!("Snapshot size: {}", snapshot_slice.len()); deno_webidl::init(),
std::fs::write(&snapshot_path, snapshot_slice).unwrap(); deno_console::init(),
println!("Snapshot written to: {} ", snapshot_path.display()); deno_url::init(),
} deno_tls::init(),
deno_web::init(deno_web::BlobStore::default(), Default::default()),
deno_fetch::init::<deno_fetch::NoFetchPermissions>(
"".to_owned(),
None,
None,
None,
None,
),
deno_websocket::init::<deno_websocket::NoWebSocketPermissions>(
"".to_owned(),
None,
None,
),
deno_webstorage::init(None),
deno_crypto::init(None),
deno_webgpu::init(false),
deno_timers::init::<deno_timers::NoTimersPermission>(),
deno_broadcast_channel::init(
deno_broadcast_channel::InMemoryBroadcastChannel::default(),
false, // No --unstable.
),
deno_ffi::init::<deno_ffi::NoFfiPermissions>(false),
deno_net::init::<deno_net::NoNetPermissions>(
None, false, // No --unstable.
None,
),
deno_http::init(),
];
fn create_runtime_snapshot(snapshot_path: &Path, files: Vec<PathBuf>) { let js_runtime = JsRuntime::new(RuntimeOptions {
let extensions: Vec<Extension> = vec![ will_snapshot: true,
deno_webidl::init(), extensions,
deno_console::init(), ..Default::default()
deno_url::init(), });
deno_tls::init(), create_snapshot(js_runtime, snapshot_path, files);
deno_web::init(deno_web::BlobStore::default(), Default::default()), }
deno_fetch::init::<deno_fetch::NoFetchPermissions>(
"".to_owned(),
None,
None,
None,
None,
),
deno_websocket::init::<deno_websocket::NoWebSocketPermissions>(
"".to_owned(),
None,
None,
),
deno_webstorage::init(None),
deno_crypto::init(None),
deno_webgpu::init(false),
deno_timers::init::<deno_timers::NoTimersPermission>(),
deno_broadcast_channel::init(
deno_broadcast_channel::InMemoryBroadcastChannel::default(),
false, // No --unstable.
),
deno_ffi::init::<deno_ffi::NoFfiPermissions>(false),
deno_net::init::<deno_net::NoNetPermissions>(
None, false, // No --unstable.
None,
),
deno_http::init(),
];
let js_runtime = JsRuntime::new(RuntimeOptions { fn get_js_files(d: &str) -> Vec<PathBuf> {
will_snapshot: true, let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
extensions, let mut js_files = std::fs::read_dir(d)
..Default::default() .unwrap()
}); .map(|dir_entry| {
create_snapshot(js_runtime, snapshot_path, files); 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
}
pub fn build_snapshot(runtime_snapshot_path: PathBuf) {
let js_files = get_js_files("js");
create_runtime_snapshot(&runtime_snapshot_path, js_files);
}
} }
fn main() { fn main() {
// Skip building from docs.rs.
if env::var_os("DOCS_RS").is_some() {
return;
}
// To debug snapshot issues uncomment: // To debug snapshot issues uncomment:
// op_fetch_asset::trace_serializer(); // op_fetch_asset::trace_serializer();
@ -95,20 +115,15 @@ fn main() {
// Main snapshot // Main snapshot
let runtime_snapshot_path = o.join("CLI_SNAPSHOT.bin"); let runtime_snapshot_path = o.join("CLI_SNAPSHOT.bin");
let js_files = get_js_files("js"); // If we're building on docs.rs we just create
create_runtime_snapshot(&runtime_snapshot_path, js_files); // and empty snapshot file and return, because `rusty_v8`
} // doesn't actually compile on docs.rs
if env::var_os("DOCS_RS").is_some() {
let snapshot_slice = &[];
std::fs::write(&runtime_snapshot_path, snapshot_slice).unwrap();
return;
}
fn get_js_files(d: &str) -> Vec<PathBuf> { #[cfg(not(feature = "docsrs"))]
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); not_docs::build_snapshot(runtime_snapshot_path)
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
} }