2022-02-09 22:08:16 +00:00
|
|
|
use crate::is_perf_true;
|
2022-01-18 08:48:28 +00:00
|
|
|
use crate::utils::{eval_source, report_error};
|
2022-02-09 22:08:16 +00:00
|
|
|
use log::info;
|
2022-02-19 20:54:43 +00:00
|
|
|
use nu_parser::ParseError;
|
|
|
|
use nu_path::canonicalize_with;
|
2022-01-18 08:48:28 +00:00
|
|
|
use nu_protocol::engine::{EngineState, Stack, StateDelta, StateWorkingSet};
|
2022-02-19 20:54:43 +00:00
|
|
|
use nu_protocol::{PipelineData, Span, Spanned};
|
2022-02-20 10:08:53 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
2022-01-18 08:48:28 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
const NUSHELL_FOLDER: &str = "nushell";
|
|
|
|
const CONFIG_FILE: &str = "config.nu";
|
|
|
|
const HISTORY_FILE: &str = "history.txt";
|
2022-02-12 20:48:17 +00:00
|
|
|
#[cfg(feature = "plugin")]
|
|
|
|
const PLUGIN_FILE: &str = "plugin.nu";
|
2022-01-18 08:48:28 +00:00
|
|
|
|
2022-02-12 20:48:17 +00:00
|
|
|
#[cfg(feature = "plugin")]
|
2022-01-18 08:48:28 +00:00
|
|
|
pub(crate) fn read_plugin_file(engine_state: &mut EngineState, stack: &mut Stack) {
|
|
|
|
// Reading signatures from signature file
|
|
|
|
// The plugin.nu file stores the parsed signature collected from each registered plugin
|
2022-02-26 08:57:51 +00:00
|
|
|
add_plugin_file(engine_state);
|
2022-01-18 08:48:28 +00:00
|
|
|
|
2022-02-26 08:57:51 +00:00
|
|
|
let plugin_path = engine_state.plugin_signatures.clone();
|
|
|
|
if let Some(plugin_path) = plugin_path {
|
2022-01-18 08:48:28 +00:00
|
|
|
let plugin_filename = plugin_path.to_string_lossy().to_owned();
|
|
|
|
|
2022-02-14 15:53:48 +00:00
|
|
|
if let Ok(contents) = std::fs::read(&plugin_path) {
|
|
|
|
eval_source(
|
|
|
|
engine_state,
|
|
|
|
stack,
|
|
|
|
&contents,
|
|
|
|
&plugin_filename,
|
|
|
|
PipelineData::new(Span::new(0, 0)),
|
|
|
|
);
|
2022-01-18 08:48:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-26 08:57:51 +00:00
|
|
|
|
2022-02-09 22:08:16 +00:00
|
|
|
if is_perf_true() {
|
|
|
|
info!("read_plugin_file {}:{}:{}", file!(), line!(), column!());
|
|
|
|
}
|
2022-01-18 08:48:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-26 08:57:51 +00:00
|
|
|
#[cfg(feature = "plugin")]
|
|
|
|
pub(crate) fn add_plugin_file(engine_state: &mut EngineState) {
|
|
|
|
if let Some(mut plugin_path) = nu_path::config_dir() {
|
|
|
|
// Path to store plugins signatures
|
|
|
|
plugin_path.push(NUSHELL_FOLDER);
|
|
|
|
plugin_path.push(PLUGIN_FILE);
|
|
|
|
engine_state.plugin_signatures = Some(plugin_path.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 20:54:43 +00:00
|
|
|
pub(crate) fn read_config_file(
|
|
|
|
engine_state: &mut EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
config_file: Option<Spanned<String>>,
|
|
|
|
) {
|
2022-01-18 08:48:28 +00:00
|
|
|
// Load config startup file
|
2022-02-19 20:54:43 +00:00
|
|
|
if let Some(file) = config_file {
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
let cwd = working_set.get_cwd();
|
|
|
|
|
|
|
|
match canonicalize_with(&file.item, cwd) {
|
|
|
|
Ok(path) => {
|
|
|
|
eval_config_contents(path, engine_state, stack);
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
let e = ParseError::FileNotFound(file.item, file.span);
|
|
|
|
report_error(&working_set, &e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if let Some(mut config_path) = nu_path::config_dir() {
|
2022-01-18 08:48:28 +00:00
|
|
|
config_path.push(NUSHELL_FOLDER);
|
|
|
|
|
|
|
|
// Create config directory if it does not exist
|
|
|
|
if !config_path.exists() {
|
|
|
|
if let Err(err) = std::fs::create_dir_all(&config_path) {
|
|
|
|
eprintln!("Failed to create config directory: {}", err);
|
2022-02-19 20:54:43 +00:00
|
|
|
return;
|
2022-01-18 08:48:28 +00:00
|
|
|
}
|
2022-02-19 20:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config_path.push(CONFIG_FILE);
|
2022-02-20 10:08:53 +00:00
|
|
|
|
|
|
|
if !config_path.exists() {
|
2022-02-25 18:58:47 +00:00
|
|
|
println!("No config file found at {}", config_path.to_string_lossy());
|
|
|
|
println!("Would you like to create one with defaults (Y/n): ");
|
2022-02-20 10:08:53 +00:00
|
|
|
|
|
|
|
let mut answer = String::new();
|
|
|
|
std::io::stdin()
|
|
|
|
.read_line(&mut answer)
|
|
|
|
.expect("Failed to read user input");
|
|
|
|
|
2022-03-03 01:22:15 +00:00
|
|
|
let config_file = include_str!("../docs/sample_config/default_config.nu");
|
2022-02-28 15:12:08 +00:00
|
|
|
|
2022-02-20 10:08:53 +00:00
|
|
|
match answer.to_lowercase().trim() {
|
2022-02-20 12:48:46 +00:00
|
|
|
"y" | "" => {
|
2022-02-20 10:08:53 +00:00
|
|
|
let mut output = File::create(&config_path).expect("Unable to create file");
|
|
|
|
write!(output, "{}", config_file).expect("Unable to write to config file");
|
2022-02-25 18:58:47 +00:00
|
|
|
println!("Config file created at: {}", config_path.to_string_lossy());
|
2022-02-20 10:08:53 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
println!("Continuing without config file");
|
2022-02-28 15:12:08 +00:00
|
|
|
// Just use the contents of "default_config.nu"
|
|
|
|
eval_source(
|
|
|
|
engine_state,
|
|
|
|
stack,
|
|
|
|
config_file.as_bytes(),
|
|
|
|
"default_config.nu",
|
|
|
|
PipelineData::new(Span::new(0, 0)),
|
|
|
|
);
|
2022-02-20 10:08:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 20:54:43 +00:00
|
|
|
eval_config_contents(config_path, engine_state, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_perf_true() {
|
|
|
|
info!("read_config_file {}:{}:{}", file!(), line!(), column!());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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().to_owned();
|
2022-01-18 08:48:28 +00:00
|
|
|
|
2022-02-19 20:54:43 +00:00
|
|
|
if let Ok(contents) = std::fs::read(&config_path) {
|
|
|
|
eval_source(
|
|
|
|
engine_state,
|
|
|
|
stack,
|
|
|
|
&contents,
|
|
|
|
&config_filename,
|
|
|
|
PipelineData::new(Span::new(0, 0)),
|
|
|
|
);
|
2022-01-18 08:48:28 +00:00
|
|
|
|
2022-02-19 20:54:43 +00:00
|
|
|
// Merge the delta 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_delta(StateDelta::new(), Some(stack), cwd) {
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &e);
|
2022-01-18 08:48:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-19 20:54:43 +00:00
|
|
|
Err(e) => {
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &e);
|
|
|
|
}
|
2022-01-18 08:48:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn create_history_path() -> Option<PathBuf> {
|
|
|
|
nu_path::config_dir().and_then(|mut history_path| {
|
|
|
|
history_path.push(NUSHELL_FOLDER);
|
|
|
|
history_path.push(HISTORY_FILE);
|
|
|
|
|
|
|
|
if !history_path.exists() {
|
|
|
|
// Creating an empty file to store the history
|
|
|
|
match std::fs::File::create(&history_path) {
|
|
|
|
Ok(_) => Some(history_path),
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Some(history_path)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|