Skip to content

Commit d7f4558

Browse files
committed
Add support for :application option in Compile.App
1 parent 5ff6bc9 commit d7f4558

File tree

5 files changed

+69
-19
lines changed

5 files changed

+69
-19
lines changed

lib/mix/lib/mix/gleam.ex

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule Mix.Gleam do
3030
deps: deps ++ dev_deps
3131
}
3232
|> maybe_gleam_version(json)
33-
|> maybe_erlang_opts(json)
33+
|> maybe_erlang_opts(json["erlang"])
3434
rescue
3535
KeyError ->
3636
Mix.raise("Command \"gleam export package-information\" unexpected format: \n" <> json)
@@ -45,7 +45,7 @@ defmodule Mix.Gleam do
4545
{dep, version, opts}
4646

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

5050
%{"git" => git, "ref" => ref} ->
5151
{dep, git: git, ref: ref}
@@ -67,17 +67,24 @@ defmodule Mix.Gleam do
6767
end
6868
end
6969

70-
defp maybe_erlang_opts(config, json) do
71-
config =
72-
case get_in(json, ["erlang", "application_start_module"]) do
73-
nil -> config
74-
mod -> Map.put(config, :mod, mod)
75-
end
70+
defp maybe_erlang_opts(config, nil), do: config
7671

77-
case get_in(json, ["erlang", "extra_applications"]) do
78-
nil -> config
79-
extra_applications -> Map.put(config, :extra_applications, extra_applications)
80-
end
72+
defp maybe_erlang_opts(config, opts) do
73+
application =
74+
opts
75+
|> Enum.filter(fn {_, value} -> value != nil end)
76+
|> Enum.map(fn
77+
{"application_start_module", module} when is_binary(module) ->
78+
{:mod, {String.to_atom(module), []}}
79+
80+
{"extra_applications", applications} when is_list(applications) ->
81+
{:extra_applications, Enum.map(applications, &String.to_atom/1)}
82+
83+
{key, value} ->
84+
IO.warn("Gleam [erlang] option not supported\n #{key}: #{inspect(value)}")
85+
end)
86+
87+
Map.put(config, :application, application)
8188
end
8289

8390
def require!() do

lib/mix/lib/mix/tasks/compile.app.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ defmodule Mix.Tasks.Compile.App do
182182
registered: [],
183183
vsn: to_charlist(version)
184184
]
185-
|> merge_project_application(project)
185+
|> merge_project_application(project, config[:application])
186186
|> handle_extra_applications(config)
187187
|> add_compile_env(current_properties)
188188
|> add_modules(modules, compile_path)
@@ -263,7 +263,7 @@ defmodule Mix.Tasks.Compile.App do
263263
end
264264
end
265265

266-
defp merge_project_application(best_guess, project) do
266+
defp merge_project_application(best_guess, project, _application = nil) do
267267
if function_exported?(project, :application, 0) do
268268
project_application = project.application()
269269

@@ -279,6 +279,14 @@ defmodule Mix.Tasks.Compile.App do
279279
end
280280
end
281281

282+
defp merge_project_application(best_guess, _project, application) do
283+
if not Keyword.keyword?(application) do
284+
Mix.raise("Application configuration passed as :application should be a keyword list")
285+
end
286+
287+
Keyword.merge(best_guess, validate_properties!(application))
288+
end
289+
282290
defp validate_properties!(properties) do
283291
Enum.each(properties, fn
284292
{:description, value} ->

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,18 @@ defmodule Mix.Tasks.Deps.Compile do
316316

317317
command =
318318
{"gleam",
319-
~w(compile-package --no-beam --target erlang --package #{package} --out #{out} --lib #{lib})}
319+
[
320+
"compile-package",
321+
"--no-beam",
322+
"--target",
323+
"erlang",
324+
"--package",
325+
package,
326+
"--out",
327+
out,
328+
"--lib",
329+
lib
330+
]}
320331

321332
shell_cmd!(dep, config, command)
322333

lib/mix/test/mix/gleam_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ defmodule Mix.GleamTest do
5353
"gleeunit" => %{"version" => ">= 1.0.0 and < 2.0.0"}
5454
},
5555
"erlang" => %{
56-
"application_start_module" => "some@application"
56+
"application_start_module" => "some@application",
57+
"extra_applications" => ["some_app"]
5758
}
5859
}
5960
|> Mix.Gleam.parse_config()
@@ -69,7 +70,8 @@ defmodule Mix.GleamTest do
6970
{:gleeunit, ">= 1.0.0 and < 2.0.0", only: :dev}
7071
],
7172
application: [
72-
mod: {:some@application, []}
73+
mod: {:some@application, []},
74+
extra_applications: [:some_app]
7375
]
7476
}
7577
end
@@ -103,8 +105,6 @@ defmodule Mix.GleamTest do
103105
{:description, ~c"gleam_dep"},
104106
{:registered, []},
105107
{:vsn, ~c"1.0.0"}
106-
# Need to add support for :application option in Compile.App
107-
# {:mod, {:gleam_dep@somemodule, []}}
108108
]
109109
}
110110
]

lib/mix/test/mix/tasks/compile.app_test.exs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,30 @@ defmodule Mix.Tasks.Compile.AppTest do
310310
end)
311311
end
312312

313+
test "dynamic project" do
314+
in_fixture("no_mixfile", fn ->
315+
config =
316+
Mix.Project.config()
317+
|> Keyword.merge(
318+
app: :dynamic_project,
319+
version: "0.1.0",
320+
application: [
321+
mod: {DynamicProject, []},
322+
applications: [:example_app, mix: :optional],
323+
extra_applications: [:logger]
324+
]
325+
)
326+
327+
Mix.ProjectStack.push(DynamicProject, config, "nofile")
328+
Mix.Tasks.Compile.Elixir.run([])
329+
Mix.Tasks.Compile.App.run([])
330+
331+
properties = parse_resource_file(:dynamic_project)
332+
assert properties[:mod] == {DynamicProject, []}
333+
assert properties[:applications] == [:kernel, :stdlib, :elixir, :logger, :example_app, :mix]
334+
end)
335+
end
336+
313337
defp parse_resource_file(app) do
314338
{:ok, [term]} = :file.consult("_build/dev/lib/#{app}/ebin/#{app}.app")
315339
{:application, ^app, properties} = term

0 commit comments

Comments
 (0)