Skip to content

Commit 8123710

Browse files
Papipoeksperimental
andcommitted
Apply suggestions from code review
Co-authored-by: Eksperimental <[email protected]>
1 parent 6ed5fe7 commit 8123710

File tree

6 files changed

+61
-67
lines changed

6 files changed

+61
-67
lines changed

lib/mix/lib/mix/dep/loader.ex

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ defmodule Mix.Dep.Loader do
8484
def load(%Mix.Dep{manager: manager, scm: scm, opts: opts} = dep, children, locked?) do
8585
# The manager for a child dependency is set based on the following rules:
8686
# 1. Set in dependency definition
87-
# 2. From SCM, so that Hex dependencies of a rebar/gleam project can be compiled with mix
87+
# 2. From SCM, so that Hex dependencies of a Rebar/Gleam project can be compiled with Mix
8888
# 3. From the parent dependency, used for rebar dependencies from git
8989
# 4. Inferred from files in dependency (mix.exs, rebar.config, Makefile, gleam.toml)
9090
manager = opts[:manager] || scm_manager(scm, opts) || manager || infer_manager(opts[:dest])
@@ -369,9 +369,9 @@ defmodule Mix.Dep.Loader do
369369

370370
defp gleam_dep(%Mix.Dep{opts: opts} = dep, _children = nil, manager, locked?) do
371371
Mix.Gleam.require!()
372-
373-
config = File.cd!(opts[:dest], fn -> Mix.Gleam.load_config(".") end)
374-
from = Path.join(opts[:dest], "gleam.toml")
372+
dest = opts[:dest]
373+
config = File.cd!(dest, fn -> Mix.Gleam.load_config(".") end)
374+
from = Path.join(dest, "gleam.toml")
375375
deps = Enum.map(config[:deps], &to_dep(&1, from, manager, locked?))
376376

377377
{dep, deps}

lib/mix/lib/mix/gleam.ex

+45-47
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# SPDX-FileCopyrightText: 2021 The Elixir Team
3+
# SPDX-FileCopyrightText: 2012 Plataformatec
4+
15
defmodule Mix.Gleam do
26
# Version that introduced `gleam export package-information` command
37
@required_gleam_version ">= 1.10.0"
48

59
def load_config(dir) do
610
File.cd!(dir, fn ->
7-
gleam!(["export", "package-information", "--out", "/dev/stdout"])
11+
gleam!(~W(export package-information --out /dev/stdout))
812
|> JSON.decode!()
913
|> Map.fetch!("gleam.toml")
1014
|> parse_config()
1115
end)
1216
end
1317

1418
def parse_config(json) do
15-
try do
16-
deps =
17-
Map.get(json, "dependencies", %{})
18-
|> Enum.map(&parse_dep/1)
19-
20-
dev_deps =
21-
Map.get(json, "dev-dependencies", %{})
22-
|> Enum.map(&parse_dep(&1, only: :dev))
23-
24-
%{
25-
name: Map.fetch!(json, "name"),
26-
version: Map.fetch!(json, "version"),
27-
deps: deps ++ dev_deps
28-
}
29-
|> maybe_gleam_version(json)
30-
|> maybe_erlang_opts(json)
31-
rescue
32-
KeyError ->
33-
Mix.raise("Command \"gleam export package-information\" unexpected format: \n" <> json)
34-
end
19+
deps =
20+
Map.get(json, "dependencies", %{})
21+
|> Enum.map(&parse_dep/1)
22+
23+
dev_deps =
24+
Map.get(json, "dev-dependencies", %{})
25+
|> Enum.map(&parse_dep(&1, only: :dev))
26+
27+
%{
28+
name: Map.fetch!(json, "name"),
29+
version: Map.fetch!(json, "version"),
30+
deps: deps ++ dev_deps
31+
}
32+
|> maybe_gleam_version(json)
33+
|> maybe_erlang_opts(json)
34+
rescue
35+
KeyError ->
36+
Mix.raise("Command \"gleam export package-information\" unexpected format: \n" <> json)
3537
end
3638

3739
defp parse_dep({dep, requirement}, opts \\ []) do
@@ -84,35 +86,31 @@ defmodule Mix.Gleam do
8486
end
8587

