|
| 1 | +defmodule Mix.Releases.Config do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + defmodule LoadError do |
| 5 | + defexception [:file, :error] |
| 6 | + |
| 7 | + def message(%LoadError{file: file, error: error}) do |
| 8 | + "could not load release config #{Path.relative_to_cwd(file)}\n " <> |
| 9 | + "#{Exception.format_banner(:error, error)}" |
| 10 | + end |
| 11 | + end |
| 12 | + |
| 13 | + defmodule ReleaseDefinition do |
| 14 | + defstruct name: "", |
| 15 | + version: "0.0.1", |
| 16 | + applications: [ |
| 17 | + :iex, # included so the elixir shell works |
| 18 | + :sasl # required for upgrades |
| 19 | + # can also use `app_name: type`, as in `some_dep: load`, |
| 20 | + # to only load the application, not start it |
| 21 | + ] |
| 22 | + |
| 23 | + def new(name, version, apps \\ []) do |
| 24 | + definition = %__MODULE__{name: name, version: version} |
| 25 | + %{definition | :applications => definition.applications ++ apps} |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + defstruct dev_mode: false, |
| 30 | + paths: [], # additional code paths to search |
| 31 | + vm_args: nil, # path to a custom vm.args |
| 32 | + sys_config: nil, # path to a custom sys.config |
| 33 | + include_erts: true, # false | path: "path/to/erts" |
| 34 | + include_src: false, # true |
| 35 | + include_system_libs: true, # false | path: "path/to/libs" |
| 36 | + strip_debug_info?: true, # false |
| 37 | + selected_release: :default, # the release being built |
| 38 | + upgrade_from: :default, # the release to upgrade from (if applicable) |
| 39 | + erl_opts: [], |
| 40 | + releases: [], # the releases to select from |
| 41 | + overrides: [ |
| 42 | + # During development its often the case that you want to substitute the app |
| 43 | + # that you are working on for a 'production' version of an app. You can |
| 44 | + # explicitly tell Mix to override all versions of an app that you specify |
| 45 | + # with an app in an arbitrary directory. Mix will then symlink that app |
| 46 | + # into the release in place of the specified app. be aware though that Mix |
| 47 | + # will check your app for consistancy so it should be a normal OTP app and |
| 48 | + # already be built. |
| 49 | + ], |
| 50 | + overlay_vars: [ |
| 51 | + # key: value |
| 52 | + ], |
| 53 | + overlays: [ |
| 54 | + # copy: {from_path, to_path} |
| 55 | + # link: {from_path, to_path} |
| 56 | + # mkdir: path |
| 57 | + # template: {template_path, output_path} |
| 58 | + ], |
| 59 | + pre_start_hook: nil, |
| 60 | + post_start_hook: nil, |
| 61 | + pre_stop_hook: nil, |
| 62 | + post_stop_hook: nil |
| 63 | + |
| 64 | + defmacro __using__(_) do |
| 65 | + quote do |
| 66 | + import Mix.Releases.Config, only: [ |
| 67 | + release: 2, release: 3, override: 2, overlay: 2, config: 1, |
| 68 | + version: 1 |
| 69 | + ] |
| 70 | + {:ok, agent} = Mix.Config.Agent.start_link |
| 71 | + var!(config_agent, Mix.Releases.Config) = agent |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + defmacro config(opts) when is_list(opts) do |
| 76 | + quote do |
| 77 | + Mix.Config.Agent.merge var!(config_agent, Mix.Releases.Config), |
| 78 | + [{:settings, unquote(opts)}] |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + defmacro release(name, version, applications \\ []) do |
| 83 | + quote do |
| 84 | + Mix.Config.Agent.merge var!(config_agent, Mix.Releases.Config), |
| 85 | + [{:releases, [{unquote(name), [{unquote(version), unquote(applications)}]}]}] |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + defmacro override(app, path) do |
| 90 | + quote do |
| 91 | + Mix.Config.Agent.merge var!(config_agent, Mix.Releases.Config), |
| 92 | + [{:overrides, [{unquote(app), unquote(path)}]}] |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + defmacro overlay(type, opts) do |
| 97 | + quote do |
| 98 | + Mix.Config.Agent.merge var!(config_agent, Mix.Releases.Config), |
| 99 | + [{:overlays, [{unquote(type), unquote(opts)}]}] |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + defmacro version(app) do |
| 104 | + quote do |
| 105 | + Application.load(unquote(app)) |
| 106 | + case Application.spec(unquote(app)) do |
| 107 | + nil -> raise ArgumentError, "could not load app #{unquote(app)}" |
| 108 | + spec -> Keyword.get(spec, :vsn) |
| 109 | + end |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + @doc """ |
| 114 | + Reads and validates a configuration file. |
| 115 | + `file` is the path to the configuration file to be read. If that file doesn't |
| 116 | + exist or if there's an error loading it, a `Mix.Releases.Config.LoadError` exception |
| 117 | + will be raised. |
| 118 | + """ |
| 119 | + def read!(file) do |
| 120 | + try do |
| 121 | + {config, binding} = Code.eval_file(file) |
| 122 | + |
| 123 | + config = case List.keyfind(binding, {:config_agent, Mix.Releases.Config}, 0) do |
| 124 | + {_, agent} -> get_config_and_stop_agent(agent) |
| 125 | + nil -> config |
| 126 | + end |
| 127 | + |
| 128 | + config = to_struct(config) |
| 129 | + validate!(config) |
| 130 | + config |
| 131 | + rescue |
| 132 | + e in [LoadError] -> reraise(e, System.stacktrace) |
| 133 | + e -> reraise(LoadError, [file: file, error: e], System.stacktrace) |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + def validate!(%__MODULE__{:releases => []}) do |
| 138 | + raise ArgumentError, |
| 139 | + "expected release config to have at least one release defined" |
| 140 | + end |
| 141 | + def validate!(%__MODULE__{} = config) do |
| 142 | + for override <- config.overrides do |
| 143 | + case override do |
| 144 | + {app, path} when is_atom(app) and is_binary(path) -> |
| 145 | + :ok |
| 146 | + value -> |
| 147 | + raise ArgumentError, |
| 148 | + "expected override to be an app name and path, but got: #{inspect value}" |
| 149 | + end |
| 150 | + end |
| 151 | + for overlay <- config.overlays do |
| 152 | + case overlay do |
| 153 | + {op, opts} when is_atom(op) and is_list(opts) -> |
| 154 | + :ok |
| 155 | + value -> |
| 156 | + raise ArgumentError, |
| 157 | + "expected overlay to be an overlay type and options, but got: #{inspect value}" |
| 158 | + end |
| 159 | + end |
| 160 | + cond do |
| 161 | + is_list(config.overlay_vars) && length(config.overlay_vars) > 0 && Keyword.keyword?(config.overlay_vars) -> |
| 162 | + :ok |
| 163 | + is_list(config.overlay_vars) && length(config.overlay_vars) == 0 -> |
| 164 | + :ok |
| 165 | + :else -> |
| 166 | + raise ArgumentError, |
| 167 | + "expected overlay_vars to be a keyword list, but got: #{inspect config.overlay_vars}" |
| 168 | + end |
| 169 | + paths_valid? = Enum.all?(config.paths, &is_binary/1) |
| 170 | + cond do |
| 171 | + not is_boolean(config.dev_mode) -> |
| 172 | + raise ArgumentError, |
| 173 | + "expected :dev_mode to be a boolean, but got: #{inspect config.dev_mode}" |
| 174 | + not paths_valid? -> |
| 175 | + raise ArgumentError, |
| 176 | + "expected :paths to be a list of strings, but got: #{inspect config.paths}" |
| 177 | + not (is_nil(config.vm_args) or is_binary(config.vm_args)) -> |
| 178 | + raise ArgumentError, |
| 179 | + "expected :vm_args to be nil or a path string, but got: #{inspect config.vm_args}" |
| 180 | + not (is_nil(config.sys_config) or is_binary(config.sys_config)) -> |
| 181 | + raise ArgumentError, |
| 182 | + "expected :sys_config to be nil or a path string, but got: #{inspect config.sys_config}" |
| 183 | + not (is_boolean(config.include_erts) or is_binary(config.include_erts)) -> |
| 184 | + raise ArgumentError, |
| 185 | + "expected :include_erts to be boolean or a path string, but got: #{inspect config.include_erts}" |
| 186 | + not (is_boolean(config.include_src) or is_binary(config.include_src)) -> |
| 187 | + raise ArgumentError, |
| 188 | + "expected :include_src to be boolean, but got: #{inspect config.include_src}" |
| 189 | + not (is_boolean(config.include_system_libs) or is_binary(config.include_system_libs)) -> |
| 190 | + raise ArgumentError, |
| 191 | + "expected :include_system_libs to be boolean or a path string, but got: #{inspect config.include_system_libs}" |
| 192 | + not is_list(config.erl_opts) -> |
| 193 | + raise ArgumentError, |
| 194 | + "expected :erl_opts to be a list, but got: #{inspect config.erl_opts}" |
| 195 | + not is_boolean(config.strip_debug_info?) -> |
| 196 | + raise ArgumentError, |
| 197 | + "expected :strip_debug_info? to be a boolean, but got: #{inspect config.strip_debug_info?}" |
| 198 | + not (is_nil(config.pre_start_hook) or is_binary(config.pre_start_hook)) -> |
| 199 | + raise ArgumentError, |
| 200 | + "expected :pre_start_hook to be nil or a path string, but got: #{inspect config.pre_start_hook}" |
| 201 | + not (is_nil(config.post_start_hook) or is_binary(config.post_start_hook)) -> |
| 202 | + raise ArgumentError, |
| 203 | + "expected :post_start_hook to be nil or a path string, but got: #{inspect config.post_start_hook}" |
| 204 | + not (is_nil(config.pre_stop_hook) or is_binary(config.pre_stop_hook)) -> |
| 205 | + raise ArgumentError, |
| 206 | + "expected :pre_stop_hook to be nil or a path string, but got: #{inspect config.pre_stop_hook}" |
| 207 | + not (is_nil(config.post_stop_hook) or is_binary(config.post_stop_hook)) -> |
| 208 | + raise ArgumentError, |
| 209 | + "expected :post_stop_hook to be nil or a path string, but got: #{inspect config.post_stop_hook}" |
| 210 | + :else -> |
| 211 | + true |
| 212 | + end |
| 213 | + end |
| 214 | + def validate!(config) do |
| 215 | + raise ArgumentError, |
| 216 | + "expected release config to be a struct, instead got: #{inspect config}" |
| 217 | + end |
| 218 | + |
| 219 | + defp get_config_and_stop_agent(agent) do |
| 220 | + config = Mix.Config.Agent.get(agent) |
| 221 | + Mix.Config.Agent.stop(agent) |
| 222 | + config |
| 223 | + end |
| 224 | + |
| 225 | + defp to_struct(config) when is_list(config) do |
| 226 | + case Keyword.keyword?(config) do |
| 227 | + false -> to_struct(:default) |
| 228 | + true -> |
| 229 | + %__MODULE__{} |
| 230 | + |> to_struct(:settings, Keyword.get(config, :settings, [])) |
| 231 | + |> to_struct(:releases, Keyword.get(config, :releases, [])) |
| 232 | + |> to_struct(:overrides, Keyword.get(config, :overrides, [])) |
| 233 | + |> to_struct(:overlays, Keyword.get(config, :overlays, [])) |
| 234 | + end |
| 235 | + end |
| 236 | + # If no config is given, generate a default release definition for the current project. |
| 237 | + # If the current project is an umbrella, generate a release which contains all applications |
| 238 | + # in the umbrella. |
| 239 | + defp to_struct(_) do |
| 240 | + current_project = Mix.Project.config |
| 241 | + case Mix.Project.umbrella?(current_project) do |
| 242 | + true -> |
| 243 | + apps_path = Keyword.fetch!(current_project, :apps_path) |
| 244 | + apps = get_umbrella_apps(apps_path) |
| 245 | + app = convert_to_name(Mix.Project.get!) |
| 246 | + version = "0.1.0" |
| 247 | + %__MODULE__{ |
| 248 | + releases: [ReleaseDefinition.new(app, version, apps)] |
| 249 | + } |
| 250 | + false -> |
| 251 | + app = Keyword.fetch!(current_project, :app) |
| 252 | + version = Keyword.fetch!(current_project, :version) |
| 253 | + %__MODULE__{ |
| 254 | + releases: [ReleaseDefinition.new(app, version, [app])] |
| 255 | + } |
| 256 | + end |
| 257 | + end |
| 258 | + |
| 259 | + defp to_struct(config, :settings, []), do: config |
| 260 | + defp to_struct(config, :settings, s) do |
| 261 | + %__MODULE__{ |
| 262 | + config | |
| 263 | + :dev_mode => Keyword.get(s, :dev_mode, config.dev_mode), |
| 264 | + :paths => Keyword.get(s, :paths, config.paths), |
| 265 | + :vm_args => Keyword.get(s, :vm_args, config.vm_args), |
| 266 | + :sys_config => Keyword.get(s, :sys_config, config.sys_config), |
| 267 | + :include_erts => Keyword.get(s, :include_erts, config.include_erts), |
| 268 | + :include_src => Keyword.get(s, :include_erts, config.include_src), |
| 269 | + :erl_opts => Keyword.get(s, :erl_opts, config.erl_opts), |
| 270 | + :include_system_libs => Keyword.get(s, :include_system_libs, config.include_system_libs), |
| 271 | + :strip_debug_info? => Keyword.get(s, :strip_debug_info?, config.strip_debug_info?), |
| 272 | + :overlay_vars => Keyword.get(s, :overlay_vars, config.overlay_vars), |
| 273 | + :pre_start_hook => Keyword.get(s, :pre_start_hook, config.pre_start_hook), |
| 274 | + :post_start_hook => Keyword.get(s, :post_start_hook, config.post_start_hook), |
| 275 | + :pre_stop_hook => Keyword.get(s, :pre_stop_hook, config.pre_stop_hook), |
| 276 | + :post_stop_hook => Keyword.get(s, :post_stop_hook, config.post_stop_hook) |
| 277 | + } |
| 278 | + end |
| 279 | + defp to_struct(config, :releases, []), do: config |
| 280 | + defp to_struct(config, :releases, r) do |
| 281 | + releases = Enum.flat_map(r, fn |
| 282 | + {app, [{version, []}]}-> |
| 283 | + [ReleaseDefinition.new(app, version, [app])] |
| 284 | + {app, [{version, apps}]} when is_list(apps) -> |
| 285 | + [ReleaseDefinition.new(app, version, Enum.uniq([app|apps]))] |
| 286 | + {app, versions} when is_list(versions) -> |
| 287 | + Enum.map(versions, fn |
| 288 | + {version, []} -> |
| 289 | + ReleaseDefinition.new(app, version) |
| 290 | + {version, apps} when is_list(apps) -> |
| 291 | + ReleaseDefinition.new(app, version, Enum.uniq([app|apps])) |
| 292 | + end) |
| 293 | + end) |
| 294 | + %__MODULE__{config | :releases => releases} |
| 295 | + end |
| 296 | + defp to_struct(config, :overrides, o) do |
| 297 | + %__MODULE__{config | :overrides => o} |
| 298 | + end |
| 299 | + defp to_struct(config, :overlays, o) do |
| 300 | + %__MODULE__{config | :overlays => o} |
| 301 | + end |
| 302 | + |
| 303 | + defp convert_to_name(module) when is_atom(module) do |
| 304 | + [name_str|_] = Module.split(module) |
| 305 | + Regex.split(~r/(?<word>[A-Z][^A-Z]*)/, name_str, on: [:word], include_captures: true, trim: true) |
| 306 | + |> Enum.map(&String.downcase/1) |
| 307 | + |> Enum.join("_") |
| 308 | + |> String.to_atom |
| 309 | + end |
| 310 | + |
| 311 | + defp get_umbrella_apps(apps_path) do |
| 312 | + Path.wildcard(Path.join(apps_path, "*")) |
| 313 | + |> Enum.map(fn path -> |
| 314 | + Mix.Project.in_project(:app, path, fn _mixfile -> |
| 315 | + Keyword.fetch!(Mix.Project.config, :app) |
| 316 | + end) |
| 317 | + end) |
| 318 | + end |
| 319 | + |
| 320 | +end |
0 commit comments