Skip to content

Commit 06bb4b0

Browse files
committed
PR feedback & ensure installer is idempotent
1 parent 96cd88e commit 06bb4b0

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

lib/mix/tasks/sentry.install.ex

+26-17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ defmodule Mix.Tasks.Sentry.Install.Docs do
1313
"""
1414
#{short_doc()}
1515
16+
This installer is built with, and requires, igniter to be used. Igniter is a tool
17+
for biulding package installers. For information on how to add igniter to your
18+
project, see the documentation https://hexdocs.pm/igniter/readme.html#installation.
19+
1620
## Example
1721
1822
```bash
@@ -51,16 +55,6 @@ if Code.ensure_loaded?(Igniter) do
5155
def igniter(igniter) do
5256
app_name = Igniter.Project.Application.app_name(igniter)
5357

54-
mix_env_code =
55-
quote do
56-
Mix.env()
57-
end
58-
59-
cwd_code =
60-
quote do
61-
[File.cwd!()]
62-
end
63-
6458
# Do your work here and return an updated igniter
6559
igniter
6660
|> Igniter.Project.Config.configure(
@@ -85,7 +79,7 @@ if Code.ensure_loaded?(Igniter) do
8579
"prod.exs",
8680
app_name,
8781
[:root_source_code_paths],
88-
{:code, quote(do: [File.cwd!()]}
82+
{:code, quote(do: [File.cwd!()])}
8983
)
9084
|> configure_phoenix()
9185
|> add_logger_handler()
@@ -106,7 +100,14 @@ if Code.ensure_loaded?(Igniter) do
106100

107101
Igniter.Project.Module.find_and_update_module(igniter, app_module, fn zipper ->
108102
with {:ok, zipper} <- Igniter.Code.Function.move_to_def(zipper, :start, 2),
109-
{:ok, zipper} <- Igniter.Code.Common.move_to_do_block(zipper) do
103+
{:ok, zipper} <- Igniter.Code.Common.move_to_do_block(zipper),
104+
:error <-
105+
Igniter.Code.Function.move_to_function_call_in_current_scope(
106+
zipper,
107+
{:logger, :add_handler},
108+
[2, 3, 4],
109+
&Igniter.Code.Function.argument_equals?(&1, 1, Sentry.LoggerHandler)
110+
) do
110111
code =
111112
"""
112113
:logger.add_handler(:sentry_handler, Sentry.LoggerHandler, %{
@@ -149,26 +150,34 @@ if Code.ensure_loaded?(Igniter) do
149150
end
150151

151152
defp add_plug_capture(zipper) do
152-
with {:ok, zipper} <- Igniter.Code.Module.move_to_use(zipper, Phoenix.Endpoint) do
153+
with :error <- Igniter.Code.Module.move_to_use(zipper, Sentry.PlugCapture),
154+
{:ok, zipper} <- Igniter.Code.Module.move_to_use(zipper, Phoenix.Endpoint) do
153155
Igniter.Code.Common.add_code(zipper, "use Sentry.PlugCapture", placement: :before)
154156
else
155157
_ ->
156-
{:ok, zipper}
158+
zipper
157159
end
158160
end
159161

160162
defp add_plug_context(zipper) do
161-
with {:ok, zipper} <-
163+
with :error <-
164+
Igniter.Code.Function.move_to_function_call_in_current_scope(
165+
zipper,
166+
:plug,
167+
[1, 2],
168+
&Igniter.Code.Function.argument_equals?(&1, 0, Sentry.PlugContext)
169+
),
170+
{:ok, zipper} <-
162171
Igniter.Code.Function.move_to_function_call_in_current_scope(
163172
zipper,
164173
:plug,
165-
[2, 1],
174+
[1, 2],
166175
&Igniter.Code.Function.argument_equals?(&1, 0, Plug.Parsers)
167176
) do
168177
Igniter.Code.Common.add_code(zipper, "plug Sentry.PlugContext", placement: :after)
169178
else
170179
_ ->
171-
{:ok, zipper}
180+
zipper
172181
end
173182
end
174183
end

mix.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"hackney": {:hex, :hackney, "1.20.1", "8d97aec62ddddd757d128bfd1df6c5861093419f8f7a4223823537bad5d064e2", [:rebar3], [{:certifi, "~> 2.12.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", "fe9094e5f1a2a2c0a7d10918fee36bfec0ec2a979994cff8cfe8058cd9af38e3"},
2424
"hpax": {:hex, :hpax, "1.0.1", "c857057f89e8bd71d97d9042e009df2a42705d6d690d54eca84c8b29af0787b0", [:mix], [], "hexpm", "4e2d5a4f76ae1e3048f35ae7adb1641c36265510a2d4638157fbcb53dda38445"},
2525
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
26-
"igniter": {:hex, :igniter, "0.5.37", "e677c009fcad535759dfb8fec0214af38acf1f27b7d6d0277d6be61d6bb391e8", [:mix], [{:glob_ex, "~> 0.1.7", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:inflex, "~> 2.0", [hex: :inflex, 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", "ca4e5f7c1edead2c6b8955886b010878660c69f3a3f97bf46641b3c39ed339ce"},
26+
"igniter": {:hex, :igniter, "0.5.38", "436a6414abc9245e539d6c92a6f4854f62270fbf6547c4acf1e5a65c0e4f4d4b", [:mix], [{:glob_ex, "~> 0.1.7", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:inflex, "~> 2.0", [hex: :inflex, 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", "e5f1474a2a7ad186f3b71074d9d1ef25d306634e12af4ea01beae06ba958491d"},
2727
"inflex": {:hex, :inflex, "2.1.0", "a365cf0821a9dacb65067abd95008ca1b0bb7dcdd85ae59965deef2aa062924c", [:mix], [], "hexpm", "14c17d05db4ee9b6d319b0bff1bdf22aa389a25398d1952c7a0b5f3d93162dd8"},
2828
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
2929
"makeup": {:hex, :makeup, "1.1.2", "9ba8837913bdf757787e71c1581c21f9d2455f4dd04cfca785c70bbfff1a76a3", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cce1566b81fbcbd21eca8ffe808f33b221f9eee2cbc7a1706fc3da9ff18e6cac"},

test/mix/sentry.install_test.exs

+20-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,28 @@ defmodule Mix.Tasks.Sentry.InstallTest do
2020
+ | plug Sentry.PlugContext
2121
""")
2222
|> assert_has_patch("lib/test/application.ex", """
23-
+ | :logger.add_handler(:my_sentry_handler, Sentry.LoggerHandler, %{
23+
+ | :logger.add_handler(:sentry_handler, Sentry.LoggerHandler, %{
2424
+ | config: %{metadata: [:file, :line]}
2525
+ | })
2626
""")
2727
end
28+
29+
test "installation is idempotent" do
30+
phx_test_project()
31+
|> Igniter.compose_task("sentry.install", ["--dsn", "test_dsn"])
32+
|> apply_igniter!()
33+
|> Igniter.compose_task("sentry.install", ["--dsn", "test_dsn"])
34+
|> assert_unchanged()
35+
end
36+
37+
test "installation will reset your dsn for you, however" do
38+
phx_test_project()
39+
|> Igniter.compose_task("sentry.install", ["--dsn", "test_dsn"])
40+
|> apply_igniter!()
41+
|> Igniter.compose_task("sentry.install", ["--dsn", "test_dsn2"])
42+
|> assert_has_patch("config/prod.exs", """
43+
- | dsn: "test_dsn",
44+
+ | dsn: "test_dsn2",
45+
""")
46+
end
2847
end

0 commit comments

Comments
 (0)