nu_command/misc/
source.rs

1use nu_engine::{command_prelude::*, get_eval_block_with_early_return};
2use nu_path::{absolute_with, is_windows_device_path};
3use nu_protocol::{BlockId, engine::CommandType, shell_error::io::IoError};
4
5/// Source a file for environment variables.
6#[derive(Clone)]
7pub struct Source;
8
9impl Command for Source {
10    fn name(&self) -> &str {
11        "source"
12    }
13
14    fn signature(&self) -> Signature {
15        Signature::build("source")
16            .input_output_types(vec![(Type::Any, Type::Any)])
17            .required(
18                "filename",
19                SyntaxShape::OneOf(vec![SyntaxShape::Filepath, SyntaxShape::Nothing]),
20                "The filepath to the script file to source (`null` for no-op).",
21            )
22            .category(Category::Core)
23    }
24
25    fn description(&self) -> &str {
26        "Runs a script file in the current context."
27    }
28
29    fn extra_description(&self) -> &str {
30        r#"This command is a parser keyword. For details, check:
31  https://www.nushell.sh/book/thinking_in_nu.html"#
32    }
33
34    fn command_type(&self) -> CommandType {
35        CommandType::Keyword
36    }
37
38    fn run(
39        &self,
40        engine_state: &EngineState,
41        stack: &mut Stack,
42        call: &Call,
43        input: PipelineData,
44    ) -> Result<PipelineData, ShellError> {
45        if call.get_parser_info(stack, "noop").is_some() {
46            return Ok(PipelineData::empty());
47        }
48        // Note: two hidden positionals are used here that are injected by the parser:
49        // 1. The block_id that corresponded to the 0th position
50        // 2. The block_id_name that corresponded to the file name at the 0th position
51        let block_id: i64 = call.req_parser_info(engine_state, stack, "block_id")?;
52        let block_id_name: String = call.req_parser_info(engine_state, stack, "block_id_name")?;
53        let block_id = BlockId::new(block_id as usize);
54        let block = engine_state.get_block(block_id).clone();
55        let cwd = engine_state.cwd_as_string(Some(stack))?;
56        let pb = std::path::PathBuf::from(block_id_name);
57        let parent = pb.parent().unwrap_or(std::path::Path::new(""));
58        let file_path = if is_windows_device_path(pb.as_path()) {
59            pb.clone()
60        } else {
61            let path = absolute_with(pb.as_path(), cwd)
62                .map_err(|err| IoError::new(err, call.head, pb.clone()))?;
63            match path.try_exists() {
64                Ok(true) => {}
65                Ok(false) => {
66                    return Err(IoError::new(ErrorKind::FileNotFound, call.head, pb.clone()).into());
67                }
68                Err(e) => return Err(IoError::new(e, call.head, pb.clone()).into()),
69            };
70            path
71        };
72
73        // Note: We intentionally left out PROCESS_PATH since it's supposed to
74        // to work like argv[0] in C, which is the name of the program being executed.
75        // Since we're not executing a program, we don't need to set it.
76
77        // Save the old env vars so we can restore them after the script has ran
78        let old_file_pwd = stack.get_env_var(engine_state, "FILE_PWD").cloned();
79        let old_current_file = stack.get_env_var(engine_state, "CURRENT_FILE").cloned();
80
81        // Add env vars so they are available to the script
82        stack.add_env_var(
83            "FILE_PWD".to_string(),
84            Value::string(parent.to_string_lossy(), Span::unknown()),
85        );
86        stack.add_env_var(
87            "CURRENT_FILE".to_string(),
88            Value::string(file_path.to_string_lossy(), Span::unknown()),
89        );
90
91        let eval_block_with_early_return = get_eval_block_with_early_return(engine_state);
92        let return_result =
93            eval_block_with_early_return(engine_state, stack, &block, input).map(|p| p.body);
94
95        // After the script has ran, restore the old values unless they didn't exist.
96        // If they didn't exist prior, remove the env vars
97        if let Some(old_file_pwd) = old_file_pwd {
98            stack.add_env_var("FILE_PWD".to_string(), old_file_pwd.clone());
99        } else {
100            stack.remove_env_var(engine_state, "FILE_PWD");
101        }
102        if let Some(old_current_file) = old_current_file {
103            stack.add_env_var("CURRENT_FILE".to_string(), old_current_file.clone());
104        } else {
105            stack.remove_env_var(engine_state, "CURRENT_FILE");
106        }
107
108        return_result
109    }
110
111    fn examples(&self) -> Vec<Example<'_>> {
112        vec![
113            Example {
114                description: "Runs foo.nu in the current context",
115                example: r#"source foo.nu"#,
116                result: None,
117            },
118            Example {
119                description: "Runs foo.nu in current context and call the command defined, suppose foo.nu has content: `def say-hi [] { echo 'Hi!' }`",
120                example: r#"source ./foo.nu; say-hi"#,
121                result: None,
122            },
123            Example {
124                description: "Sourcing `null` is a no-op.",
125                example: r#"source null"#,
126                result: None,
127            },
128            Example {
129                description: "Source can be used with const variables.",
130                example: r#"const file = if $nu.is-interactive { "interactive.nu" } else { null }; source $file"#,
131                result: None,
132            },
133        ]
134    }
135}