Skip to content

Commit 700def0

Browse files
committed
Generate app file for gleam deps on compilation
1 parent cca9cd9 commit 700def0

File tree

4 files changed

+63
-46
lines changed

4 files changed

+63
-46
lines changed

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

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -374,46 +374,13 @@ defmodule Mix.Dep.Loader do
374374
from = Path.join(opts[:dest], "gleam.toml")
375375
deps = Enum.map(config[:deps], &to_dep(&1, from, _manager = nil, locked?))
376376

377-
properties =
378-
[{:vsn, to_charlist(config[:version])}]
379-
|> gleam_mod(config)
380-
|> gleam_applications(config)
381-
382-
contents = :io_lib.format("~p.~n", [{:application, dep.app, properties}])
383-
384-
[opts[:build], "ebin"]
385-
|> Path.join()
386-
|> File.mkdir_p!()
387-
388-
[opts[:build], "ebin", "#{dep.app}.app"]
389-
|> Path.join()
390-
|> File.write!(IO.chardata_to_string(contents))
391-
392377
{dep, deps}
393378
end
394379

395380
defp gleam_dep(%Mix.Dep{opts: opts} = dep, children, locked?) do
396381
{dep, Enum.map(children, &to_dep(&1, opts[:dest], _manager = nil, locked?))}
397382
end
398383

399-
defp gleam_mod(properties, config) do
400-
case config[:mod] do
401-
nil -> properties
402-
mod -> [{:mod, {String.to_atom(mod), []}} | properties]
403-
end
404-
end
405-
406-
defp gleam_applications(properties, config) do
407-
case config[:extra_applications] do
408-
nil ->
409-
properties
410-
411-
applications ->
412-
applications = Enum.map(applications, &String.to_atom/1)
413-
[{:applications, applications} | properties]
414-
end
415-
end
416-
417384
defp mix_children(config, locked?, opts) do
418385
from = Mix.Project.project_file()
419386

lib/mix/lib/mix/gleam.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ defmodule Mix.Gleam do
4343
{dep, version, opts}
4444

4545
%{"path" => path} ->
46-
{dep, Keyword.merge(opts, path: path)}
46+
{dep, Keyword.merge(opts, path: Path.expand(path))}
4747

4848
%{"git" => git, "ref" => ref} ->
4949
{dep, git: git, ref: ref}

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,55 @@ defmodule Mix.Tasks.Deps.Compile do
308308
["compile-package", "--target", "erlang", "--package", package, "--out", out, "--lib", lib]}
309309

310310
shell_cmd!(dep, config, command)
311-
Code.prepend_path(Path.join(out, "ebin"), cache: true)
311+
312+
ebin = Path.join(out, "ebin")
313+
app_file_path = Keyword.get(opts, :app, Path.join(ebin, "#{dep.app}.app"))
314+
create_app_file = app_file_path && !File.exists?(app_file_path)
315+
316+
if create_app_file do
317+
generate_gleam_app_file(opts)
318+
end
319+
320+
Code.prepend_path(ebin, cache: true)
321+
end
322+
323+
defp gleam_extra_applications(config) do
324+
config
325+
|> Map.get(:extra_applications, [])
326+
|> Enum.map(&String.to_atom/1)
327+
end
328+
329+
defp gleam_mod(config) do
330+
case config[:mod] do
331+
nil -> []
332+
mod -> {String.to_atom(mod), []}
333+
end
334+
end
335+
336+
defp generate_gleam_app_file(opts) do
337+
toml = File.cd!(opts[:dest], fn -> Mix.Gleam.load_config(".") end)
338+
339+
module =
340+
quote do
341+
def project do
342+
[
343+
app: unquote(toml.name) |> String.to_atom(),
344+
version: "#{unquote(toml.version)}"
345+
]
346+
end
347+
348+
def application do
349+
[
350+
mod: unquote(gleam_mod(toml)),
351+
extra_applications: unquote(gleam_extra_applications(toml))
352+
]
353+
end
354+
end
355+
356+
module_name = String.to_atom("Gleam.#{toml.name}")
357+
Module.create(module_name, module, Macro.Env.location(__ENV__))
358+
Mix.Project.push(module_name)
359+
Mix.Tasks.Compile.App.run([])
312360
end
313361

314362
defp make_command(dep) do

lib/mix/test/mix/gleam_test.exs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,22 @@ defmodule Mix.GleamTest do
8787
assert :gleam_dep.main()
8888
assert :gleam@int.to_string(1) == "1"
8989

90-
load_paths =
91-
Mix.Dep.Converger.converge([])
92-
|> Enum.map(&Mix.Dep.load_paths(&1))
93-
|> Enum.concat()
94-
95-
assert Enum.any?(load_paths, &String.ends_with?(&1, "gleam_dep/ebin"))
96-
assert Enum.any?(load_paths, &String.ends_with?(&1, "gleam_stdlib/ebin"))
97-
# Dep of a dep
98-
assert Enum.any?(load_paths, &String.ends_with?(&1, "gleam_erlang/ebin"))
9990
{:ok, content} = :file.consult("_build/dev/lib/gleam_dep/ebin/gleam_dep.app")
10091

10192
assert content == [
102-
{:application, :gleam_dep,
103-
[applications: [:ssl], mod: {:gleam_dep@somemodule, []}, vsn: ~c"1.0.0"]}
93+
{
94+
:application,
95+
:gleam_dep,
96+
[
97+
{:modules, [:gleam_dep]},
98+
{:optional_applications, []},
99+
{:applications, [:kernel, :stdlib, :elixir, :ssl]},
100+
{:description, ~c"gleam_dep"},
101+
{:registered, []},
102+
{:vsn, ~c"1.0.0"},
103+
{:mod, {:gleam_dep@somemodule, []}}
104+
]
105+
}
104106
]
105107
end)
106108
end

0 commit comments

Comments
 (0)