From 916d45e4432581dba2e77aa17b8f624b73aa27ea Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Fri, 6 Sep 2024 14:57:49 -0400 Subject: [PATCH] improvement: add `configure_runtime_env` codemod --- lib/igniter/project/config.ex | 93 +++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/lib/igniter/project/config.ex b/lib/igniter/project/config.ex index c22815b..b7abc91 100644 --- a/lib/igniter/project/config.ex +++ b/lib/igniter/project/config.ex @@ -27,6 +27,99 @@ defmodule Igniter.Project.Config do ) end + @spec configure_runtime_env( + Igniter.t(), + atom(), + atom(), + list(atom), + term(), + opts :: Keyword.t() + ) :: + Igniter.t() + def configure_runtime_env(igniter, env, app_name, config_path, value, opts \\ []) do + default_runtime = + """ + import Config + + if config_env() == #{inspect(env)} do + end + """ + + igniter + |> Igniter.create_or_update_file("config/runtime.exs", default_runtime, &{:ok, &1}) + |> Igniter.update_elixir_file("config/runtime.exs", fn zipper -> + patterns = [ + """ + if config_env() == :prod do + __cursor__() + end + """, + """ + if :prod == config_env() do + __cursor__() + end + """ + ] + + modify_to = + case value do + {:code, value} -> value + value -> Sourceror.parse_string!(Sourceror.to_string(Macro.escape(value))) + end + + zipper + |> Igniter.Code.Common.move_to_cursor_match_in_scope(patterns) + |> case do + {:ok, zipper} -> + modify_configuration_code( + zipper, + config_path, + app_name, + modify_to, + opts[:updater] || (&{:ok, &1}) + ) + + :error -> + zipper + |> Igniter.Code.Common.add_code(""" + if config_env() == :prod do + end + """) + |> Igniter.Code.Common.move_to_cursor_match_in_scope(patterns) + |> case do + {:ok, zipper} -> + modify_configuration_code( + zipper, + config_path, + app_name, + modify_to, + opts[:updater] || (&{:ok, &1}) + ) + + _ -> + value = + case value do + {:code, value} -> Sourceror.to_string(value) + value -> inspect(value) + end + + {:warning, + """ + Could not set #{inspect([app_name | config_path])} in `#{inspect(env)}` of `config/runtime.exs`. + + ```elixir + # in `runtime.exs` + if config_env() == :prod do + # Please configure #{inspect([app_name | config_path])} it to the following value + #{value} + end + ``` + """} + end + end + end) + end + @doc """ Sets a config value in the given configuration file, updating it with `updater` if it is already set.