-
Notifications
You must be signed in to change notification settings - Fork 414
/
sources.ex
178 lines (141 loc) · 4.06 KB
/
sources.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
defmodule Credo.Sources do
@moduledoc """
This module is used to find and read all source files for analysis.
"""
alias Credo.SourceFile
@default_sources_glob ~w(** *.{ex,exs})
@stdin_filename "stdin"
@doc """
Finds sources for a given `Credo.Execution`.
Through the `files` key, configs may contain a list of `included` and `excluded`
patterns. For `included`, patterns can be file paths, directory paths and globs.
For `excluded`, patterns can also be specified as regular expressions.
iex> Sources.find(%Credo.Execution{files: %{excluded: ["not_me.ex"], included: ["*.ex"]}})
iex> Sources.find(%Credo.Execution{files: %{excluded: [~r/messy/], included: ["lib/mix", "root.ex"]}})
"""
def find(exec)
def find(%Credo.Execution{read_from_stdin: true, files: %{included: [filename]}}) do
filename
|> source_file_from_stdin()
|> List.wrap()
end
def find(%Credo.Execution{read_from_stdin: true}) do
@stdin_filename
|> source_file_from_stdin()
|> List.wrap()
end
def find(%Credo.Execution{files: files, parse_timeout: parse_timeout}) do
parse_timeout =
if is_nil(parse_timeout) do
:infinity
else
parse_timeout
end
MapSet.new()
|> include(files.included)
|> exclude(files.excluded)
|> Enum.sort()
|> Enum.take(max_file_count())
|> read_files(parse_timeout)
end
def find(paths) when is_list(paths) do
Enum.flat_map(paths, &find/1)
end
def find(path) when is_binary(path) do
recurse_path(path)
end
defp max_file_count do
max_files = System.get_env("MAX_FILES")
if max_files do
String.to_integer(max_files)
else
1_000_000
end
end
defp include(files, []), do: files
defp include(files, [path | remaining_paths]) do
include_paths =
path
|> recurse_path
|> Enum.into(MapSet.new())
files
|> MapSet.union(include_paths)
|> include(remaining_paths)
end
defp exclude(files, []), do: files
defp exclude(files, [pattern | remaining_patterns]) when is_list(files) do
files
|> Enum.into(MapSet.new())
|> exclude([pattern | remaining_patterns])
end
defp exclude(files, [pattern | remaining_patterns]) when is_binary(pattern) do
exclude_paths =
pattern
|> recurse_path
|> Enum.into(MapSet.new())
files
|> MapSet.difference(exclude_paths)
|> exclude(remaining_patterns)
end
defp exclude(files, [pattern | remaining_patterns]) do
files
|> Enum.reject(&String.match?(&1, pattern))
|> exclude(remaining_patterns)
end
defp recurse_path(path) do
paths =
cond do
File.regular?(path) ->
[path]
File.dir?(path) ->
[path | @default_sources_glob]
|> Path.join()
|> Path.wildcard()
true ->
path
|> Path.wildcard()
|> Enum.flat_map(&recurse_path/1)
end
Enum.map(paths, &Path.expand/1)
end
defp read_files(filenames, parse_timeout) do
tasks = Enum.map(filenames, &Task.async(fn -> to_source_file(&1) end))
task_dictionary =
tasks
|> Enum.zip(filenames)
|> Enum.into(%{})
tasks_with_results = Task.yield_many(tasks, parse_timeout)
results =
Enum.map(tasks_with_results, fn {task, res} ->
# Shutdown the tasks that did not reply nor exit
{task, res || Task.shutdown(task, :brutal_kill)}
end)
Enum.map(results, fn
{_task, {:ok, value}} -> value
{task, nil} -> SourceFile.timed_out(task_dictionary[task])
end)
end
defp to_source_file(filename) do
filename
|> File.read!()
|> SourceFile.parse(filename)
end
defp source_file_from_stdin(filename) do
SourceFile.parse(read_from_stdin!(), filename)
end
defp read_from_stdin! do
{:ok, source} = read_from_stdin()
source
end
defp read_from_stdin(source \\ "") do
case IO.read(:stdio, :line) do
{:error, reason} ->
{:error, reason}
:eof ->
{:ok, source}
data ->
source = source <> data
read_from_stdin(source)
end
end
end