Skip to content

Commit c77942c

Browse files
committed
fix: add to PATH based on installed tool versions
1 parent db67ba9 commit c77942c

File tree

1 file changed

+42
-19
lines changed

1 file changed

+42
-19
lines changed

apps/expert/lib/expert/port.ex

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,20 @@ defmodule Expert.Port do
6565
# which contains path munging. This initial environment is present in the running
6666
# VM, and needs to be undone so we can find the correct elixir executable in the project.
6767
defp reset_env("asdf", root_path) do
68-
{env, _} = System.cmd("asdf", ~w(env elixir), cd: root_path)
68+
data_dir = System.get_env("ASDF_DATA_DIR") || Path.join(System.user_home!(), ".asdf")
69+
installs_dir = Path.join(data_dir, "installs")
6970

70-
env =
71-
env
72-
|> String.trim()
73-
|> String.split("\n")
74-
|> Enum.map(fn key_and_value ->
75-
[key, value] =
76-
key_and_value
77-
|> String.split("=", parts: 2)
78-
|> Enum.map(&String.trim/1)
71+
{current, 0} = System.cmd("asdf", ["list"], cd: root_path)
7972

80-
{key, value}
81-
end)
82-
|> Enum.reject(&is_nil/1)
73+
versions = parse_asdf_installed_versions(current, %{})
8374

84-
asdf_path =
85-
case List.keyfind(env, "ASDF_INSTALL_PATH", 0) do
86-
{_, path} -> Path.join(path, "../../../shims")
87-
_ -> ""
88-
end
75+
installed_bin_paths =
76+
versions
77+
|> Enum.map(fn {tool, version} -> Path.join([installs_dir, tool, version, "bin"]) end)
78+
|> Enum.join(":")
8979

9080
Enum.map(System.get_env(), fn
91-
{"PATH", path} -> {"PATH", "#{asdf_path}:#{path}"}
81+
{"PATH", path} -> {"PATH", "#{installed_bin_paths}:#{path}"}
9282
other -> other
9383
end)
9484
end
@@ -143,6 +133,39 @@ defmodule Expert.Port do
143133
|> Enum.reject(&is_nil/1)
144134
end
145135

136+
defp parse_asdf_installed_versions(output, acc) when is_binary(output) do
137+
output
138+
|> String.split("\n", trim: true)
139+
|> parse_asdf_installed_versions(acc)
140+
end
141+
142+
defp parse_asdf_installed_versions([], acc), do: acc
143+
144+
defp parse_asdf_installed_versions([line | rest], acc) do
145+
tool_fn? = &(not String.starts_with?(&1, " "))
146+
147+
case tool_fn?.(line) do
148+
true ->
149+
{versions, rest} = Enum.split_while(rest, &(not tool_fn?.(&1)))
150+
versions = Enum.map(versions, &String.trim/1)
151+
version = Enum.find(versions, &String.starts_with?(&1, "*"))
152+
153+
case version do
154+
nil ->
155+
parse_asdf_installed_versions(rest, acc)
156+
157+
version when is_binary(version) ->
158+
version = String.trim(version, "*")
159+
acc = Map.put(acc, line, version)
160+
161+
parse_asdf_installed_versions(rest, acc)
162+
end
163+
164+
false ->
165+
parse_asdf_installed_versions(rest, acc)
166+
end
167+
end
168+
146169
@doc """
147170
Launches an executable in the project context via a port.
148171
"""

0 commit comments

Comments
 (0)