Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve pkg keyword #52

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
91 changes: 91 additions & 0 deletions resolution.nu
Original file line number Diff line number Diff line change
@@ -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)
"
]
}
3 changes: 3 additions & 0 deletions tests/packages/spam_resolution/package.nuon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
name: "spam_resolution"
}
6 changes: 6 additions & 0 deletions tests/packages/spam_resolution/spam_resolution/lib/bar.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use lib

export def main [] {
print "this is bar"
lib
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export def yeah [] {
print "this is yeah"
}
3 changes: 3 additions & 0 deletions tests/packages/spam_resolution/spam_resolution/lib/lib.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export def main [] {
print "this is lib"
}
10 changes: 10 additions & 0 deletions tests/packages/spam_resolution/spam_resolution/mod.nu
Original file line number Diff line number Diff line change
@@ -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
}
Loading