|
| 1 | +defmodule Mix.Tasks.ErrorTracker.Install.Docs do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + def short_doc do |
| 5 | + "Install and configure ErrorTracker for use in this application." |
| 6 | + end |
| 7 | + |
| 8 | + def example do |
| 9 | + "mix error_tracker.install" |
| 10 | + end |
| 11 | + |
| 12 | + def long_doc do |
| 13 | + """ |
| 14 | + #{short_doc()} |
| 15 | +
|
| 16 | + ## Example |
| 17 | +
|
| 18 | + ```bash |
| 19 | + #{example()} |
| 20 | + ``` |
| 21 | + """ |
| 22 | + end |
| 23 | +end |
| 24 | + |
| 25 | +if Code.ensure_loaded?(Igniter) do |
| 26 | + defmodule Mix.Tasks.ErrorTracker.Install do |
| 27 | + @shortdoc "#{__MODULE__.Docs.short_doc()}" |
| 28 | + |
| 29 | + @moduledoc __MODULE__.Docs.long_doc() |
| 30 | + |
| 31 | + use Igniter.Mix.Task |
| 32 | + |
| 33 | + @impl Igniter.Mix.Task |
| 34 | + def info(_argv, _composing_task) do |
| 35 | + %Igniter.Mix.Task.Info{ |
| 36 | + # Groups allow for overlapping arguments for tasks by the same author |
| 37 | + # See the generators guide for more. |
| 38 | + group: :error_tracker, |
| 39 | + # *other* dependencies to add |
| 40 | + # i.e `{:foo, "~> 2.0"}` |
| 41 | + adds_deps: [], |
| 42 | + # *other* dependencies to add and call their associated installers, if they exist |
| 43 | + # i.e `{:foo, "~> 2.0"}` |
| 44 | + installs: [], |
| 45 | + # An example invocation |
| 46 | + example: __MODULE__.Docs.example(), |
| 47 | + # A list of environments that this should be installed in. |
| 48 | + only: nil, |
| 49 | + # a list of positional arguments, i.e `[:file]` |
| 50 | + positional: [], |
| 51 | + # Other tasks your task composes using `Igniter.compose_task`, passing in the CLI argv |
| 52 | + # This ensures your option schema includes options from nested tasks |
| 53 | + composes: [], |
| 54 | + # `OptionParser` schema |
| 55 | + schema: [], |
| 56 | + # Default values for the options in the `schema` |
| 57 | + defaults: [], |
| 58 | + # CLI aliases |
| 59 | + aliases: [], |
| 60 | + # A list of options in the schema that are required |
| 61 | + required: [] |
| 62 | + } |
| 63 | + end |
| 64 | + |
| 65 | + @impl Igniter.Mix.Task |
| 66 | + def igniter(igniter) do |
| 67 | + app_name = Igniter.Project.Application.app_name(igniter) |
| 68 | + repo_module = Igniter.Project.Module.module_name(igniter, "Repo") |
| 69 | + web_module = Igniter.Libs.Phoenix.web_module(igniter) |
| 70 | + |
| 71 | + igniter |
| 72 | + |> configure(app_name, repo_module) |
| 73 | + |> set_up_database(repo_module) |
| 74 | + |> set_up_web_ui(web_module) |
| 75 | + end |
| 76 | + |
| 77 | + defp configure(igniter, app_name, repo_module) do |
| 78 | + igniter |
| 79 | + |> Igniter.Project.Config.configure("config.exs", :error_tracker, [:repo], repo_module) |
| 80 | + |> Igniter.Project.Config.configure("config.exs", :error_tracker, [:otp_app], app_name) |
| 81 | + |> Igniter.Project.Config.configure("config.exs", :error_tracker, [:enabled], true) |
| 82 | + end |
| 83 | + |
| 84 | + defp set_up_database(igniter, repo_module) do |
| 85 | + migration_body = """ |
| 86 | + def up, do: ErrorTracker.Migration.up() |
| 87 | + def down, do: ErrorTracker.Migration.down(version: 1) |
| 88 | + """ |
| 89 | + |
| 90 | + Igniter.Libs.Ecto.gen_migration(igniter, repo_module, "add_error_tracker", |
| 91 | + body: migration_body, |
| 92 | + on_exists: :skip |
| 93 | + ) |
| 94 | + end |
| 95 | + |
| 96 | + defp set_up_web_ui(igniter, web_module) do |
| 97 | + content = |
| 98 | + """ |
| 99 | + # TODO: This path should be protected from unauthorized user access |
| 100 | + error_tracker_dashboard "/errors" |
| 101 | + """ |
| 102 | + |
| 103 | + Igniter.Libs.Phoenix.append_to_scope(igniter, "/", content, arg2: web_module) |
| 104 | + end |
| 105 | + end |
| 106 | +else |
| 107 | + defmodule Mix.Tasks.ErrorTracker.Install do |
| 108 | + @shortdoc "#{__MODULE__.Docs.short_doc()} | Install `igniter` to use" |
| 109 | + |
| 110 | + @moduledoc __MODULE__.Docs.long_doc() |
| 111 | + |
| 112 | + use Mix.Task |
| 113 | + |
| 114 | + def run(_argv) do |
| 115 | + Mix.shell().error(""" |
| 116 | + The task 'error_tracker.install' requires igniter. Please install igniter and try again. |
| 117 | +
|
| 118 | + For more information, see: https://hexdocs.pm/igniter/readme.html#installation |
| 119 | + """) |
| 120 | + |
| 121 | + exit({:shutdown, 1}) |
| 122 | + end |
| 123 | + end |
| 124 | +end |
0 commit comments