Skip to content

Commit b75dc61

Browse files
committed
Add support for :application option in Compile.App
1 parent 8123710 commit b75dc61

File tree

5 files changed

+69
-19
lines changed

5 files changed

+69
-19
lines changed

lib/mix/lib/mix/gleam.ex

+19-12
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

+10-2
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)
@@ -252,7 +252,7 @@ defmodule Mix.Tasks.Compile.App do
252252
end
253253
end
254254

255-
defp merge_project_application(best_guess, project) do
255+
defp merge_project_application(best_guess, project, _application = nil) do
256256
if function_exported?(project, :application, 0) do
257257
project_application = project.application()
258258

@@ -268,6 +268,14 @@ defmodule Mix.Tasks.Compile.App do
268268
end
269269
end
270270

271+
defp merge_project_application(best_guess, _project, application) do
272+
if not Keyword.keyword?(application) do
273+
Mix.raise("Application configuration passed as :application should be a keyword list")
274+
end
275+
276+
Keyword.merge(best_guess, validate_properties!(application))
277+
end
278+
271279
defp validate_properties!(properties) do
272280
Enum.each(properties, fn
273281
{:description, value} ->

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

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

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

339350
shell_cmd!(dep, config, command)
340351

lib/mix/test/mix/gleam_test.exs

+4-4
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

+24
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,30 @@ defmodule Mix.Tasks.Compile.AppTest do
283283
end)
284284
end
285285

286+
test "dynamic project" do
287+
in_fixture("no_mixfile", fn ->
288+
config =
289+
Mix.Project.config()
290+
|> Keyword.merge(
291+
app: :dynamic_project,
292+
version: "0.1.0",
293+
application: [
294+
mod: {DynamicProject, []},
295+
applications: [:example_app, mix: :optional],
296+
extra_applications: [:logger]
297+
]
298+
)
299+
300+
Mix.ProjectStack.push(DynamicProject, config, "nofile")
301+
Mix.Tasks.Compile.Elixir.run([])
302+
Mix.Tasks.Compile.App.run([])
303+
304+
properties = parse_resource_file(:dynamic_project)
305+
assert properties[:mod] == {DynamicProject, []}
306+
assert properties[:applications] == [:kernel, :stdlib, :elixir, :logger, :example_app, :mix]
307+
end)
308+
end
309+
286310
defp parse_resource_file(app) do
287311
{:ok, [term]} = :file.consult("_build/dev/lib/#{app}/ebin/#{app}.app")
288312
{:application, ^app, properties} = term

0 commit comments

Comments
 (0)