From 4c710ebdf92fd6b4ce499391ef87f5ab12ea056b Mon Sep 17 00:00:00 2001 From: amtoine Date: Tue, 12 Dec 2023 15:59:03 +0100 Subject: [PATCH 01/10] add a test module --- tests/packages/spam_resolution/package.nuon | 3 +++ .../spam_resolution/spam_resolution/lib/bar.nu | 6 ++++++ .../spam_resolution/spam_resolution/lib/internal.nu | 3 +++ .../spam_resolution/spam_resolution/lib/lib.nu | 3 +++ tests/packages/spam_resolution/spam_resolution/mod.nu | 10 ++++++++++ 5 files changed, 25 insertions(+) create mode 100644 tests/packages/spam_resolution/package.nuon create mode 100644 tests/packages/spam_resolution/spam_resolution/lib/bar.nu create mode 100644 tests/packages/spam_resolution/spam_resolution/lib/internal.nu create mode 100644 tests/packages/spam_resolution/spam_resolution/lib/lib.nu create mode 100644 tests/packages/spam_resolution/spam_resolution/mod.nu 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 +} From 8414d68e40075b8817abd642751c7642dc27221c Mon Sep 17 00:00:00 2001 From: amtoine Date: Tue, 12 Dec 2023 16:11:55 +0100 Subject: [PATCH 02/10] add build and run --- resolution.nu | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 resolution.nu diff --git a/resolution.nu b/resolution.nu new file mode 100644 index 0000000..d36de80 --- /dev/null +++ b/resolution.nu @@ -0,0 +1,39 @@ +# NOTE: just to wait for 0.88 and `std null-device` +def null-device []: nothing -> path { + "/dev/null" +} + +export def build [package: path, --target-directory: path = "target/"] { + let pkg = open ($package | path join "package.nuon") | get name + let target_directory = $target_directory | path expand + + # TODO: use `mktemp` from `0.88` + let head = ^mktemp -t -d 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 = open ($package | path join "package.nuon") | get name + let target_directory = $target_directory | path expand + + nu --execute $" + overlay use ($target_directory | path join $pkg (^git rev-parse HEAD) "activate.nu") + const PKG = ($package | path join $pkg) + " +} From 29fcf6b09a62f581e579383fdc77b170d7f48f8c Mon Sep 17 00:00:00 2001 From: amtoine Date: Tue, 12 Dec 2023 16:14:40 +0100 Subject: [PATCH 03/10] refactor mktemp --- resolution.nu | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/resolution.nu b/resolution.nu index d36de80..7d31a6b 100644 --- a/resolution.nu +++ b/resolution.nu @@ -3,12 +3,16 @@ 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 = open ($package | path join "package.nuon") | get name let target_directory = $target_directory | path expand - # TODO: use `mktemp` from `0.88` - let head = ^mktemp -t -d nupm_install_XXXXXXX + 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) From f38489a68c9b968f7b1ae45c96ea60a2e578ffad Mon Sep 17 00:00:00 2001 From: amtoine Date: Tue, 12 Dec 2023 16:15:04 +0100 Subject: [PATCH 04/10] add `target/` to the Git ignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ From 260889621c6e5c74d4cf64b206ddbb255ffb6443 Mon Sep 17 00:00:00 2001 From: amtoine Date: Tue, 12 Dec 2023 18:12:14 +0100 Subject: [PATCH 05/10] do not require extra user intervention --- resolution.nu | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/resolution.nu b/resolution.nu index 7d31a6b..7f409ca 100644 --- a/resolution.nu +++ b/resolution.nu @@ -36,8 +36,16 @@ export def run [package: path, --target-directory: path = "target/"] { let pkg = open ($package | path join "package.nuon") | get name let target_directory = $target_directory | path expand - nu --execute $" - overlay use ($target_directory | path join $pkg (^git rev-parse HEAD) "activate.nu") - const PKG = ($package | path join $pkg) - " + $"overlay use ($target_directory | path join $pkg (^git rev-parse HEAD) "activate.nu")" + | save --force ($nu.temp-path | path join env.nu) + "$env.config.show_banner = false" | save --force ($nu.temp-path | path join config.nu) + + nu [ + --config ($nu.temp-path | path join config.nu) + --env-config ($nu.temp-path | path join env.nu) + --execute $" + $env.PROMPT_COMMAND = '($pkg)' + use ($package | path join $pkg) + " + ] } From 146879a678bbacf57da8fd4cd5d2b91fb7cb7ea6 Mon Sep 17 00:00:00 2001 From: amtoine Date: Tue, 12 Dec 2023 18:16:51 +0100 Subject: [PATCH 06/10] check for invalid package --- resolution.nu | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/resolution.nu b/resolution.nu index 7f409ca..7973987 100644 --- a/resolution.nu +++ b/resolution.nu @@ -1,3 +1,5 @@ +const PACKAGE_FILE = "package.nuon" + # NOTE: just to wait for 0.88 and `std null-device` def null-device []: nothing -> path { "/dev/null" @@ -9,7 +11,19 @@ def mktemp [pattern: string, --tmpdir, --directory] { } export def build [package: path, --target-directory: path = "target/"] { - let pkg = open ($package | path join "package.nuon") | get name + 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 @@ -33,7 +47,19 @@ export def build [package: path, --target-directory: path = "target/"] { } export def run [package: path, --target-directory: path = "target/"] { - let pkg = open ($package | path join "package.nuon") | get name + 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 $"overlay use ($target_directory | path join $pkg (^git rev-parse HEAD) "activate.nu")" From 8925bd97bdb7a08d6ee596a4a5192902bc171f57 Mon Sep 17 00:00:00 2001 From: amtoine Date: Tue, 12 Dec 2023 18:18:52 +0100 Subject: [PATCH 07/10] refactor config file names --- resolution.nu | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/resolution.nu b/resolution.nu index 7973987..1ffaaf9 100644 --- a/resolution.nu +++ b/resolution.nu @@ -62,13 +62,16 @@ export def run [package: path, --target-directory: path = "target/"] { 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 + $"overlay use ($target_directory | path join $pkg (^git rev-parse HEAD) "activate.nu")" - | save --force ($nu.temp-path | path join env.nu) - "$env.config.show_banner = false" | save --force ($nu.temp-path | path join config.nu) + | save --force $env_file + "$env.config.show_banner = false" | save --force $config_file nu [ - --config ($nu.temp-path | path join config.nu) - --env-config ($nu.temp-path | path join env.nu) + --config $config_file + --env-config $env_file --execute $" $env.PROMPT_COMMAND = '($pkg)' use ($package | path join $pkg) From bc7fc6f90161b70625554fdf163da4072004da5b Mon Sep 17 00:00:00 2001 From: amtoine Date: Tue, 12 Dec 2023 18:19:11 +0100 Subject: [PATCH 08/10] use `$nu.current-exe` --- resolution.nu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resolution.nu b/resolution.nu index 1ffaaf9..a04cb2e 100644 --- a/resolution.nu +++ b/resolution.nu @@ -69,7 +69,7 @@ export def run [package: path, --target-directory: path = "target/"] { | save --force $env_file "$env.config.show_banner = false" | save --force $config_file - nu [ + ^$nu.current-exe [ --config $config_file --env-config $env_file --execute $" From a96111cbd671333f6785717fe970680d903da46b Mon Sep 17 00:00:00 2001 From: amtoine Date: Tue, 12 Dec 2023 18:21:43 +0100 Subject: [PATCH 09/10] give error when package not built --- resolution.nu | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/resolution.nu b/resolution.nu index a04cb2e..9109f0f 100644 --- a/resolution.nu +++ b/resolution.nu @@ -65,8 +65,19 @@ export def run [package: path, --target-directory: path = "target/"] { let config_file = $nu.temp-path | path join config.nu let env_file = $nu.temp-path | path join env.nu - $"overlay use ($target_directory | path join $pkg (^git rev-parse HEAD) "activate.nu")" - | save --force $env_file + 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 [ From 5c5c2cf7d6b7dbdb4d1702c5384d092743e7bf2d Mon Sep 17 00:00:00 2001 From: amtoine Date: Tue, 12 Dec 2023 18:22:00 +0100 Subject: [PATCH 10/10] remove useless interpolations --- resolution.nu | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resolution.nu b/resolution.nu index 9109f0f..6b64fe0 100644 --- a/resolution.nu +++ b/resolution.nu @@ -16,7 +16,7 @@ export def build [package: path, --target-directory: path = "target/"] { error make { msg: $"(ansi red_bold)not_a_package(ansi reset)", label: { - text: $"does not appear to be a package", + text: "does not appear to be a package", span: (metadata $package).span, }, help: $"does not contain a `($PACKAGE_FILE)` file", @@ -52,7 +52,7 @@ export def run [package: path, --target-directory: path = "target/"] { error make { msg: $"(ansi red_bold)not_a_package(ansi reset)", label: { - text: $"does not appear to be a package", + text: "does not appear to be a package", span: (metadata $package).span, }, help: $"does not contain a `($PACKAGE_FILE)` file",