Skip to content

Commit 160b669

Browse files
committed
Generate app file for gleam deps on compilation
1 parent 7e9137a commit 160b669

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
@@ -334,7 +334,55 @@ defmodule Mix.Tasks.Deps.Compile do
334334
["compile-package", "--target", "erlang", "--package", package, "--out", out, "--lib", lib]}
335335

336336
shell_cmd!(dep, config, command)
337-
Code.prepend_path(Path.join(out, "ebin"), cache: true)
337+
338+
ebin = Path.join(out, "ebin")
339+
app_file_path = Keyword.get(opts, :app, Path.join(ebin, "#{dep.app}.app"))
340+
create_app_file = app_file_path && !File.exists?(app_file_path)
341+
342+
if create_app_file do
343+
generate_gleam_app_file(opts)
344+
end
345+
346+
Code.prepend_path(ebin, cache: true)
347+
end
348+
349+
defp gleam_extra_applications(config) do
350+
config
351+
|> Map.get(:extra_applications, [])
352+
|> Enum.map(&String.to_atom/1)
353+
end
354+
355+
defp gleam_mod(config) do
356+
case config[:mod] do
357+
nil -> []
358+
mod -> {String.to_atom(mod), []}
359+
end
360+
end
361+
362+
defp generate_gleam_app_file(opts) do
363+
toml = File.cd!(opts[:dest], fn -> Mix.Gleam.load_config(".") end)
364+
365+
module =
366+
quote do
367+
def project do
368+
[
369+
app: unquote(toml.name) |> String.to_atom(),
370+
version: "#{unquote(toml.version)}"
371+
]
372+
end
373+
374+
def application do
375+
[
376+
mod: unquote(gleam_mod(toml)),
377+
extra_applications: unquote(gleam_extra_applications(toml))
378+
]
379+
end
380+
end
381+
382+
module_name = String.to_atom("Gleam.#{toml.name}")
383+
Module.create(module_name, module, Macro.Env.location(__ENV__))
384+
Mix.Project.push(module_name)
385+
Mix.Tasks.Compile.App.run([])
338386
end
339387

340388
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)