mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 23:37:48 +02:00
# Description This PR just adds better logging for shell_integration and tweaks the ansi escapes so they're closer to where the action happens. I also added some perf log entries to help better understand plugin file load and eval performance. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
154 lines
5.0 KiB
Rust
154 lines
5.0 KiB
Rust
use crate::util::eval_source;
|
|
#[cfg(feature = "plugin")]
|
|
use nu_path::canonicalize_with;
|
|
use nu_protocol::{
|
|
engine::{EngineState, Stack, StateWorkingSet},
|
|
report_error, HistoryFileFormat, PipelineData,
|
|
};
|
|
#[cfg(feature = "plugin")]
|
|
use nu_protocol::{ParseError, Spanned};
|
|
#[cfg(feature = "plugin")]
|
|
use nu_utils::utils::perf;
|
|
use std::path::PathBuf;
|
|
|
|
#[cfg(feature = "plugin")]
|
|
const PLUGIN_FILE: &str = "plugin.nu";
|
|
|
|
const HISTORY_FILE_TXT: &str = "history.txt";
|
|
const HISTORY_FILE_SQLITE: &str = "history.sqlite3";
|
|
|
|
#[cfg(feature = "plugin")]
|
|
pub fn read_plugin_file(
|
|
engine_state: &mut EngineState,
|
|
stack: &mut Stack,
|
|
plugin_file: Option<Spanned<String>>,
|
|
storage_path: &str,
|
|
) {
|
|
let mut start_time = std::time::Instant::now();
|
|
// Reading signatures from signature file
|
|
// The plugin.nu file stores the parsed signature collected from each registered plugin
|
|
add_plugin_file(engine_state, plugin_file, storage_path);
|
|
perf(
|
|
"add plugin file to engine_state",
|
|
start_time,
|
|
file!(),
|
|
line!(),
|
|
column!(),
|
|
engine_state.get_config().use_ansi_coloring,
|
|
);
|
|
|
|
start_time = std::time::Instant::now();
|
|
let plugin_path = engine_state.plugin_signatures.clone();
|
|
if let Some(plugin_path) = plugin_path {
|
|
let plugin_filename = plugin_path.to_string_lossy();
|
|
let plug_path = plugin_filename.to_string();
|
|
|
|
if let Ok(contents) = std::fs::read(&plugin_path) {
|
|
perf(
|
|
&format!("read plugin file {}", &plug_path),
|
|
start_time,
|
|
file!(),
|
|
line!(),
|
|
column!(),
|
|
engine_state.get_config().use_ansi_coloring,
|
|
);
|
|
start_time = std::time::Instant::now();
|
|
eval_source(
|
|
engine_state,
|
|
stack,
|
|
&contents,
|
|
&plugin_filename,
|
|
PipelineData::empty(),
|
|
false,
|
|
);
|
|
perf(
|
|
&format!("eval_source plugin file {}", &plug_path),
|
|
start_time,
|
|
file!(),
|
|
line!(),
|
|
column!(),
|
|
engine_state.get_config().use_ansi_coloring,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "plugin")]
|
|
pub fn add_plugin_file(
|
|
engine_state: &mut EngineState,
|
|
plugin_file: Option<Spanned<String>>,
|
|
storage_path: &str,
|
|
) {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
let cwd = working_set.get_cwd();
|
|
|
|
if let Some(plugin_file) = plugin_file {
|
|
if let Ok(path) = canonicalize_with(&plugin_file.item, cwd) {
|
|
engine_state.plugin_signatures = Some(path)
|
|
} else {
|
|
let e = ParseError::FileNotFound(plugin_file.item, plugin_file.span);
|
|
report_error(&working_set, &e);
|
|
}
|
|
} else if let Some(mut plugin_path) = nu_path::config_dir() {
|
|
// Path to store plugins signatures
|
|
plugin_path.push(storage_path);
|
|
let mut plugin_path = canonicalize_with(&plugin_path, &cwd).unwrap_or(plugin_path);
|
|
plugin_path.push(PLUGIN_FILE);
|
|
let plugin_path = canonicalize_with(&plugin_path, &cwd).unwrap_or(plugin_path);
|
|
engine_state.plugin_signatures = Some(plugin_path);
|
|
}
|
|
}
|
|
|
|
pub fn eval_config_contents(
|
|
config_path: PathBuf,
|
|
engine_state: &mut EngineState,
|
|
stack: &mut Stack,
|
|
) {
|
|
if config_path.exists() & config_path.is_file() {
|
|
let config_filename = config_path.to_string_lossy();
|
|
|
|
if let Ok(contents) = std::fs::read(&config_path) {
|
|
// Change currently parsed directory
|
|
let prev_currently_parsed_cwd = engine_state.currently_parsed_cwd.clone();
|
|
engine_state.start_in_file(Some(&config_filename));
|
|
|
|
eval_source(
|
|
engine_state,
|
|
stack,
|
|
&contents,
|
|
&config_filename,
|
|
PipelineData::empty(),
|
|
false,
|
|
);
|
|
|
|
// Restore the currently parsed directory back
|
|
engine_state.currently_parsed_cwd = prev_currently_parsed_cwd;
|
|
|
|
// Merge the environment in case env vars changed in the config
|
|
match nu_engine::env::current_dir(engine_state, stack) {
|
|
Ok(cwd) => {
|
|
if let Err(e) = engine_state.merge_env(stack, cwd) {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
report_error(&working_set, &e);
|
|
}
|
|
}
|
|
Err(e) => {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
report_error(&working_set, &e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn get_history_path(storage_path: &str, mode: HistoryFileFormat) -> Option<PathBuf> {
|
|
nu_path::config_dir().map(|mut history_path| {
|
|
history_path.push(storage_path);
|
|
history_path.push(match mode {
|
|
HistoryFileFormat::PlainText => HISTORY_FILE_TXT,
|
|
HistoryFileFormat::Sqlite => HISTORY_FILE_SQLITE,
|
|
});
|
|
history_path
|
|
})
|
|
}
|