@@ -318,7 +318,55 @@ defmodule Mix.Tasks.Deps.Compile do
318318 [ "compile-package" , "--target" , "erlang" , "--package" , package , "--out" , out , "--lib" , lib ] }
319319
320320 shell_cmd! ( dep , config , command )
321- Code . prepend_path ( Path . join ( out , "ebin" ) , cache: true )
321+
322+ ebin = Path . join ( out , "ebin" )
323+ app_file_path = Keyword . get ( opts , :app , Path . join ( ebin , "#{ dep . app } .app" ) )
324+ create_app_file = app_file_path && ! File . exists? ( app_file_path )
325+
326+ if create_app_file do
327+ generate_gleam_app_file ( opts )
328+ end
329+
330+ Code . prepend_path ( ebin , cache: true )
331+ end
332+
333+ defp gleam_extra_applications ( config ) do
334+ config
335+ |> Map . get ( :extra_applications , [ ] )
336+ |> Enum . map ( & String . to_atom / 1 )
337+ end
338+
339+ defp gleam_mod ( config ) do
340+ case config [ :mod ] do
341+ nil -> [ ]
342+ mod -> { String . to_atom ( mod ) , [ ] }
343+ end
344+ end
345+
346+ defp generate_gleam_app_file ( opts ) do
347+ toml = File . cd! ( opts [ :dest ] , fn -> Mix.Gleam . load_config ( "." ) end )
348+
349+ module =
350+ quote do
351+ def project do
352+ [
353+ app: unquote ( toml . name ) |> String . to_atom ( ) ,
354+ version: "#{ unquote ( toml . version ) } "
355+ ]
356+ end
357+
358+ def application do
359+ [
360+ mod: unquote ( gleam_mod ( toml ) ) ,
361+ extra_applications: unquote ( gleam_extra_applications ( toml ) )
362+ ]
363+ end
364+ end
365+
366+ module_name = String . to_atom ( "Gleam.#{ toml . name } " )
367+ Module . create ( module_name , module , Macro.Env . location ( __ENV__ ) )
368+ Mix.Project . push ( module_name )
369+ Mix.Tasks.Compile.App . run ( [ ] )
322370 end
323371
324372 defp make_command ( dep ) do
0 commit comments