mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 16:54:58 +02:00
Improves startup time when using std-lib
This commit is contained in:
@ -163,7 +163,7 @@ pub fn evaluate_repl(
|
|||||||
eval_source(
|
eval_source(
|
||||||
engine_state,
|
engine_state,
|
||||||
&mut unique_stack,
|
&mut unique_stack,
|
||||||
r#"use std banner; banner"#.as_bytes(),
|
r#"banner"#.as_bytes(),
|
||||||
"show_banner",
|
"show_banner",
|
||||||
PipelineData::empty(),
|
PipelineData::empty(),
|
||||||
false,
|
false,
|
||||||
|
@ -9,20 +9,16 @@ use nu_protocol::{
|
|||||||
};
|
};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
// Virtual std directory unlikely to appear in user's file system
|
|
||||||
const NU_STDLIB_VIRTUAL_DIR: &str = "NU_STDLIB_VIRTUAL_DIR";
|
|
||||||
|
|
||||||
pub fn load_standard_library(
|
pub fn load_standard_library(
|
||||||
engine_state: &mut nu_protocol::engine::EngineState,
|
engine_state: &mut nu_protocol::engine::EngineState,
|
||||||
) -> Result<(), miette::ErrReport> {
|
) -> Result<(), miette::ErrReport> {
|
||||||
trace!("load_standard_library");
|
trace!("load_standard_library");
|
||||||
let (block, delta) = {
|
let (block, delta) = {
|
||||||
// Using full virtual path to avoid potential conflicts with user having 'std' directory
|
let std_dir = PathBuf::from("std");
|
||||||
// in their working directory.
|
|
||||||
let std_dir = PathBuf::from(NU_STDLIB_VIRTUAL_DIR).join("std");
|
|
||||||
|
|
||||||
let mut std_files = vec![
|
let mut std_files = vec![
|
||||||
("mod.nu", include_str!("../std/mod.nu")),
|
("mod.nu", include_str!("../std/mod.nu")),
|
||||||
|
("core.nu", include_str!("../std/core.nu")),
|
||||||
("dirs.nu", include_str!("../std/dirs.nu")),
|
("dirs.nu", include_str!("../std/dirs.nu")),
|
||||||
("dt.nu", include_str!("../std/dt.nu")),
|
("dt.nu", include_str!("../std/dt.nu")),
|
||||||
("help.nu", include_str!("../std/help.nu")),
|
("help.nu", include_str!("../std/help.nu")),
|
||||||
@ -52,26 +48,15 @@ pub fn load_standard_library(
|
|||||||
|
|
||||||
let std_dir = std_dir.to_string_lossy().to_string();
|
let std_dir = std_dir.to_string_lossy().to_string();
|
||||||
let source = r#"
|
let source = r#"
|
||||||
# Define the `std` module
|
|
||||||
module std
|
|
||||||
|
|
||||||
# Prelude
|
# Prelude
|
||||||
use std dirs [
|
use std/core.nu *
|
||||||
enter
|
|
||||||
shells
|
|
||||||
g
|
|
||||||
n
|
|
||||||
p
|
|
||||||
dexit
|
|
||||||
]
|
|
||||||
use std pwd
|
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let _ = working_set.add_virtual_path(std_dir, VirtualPath::Dir(std_virt_paths));
|
let _ = working_set.add_virtual_path(std_dir, VirtualPath::Dir(std_virt_paths));
|
||||||
|
|
||||||
// Add a placeholder file to the stack of files being evaluated.
|
// Add a placeholder file to the stack of files being evaluated.
|
||||||
// The name of this file doesn't matter; it's only there to set the current working directory to NU_STDLIB_VIRTUAL_DIR.
|
// The name of this file doesn't matter; it's only there to set the current working directory to NU_STDLIB_VIRTUAL_DIR.
|
||||||
let placeholder = PathBuf::from(NU_STDLIB_VIRTUAL_DIR).join("loading stdlib");
|
let placeholder = PathBuf::from("loading stdlib");
|
||||||
working_set.files = FileStack::with_file(placeholder);
|
working_set.files = FileStack::with_file(placeholder);
|
||||||
|
|
||||||
let block = parse(
|
let block = parse(
|
||||||
|
33
crates/nu-std/std/core.nu
Normal file
33
crates/nu-std/std/core.nu
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
use dt.nu [datetime-diff, pretty-print-duration]
|
||||||
|
|
||||||
|
# Print a banner for nushell with information about the project
|
||||||
|
export def banner [] {
|
||||||
|
let dt = (datetime-diff (date now) 2019-05-10T09:59:12-07:00)
|
||||||
|
$"(ansi green) __ ,(ansi reset)
|
||||||
|
(ansi green) .--\(\)°'.' (ansi reset)Welcome to (ansi green)Nushell(ansi reset),
|
||||||
|
(ansi green)'|, . ,' (ansi reset)based on the (ansi green)nu(ansi reset) language,
|
||||||
|
(ansi green) !_-\(_\\ (ansi reset)where all data is structured!
|
||||||
|
|
||||||
|
Please join our (ansi purple)Discord(ansi reset) community at (ansi purple)https://discord.gg/NtAbbGn(ansi reset)
|
||||||
|
Our (ansi green_bold)GitHub(ansi reset) repository is at (ansi green_bold)https://github.com/nushell/nushell(ansi reset)
|
||||||
|
Our (ansi green)Documentation(ansi reset) is located at (ansi green)https://nushell.sh(ansi reset)
|
||||||
|
(ansi cyan)Tweet(ansi reset) us at (ansi cyan_bold)@nu_shell(ansi reset)
|
||||||
|
Learn how to remove this at: (ansi green)https://nushell.sh/book/configuration.html#remove-welcome-message(ansi reset)
|
||||||
|
|
||||||
|
It's been this long since (ansi green)Nushell(ansi reset)'s first commit:
|
||||||
|
(pretty-print-duration $dt)
|
||||||
|
|
||||||
|
Startup Time: ($nu.startup-time)
|
||||||
|
"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Return the current working directory
|
||||||
|
export def pwd [
|
||||||
|
--physical (-P) # resolve symbolic links
|
||||||
|
] {
|
||||||
|
if $physical {
|
||||||
|
$env.PWD | path expand
|
||||||
|
} else {
|
||||||
|
$env.PWD
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
# std.nu, `used` to load all standard library components
|
# std.nu, `used` to load all standard library components
|
||||||
|
|
||||||
|
export module core.nu
|
||||||
export module assert.nu
|
export module assert.nu
|
||||||
export module dirs.nu
|
export module dirs.nu
|
||||||
export module dt.nu
|
export module dt.nu
|
||||||
@ -15,8 +16,6 @@ export-env {
|
|||||||
use log.nu []
|
use log.nu []
|
||||||
}
|
}
|
||||||
|
|
||||||
use dt.nu [datetime-diff, pretty-print-duration]
|
|
||||||
|
|
||||||
# Add the given paths to the PATH.
|
# Add the given paths to the PATH.
|
||||||
#
|
#
|
||||||
# # Example
|
# # Example
|
||||||
@ -160,27 +159,6 @@ export def bench [
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Print a banner for nushell with information about the project
|
|
||||||
export def banner [] {
|
|
||||||
let dt = (datetime-diff (date now) 2019-05-10T09:59:12-07:00)
|
|
||||||
$"(ansi green) __ ,(ansi reset)
|
|
||||||
(ansi green) .--\(\)°'.' (ansi reset)Welcome to (ansi green)Nushell(ansi reset),
|
|
||||||
(ansi green)'|, . ,' (ansi reset)based on the (ansi green)nu(ansi reset) language,
|
|
||||||
(ansi green) !_-\(_\\ (ansi reset)where all data is structured!
|
|
||||||
|
|
||||||
Please join our (ansi purple)Discord(ansi reset) community at (ansi purple)https://discord.gg/NtAbbGn(ansi reset)
|
|
||||||
Our (ansi green_bold)GitHub(ansi reset) repository is at (ansi green_bold)https://github.com/nushell/nushell(ansi reset)
|
|
||||||
Our (ansi green)Documentation(ansi reset) is located at (ansi green)https://nushell.sh(ansi reset)
|
|
||||||
(ansi cyan)Tweet(ansi reset) us at (ansi cyan_bold)@nu_shell(ansi reset)
|
|
||||||
Learn how to remove this at: (ansi green)https://nushell.sh/book/configuration.html#remove-welcome-message(ansi reset)
|
|
||||||
|
|
||||||
It's been this long since (ansi green)Nushell(ansi reset)'s first commit:
|
|
||||||
(pretty-print-duration $dt)
|
|
||||||
|
|
||||||
Startup Time: ($nu.startup-time)
|
|
||||||
"
|
|
||||||
}
|
|
||||||
|
|
||||||
# the cute and friendly mascot of Nushell :)
|
# the cute and friendly mascot of Nushell :)
|
||||||
export def ellie [] {
|
export def ellie [] {
|
||||||
let ellie = [
|
let ellie = [
|
||||||
@ -193,17 +171,6 @@ export def ellie [] {
|
|||||||
$ellie | str join "\n" | $"(ansi green)($in)(ansi reset)"
|
$ellie | str join "\n" | $"(ansi green)($in)(ansi reset)"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Return the current working directory
|
|
||||||
export def pwd [
|
|
||||||
--physical (-P) # resolve symbolic links
|
|
||||||
] {
|
|
||||||
if $physical {
|
|
||||||
$env.PWD | path expand
|
|
||||||
} else {
|
|
||||||
$env.PWD
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# repeat anything a bunch of times, yielding a list of *n* times the input
|
# repeat anything a bunch of times, yielding a list of *n* times the input
|
||||||
#
|
#
|
||||||
# # Examples
|
# # Examples
|
||||||
|
7
crates/nu-std/tests/test_core.nu
Normal file
7
crates/nu-std/tests/test_core.nu
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
use std assert
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
def banner [] {
|
||||||
|
use std core
|
||||||
|
assert ((core banner | lines | length) == 15)
|
||||||
|
}
|
@ -1,5 +1,4 @@
|
|||||||
use std assert
|
use std assert
|
||||||
use std assert
|
|
||||||
use std log
|
use std log
|
||||||
|
|
||||||
# A couple of nuances to understand when testing module that exports environment:
|
# A couple of nuances to understand when testing module that exports environment:
|
||||||
|
@ -70,11 +70,6 @@ def path_add_expand [] {
|
|||||||
rm $real_dir $link_dir
|
rm $real_dir $link_dir
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
def banner [] {
|
|
||||||
std assert ((std banner | lines | length) == 15)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
def repeat_things [] {
|
def repeat_things [] {
|
||||||
std assert error { "foo" | std repeat -1 }
|
std assert error { "foo" | std repeat -1 }
|
||||||
|
@ -1,15 +1,5 @@
|
|||||||
use crate::repl::tests::{fail_test, run_test_std, TestResult};
|
use crate::repl::tests::{fail_test, run_test_std, TestResult};
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn library_loaded() -> TestResult {
|
|
||||||
run_test_std("scope modules | where name == 'std' | length", "1")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn prelude_loaded() -> TestResult {
|
|
||||||
run_test_std("shells | length", "1")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn not_loaded() -> TestResult {
|
fn not_loaded() -> TestResult {
|
||||||
fail_test("log info", "")
|
fail_test("log info", "")
|
||||||
|
Reference in New Issue
Block a user