@@ -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
0 commit comments