-
-
Notifications
You must be signed in to change notification settings - Fork 214
Add installer (based on Igniter) #876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3981ff7
improvement: add igniter installer
zachdaniel cfc63d0
chore: add to changelog, fix dialyzer warning
zachdaniel 5cccfaf
chore: remove file accidentally added
zachdaniel 052e735
chore: properly base mix.lock off of main
zachdaniel a315b24
chore: update spitfire
zachdaniel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| defmodule Mix.Tasks.Sentry.Install.Docs do | ||
| @moduledoc false | ||
|
|
||
| def short_doc do | ||
| "Installs sentry. Requires igniter to be installed." | ||
|
zachdaniel marked this conversation as resolved.
|
||
| end | ||
|
|
||
| def example do | ||
| "mix sentry.install --dsn <your_dsn>" | ||
| end | ||
|
|
||
| def long_doc do | ||
| """ | ||
| #{short_doc()} | ||
|
zachdaniel marked this conversation as resolved.
|
||
|
|
||
| This installer is built with, and requires, igniter to be used. Igniter is a tool | ||
| for biulding package installers. For information on how to add igniter to your | ||
| project, see the documentation https://hexdocs.pm/igniter/readme.html#installation. | ||
|
|
||
| ## Example | ||
|
|
||
| ```bash | ||
| #{example()} | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| * `--dsn` - Your Sentry DSN. | ||
|
|
||
| """ | ||
| end | ||
| end | ||
|
|
||
| if Code.ensure_loaded?(Igniter) do | ||
| defmodule Mix.Tasks.Sentry.Install do | ||
| @shortdoc "#{__MODULE__.Docs.short_doc()}" | ||
|
|
||
| @moduledoc __MODULE__.Docs.long_doc() | ||
|
|
||
| use Igniter.Mix.Task | ||
|
|
||
| @impl Igniter.Mix.Task | ||
| def info(_argv, _composing_task) do | ||
| %Igniter.Mix.Task.Info{ | ||
| group: :sentry, | ||
| adds_deps: [{:jason, "~> 1.2"}, {:hackney, "~> 1.8"}], | ||
| example: __MODULE__.Docs.example(), | ||
| schema: [dsn: :string], | ||
| defaults: [dsn: "<your_dsn>"] | ||
| } | ||
| end | ||
|
|
||
| @impl Igniter.Mix.Task | ||
| def igniter(igniter) do | ||
| app_name = Igniter.Project.Application.app_name(igniter) | ||
|
|
||
| igniter | ||
| |> Igniter.Project.Config.configure( | ||
| "prod.exs", | ||
| app_name, | ||
| [:dsn], | ||
| igniter.args.options[:dsn] | ||
| ) | ||
| |> Igniter.Project.Config.configure( | ||
| "prod.exs", | ||
| app_name, | ||
| [:environment_name], | ||
| {:code, quote(do: Mix.env())} | ||
| ) | ||
| |> Igniter.Project.Config.configure( | ||
| "prod.exs", | ||
| app_name, | ||
| [:enable_source_code_context], | ||
| true | ||
| ) | ||
| |> Igniter.Project.Config.configure( | ||
| "prod.exs", | ||
| app_name, | ||
| [:root_source_code_paths], | ||
| {:code, quote(do: [File.cwd!()])} | ||
| ) | ||
| |> configure_phoenix() | ||
| |> add_logger_handler() | ||
| |> Igniter.add_notice(""" | ||
| Sentry: | ||
|
|
||
| Add a call to | ||
|
|
||
| mix sentry.package_source_code | ||
|
|
||
| in your release script to make sure the stacktraces you receive | ||
| are correctly categorized. | ||
| """) | ||
| end | ||
|
|
||
| defp add_logger_handler(igniter) do | ||
| app_module = Igniter.Project.Application.app_module(igniter) | ||
|
|
||
| Igniter.Project.Module.find_and_update_module(igniter, app_module, fn zipper -> | ||
| with {:ok, zipper} <- Igniter.Code.Function.move_to_def(zipper, :start, 2), | ||
| :error <- | ||
| Igniter.Code.Function.move_to_function_call_in_current_scope( | ||
| zipper, | ||
| {:logger, :add_handler}, | ||
| [2, 3, 4], | ||
| &Igniter.Code.Function.argument_equals?(&1, 1, Sentry.LoggerHandler) | ||
| ) do | ||
| code = | ||
| """ | ||
| :logger.add_handler(:sentry_handler, Sentry.LoggerHandler, %{ | ||
| config: %{metadata: [:file, :line]} | ||
| }) | ||
| """ | ||
|
|
||
| {:ok, Igniter.Code.Common.add_code(zipper, code, placement: :before)} | ||
| else | ||
| _ -> {:ok, zipper} | ||
| end | ||
| end) | ||
| |> case do | ||
| {:ok, igniter} -> igniter | ||
| _ -> igniter | ||
| end | ||
| end | ||
|
|
||
| defp configure_phoenix(igniter) do | ||
| {igniter, routers} = | ||
| Igniter.Libs.Phoenix.list_routers(igniter) | ||
|
|
||
| {igniter, endpoints} = | ||
| Enum.reduce(routers, {igniter, []}, fn router, {igniter, endpoints} -> | ||
| {igniter, new_endpoints} = Igniter.Libs.Phoenix.endpoints_for_router(igniter, router) | ||
| {igniter, endpoints ++ new_endpoints} | ||
| end) | ||
|
|
||
| Enum.reduce(endpoints, igniter, fn endpoint, igniter -> | ||
| igniter | ||
| |> setup_endpoint(endpoint) | ||
| end) | ||
| end | ||
|
|
||
| defp setup_endpoint(igniter, endpoint) do | ||
| Igniter.Project.Module.find_and_update_module!(igniter, endpoint, fn zipper -> | ||
| zipper | ||
| |> Igniter.Code.Common.within(&add_plug_capture/1) | ||
| |> Igniter.Code.Common.within(&add_plug_context/1) | ||
| |> then(&{:ok, &1}) | ||
| end) | ||
| end | ||
|
|
||
| defp add_plug_capture(zipper) do | ||
| with :error <- Igniter.Code.Module.move_to_use(zipper, Sentry.PlugCapture), | ||
| {:ok, zipper} <- Igniter.Code.Module.move_to_use(zipper, Phoenix.Endpoint) do | ||
| Igniter.Code.Common.add_code(zipper, "use Sentry.PlugCapture", placement: :before) | ||
| else | ||
| _ -> | ||
| zipper | ||
| end | ||
| end | ||
|
|
||
| defp add_plug_context(zipper) do | ||
| with :error <- | ||
| Igniter.Code.Function.move_to_function_call_in_current_scope( | ||
| zipper, | ||
| :plug, | ||
| [1, 2], | ||
| &Igniter.Code.Function.argument_equals?(&1, 0, Sentry.PlugContext) | ||
| ), | ||
| {:ok, zipper} <- | ||
| Igniter.Code.Function.move_to_function_call_in_current_scope( | ||
| zipper, | ||
| :plug, | ||
| [1, 2], | ||
| &Igniter.Code.Function.argument_equals?(&1, 0, Plug.Parsers) | ||
| ) do | ||
| Igniter.Code.Common.add_code(zipper, "plug Sentry.PlugContext", placement: :after) | ||
| else | ||
| _ -> | ||
| zipper | ||
| end | ||
| end | ||
| end | ||
| else | ||
| defmodule Mix.Tasks.Sentry.Install do | ||
| @shortdoc "#{__MODULE__.Docs.short_doc()} | Install `igniter` to use" | ||
|
|
||
| @moduledoc __MODULE__.Docs.long_doc() | ||
|
|
||
| use Mix.Task | ||
|
|
||
| def run(_argv) do | ||
| Mix.shell().error(""" | ||
| The task 'sentry.install' requires igniter. Please install igniter and try again. | ||
|
|
||
| For more information, see: https://hexdocs.pm/igniter/readme.html#installation | ||
| """) | ||
|
|
||
| exit({:shutdown, 1}) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.