diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/resolution.nu b/resolution.nu new file mode 100644 index 0000000..6b64fe0 --- /dev/null +++ b/resolution.nu @@ -0,0 +1,91 @@ +const PACKAGE_FILE = "package.nuon" + +# NOTE: just to wait for 0.88 and `std null-device` +def null-device []: nothing -> path { + "/dev/null" +} + +# TODO: just to wait for 0.88 and built-in `mktemp` +def mktemp [pattern: string, --tmpdir, --directory] { + ^mktemp -t -d $pattern +} + +export def build [package: path, --target-directory: path = "target/"] { + let pkg_file = $package | path join $PACKAGE_FILE + if not ($pkg_file | path exists) { + error make { + msg: $"(ansi red_bold)not_a_package(ansi reset)", + label: { + text: "does not appear to be a package", + span: (metadata $package).span, + }, + help: $"does not contain a `($PACKAGE_FILE)` file", + } + } + + let pkg = open $pkg_file | get name + let target_directory = $target_directory | path expand + + let head = mktemp --tmpdir --directory nupm_install_XXXXXXX + ^git worktree add --detach $head HEAD out+err> (null-device) + rm --recursive ($head | path join ".git") + ^git worktree prune out+err> (null-device) + + let target = $target_directory | path join $pkg (^git rev-parse HEAD) + if ($target | path exists) { + rm --recursive $target + } + mkdir $target + + let package_files = $head | path join $package $pkg + cp --recursive $package_files ($target | path join "pkg") + + let activation = $"export-env { + $env.NU_LIB_DIRS = \($env.NU_LIB_DIRS? | default [] | prepend ($target)\) + }" + $activation | save --force ($target | path join "activate.nu") +} + +export def run [package: path, --target-directory: path = "target/"] { + let pkg_file = $package | path join $PACKAGE_FILE + if not ($pkg_file | path exists) { + error make { + msg: $"(ansi red_bold)not_a_package(ansi reset)", + label: { + text: "does not appear to be a package", + span: (metadata $package).span, + }, + help: $"does not contain a `($PACKAGE_FILE)` file", + } + } + + let pkg = open $pkg_file | get name + let target_directory = $target_directory | path expand + + let config_file = $nu.temp-path | path join config.nu + let env_file = $nu.temp-path | path join env.nu + + let activation_file = $target_directory | path join $pkg (^git rev-parse HEAD) "activate.nu" + if not ($activation_file | path exists) { + error make { + msg: $"(ansi red_bold)package_not_built(ansi reset)", + label: { + text: "does not appear to be built", + span: (metadata $package).span, + }, + help: $"could not find `($activation_file)`", + } + } + + $"overlay use ($activation_file)" | save --force $env_file + "$env.config.show_banner = false" | save --force $config_file + + ^$nu.current-exe [ + --config $config_file + --env-config $env_file + --execute $" + $env.PROMPT_COMMAND = '($pkg)' + use ($package | path join $pkg) + " + ] +} diff --git a/tests/packages/spam_resolution/package.nuon b/tests/packages/spam_resolution/package.nuon new file mode 100644 index 0000000..6b96569 --- /dev/null +++ b/tests/packages/spam_resolution/package.nuon @@ -0,0 +1,3 @@ +{ + name: "spam_resolution" +} diff --git a/tests/packages/spam_resolution/spam_resolution/lib/bar.nu b/tests/packages/spam_resolution/spam_resolution/lib/bar.nu new file mode 100644 index 0000000..7365572 --- /dev/null +++ b/tests/packages/spam_resolution/spam_resolution/lib/bar.nu @@ -0,0 +1,6 @@ +use lib + +export def main [] { + print "this is bar" + lib +} diff --git a/tests/packages/spam_resolution/spam_resolution/lib/internal.nu b/tests/packages/spam_resolution/spam_resolution/lib/internal.nu new file mode 100644 index 0000000..e5914b0 --- /dev/null +++ b/tests/packages/spam_resolution/spam_resolution/lib/internal.nu @@ -0,0 +1,3 @@ +export def yeah [] { + print "this is yeah" +} diff --git a/tests/packages/spam_resolution/spam_resolution/lib/lib.nu b/tests/packages/spam_resolution/spam_resolution/lib/lib.nu new file mode 100644 index 0000000..a401f96 --- /dev/null +++ b/tests/packages/spam_resolution/spam_resolution/lib/lib.nu @@ -0,0 +1,3 @@ +export def main [] { + print "this is lib" +} diff --git a/tests/packages/spam_resolution/spam_resolution/mod.nu b/tests/packages/spam_resolution/spam_resolution/mod.nu new file mode 100644 index 0000000..660c5a2 --- /dev/null +++ b/tests/packages/spam_resolution/spam_resolution/mod.nu @@ -0,0 +1,10 @@ +module pkg/lib/lib.nu # allow to `use lib` in the whole module +export module pkg/lib/bar.nu # re-export a submodule named `bar` +export use pkg/lib/internal.nu yeah # re-export an internal command named `yeah` + +use lib + +export def main [] { + print "this is foo" + lib +}