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