8688
defp available_version do
87-
try do
88-
case gleam!(["--version"]) do
89-
"gleam " <> version -> Version.parse!(version) |> Version.to_string()
90-
output -> Mix.raise("Command \"gleam --version\" unexpected format: #{output}")
91-
end
92-
rescue
93-
e in Version.InvalidVersionError ->
94-
Mix.raise("Command \"gleam --version\" invalid version format: #{e.version}")
89+
case gleam!(["--version"]) do
90+
"gleam " <> version -> Version.parse!(version) |> Version.to_string()
91+
output -> Mix.raise("Command \"gleam --version\" unexpected format: #{output}")
9592
end
93+
rescue
94+
e in Version.InvalidVersionError ->
95+
Mix.raise("Command \"gleam --version\" invalid version format: #{e.version}")
9696
end
9797

9898
defp gleam!(args) do
99-
try do
100-
System.cmd("gleam", args)
101-
catch
102-
:error, :enoent ->
103-
Mix.raise(
104-
"The \"gleam\" executable is not available in your PATH. " <>
105-
"Please install it, as one of your dependencies requires it. "
106-
)
107-
else
108-
{response, 0} ->
109-
String.trim(response)
110-
111-
{response, _} when is_binary(response) ->
112-
Mix.raise("Command \"gleam #{Enum.join(args, " ")}\" failed with reason: #{response}")
113-
114-
{_, _} ->
115-
Mix.raise("Command \"gleam #{Enum.join(args, " ")}\" failed")
116-
end
99+
System.cmd("gleam", args)
100+
catch
101+
:error, :enoent ->
102+
Mix.raise(
103+
"The \"gleam\" executable is not available in your PATH. " <>
104+
"Please install it, as one of your dependencies requires it. "
105+
)
106+
else
107+
{response, 0} ->
108+
String.trim(response)
109+
110+
{response, _} when is_binary(response) ->
111+
Mix.raise("Command \"gleam #{Enum.join(args, " ")}\" failed with reason: #{response}")
112+
113+
{_, _} ->
114+
Mix.raise("Command \"gleam #{Enum.join(args, " ")}\" failed")
117115
end
118116
end

lib/mix/lib/mix/tasks/deps.compile.ex

+1-12
Original file line numberDiff line numberDiff line change
@@ -334,18 +334,7 @@ defmodule Mix.Tasks.Deps.Compile do
334334

335335
command =
336336
{"gleam",
337-
[
338-
"compile-package",
339-
"--no-beam",
340-
"--target",
341-
"erlang",
342-
"--package",
343-
package,
344-
"--out",
345-
out,
346-
"--lib",
347-
lib
348-
]}
337+
~w(compile-package --no-beam --target erlang --package #{package} --out #{out} --lib #{lib})}
349338

350339
shell_cmd!(dep, config, command)
351340

lib/mix/lib/mix/tasks/deps.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ defmodule Mix.Tasks.Deps do
101101
* `:override` - if set to `true` the dependency will override any other
102102
definitions of itself by other dependencies
103103
104-
* `:manager` - Mix can also compile Rebar3, makefile and gleam projects
104+
* `:manager` - Mix can also compile Rebar3, makefile and Gleam projects
105105
and can fetch sub dependencies of Rebar3 projects. Mix will
106106
try to infer the type of project but it can be overridden with this
107107
option by setting it to `:mix`, `:rebar3`, `:make` or `:gleam`. In case

lib/mix/test/fixtures/gleam_dep/.gitignore

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If the VM crashes, it generates a dump, let's ignore it too.
5+
erl_crash.dump
6+
7+
# BEAM bytecode files.
18
*.beam
9+
10+
# Also ignore archive artifacts (built via "mix archive.build").
211
*.ez
3-
/build
4-
erl_crash.dump

lib/mix/test/mix/gleam_test.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ defmodule Mix.GleamTest do
3737
end
3838
end
3939

40-
describe "gleam export package-information format" do
40+
describe "Gleam export package-information format" do
4141
test "parse_config" do
4242
config =
4343
%{

0 commit comments

Comments
 (0)