Skip to content

Commit d228707

Browse files
committed
Allow zvm use command to install a Zig version first
1 parent 33a6ba4 commit d228707

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

modules/zvm/zvm.nu

+35-17
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,18 @@ export def "zvm install" [version: string] {
6363
export def "zvm use" [
6464
version: string # Version to use. Nightly version is expressed using one of these terms: master, dev or nightly.
6565
] {
66-
let zig_to_use = find_installed_version_or_err $version
66+
let actual_version = get_actual_version $version
67+
mut zig_to_use = find_installed_version $actual_version
68+
69+
if $zig_to_use == null {
70+
print $"Version ($actual_version) is not currently installed. Would you like to install it first?"
71+
let install_first = [yes no] | input list
72+
if $install_first == no { return }
73+
74+
zvm install $version
75+
$zig_to_use = { version: $actual_version active: false }
76+
}
77+
6778
if $zig_to_use.active {
6879
error make {
6980
msg: $"Version ($zig_to_use.version) is already in use."
@@ -92,7 +103,18 @@ export def "zvm use" [
92103
export def "zvm remove" [
93104
version: string # Version to remove. Nightly version is expressed using one of these terms: master, dev or nightly.
94105
] {
95-
let zig_to_remove = find_installed_version_or_err $version
106+
let actual_version = get_actual_version $version
107+
let zig_to_remove = find_installed_version $actual_version
108+
if $zig_to_remove == null {
109+
error make {
110+
msg: $"Version ($actual_version) is not installed."
111+
label: {
112+
text: $"Version ($version) not found on system"
113+
span: (metadata $version).span
114+
}
115+
}
116+
}
117+
96118
let path_prefix = get_or_create_path_prefix
97119
rm --recursive --permanent $"($path_prefix)/($zig_to_remove.version)"
98120

@@ -152,22 +174,18 @@ def get_current_version [path_prefix: string] {
152174
}
153175
}
154176

155-
def find_installed_version_or_err [version: string] {
156-
let actual_version = match $version {
157-
"master" | "dev" | "nightly" => (zvm info master | get version)
158-
_ => $version
159-
}
160-
161-
let version_search_result = zvm list --system | where version == $actual_version
177+
def find_installed_version [version: string] {
178+
let version_search_result = zvm list --system | where version == $version
162179
if $version_search_result == [] {
163-
error make {
164-
msg: $"Version ($actual_version) is not installed."
165-
label: {
166-
text: $"Version ($version) not found on system"
167-
span: (metadata $version).span
168-
}
169-
}
180+
null
181+
} else {
182+
$version_search_result | first
170183
}
184+
}
171185

172-
$version_search_result | first
186+
def get_actual_version [version: string] {
187+
match $version {
188+
"master" | "dev" | "nightly" => (zvm info master | get version)
189+
_ => $version
190+
}
173191
}

0 commit comments

Comments
 (0)