@@ -8,11 +8,8 @@ defmodule Engine do
88 alias Engine.Api.Proxy
99 alias Engine.CodeAction
1010 alias Engine.CodeIntelligence
11- alias Engine.ProjectNode
1211 alias Forge.Project
1312
14- alias Mix.Tasks.Namespace
15-
1613 require Logger
1714
1815 @ excluded_apps [ :patch , :nimble_parsec ]
@@ -67,21 +64,19 @@ defmodule Engine do
6764
6865 def list_apps do
6966 for { app , _ , _ } <- :application . loaded_applications ( ) ,
70- not Namespace.Module . prefixed? ( app ) ,
67+ not Forge. Namespace.Module. prefixed? ( app ) ,
7168 do: app
7269 end
7370
74- def start_link ( % Project { } = project ) do
75- :ok = ensure_epmd_started ( )
76- start_net_kernel ( project )
77-
78- apps_to_start = [ :elixir | @ allowed_apps ] ++ [ :runtime_tools ]
79- node = Project . node_name ( project )
71+ def ensure_apps_started do
72+ apps_to_start = [ :elixir , :runtime_tools | @ allowed_apps ]
8073
81- with { :ok , node_pid } <- ProjectNode . start ( project , glob_paths ( ) ) ,
82- :ok <- ensure_apps_started ( node , apps_to_start ) do
83- { :ok , node , node_pid }
84- end
74+ Enum . reduce_while ( apps_to_start , :ok , fn app_name , _ ->
75+ case :application . ensure_all_started ( app_name ) do
76+ { :ok , _ } -> { :cont , :ok }
77+ error -> { :halt , error }
78+ end
79+ end )
8580 end
8681
8782 def deps_paths do
@@ -116,140 +111,4 @@ defmodule Engine do
116111 def set_project ( % Project { } = project ) do
117112 :persistent_term . put ( { __MODULE__ , :project } , project )
118113 end
119-
120- defdelegate stop ( project ) , to: ProjectNode
121-
122- def call ( % Project { } = project , m , f , a \\ [ ] ) do
123- project
124- |> Project . node_name ( )
125- |> :erpc . call ( m , f , a )
126- end
127-
128- def manager_node_name ( % Project { } = project ) do
129- :"manager-#{ Project . name ( project ) } -#{ Project . entropy ( project ) } @127.0.0.1"
130- end
131-
132- defp start_net_kernel ( % Project { } = project ) do
133- manager = manager_node_name ( project )
134- :net_kernel . start ( manager , % { name_domain: :longnames } )
135- end
136-
137- defp ensure_apps_started ( node , app_names ) do
138- Enum . reduce_while ( app_names , :ok , fn app_name , _ ->
139- case :rpc . call ( node , :application , :ensure_all_started , [ app_name ] ) do
140- { :ok , _ } -> { :cont , :ok }
141- error -> { :halt , error }
142- end
143- end )
144- end
145-
146- defp glob_paths do
147- for entry <- :code . get_path ( ) ,
148- entry_string = List . to_string ( entry ) ,
149- entry_string != "." ,
150- Enum . any? ( app_globs ( ) , & PathGlob . match? ( entry_string , & 1 , match_dot: true ) ) do
151- entry
152- end
153- end
154-
155- def elixir_executable ( % Project { } = project ) do
156- root_path = Project . root_path ( project )
157-
158- { path_result , env } =
159- with nil <- version_manager_path_and_env ( "asdf" , root_path ) ,
160- nil <- version_manager_path_and_env ( "mise" , root_path ) ,
161- nil <- version_manager_path_and_env ( "rtx" , root_path ) do
162- { File . cd! ( root_path , fn -> System . find_executable ( "elixir" ) end ) , System . get_env ( ) }
163- end
164-
165- case path_result do
166- nil ->
167- { :error , :no_elixir }
168-
169- executable when is_binary ( executable ) ->
170- { :ok , executable , env }
171- end
172- end
173-
174- defp app_globs do
175- app_globs = Enum . map ( @ allowed_apps , fn app_name -> "/**/#{ app_name } */ebin" end )
176- [ "/**/priv" | app_globs ]
177- end
178-
179- defp ensure_epmd_started do
180- case System . cmd ( "epmd" , ~w( -daemon) ) do
181- { "" , 0 } ->
182- :ok
183-
184- _ ->
185- { :error , :epmd_failed }
186- end
187- end
188-
189- defp version_manager_path_and_env ( manager , root_path ) do
190- with true <- is_binary ( System . find_executable ( manager ) ) ,
191- env = reset_env ( manager , root_path ) ,
192- { path , 0 } <- System . cmd ( manager , ~w( which elixir) , cd: root_path , env: env ) do
193- { String . trim ( path ) , env }
194- else
195- _ ->
196- nil
197- end
198- end
199-
200- # We launch expert by asking the version managers to provide an environment,
201- # which contains path munging. This initial environment is present in the running
202- # VM, and needs to be undone so we can find the correct elixir executable in the project.
203- defp reset_env ( "asdf" , _root_path ) do
204- orig_path = System . get_env ( "PATH_SAVE" , System . get_env ( "PATH" ) )
205-
206- Enum . map ( System . get_env ( ) , fn
207- { "ASDF_ELIXIR_VERSION" , _ } -> { "ASDF_ELIXIR_VERSION" , nil }
208- { "ASDF_ERLANG_VERSION" , _ } -> { "ASDF_ERLANG_VERSION" , nil }
209- { "PATH" , _ } -> { "PATH" , orig_path }
210- other -> other
211- end )
212- end
213-
214- defp reset_env ( "rtx" , root_path ) do
215- { env , _ } = System . cmd ( "rtx" , ~w( env -s bash) , cd: root_path )
216-
217- env
218- |> String . trim ( )
219- |> String . split ( "\n " )
220- |> Enum . map ( fn
221- "export " <> key_and_value ->
222- [ key , value ] =
223- key_and_value
224- |> String . split ( "=" , parts: 2 )
225- |> Enum . map ( & String . trim / 1 )
226-
227- { key , value }
228-
229- _ ->
230- nil
231- end )
232- |> Enum . reject ( & is_nil / 1 )
233- end
234-
235- defp reset_env ( "mise" , root_path ) do
236- { env , _ } = System . cmd ( "mise" , ~w( env -s bash) , cd: root_path )
237-
238- env
239- |> String . trim ( )
240- |> String . split ( "\n " )
241- |> Enum . map ( fn
242- "export " <> key_and_value ->
243- [ key , value ] =
244- key_and_value
245- |> String . split ( "=" , parts: 2 )
246- |> Enum . map ( & String . trim / 1 )
247-
248- { key , value }
249-
250- _ ->
251- nil
252- end )
253- |> Enum . reject ( & is_nil / 1 )
254- end
255114end
0 commit comments