-
Notifications
You must be signed in to change notification settings - Fork 13
/
config.ex
189 lines (163 loc) · 5.39 KB
/
config.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
defmodule Recode.Config do
@moduledoc """
Functions to read and merge the `Recode` configuration.
"""
alias Recode.Task.Format
@type config :: keyword()
@config_filename ".recode.exs"
@config_version "0.7.2"
# The minimum version of the config to run recode. This version marks the last
# breaking change for handle the config.
@config_min_version "0.7.1"
@config_keys [:version, :autocorrect, :dry, :verbose, :inputs, :formatters, :tasks, :color]
# The default configuration used by mix tasks recode.gen.config and
# recode.update.config.
@config_default [
version: @config_version,
autocorrect: true,
dry: false,
color: true,
verbose: false,
inputs: ["{mix,.formatter}.exs", "{apps,config,lib,test}/**/*.{ex,exs}"],
formatters: [Recode.CLIFormatter],
tasks: [
{Recode.Task.AliasExpansion, []},
{Recode.Task.AliasOrder, []},
{Recode.Task.Dbg, [autocorrect: false]},
{Recode.Task.EnforceLineLength, [active: false]},
{Recode.Task.FilterCount, []},
{Recode.Task.IOInspect, [autocorrect: false]},
# {Recode.Task.Moduledoc, []},
{Recode.Task.Nesting, []},
{Recode.Task.PipeFunOne, []},
{Recode.Task.SinglePipe, []},
{Recode.Task.Specs, [exclude: ["test/**/*.{ex,exs}", "mix.exs"], config: [only: :visible]]},
{Recode.Task.TagFIXME, exit_code: 2},
{Recode.Task.TagTODO, exit_code: 4},
{Recode.Task.TestFileExt, []},
{Recode.Task.UnusedVariable, [active: false]}
]
]
@doc """
Returns the default configuration.
"""
@spec default() :: config()
def default, do: @config_default
@doc """
Returns the given config as a formatted string with comments.
"""
@spec to_string(config()) :: String.t()
def to_string(config \\ default()) do
config = Keyword.validate!(config, @config_keys)
template = """
[
version: "<%= @config[:version] %>",
# Can also be set/reset with `--autocorrect`/`--no-autocorrect`.
autocorrect: <%= @config[:autocorrect] %>,
# With "--dry" no changes will be written to the files.
# Can also be set/reset with `--dry`/`--no-dry`.
# If dry is true then verbose is also active.
dry: <%= @config[:dry] %>,
# Enables or disables color in the output.
color: <%= @config[:color] %>,
# Can also be set/reset with `--verbose`/`--no-verbose`.
verbose: <%= @config[:verbose] %>,
# Can be overwritten by calling `mix recode "lib/**/*.ex"`.
inputs: <%= inspect @config[:inputs] %>,
formatters: <%= inspect @config[:formatters] %>,
tasks: [
# Tasks could be added by a tuple of the tasks module name and an options
# keyword list. A task can be deactivated by `active: false`. The execution of
# a deactivated task can be forced by calling `mix recode --task ModuleName`.
<%= for task <- @config[:tasks] do %><%= inspect task %>,
<% end %>
]
]
"""
template
|> EEx.eval_string(assigns: [config: config])
|> Code.format_string!()
|> IO.iodata_to_binary()
end
@doc """
Merges two configs into one.
The merge will do a deep merge. The merge will do a deep merge. The merge
takes the version from the `right` config.
## Examples
iex> new = [version: "0.0.2", verbose: false, autocorrect: true]
...> old = [version: "0.0.1", verbose: true]
iex> Recode.Config.merge(new ,old) |> Enum.sort()
[autocorrect: true, verbose: true, version: "0.0.2"]
"""
def merge(left \\ default(), right) do
left
|> Keyword.merge(right, fn
:version, version, _ -> version
:tasks, left, right -> merge_tasks(left, right)
_, _, value -> value
end)
end
defp merge_tasks(left, right) do
left
|> Keyword.merge(right, fn
_key, left, [] -> left
_key, left, right -> merge_task_config(left, right)
end)
|> Enum.sort()
end
defp merge_task_config(left, right) do
Keyword.merge(left, right, fn
:config, left, right -> left |> Keyword.merge(right) |> Enum.sort()
_key, _left, right -> right
end)
end
@doc """
Reads the `Recode` cofiguration from the given `path`.
"""
@spec read(Path.t()) :: {:ok, config()} | {:error, :not_found}
def read(path \\ @config_filename) when is_binary(path) do
case File.exists?(path) do
true ->
config =
path
|> Code.eval_file()
|> elem(0)
|> default(:tasks)
|> update(:inputs)
{:ok, config}
false ->
{:error, :not_found}
end
end
@doc """
Validates the config version and tasks.
"""
@spec validate(config()) :: :ok | {:error, :out_of_date | :no_tasks}
def validate(config) do
with :ok <- validate_version(config) do
validate_tasks(config)
end
end
defp validate_version(config) do
cmp =
config
|> Keyword.get(:version, @config_min_version)
|> Version.compare(@config_min_version)
if cmp == :lt do
{:error, :out_of_date}
else
:ok
end
end
defp validate_tasks(config) do
if Keyword.has_key?(config, :tasks), do: :ok, else: {:error, :no_tasks}
end
defp default(config, :tasks) do
Keyword.update!(config, :tasks, fn tasks -> [{Format, []} | tasks] end)
end
defp update(config, :inputs) do
Keyword.update(config, :inputs, [], fn inputs ->
inputs |> List.wrap() |> Enum.map(fn input -> GlobEx.compile!(input) end)
end)
end
end