Allow creating modules from directories (#9066)

This commit is contained in:
Jakub Žádník
2023-05-06 21:39:54 +03:00
committed by GitHub
parent 6dc7ff2335
commit a2a346e39c
20 changed files with 1220 additions and 440 deletions

View File

@ -0,0 +1,75 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
#[derive(Clone)]
pub struct ExportModule;
impl Command for ExportModule {
fn name(&self) -> &str {
"export module"
}
fn usage(&self) -> &str {
"Export a custom module from a module."
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("export module")
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
.allow_variants_without_examples(true)
.required("module", SyntaxShape::String, "module name or module path")
.optional(
"block",
SyntaxShape::Block,
"body of the module if 'module' parameter is not a path",
)
.category(Category::Core)
}
fn extra_usage(&self) -> &str {
r#"This command is a parser keyword. For details, check:
https://www.nushell.sh/book/thinking_in_nu.html"#
}
fn is_parser_keyword(&self) -> bool {
true
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
_call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
Ok(PipelineData::empty())
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Define a custom command in a submodule of a module and call it",
example: r#"module spam {
export module eggs {
export def foo [] { "foo" }
}
}
use spam eggs
eggs foo"#,
result: Some(Value::test_string("foo")),
}]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(ExportModule {})
}
}

View File

@ -14,6 +14,7 @@ mod export_alias;
mod export_def;
mod export_def_env;
mod export_extern;
mod export_module;
mod export_use;
mod extern_;
mod for_;
@ -55,6 +56,7 @@ pub use export_alias::ExportAlias;
pub use export_def::ExportDef;
pub use export_def_env::ExportDefEnv;
pub use export_extern::ExportExtern;
pub use export_module::ExportModule;
pub use export_use::ExportUse;
pub use extern_::Extern;
pub use for_::For;

View File

@ -19,8 +19,13 @@ impl Command for Module {
fn signature(&self) -> nu_protocol::Signature {
Signature::build("module")
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
.required("module_name", SyntaxShape::String, "module name")
.required("block", SyntaxShape::Block, "body of the module")
.allow_variants_without_examples(true)
.required("module", SyntaxShape::String, "module name or module path")
.optional(
"block",
SyntaxShape::Block,
"body of the module if 'module' parameter is not a module path",
)
.category(Category::Core)
}

View File

@ -21,7 +21,7 @@ impl Command for Use {
Signature::build("use")
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
.required("module", SyntaxShape::String, "Module or module file")
.optional(
.rest(
"members",
SyntaxShape::Any,
"Which members of the module to import",