Make $nu constant (#10160)

This commit is contained in:
Jakub Žádník
2023-09-01 09:18:55 +03:00
committed by GitHub
parent 7d6b23ee2f
commit f35808cb89
13 changed files with 284 additions and 218 deletions

View File

@@ -24,8 +24,10 @@ use log::Level;
use miette::Result;
use nu_cli::gather_parent_env_vars;
use nu_cmd_base::util::get_init_cwd;
use nu_protocol::{engine::EngineState, report_error_new, Value};
use nu_protocol::{util::BufferedReader, PipelineData, RawStream};
use nu_protocol::{
engine::EngineState, eval_const::create_nu_constant, report_error_new, util::BufferedReader,
PipelineData, RawStream, Span, Value, NU_VARIABLE_ID,
};
use nu_std::load_standard_library;
use nu_utils::utils::perf;
use run::{run_commands, run_file, run_repl};
@@ -274,6 +276,10 @@ fn main() -> Result<()> {
use_color,
);
// Set up the $nu constant before evaluating config files (need to have $nu available in them)
let nu_const = create_nu_constant(&engine_state, input.span().unwrap_or_else(Span::unknown))?;
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
if let Some(commands) = parsed_nu_cli_args.commands.clone() {
run_commands(
&mut engine_state,

View File

@@ -7,7 +7,8 @@ use crate::{
#[cfg(feature = "plugin")]
use nu_cli::read_plugin_file;
use nu_cli::{evaluate_commands, evaluate_file, evaluate_repl};
use nu_protocol::PipelineData;
use nu_protocol::eval_const::create_nu_constant;
use nu_protocol::{PipelineData, Span, NU_VARIABLE_ID};
use nu_utils::utils::perf;
pub(crate) fn run_commands(
@@ -82,8 +83,14 @@ pub(crate) fn run_commands(
use_color,
);
}
// Before running commands, set up the startup time
engine_state.set_startup_time(entire_start_time.elapsed().as_nanos() as i64);
// Regenerate the $nu constant to contain the startup time and any other potential updates
let nu_const = create_nu_constant(engine_state, commands.span)?;
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
let start_time = std::time::Instant::now();
let ret_val = evaluate_commands(
commands,
@@ -169,6 +176,10 @@ pub(crate) fn run_file(
use_color,
);
// Regenerate the $nu constant to contain the startup time and any other potential updates
let nu_const = create_nu_constant(engine_state, input.span().unwrap_or_else(Span::unknown))?;
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
let start_time = std::time::Instant::now();
let ret_val = evaluate_file(
script_name,