-
Notifications
You must be signed in to change notification settings - Fork 13
/
recode.ex
223 lines (178 loc) · 5.59 KB
/
recode.ex
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
defmodule Mix.Tasks.Recode do
@shortdoc "Runs the linter"
@moduledoc """
#{@shortdoc}.
```shell
> mix recode [options] [inputs]
```
Without a `inputs` argument the `inputs` value from the config is used. The
`inputs` argument accepts a wildcard.
If `inputs` value is `-`, then the input is read from stdin.
Without the option `--config file` the config file `.recode.exs` is used. A
default `.recode.exs` can be generated with `mix recode.gen.config`.
## Command line Option
* `-a`, `--autocorrect`, `--no-autocorrect` - Activates/deactivates
autocrrection. Overwrites the corresponding value in the configuration.
* `-c`, `--config` - specifies an alternative config file.
* `-d`, `--dry`, `--no-dry` - Activates/deactivates the dry mode. No file is
overwritten in dry mode. Overwrites the corresponding value in the
configuration.
* `-v`, `--verbose`, `--no-verbose` - Activate/deactivates the verbose mode.
Overwrites the corresponding value in the configuration.
* `-t`, `--task`, specifies the task to use. With this option, the task is
used even if it is specified as `active: false` in the configuration.
This option can appear multiple times in a call.
* `--slowest-tasks` - prints timing information for the N slowest tasks.
* `--color` - enables color in the output.
"""
use Mix.Task
alias Recode.Config
alias Recode.Runner
@opts strict: [
autocorrect: :boolean,
color: :boolean,
config: :string,
debug: :boolean,
dry: :boolean,
task: :keep,
verbose: :boolean,
slowest_tasks: :integer
],
aliases: [
a: :autocorrect,
c: :config,
d: :dry,
t: :task,
v: :verbose
]
@task_config_keys [
:active,
:autocorrect,
:check,
:config,
:exclude,
:exit_code
]
@impl Mix.Task
@spec run(list()) :: no_return()
def run(opts) do
{:ok, _apps} = Application.ensure_all_started(:recode)
opts = opts!(opts)
opts =
opts
|> Keyword.get(:config, ".recode.exs")
|> config!()
|> validate_config!()
|> validate_tasks!()
|> update_task_configs!()
|> merge_opts(opts)
|> Keyword.put(:cli_opts, acc_tasks(opts))
|> update(:verbose)
|> put_debug(opts)
case Runner.run(opts) do
{:ok, 0} ->
exit(:normal)
{:ok, exit_code} ->
exit({:shutdown, exit_code})
{:error, :no_sources} ->
Mix.raise("No sources found")
end
end
defp merge_opts(config, opts) do
Keyword.merge(
config,
Keyword.take(opts, [:verbose, :autocorrect, :dry, :inputs, :slowest_tasks, :color])
)
end
defp opts!(opts) do
case OptionParser.parse!(opts, @opts) do
{opts, []} -> opts
{opts, inputs} -> Keyword.put(opts, :inputs, inputs)
end
end
defp acc_tasks(opts) do
tasks =
Enum.reduce(opts, [], fn {key, value}, acc ->
case key do
:task -> [value | acc]
_else -> acc
end
end)
opts
|> Keyword.delete(:task)
|> Keyword.put(:tasks, tasks)
end
defp config!(opts) do
case Config.read(opts) do
{:ok, config} ->
config
{:error, :not_found} ->
Mix.raise("Config file not found. Run `mix recode.gen.config` to create `.recode.exs`.")
end
end
defp validate_config!(config) do
case Config.validate(config) do
:ok ->
config
{:error, :out_of_date} ->
Mix.raise("The config is out of date. Run `mix recode.update.config` to update.")
{:error, :no_tasks} ->
Mix.raise("No `:tasks` key found in configuration.")
end
end
defp validate_tasks!(config) do
Enum.each(config[:tasks], fn {task, config} ->
task |> Code.ensure_loaded() |> validate_task!(task)
validate_task_config!(task, config)
end)
config
end
defp validate_task_config!(task, config) do
keys = Keyword.keys(config) -- @task_config_keys
unless Enum.empty?(keys) do
config =
Enum.reduce(keys, config, fn key, config ->
{value, config} = Keyword.pop!(config, key)
Keyword.update(config, :config, [{key, value}], fn task_config ->
Keyword.put(task_config, key, value)
end)
end)
Mix.raise("""
Invalid config keys #{inspect(keys)} for #{inspect(task)} found.
Did you want to create a task-specific configuration:
{#{inspect(task)}, #{inspect(config)}}
""")
end
end
defp validate_task!({:error, :nofile}, task) do
Mix.raise("Recode task #{inspect(task)} not found.")
end
defp validate_task!({:module, _module}, task) do
unless Recode.Task in task.__info__(:attributes)[:behaviour] do
Mix.raise("The module #{inspect(task)} does not implement the Recode.Task behaviour.")
end
end
defp update_task_configs!(config) do
Keyword.update!(config, :tasks, fn tasks ->
Enum.map(tasks, fn {task, config} ->
task_config = Keyword.get(config, :config, [])
case task.init(task_config) do
{:ok, task_config} ->
{task, Keyword.put(config, :config, task_config)}
{:error, message} ->
Mix.raise("The task #{inspect(task)} has an invalid config:\n#{message}")
end
end)
end)
end
defp update(opts, :verbose) do
case opts[:dry] do
true -> Keyword.put(opts, :verbose, true)
false -> opts
end
end
defp put_debug(config, opts) do
debug = Keyword.get(opts, :debug, false)
Keyword.put(config, :debug, debug)
end
end