diff --git a/crates/nu-std/src/lib.rs b/crates/nu-std/src/lib.rs index 2857290f41..09585b8697 100644 --- a/crates/nu-std/src/lib.rs +++ b/crates/nu-std/src/lib.rs @@ -5,55 +5,85 @@ use nu_parser::parse; use nu_protocol::{ debugger::WithoutDebug, engine::{FileStack, Stack, StateWorkingSet, VirtualPath}, - report_parse_error, PipelineData, + report_parse_error, PipelineData, VirtualPathId, }; use std::path::PathBuf; +fn create_virt_file(working_set: &mut StateWorkingSet, name: &str, content: &str) -> VirtualPathId { + let sanitized_name = PathBuf::from(name).to_string_lossy().to_string(); + let file_id = working_set.add_file(sanitized_name.clone(), content.as_bytes()); + + working_set.add_virtual_path(sanitized_name, VirtualPath::File(file_id)) +} + pub fn load_standard_library( engine_state: &mut nu_protocol::engine::EngineState, ) -> Result<(), miette::ErrReport> { trace!("load_standard_library"); + + let mut working_set = StateWorkingSet::new(engine_state); + // Contents of the std virtual directory + let mut std_virt_paths = vec![]; + + // std/mod.nu + let std_mod_virt_file_id = create_virt_file( + &mut working_set, + "std/mod.nu", + include_str!("../std/mod.nu"), + ); + std_virt_paths.push(std_mod_virt_file_id); + + // Submodules/subdirectories ... std//mod.nu + let mut std_submodules = vec![ + // Loaded at startup - Not technically part of std + ("mod.nu", "std/core", include_str!("../std/core/mod.nu")), + // std submodules + ("mod.nu", "std/assert", include_str!("../std/assert/mod.nu")), + ("mod.nu", "std/bench", include_str!("../std/bench/mod.nu")), + ("mod.nu", "std/dirs", include_str!("../std/dirs/mod.nu")), + ("mod.nu", "std/dt", include_str!("../std/dt/mod.nu")), + ( + "mod.nu", + "std/formats", + include_str!("../std/formats/mod.nu"), + ), + ("mod.nu", "std/help", include_str!("../std/help/mod.nu")), + ("mod.nu", "std/input", include_str!("../std/input/mod.nu")), + ("mod.nu", "std/iter", include_str!("../std/iter/mod.nu")), + ("mod.nu", "std/log", include_str!("../std/log/mod.nu")), + ("mod.nu", "std/math", include_str!("../std/math/mod.nu")), + ("mod.nu", "std/util", include_str!("../std/util/mod.nu")), + ("mod.nu", "std/xml", include_str!("../std/xml/mod.nu")), + // Remove in following release + ( + "mod.nu", + "std/deprecated_dirs", + include_str!("../std/deprecated_dirs/mod.nu"), + ), + ]; + + for (filename, std_subdir_name, content) in std_submodules.drain(..) { + let mod_dir = PathBuf::from(std_subdir_name); + let name = mod_dir.join(filename); + let virt_file_id = create_virt_file(&mut working_set, &name.to_string_lossy(), content); + + // Place file in virtual subdir + let mod_dir_filelist = vec![virt_file_id]; + + let virt_dir_id = working_set.add_virtual_path( + mod_dir.to_string_lossy().to_string(), + VirtualPath::Dir(mod_dir_filelist), + ); + // Add the subdir to the list of paths in std + std_virt_paths.push(virt_dir_id); + } + + // Create std virtual dir with all subdirs and files + let std_dir = PathBuf::from("std").to_string_lossy().to_string(); + let _ = working_set.add_virtual_path(std_dir, VirtualPath::Dir(std_virt_paths)); + + // Load prelude let (block, delta) = { - let std_dir = PathBuf::from("std"); - - let mut std_files = vec![ - // Loaded at startup - ("core", include_str!("../std/core.nu")), - // std module - Loads all commands and submodules - ("mod.nu", include_str!("../std/mod.nu")), - // std submodules - ("assert", include_str!("../std/assert.nu")), - ("bench", include_str!("../std/bench.nu")), - ("dirs", include_str!("../std/dirs.nu")), - ("dt", include_str!("../std/dt.nu")), - ("formats", include_str!("../std/formats.nu")), - ("help", include_str!("../std/help.nu")), - ("input", include_str!("../std/input.nu")), - ("iter", include_str!("../std/iter.nu")), - ("log", include_str!("../std/log.nu")), - ("math", include_str!("../std/math.nu")), - ("util", include_str!("../std/util.nu")), - ("xml", include_str!("../std/xml.nu")), - // Remove in following release - ("deprecated_dirs", include_str!("../std/deprecated_dirs.nu")), - ]; - - let mut working_set = StateWorkingSet::new(engine_state); - let mut std_virt_paths = vec![]; - - for (name, content) in std_files.drain(..) { - let name = std_dir.join(name); - - let file_id = - working_set.add_file(name.to_string_lossy().to_string(), content.as_bytes()); - let virtual_file_id = working_set.add_virtual_path( - name.to_string_lossy().to_string(), - VirtualPath::File(file_id), - ); - std_virt_paths.push(virtual_file_id); - } - - let std_dir = std_dir.to_string_lossy().to_string(); let source = r#" # Prelude use std/core * @@ -67,8 +97,6 @@ use std/deprecated_dirs [ ] "#; - let _ = working_set.add_virtual_path(std_dir, VirtualPath::Dir(std_virt_paths)); - // Add a placeholder file to the stack of files being evaluated. // The name of this file doesn't matter; it's only there to set the current working directory to NU_STDLIB_VIRTUAL_DIR. let placeholder = PathBuf::from("load std/core"); diff --git a/crates/nu-std/std/assert.nu b/crates/nu-std/std/assert/mod.nu similarity index 100% rename from crates/nu-std/std/assert.nu rename to crates/nu-std/std/assert/mod.nu diff --git a/crates/nu-std/std/bench.nu b/crates/nu-std/std/bench/mod.nu similarity index 100% rename from crates/nu-std/std/bench.nu rename to crates/nu-std/std/bench/mod.nu diff --git a/crates/nu-std/std/core.nu b/crates/nu-std/std/core/mod.nu similarity index 96% rename from crates/nu-std/std/core.nu rename to crates/nu-std/std/core/mod.nu index 1a777bcd0f..3e28937dad 100644 --- a/crates/nu-std/std/core.nu +++ b/crates/nu-std/std/core/mod.nu @@ -1,4 +1,4 @@ -use dt [datetime-diff, pretty-print-duration] +use std/dt [datetime-diff, pretty-print-duration] # Print a banner for nushell with information about the project export def banner [] { diff --git a/crates/nu-std/std/deprecated_dirs.nu b/crates/nu-std/std/deprecated_dirs/mod.nu similarity index 100% rename from crates/nu-std/std/deprecated_dirs.nu rename to crates/nu-std/std/deprecated_dirs/mod.nu diff --git a/crates/nu-std/std/dirs.nu b/crates/nu-std/std/dirs/mod.nu similarity index 100% rename from crates/nu-std/std/dirs.nu rename to crates/nu-std/std/dirs/mod.nu diff --git a/crates/nu-std/std/dt.nu b/crates/nu-std/std/dt/mod.nu similarity index 100% rename from crates/nu-std/std/dt.nu rename to crates/nu-std/std/dt/mod.nu diff --git a/crates/nu-std/std/formats.nu b/crates/nu-std/std/formats/mod.nu similarity index 100% rename from crates/nu-std/std/formats.nu rename to crates/nu-std/std/formats/mod.nu diff --git a/crates/nu-std/std/help.nu b/crates/nu-std/std/help/mod.nu similarity index 100% rename from crates/nu-std/std/help.nu rename to crates/nu-std/std/help/mod.nu diff --git a/crates/nu-std/std/input.nu b/crates/nu-std/std/input/mod.nu similarity index 100% rename from crates/nu-std/std/input.nu rename to crates/nu-std/std/input/mod.nu diff --git a/crates/nu-std/std/iter.nu b/crates/nu-std/std/iter/mod.nu similarity index 100% rename from crates/nu-std/std/iter.nu rename to crates/nu-std/std/iter/mod.nu diff --git a/crates/nu-std/std/log.nu b/crates/nu-std/std/log/mod.nu similarity index 100% rename from crates/nu-std/std/log.nu rename to crates/nu-std/std/log/mod.nu diff --git a/crates/nu-std/std/math.nu b/crates/nu-std/std/math/mod.nu similarity index 100% rename from crates/nu-std/std/math.nu rename to crates/nu-std/std/math/mod.nu diff --git a/crates/nu-std/std/util.nu b/crates/nu-std/std/util/mod.nu similarity index 100% rename from crates/nu-std/std/util.nu rename to crates/nu-std/std/util/mod.nu diff --git a/crates/nu-std/std/xml.nu b/crates/nu-std/std/xml/mod.nu similarity index 100% rename from crates/nu-std/std/xml.nu rename to crates/nu-std/std/xml/mod.nu