forked from rrrene/credo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
first_run_hint.ex
231 lines (190 loc) · 5.37 KB
/
first_run_hint.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
224
225
226
227
228
229
230
231
defmodule Credo.CLI.Output.FirstRunHint do
alias Credo.CLI.Output
alias Credo.CLI.Output.UI
alias Credo.Execution
@lots_of_issue_threshold 30
@command_padding 40
@category_count 5
def call(exec) do
term_width = Output.term_columns()
issues = Execution.get_issues(exec)
headline = " 8< "
bar = String.pad_leading("", div(term_width - String.length(headline), 2), "-")
UI.puts()
UI.puts()
UI.puts([:magenta, :bright, "#{bar} 8< #{bar}"])
UI.puts()
UI.puts()
issue_count = Enum.count(issues)
readability_issue_count =
issues
|> Enum.count(&(&1.category == :readability))
relative_issue_count_per_category = div(issue_count, @category_count)
mostly_readability_issues =
readability_issue_count >= div(@lots_of_issue_threshold, 2) &&
readability_issue_count > relative_issue_count_per_category * 2
readability_hint =
if mostly_readability_issues do
[
"""
While not recommended, you could simply start ignoring issues for the time being:
""",
:cyan,
String.pad_trailing(" mix credo --ignore readability", @command_padding),
:faint,
"# exclude checks matching a given phrase",
"\n",
:reset
]
else
[]
end
if issue_count >= @lots_of_issue_threshold do
UI.puts([
:reset,
:orange,
"""
# Where to start?
""",
:reset,
"""
That's a lot of issues to deal with at once.
""",
readability_hint
])
else
UI.puts([
:reset,
:orange,
"""
# How to introduce Credo
""",
:reset,
"""
This is looking pretty already! You can probably just fix the issues above in one go.
""",
readability_hint
])
end
print_lots_of_issues(exec)
UI.puts([
:reset,
:orange,
"""
## Every project is different
""",
:reset,
"""
Introducing code analysis to an existing codebase should not be about following any
"best practice" in particular, it should be about helping you to get to know the ropes
and make the changes you want.
Try the options outlined above to see which one is working for this project!
"""
])
end
defp print_lots_of_issues(exec) do
working_dir = Execution.working_dir(exec)
now = now()
default_branch = default_branch(working_dir)
latest_commit_on_default_branch = latest_commit_on_default_branch(working_dir)
latest_tag = latest_tag(working_dir)
current_branch = current_branch(working_dir)
if current_branch != default_branch do
UI.puts([
:reset,
"""
You can use `diff` to only show the issues that were introduced on this branch:
""",
:cyan,
"""
mix credo diff #{default_branch}
"""
])
end
UI.puts([
:reset,
:orange,
"""
## Compare to a point in history
""",
:reset,
"""
Alternatively, you can use `diff` to only show the issues that were introduced after a certain tag or commit:
"""
])
if latest_tag do
UI.puts([
:cyan,
String.pad_trailing(" mix credo diff #{latest_tag} ", @command_padding),
:faint,
"# use the latest tag",
"\n"
])
end
UI.puts([
:reset,
:cyan,
String.pad_trailing(
" mix credo diff #{latest_commit_on_default_branch}",
@command_padding
),
:faint,
"# use the current HEAD of #{default_branch}",
"\n\n",
:reset,
"""
Lastly, you can compare your working dir against this point in time:
""",
:cyan,
String.pad_trailing(" mix credo diff --since #{now}", @command_padding),
:faint,
"# use the current date",
"\n"
])
end
defp latest_tag(working_dir) do
case System.cmd("git", ~w"rev-list --tags --max-count=1", cd: working_dir) do
{"", 0} ->
nil
{latest_tag_sha1, 0} ->
case System.cmd("git", ~w"describe --tags #{latest_tag_sha1}", cd: working_dir) do
{tagname, 0} -> String.trim(tagname)
_ -> nil
end
_ ->
nil
end
end
defp current_branch(working_dir) do
case System.cmd("git", ~w"rev-parse --abbrev-ref HEAD", cd: working_dir) do
{output, 0} -> String.trim(output)
_ -> nil
end
end
defp default_branch(working_dir) do
remote_name = default_remote_name(working_dir)
case System.cmd("git", ~w"symbolic-ref refs/remotes/#{remote_name}/HEAD", cd: working_dir) do
{output, 0} -> ~r"refs/remotes/#{remote_name}/(.+)$" |> Regex.run(output) |> Enum.at(1)
_ -> nil
end
end
defp default_remote_name(_working_dir) do
"origin"
end
defp latest_commit_on_default_branch(working_dir) do
case System.cmd(
"git",
~w"rev-parse --short #{default_remote_name(working_dir)}/#{default_branch(working_dir)}",
cd: working_dir
) do
{output, 0} -> String.trim(output)
_ -> nil
end
end
defp now do
%{year: year, month: month, day: day} = DateTime.utc_now()
"#{year}-#{pad(month)}-#{pad(day)}"
end
defp pad(number) when number < 10, do: "0#{number}"
defp pad(number), do: to_string(number)
end