2023-04-07 20:12:27 +00:00
|
|
|
use nu_parser::{parse, parse_module_block};
|
2023-04-08 11:53:43 +00:00
|
|
|
use nu_protocol::report_error;
|
2023-04-07 20:12:27 +00:00
|
|
|
use nu_protocol::{engine::StateWorkingSet, Module, ShellError, Span};
|
|
|
|
|
2023-04-09 17:00:20 +00:00
|
|
|
fn add_file(
|
|
|
|
working_set: &mut StateWorkingSet,
|
|
|
|
name: &String,
|
|
|
|
content: &[u8],
|
|
|
|
) -> (Module, Vec<Span>) {
|
2023-04-09 20:55:47 +00:00
|
|
|
let file_id = working_set.add_file(name.clone(), content);
|
|
|
|
let new_span = working_set.get_span_for_file(file_id);
|
2023-04-09 17:00:20 +00:00
|
|
|
|
2023-04-09 20:55:47 +00:00
|
|
|
let (_, module, comments) = parse_module_block(working_set, new_span, name.as_bytes());
|
2023-04-09 17:00:20 +00:00
|
|
|
|
|
|
|
if let Some(err) = working_set.parse_errors.first() {
|
|
|
|
report_error(working_set, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
parse(working_set, Some(name), content, true);
|
|
|
|
|
|
|
|
if let Some(err) = working_set.parse_errors.first() {
|
|
|
|
report_error(working_set, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
(module, comments)
|
2023-04-07 20:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn load_prelude(working_set: &mut StateWorkingSet, prelude: Vec<(&str, &str)>, module: &Module) {
|
|
|
|
let mut decls = Vec::new();
|
|
|
|
let mut errs = Vec::new();
|
|
|
|
for (name, search_name) in prelude {
|
|
|
|
if let Some(id) = module.decls.get(&search_name.as_bytes().to_vec()) {
|
|
|
|
let decl = (name.as_bytes().to_vec(), id.to_owned());
|
|
|
|
decls.push(decl);
|
|
|
|
} else {
|
|
|
|
errs.push(ShellError::GenericError(
|
|
|
|
format!("could not load `{}` from `std`.", search_name),
|
|
|
|
String::new(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !errs.is_empty() {
|
|
|
|
report_error(
|
|
|
|
working_set,
|
|
|
|
&ShellError::GenericError(
|
|
|
|
"Unable to load the prelude of the standard library.".into(),
|
|
|
|
String::new(),
|
|
|
|
None,
|
|
|
|
Some("this is a bug: please file an issue in the [issue tracker](https://github.com/nushell/nushell/issues/new/choose)".to_string()),
|
|
|
|
errs,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
working_set.use_decls(decls);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_standard_library(
|
|
|
|
engine_state: &mut nu_protocol::engine::EngineState,
|
|
|
|
) -> Result<(), miette::ErrReport> {
|
|
|
|
let delta = {
|
|
|
|
let name = "std".to_string();
|
2023-04-09 17:00:20 +00:00
|
|
|
let content = include_str!("../lib/mod.nu");
|
|
|
|
|
2023-04-10 18:32:33 +00:00
|
|
|
// these modules are loaded in the order they appear in this list
|
|
|
|
#[rustfmt::skip]
|
2023-04-09 17:00:20 +00:00
|
|
|
let submodules = vec![
|
2023-04-10 18:32:33 +00:00
|
|
|
// helper modules that could be used in other parts of the library
|
|
|
|
("log", include_str!("../lib/log.nu")),
|
|
|
|
|
|
|
|
// the rest of the library
|
2023-04-09 17:00:20 +00:00
|
|
|
("dirs", include_str!("../lib/dirs.nu")),
|
2023-04-18 21:01:36 +00:00
|
|
|
("iter", include_str!("../lib/iter.nu")),
|
2023-04-09 17:00:20 +00:00
|
|
|
("help", include_str!("../lib/help.nu")),
|
2023-04-13 19:46:37 +00:00
|
|
|
("testing", include_str!("../lib/testing.nu")),
|
2023-04-09 17:00:20 +00:00
|
|
|
("xml", include_str!("../lib/xml.nu")),
|
2023-05-10 12:05:01 +00:00
|
|
|
("dt", include_str!("../lib/dt.nu")),
|
2023-04-09 17:00:20 +00:00
|
|
|
];
|
2023-04-07 20:12:27 +00:00
|
|
|
|
2023-04-17 17:13:50 +00:00
|
|
|
// Define commands to be preloaded into the default (top level, unprefixed) namespace.
|
|
|
|
// User can invoke these without having to `use std` beforehand.
|
|
|
|
// Entries are: (name to add to default namespace, path under std to find implementation)
|
|
|
|
//
|
|
|
|
// Conventionally, for a command implemented as `std foo`, the name added
|
|
|
|
// is either `std foo` or bare `foo`, not some arbitrary rename.
|
|
|
|
|
REFACTOR: remove the shell commands (#8415)
Related to #8368.
# Description
as planned in #8311, the `enter`, `shells`, `g`, `n` and `p` commands
have been re-implemented in pure-`nushell` in the standard library.
this PR removes the `rust` implementations of these commands.
- all the "shells" tests have been removed from
`crates/nu-commnand/tests/commands/` in
2cc6a82da6f77c01f2a0d06847eabde168a2e56b, except for the `exit` command
- `cd` does not use the `shells` feature in its source code anymore =>
that does not change its single-shell behaviour
- all the command implementations have been removed from
`crates/nu-command/src/shells/`, except for `exit.rs` => `mod.rs` has
been modified accordingly
- the `exit` command now does not compute any "shell" related things
- the `--now` option has been removed from `exit`, as it does not serve
any purpose without sub-shells
# User-Facing Changes
users may now not use `enter`, `shells`, `g`, `n` and `p`
now they would have to use the standard library to have access to
equivalent features, thanks to the `dirs.nu` module introduced by @bobhy
in #8368
# Tests + Formatting
- :green_circle: `toolkit fmt`
- :green_circle: `toolkit clippy`
- :black_circle: `toolkit test`
- :black_circle: `toolkit test stdlib`
# After Submitting
the website will have to be regenerated to reflect the removed commands
:+1:
2023-05-13 17:40:11 +00:00
|
|
|
#[rustfmt::skip]
|
2023-04-07 20:12:27 +00:00
|
|
|
let prelude = vec![
|
|
|
|
("std help", "help"),
|
|
|
|
("std help commands", "help commands"),
|
|
|
|
("std help aliases", "help aliases"),
|
|
|
|
("std help modules", "help modules"),
|
|
|
|
("std help externs", "help externs"),
|
|
|
|
("std help operators", "help operators"),
|
REFACTOR: remove the shell commands (#8415)
Related to #8368.
# Description
as planned in #8311, the `enter`, `shells`, `g`, `n` and `p` commands
have been re-implemented in pure-`nushell` in the standard library.
this PR removes the `rust` implementations of these commands.
- all the "shells" tests have been removed from
`crates/nu-commnand/tests/commands/` in
2cc6a82da6f77c01f2a0d06847eabde168a2e56b, except for the `exit` command
- `cd` does not use the `shells` feature in its source code anymore =>
that does not change its single-shell behaviour
- all the command implementations have been removed from
`crates/nu-command/src/shells/`, except for `exit.rs` => `mod.rs` has
been modified accordingly
- the `exit` command now does not compute any "shell" related things
- the `--now` option has been removed from `exit`, as it does not serve
any purpose without sub-shells
# User-Facing Changes
users may now not use `enter`, `shells`, `g`, `n` and `p`
now they would have to use the standard library to have access to
equivalent features, thanks to the `dirs.nu` module introduced by @bobhy
in #8368
# Tests + Formatting
- :green_circle: `toolkit fmt`
- :green_circle: `toolkit clippy`
- :black_circle: `toolkit test`
- :black_circle: `toolkit test stdlib`
# After Submitting
the website will have to be regenerated to reflect the removed commands
:+1:
2023-05-13 17:40:11 +00:00
|
|
|
|
2023-05-19 20:27:45 +00:00
|
|
|
("enter", "dirs enter"),
|
|
|
|
("shells", "dirs shells"),
|
|
|
|
("g", "dirs g"),
|
|
|
|
("n", "dirs n"),
|
|
|
|
("p", "dirs p"),
|
|
|
|
("dexit", "dirs dexit"),
|
2023-04-07 20:12:27 +00:00
|
|
|
];
|
|
|
|
|
2023-04-09 17:00:20 +00:00
|
|
|
let mut working_set = StateWorkingSet::new(engine_state);
|
2023-04-07 20:12:27 +00:00
|
|
|
|
2023-04-09 17:00:20 +00:00
|
|
|
for (name, content) in submodules {
|
|
|
|
let (module, comments) =
|
|
|
|
add_file(&mut working_set, &name.to_string(), content.as_bytes());
|
|
|
|
working_set.add_module(name, module, comments);
|
|
|
|
}
|
|
|
|
|
|
|
|
let (module, comments) = add_file(&mut working_set, &name, content.as_bytes());
|
|
|
|
load_prelude(&mut working_set, prelude, &module);
|
2023-04-07 20:12:27 +00:00
|
|
|
working_set.add_module(&name, module, comments);
|
|
|
|
|
|
|
|
working_set.render()
|
|
|
|
};
|
|
|
|
|
|
|
|
engine_state.merge_delta(delta)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|