diff --git a/nupm/install.nu b/nupm/install.nu index 684bceb..8da0282 100644 --- a/nupm/install.nu +++ b/nupm/install.nu @@ -38,19 +38,20 @@ def install-scripts [ ]: list -> nothing { each {|script| let src_path = $pkg_dir | path join $script + let dst_path = $scripts_dir | path join $script if ($src_path | path type) != file { throw-error "script_not_found" $"Script ($src_path) does not exist" } - if (($scripts_dir - | path join ($script | path basename) - | path type) == file - and (not $force) - ) { - throw-error "package_already_installed" ( + if $force { + rm --recursive --force $dst_path + } + + if ($dst_path | path type) == file and (not $force) { + throw-error "script_already_installed" ( $"Script ($src_path) is already installed in" - + $" ($scripts_dir). Use `--force` to override the package." + + $" ($scripts_dir). Use `--force` to override it." ) } @@ -61,6 +62,41 @@ def install-scripts [ null } +# Install list of modules into a directory +# +# Input: Modules taken from 'package.nuon' +def install-modules [ + pkg_dir: path # Package directory + modules_dir: path # Target directory where to install + --force(-f): bool # Overwrite already installed modules +]: list -> nothing { + each {|module| + let src_path = $pkg_dir | path join $module + let dst_path = $modules_dir | path join $module + + if not ($src_path | path exists) { + throw-error "module_not_found" $"Module ($src_path) does not exist" + } + + if $force { + rm --recursive --force $dst_path + } + + if ($dst_path | path exists) == file and (not $force) { + throw-error "module_already_installed" ( + $"Module ($src_path) is already installed in" + + $" ($modules_dir). Use `--force` to override it." + ) + } + + log debug $"installing module `($src_path)` to `($modules_dir)`" + cp -r $src_path $modules_dir + } + + null +} + + # Install package from a directory containing 'project.nuon' def install-path [ pkg_dir: path # Directory (hopefully) containing 'package.nuon' @@ -74,44 +110,34 @@ def install-path [ match $package.type { "module" => { - let mod_dir = $pkg_dir | path join $package.name + let default_name = $package.name - if ($mod_dir | path type) != dir { - throw-error "invalid_module_package" ( - $"Module package '($package.name)' does not" - + $" contain directory '($package.name)'" - ) + if ($pkg_dir | path join $default_name | path exists) { + [ $default_name ] + } else { + [] } + | append ($package.modules? | default []) + | install-modules $pkg_dir (module-dir --ensure) --force $force - let module_dir = module-dir --ensure - let destination = $module_dir | path join $package.name - - if $force { - rm --recursive --force $destination - } - - if ($destination | path type) == dir { - throw-error "package_already_installed" ( - $"Package ($package.name) is already installed." - + "Use `--force` to override the package" - ) - } - - cp --recursive $mod_dir $module_dir - - if $package.scripts? != null { - log debug $"installing scripts for package ($package.name)" - - $package.scripts - | install-scripts $pkg_dir (script-dir --ensure) --force $force - } + $package.scripts? + | default [] + | install-scripts $pkg_dir (script-dir --ensure) --force $force }, "script" => { - log debug $"installing scripts for package ($package.name)" + let default_name = $"($package.name).nu" - [ ($pkg_dir | path join $"($package.name).nu") ] + if ($pkg_dir | path join $default_name | path exists) { + [ $default_name ] + } else { + [] + } | append ($package.scripts? | default []) | install-scripts $pkg_dir (script-dir --ensure) --force $force + + $package.modules? + | default [] + | install-modules $pkg_dir (module-dir --ensure) --force $force }, "custom" => { let build_file = $pkg_dir | path join "build.nu" diff --git a/tests/mod.nu b/tests/mod.nu index b20e9bc..8666913 100644 --- a/tests/mod.nu +++ b/tests/mod.nu @@ -37,6 +37,18 @@ export def install-module [] { } } +export def install-module-nodefault [] { + with-nupm-home { + cd tests/packages/spam_module_nodefault + + nupm install --path . + assert ([$env.NUPM_HOME modules nodefault ] | path join | path exists) + assert ([$env.NUPM_HOME modules nodefault mod.nu] + | path join + | path exists) + } +} + export def install-custom [] { with-nupm-home { cd tests/packages/spam_custom diff --git a/tests/packages/spam_module_nodefault/package.nuon b/tests/packages/spam_module_nodefault/package.nuon new file mode 100644 index 0000000..03ab982 --- /dev/null +++ b/tests/packages/spam_module_nodefault/package.nuon @@ -0,0 +1,6 @@ +{ + name: spam_module_nodefault, + type: module, + version: "0.1.0" + modules: src/nodefault +} diff --git a/tests/packages/spam_module_nodefault/src/nodefault/mod.nu b/tests/packages/spam_module_nodefault/src/nodefault/mod.nu new file mode 100644 index 0000000..7b31588 --- /dev/null +++ b/tests/packages/spam_module_nodefault/src/nodefault/mod.nu @@ -0,0 +1,3 @@ +export def main [] { + "No default module!" +}