Skip to content

Commit 1c73b96

Browse files
committed
feat: add igniter support
1 parent b89a125 commit 1c73b96

4 files changed

Lines changed: 285 additions & 1 deletion

File tree

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
if Code.ensure_loaded?(Igniter) && Code.ensure_loaded?(Tower.Igniter) do
2+
defmodule Mix.Tasks.TowerEmail.Install do
3+
@example "mix igniter.install tower_email"
4+
5+
@shortdoc "Installs TowerEmail. Invoke with `mix igniter.install tower_email`"
6+
@moduledoc """
7+
#{@shortdoc}
8+
9+
## Example
10+
11+
```bash
12+
#{@example}
13+
```
14+
"""
15+
16+
alias Sourceror.Zipper
17+
18+
use Igniter.Mix.Task
19+
20+
import Tower.Igniter
21+
22+
@impl Igniter.Mix.Task
23+
def info(_argv, _composing_task) do
24+
%Igniter.Mix.Task.Info{
25+
group: :tower,
26+
adds_deps: [],
27+
installs: [],
28+
example: @example,
29+
only: nil,
30+
positional: [],
31+
composes: [],
32+
schema: [],
33+
defaults: [],
34+
aliases: [],
35+
required: []
36+
}
37+
end
38+
39+
@impl Igniter.Mix.Task
40+
def igniter(igniter) do
41+
app_name = Igniter.Project.Application.app_name(igniter)
42+
prod_file_path = config_file_path(igniter, "prod.exs")
43+
runtime_file_path = config_file_path(igniter, "runtime.exs")
44+
45+
igniter
46+
|> add_reporter_to_config(TowerEmail)
47+
|> Igniter.Project.Config.configure(
48+
"config.exs",
49+
:tower_email,
50+
[:from],
51+
{:code, Sourceror.parse_string!("{\"Tower\", \"tower@<YOUR_DOMAIN>\"}")},
52+
after: &match?({:config, _, [{_, _, [:tower]} | _]}, &1.node)
53+
)
54+
|> Igniter.Project.Config.configure(
55+
"config.exs",
56+
:tower_email,
57+
[:to],
58+
"<RECIPIENT EMAIL ADDRESS>"
59+
)
60+
|> Igniter.Project.Config.configure(
61+
"runtime.exs",
62+
:tower_email,
63+
[:otp_app],
64+
app_name
65+
)
66+
|> Igniter.Project.Config.configure(
67+
"runtime.exs",
68+
:tower_email,
69+
[:environment],
70+
{:code,
71+
Sourceror.parse_string!("System.get_env(\"DEPLOYMENT_ENV\", to_string(config_env()))")}
72+
)
73+
|> Igniter.Project.Config.configure(
74+
"dev.exs",
75+
:tower_email,
76+
[TowerEmail.Mailer, :adapter],
77+
Swoosh.Adapters.Local
78+
)
79+
|> add_commented_config(
80+
prod_file_path,
81+
"Uncomment this line to use Postmark as Provider or use the provider of your choosing",
82+
"config :tower_email, TowerEmail.Mailer, adapter: Swoosh.Adapter.Postmark"
83+
)
84+
|> add_commented_config(
85+
runtime_file_path,
86+
"Uncomment this line to use configure Postmark's API key, or configure your providers env variables",
87+
"config :tower_email, TowerEmail.Mailer, api_key: System.fetch_env!(\"POSTMARK_API_KEY\")"
88+
)
89+
end
90+
91+
defp config_file_path(igniter, file_name) do
92+
case igniter |> Igniter.Project.Application.config_path() |> Path.split() do
93+
[path] -> [path]
94+
path -> Enum.drop(path, -1)
95+
end
96+
|> Path.join()
97+
|> Path.join(file_name)
98+
end
99+
100+
defp add_commented_config(igniter, file_path, comment, config_line) do
101+
Igniter.update_elixir_file(igniter, file_path, fn zipper ->
102+
# Check if the comment already exists to avoid duplicate comments
103+
file_content =
104+
zipper
105+
|> Zipper.root()
106+
|> Sourceror.to_string()
107+
108+
if String.contains?(file_content, "# #{comment}") do
109+
# Comment already exists, don't add it again
110+
{:ok, zipper}
111+
else
112+
# Add the commented lines directly
113+
zipper =
114+
zipper
115+
|> Zipper.topmost()
116+
|> Zipper.update(fn node ->
117+
case node do
118+
{:__block__, meta, children} ->
119+
new_meta =
120+
meta
121+
|> Keyword.update(
122+
:trailing_comments,
123+
[
124+
%{
125+
line: 1,
126+
text: "# #{comment}",
127+
column: 1,
128+
next_eol_count: 0,
129+
previous_eol_count: 2
130+
},
131+
%{
132+
line: 2,
133+
text: "# #{config_line}",
134+
column: 1,
135+
next_eol_count: 1,
136+
previous_eol_count: 1
137+
}
138+
],
139+
fn existing_comments ->
140+
existing_comments ++
141+
[
142+
%{
143+
line: 1,
144+
text: "# #{comment}",
145+
column: 1,
146+
next_eol_count: 0,
147+
previous_eol_count: 2
148+
},
149+
%{
150+
line: 2,
151+
text: "# #{config_line}",
152+
column: 1,
153+
next_eol_count: 1,
154+
previous_eol_count: 1
155+
}
156+
]
157+
end
158+
)
159+
160+
{:__block__, new_meta, children}
161+
162+
other ->
163+
other
164+
end
165+
end)
166+
167+
{:ok, zipper}
168+
end
169+
end)
170+
end
171+
end
172+
else
173+
defmodule Mix.Tasks.TowerEmail.Install do
174+
@example "mix igniter.install tower_email"
175+
176+
@shortdoc "Installs TowerEmail. Invoke with `mix igniter.install tower_email`"
177+
178+
@moduledoc """
179+
#{@shortdoc}
180+
181+
## Example
182+
183+
```bash
184+
#{@example}
185+
```
186+
"""
187+
188+
use Mix.Task
189+
190+
@impl Mix.Task
191+
def run(_argv) do
192+
Mix.shell().error("""
193+
The task 'tower_email.install' requires igniter. Please install igniter and try again.
194+
195+
For more information, see: https://hexdocs.pm/igniter/readme.html#installation
196+
""")
197+
198+
exit({:shutdown, 1})
199+
end
200+
end
201+
end

mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ defmodule TowerEmail.MixProject do
3737

3838
# Optional
3939
{:hackney, "~> 1.20", optional: true},
40+
{:igniter, "~> 0.6", optional: true},
4041

4142
# Dev
4243
{:blend, "~> 0.5.0", only: :dev},

mix.lock

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,34 @@
33
"certifi": {:hex, :certifi, "2.14.0", "ed3bef654e69cde5e6c022df8070a579a79e8ba2368a00acf3d75b82d9aceeed", [:rebar3], [], "hexpm", "ea59d87ef89da429b8e905264fdec3419f84f2215bb3d81e07a18aac919026c3"},
44
"earmark_parser": {:hex, :earmark_parser, "1.4.43", "34b2f401fe473080e39ff2b90feb8ddfeef7639f8ee0bbf71bb41911831d77c5", [:mix], [], "hexpm", "970a3cd19503f5e8e527a190662be2cee5d98eed1ff72ed9b3d1a3d466692de8"},
55
"ex_doc": {:hex, :ex_doc, "0.37.3", "f7816881a443cd77872b7d6118e8a55f547f49903aef8747dbcb345a75b462f9", [:mix], [{:earmark_parser, "~> 1.4.42", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "e6aebca7156e7c29b5da4daa17f6361205b2ae5f26e5c7d8ca0d3f7e18972233"},
6+
"finch": {:hex, :finch, "0.19.0", "c644641491ea854fc5c1bbaef36bfc764e3f08e7185e1f084e35e0672241b76d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "fc5324ce209125d1e2fa0fcd2634601c52a787aff1cd33ee833664a5af4ea2b6"},
7+
"glob_ex": {:hex, :glob_ex, "0.1.11", "cb50d3f1ef53f6ca04d6252c7fde09fd7a1cf63387714fe96f340a1349e62c93", [:mix], [], "hexpm", "342729363056e3145e61766b416769984c329e4378f1d558b63e341020525de4"},
68
"hackney": {:hex, :hackney, "1.23.0", "55cc09077112bcb4a69e54be46ed9bc55537763a96cd4a80a221663a7eafd767", [:rebar3], [{:certifi, "~> 2.14.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "6cd1c04cd15c81e5a493f167b226a15f0938a84fc8f0736ebe4ddcab65c0b44e"},
9+
"hpax": {:hex, :hpax, "1.0.3", "ed67ef51ad4df91e75cc6a1494f851850c0bd98ebc0be6e81b026e765ee535aa", [:mix], [], "hexpm", "8eab6e1cfa8d5918c2ce4ba43588e894af35dbd8e91e6e55c817bca5847df34a"},
710
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
11+
"igniter": {:hex, :igniter, "0.6.9", "99dd9ea7bcf2fe829617dac660069b3461183e4efbf303dd120fdef96923287d", [:mix], [{:glob_ex, "~> 0.1.7", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:owl, "~> 0.11", [hex: :owl, repo: "hexpm", optional: false]}, {:phx_new, "~> 1.7", [hex: :phx_new, repo: "hexpm", optional: true]}, {:req, "~> 0.5", [hex: :req, repo: "hexpm", optional: false]}, {:rewrite, ">= 1.1.1 and < 2.0.0-0", [hex: :rewrite, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.4", [hex: :sourceror, repo: "hexpm", optional: false]}, {:spitfire, ">= 0.1.3 and < 1.0.0-0", [hex: :spitfire, repo: "hexpm", optional: false]}], "hexpm", "5fe407e10bc9416f7cd6af90d0409c8226ff2acacb9a7e7b9a097a66c8b5caef"},
812
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
913
"makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"},
1014
"makeup_elixir": {:hex, :makeup_elixir, "1.0.1", "e928a4f984e795e41e3abd27bfc09f51db16ab8ba1aebdba2b3a575437efafc2", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "7284900d412a3e5cfd97fdaed4f5ed389b8f2b4cb49efc0eb3bd10e2febf9507"},
1115
"makeup_erlang": {:hex, :makeup_erlang, "1.0.2", "03e1804074b3aa64d5fad7aa64601ed0fb395337b982d9bcf04029d68d51b6a7", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "af33ff7ef368d5893e4a267933e7744e46ce3cf1f61e2dccf53a111ed3aa3727"},
1216
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
1317
"mime": {:hex, :mime, "2.0.6", "8f18486773d9b15f95f4f4f1e39b710045fa1de891fada4516559967276e4dc2", [:mix], [], "hexpm", "c9945363a6b26d747389aac3643f8e0e09d30499a138ad64fe8fd1d13d9b153e"},
1418
"mimerl": {:hex, :mimerl, "1.3.0", "d0cd9fc04b9061f82490f6581e0128379830e78535e017f7780f37fea7545726", [:rebar3], [], "hexpm", "a1e15a50d1887217de95f0b9b0793e32853f7c258a5cd227650889b38839fe9d"},
19+
"mint": {:hex, :mint, "1.7.1", "113fdb2b2f3b59e47c7955971854641c61f378549d73e829e1768de90fc1abf1", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "fceba0a4d0f24301ddee3024ae116df1c3f4bb7a563a731f45fdfeb9d39a231b"},
20+
"nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"},
1521
"nimble_parsec": {:hex, :nimble_parsec, "1.4.2", "8efba0122db06df95bfaa78f791344a89352ba04baedd3849593bfce4d0dc1c6", [:mix], [], "hexpm", "4b21398942dda052b403bbe1da991ccd03a053668d147d53fb8c4e0efe09c973"},
22+
"nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"},
23+
"owl": {:hex, :owl, "0.12.2", "65906b525e5c3ef51bab6cba7687152be017aebe1da077bb719a5ee9f7e60762", [:mix], [{:ucwidth, "~> 0.2", [hex: :ucwidth, repo: "hexpm", optional: true]}], "hexpm", "6398efa9e1fea70a04d24231e10dcd66c1ac1aa2da418d20ef5357ec61de2880"},
1624
"parse_trans": {:hex, :parse_trans, "3.4.1", "6e6aa8167cb44cc8f39441d05193be6e6f4e7c2946cb2759f015f8c56b76e5ff", [:rebar3], [], "hexpm", "620a406ce75dada827b82e453c19cf06776be266f5a67cff34e1ef2cbb60e49a"},
25+
"req": {:hex, :req, "0.5.12", "7ce85835867a114c28b6cfc2d8a412f86660290907315ceb173a00e587b853d2", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "d65f3d0e7032eb245706554cb5240dbe7a07493154e2dd34e7bb65001aa6ef32"},
26+
"rewrite": {:hex, :rewrite, "1.1.2", "f5a5d10f5fed1491a6ff48e078d4585882695962ccc9e6c779bae025d1f92eda", [:mix], [{:glob_ex, "~> 0.1", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.0", [hex: :sourceror, repo: "hexpm", optional: false]}, {:text_diff, "~> 0.1", [hex: :text_diff, repo: "hexpm", optional: false]}], "hexpm", "7f8b94b1e3528d0a47b3e8b7bfeca559d2948a65fa7418a9ad7d7712703d39d4"},
27+
"sourceror": {:hex, :sourceror, "1.10.0", "38397dedbbc286966ec48c7af13e228b171332be1ad731974438c77791945ce9", [:mix], [], "hexpm", "29dbdfc92e04569c9d8e6efdc422fc1d815f4bd0055dc7c51b8800fb75c4b3f1"},
28+
"spitfire": {:hex, :spitfire, "0.2.1", "29e154873f05444669c7453d3d931820822cbca5170e88f0f8faa1de74a79b47", [:mix], [], "hexpm", "6eeed75054a38341b2e1814d41bb0a250564092358de2669fdb57ff88141d91b"},
1729
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
1830
"swoosh": {:hex, :swoosh, "1.19.0", "b2d62ed899faba6a499bbc19dd8e09452121133a6c8c2c867fc38e37c811c890", [:mix], [{:bandit, ">= 1.0.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:cowboy, "~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:ex_aws, "~> 2.1", [hex: :ex_aws, repo: "hexpm", optional: true]}, {:finch, "~> 0.6", [hex: :finch, repo: "hexpm", optional: true]}, {:gen_smtp, "~> 0.13 or ~> 1.0", [hex: :gen_smtp, repo: "hexpm", optional: true]}, {:hackney, "~> 1.9", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mail, "~> 0.2", [hex: :mail, repo: "hexpm", optional: true]}, {:mime, "~> 1.1 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mua, "~> 0.2.3", [hex: :mua, repo: "hexpm", optional: true]}, {:multipart, "~> 0.4", [hex: :multipart, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: true]}, {:plug_cowboy, ">= 1.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:req, "~> 0.5.10 or ~> 0.6 or ~> 1.0", [hex: :req, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e4ab3fd9dd69db4c89c518c62a8a8f2b879a4885bdcbcbc4be46b6b5381e9f12"},
1931
"telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"},
20-
"tower": {:hex, :tower, "0.8.2", "85bf5a5d48085ff0a9b58b20ca93d0b7be63e64b7ddede022512e677da967447", [:mix], [{:bandit, "~> 1.6", [hex: :bandit, repo: "hexpm", optional: true]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}, {:uuid_v7, "~> 0.6.0", [hex: :uuid_v7, repo: "hexpm", optional: false]}], "hexpm", "1aae6bfae9056758a9637fd0314c6d9b17c156b5da3940c013b20fed53fb2316"},
32+
"text_diff": {:hex, :text_diff, "0.1.0", "1caf3175e11a53a9a139bc9339bd607c47b9e376b073d4571c031913317fecaa", [:mix], [], "hexpm", "d1ffaaecab338e49357b6daa82e435f877e0649041ace7755583a0ea3362dbd7"},
33+
"tower": {:hex, :tower, "0.8.3", "853c26b390e14dbb6d39022a5fe526a35a8a8732f4ed53d91cc0e053cda344da", [:mix], [{:bandit, "~> 1.6", [hex: :bandit, repo: "hexpm", optional: true]}, {:igniter, "~> 0.6", [hex: :igniter, repo: "hexpm", optional: true]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}, {:uuid_v7, "~> 0.6.0", [hex: :uuid_v7, repo: "hexpm", optional: false]}], "hexpm", "e8af9b32d2d2b1b0ec79a066a0939f57b59e88eaefde47a13f708596fd6ef9fa"},
2134
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
2235
"uuid_v7": {:hex, :uuid_v7, "0.6.0", "1d65727ade8ca619ed40fdef90c4186b50c84657d2b412f7cb79777ab2d47559", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "1dc401134e61da847a7b2a3b28d2593893f457b9f2704893b1ba3ff7946ce91f"},
2336
}

0 commit comments

Comments
 (0)