cover/Elixir.Utils.File.html

1 defmodule Utils.File do
2 import Enum, only: [map: 2, find_value: 2]
3 import File, only: [read: 1]
4 import Path, only: [expand: 2, wildcard: 1, join: 2]
5
6 @app_root expand("../..", __DIR__)
7 @repo_root expand("../../../..", __DIR__)
8
9 @paths [
10 "",
11 "agents/",
12 "test/specs/"
13 ]
14
15 def file(name) do
16 paths(name)
17 |> find_value(&fetch/1)
18 170 |> ensure(name)
19 end
20
21 defp paths(name) do
22 170 map(@paths, fn path -> concat("#{@app_root}/#{path}", name) end) ++
23 nested(name) ++
24 rooted(name)
25 end
26
27 defp nested(name) do
28 170 wildcard(join(@app_root, "agents/**/#{name}"))
29 end
30
31 defp rooted(name) do
32 170 wildcard(join(@repo_root, "agents/**/#{name}"))
33 end
34
35
:-(
defp ensure(nil, name), do: "file not found: #{name}"
36 170 defp ensure(content, _name), do: content
37
38 510 defp concat(path, name), do: "#{path}#{name}"
39
40 170 defp fetch({:ok, content}), do: content
41 507 defp fetch({:error, _}), do: nil
42
43 defp fetch(path) do
44 677 read(path) |> fetch()
45 end
46 end
Line Hits Source