Add export alias and export extern (#4878)

* export alias

* export extern
This commit is contained in:
JT
2022-03-20 07:58:01 +13:00
committed by GitHub
parent 285f91e67a
commit f3bb1d11d3
10 changed files with 292 additions and 15 deletions

View File

@ -0,0 +1,45 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, Signature, SyntaxShape};
#[derive(Clone)]
pub struct ExportAlias;
impl Command for ExportAlias {
fn name(&self) -> &str {
"export alias"
}
fn usage(&self) -> &str {
"Define an alias and export it from a module"
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("export def")
.required("name", SyntaxShape::String, "name of the alias")
.required(
"initial_value",
SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::Expression)),
"equals sign followed by value",
)
.category(Category::Core)
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "export an alias of ll to ls -l, from a module",
example: "export alias ll = ls -l",
result: None,
}]
}
}

View File

@ -0,0 +1,41 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, Signature, SyntaxShape};
#[derive(Clone)]
pub struct ExportExtern;
impl Command for ExportExtern {
fn name(&self) -> &str {
"export extern"
}
fn usage(&self) -> &str {
"Define an extern and export it from a module"
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("export extern")
.required("def_name", SyntaxShape::String, "definition name")
.required("params", SyntaxShape::Signature, "parameters")
.category(Category::Core)
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Export the signature for an external command",
example: r#"export extern echo [text: string]"#,
result: None,
}]
}
}

View File

@ -7,9 +7,11 @@ mod do_;
mod echo;
mod error_make;
mod export;
mod export_alias;
mod export_def;
mod export_def_env;
mod export_env;
mod export_extern;
mod extern_;
mod for_;
mod help;
@ -34,9 +36,11 @@ pub use do_::Do;
pub use echo::Echo;
pub use error_make::ErrorMake;
pub use export::ExportCommand;
pub use export_alias::ExportAlias;
pub use export_def::ExportDef;
pub use export_def_env::ExportDefEnv;
pub use export_env::ExportEnv;
pub use export_extern::ExportExtern;
pub use extern_::Extern;
pub use for_::For;
pub use help::Help;

View File

@ -57,7 +57,7 @@ impl Command for Use {
if let Some(id) = overlay.get_env_var_id(name) {
output.push((name.clone(), id));
} else if !overlay.has_decl(name) {
} else if !overlay.has_decl(name) && !overlay.has_alias(name) {
return Err(ShellError::EnvVarNotFoundAtRuntime(
String::from_utf8_lossy(name).into(),
*span,

View File

@ -34,10 +34,12 @@ pub fn create_default_context(cwd: impl AsRef<Path>) -> EngineState {
Du,
Echo,
ErrorMake,
ExportAlias,
ExportCommand,
ExportDef,
ExportDefEnv,
ExportEnv,
ExportExtern,
Extern,
For,
Help,