config.ex (3279B)
1 require Logger 2 3 defmodule ExSync.Config do 4 def reload_timeout do 5 Application.get_env(application(), :reload_timeout, 150) 6 end 7 8 def reload_callback do 9 Application.get_env(application(), :reload_callback) 10 end 11 12 def beam_dirs do 13 if Mix.Project.umbrella?() do 14 for %Mix.Dep{app: app, opts: opts} <- Mix.Dep.Umbrella.loaded() do 15 config = [ 16 umbrella?: true, 17 app_path: opts[:build] 18 ] 19 20 Mix.Project.in_project(app, opts[:path], config, fn _ -> beam_dirs() end) 21 end 22 else 23 dep_paths = 24 Mix.Dep.cached() 25 |> Enum.filter(fn dep -> dep.opts[:path] != nil end) 26 |> Enum.map(fn %Mix.Dep{app: app, opts: opts} = dep -> 27 config = [ 28 umbrella?: opts[:in_umbrella], 29 app_path: opts[:build] 30 ] 31 32 path = resolve_dep_path(dep) 33 34 Mix.Project.in_project(app, path, config, fn _ -> beam_dirs() end) 35 end) 36 37 [Mix.Project.compile_path() | dep_paths] 38 end 39 |> List.flatten() 40 |> Enum.uniq() 41 end 42 43 def src_monitor_enabled do 44 case Application.fetch_env(application(), :src_monitor) do 45 :error -> 46 Logger.debug( 47 "Defaulting to enable source monitor, set config :exsync, src_monitor: false to disable" 48 ) 49 50 true 51 52 {:ok, value} when value in [true, false] -> 53 value 54 55 {:ok, invalid} -> 56 Logger.error( 57 "Value #{inspect(invalid)} not valid for setting :src_monitor, expected true or false. Enabling source monitor." 58 ) 59 60 true 61 end 62 end 63 64 def src_dirs do 65 src_default_dirs() ++ src_addition_dirs() 66 end 67 68 defp src_default_dirs do 69 if Mix.Project.umbrella?() do 70 for %Mix.Dep{app: app, opts: opts} <- Mix.Dep.Umbrella.loaded() do 71 Mix.Project.in_project(app, opts[:path], fn _ -> src_default_dirs() end) 72 end 73 else 74 dep_paths = 75 Mix.Dep.cached() 76 |> Enum.filter(fn dep -> dep.opts[:path] != nil end) 77 |> Enum.map(fn %Mix.Dep{app: app} = dep -> 78 path = resolve_dep_path(dep) 79 80 Mix.Project.in_project(app, path, fn _ -> 81 src_default_dirs() 82 end) 83 end) 84 85 self_paths = 86 Mix.Project.config() 87 |> Keyword.take([:elixirc_paths, :erlc_paths, :erlc_include_path]) 88 |> Keyword.values() 89 |> List.flatten() 90 |> Enum.map(&Path.join(app_source_dir(), &1)) 91 |> Enum.filter(&File.exists?/1) 92 93 [self_paths | dep_paths] 94 end 95 |> List.flatten() 96 |> Enum.uniq() 97 end 98 99 defp src_addition_dirs do 100 Application.get_env(:exsync, :addition_dirs, []) 101 |> Enum.map(&Path.join(app_source_dir(), &1)) 102 |> Enum.filter(&File.exists?/1) 103 end 104 105 # Resolve dep path (which may be a relative path) 106 defp resolve_dep_path(%Mix.Dep{} = dep) do 107 %Mix.Dep{from: from, opts: opts} = dep 108 dep_path = opts[:path] 109 dep_dir = Path.dirname(from) 110 Path.expand(dep_path, dep_dir) 111 end 112 113 def src_extensions do 114 Application.get_env( 115 :exsync, 116 :extensions, 117 [".erl", ".hrl", ".ex", ".eex"] ++ Application.get_env(:exsync, :extra_extensions, []) 118 ) 119 end 120 121 def application do 122 :exsync 123 end 124 125 def app_source_dir do 126 Path.dirname(Mix.ProjectStack.peek().file) 127 end 128 end