-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy patherror_tracker.install.ex
More file actions
151 lines (126 loc) · 4.26 KB
/
error_tracker.install.ex
File metadata and controls
151 lines (126 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
defmodule Mix.Tasks.ErrorTracker.Install.Docs do
@moduledoc false
def short_doc do
"Install and configure ErrorTracker for use in this application."
end
def example do
"mix error_tracker.install"
end
def long_doc do
"""
#{short_doc()}
## Example
```bash
#{example()}
```
"""
end
end
if Code.ensure_loaded?(Igniter) do
defmodule Mix.Tasks.ErrorTracker.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{
# Groups allow for overlapping arguments for tasks by the same author
# See the generators guide for more.
group: :error_tracker,
# *other* dependencies to add
# i.e `{:foo, "~> 2.0"}`
adds_deps: [],
# *other* dependencies to add and call their associated installers, if they exist
# i.e `{:foo, "~> 2.0"}`
installs: [],
# An example invocation
example: __MODULE__.Docs.example(),
# A list of environments that this should be installed in.
only: nil,
# a list of positional arguments, i.e `[:file]`
positional: [],
# Other tasks your task composes using `Igniter.compose_task`, passing in the CLI argv
# This ensures your option schema includes options from nested tasks
composes: [],
# `OptionParser` schema
schema: [],
# Default values for the options in the `schema`
defaults: [],
# CLI aliases
aliases: [],
# A list of options in the schema that are required
required: []
}
end
@impl Igniter.Mix.Task
def igniter(igniter) do
app_name = Igniter.Project.Application.app_name(igniter)
{igniter, repo} = Igniter.Libs.Ecto.select_repo(igniter)
{igniter, router} = Igniter.Libs.Phoenix.select_router(igniter)
igniter
|> set_up_configuration(app_name, repo)
|> set_up_formatter()
|> set_up_database(repo)
|> set_up_web_ui(app_name, router)
end
defp set_up_configuration(igniter, app_name, repo) do
igniter
|> Igniter.Project.Config.configure_new("config.exs", :error_tracker, [:repo], repo)
|> Igniter.Project.Config.configure_new("config.exs", :error_tracker, [:otp_app], app_name)
|> Igniter.Project.Config.configure_new("config.exs", :error_tracker, [:enabled], true)
end
defp set_up_formatter(igniter) do
Igniter.Project.Formatter.import_dep(igniter, :error_tracker)
end
defp set_up_database(igniter, repo) do
migration_body = """
def up, do: ErrorTracker.Migration.up()
def down, do: ErrorTracker.Migration.down(version: 1)
"""
Igniter.Libs.Ecto.gen_migration(igniter, repo, "add_error_tracker",
body: migration_body,
on_exists: :skip
)
end
defp set_up_web_ui(igniter, app_name, router) do
if router do
Igniter.Project.Module.find_and_update_module!(igniter, router, fn zipper ->
zipper =
Igniter.Code.Common.add_code(
zipper,
"""
if Application.compile_env(#{inspect(app_name)}, :dev_routes) do
use ErrorTracker.Web, :router
scope "/dev" do
pipe_through :browser
error_tracker_dashboard "/errors"
end
end
""",
placement: :after
)
{:ok, zipper}
end)
else
Igniter.add_warning(igniter, """
No Phoenix router found or selected. Please ensure that Phoenix is set up
and then run this installer again with
mix igniter.install error_tracker
""")
end
end
end
else
defmodule Mix.Tasks.ErrorTracker.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 'error_tracker.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