|
| 1 | +defmodule Sloc do |
| 2 | + |
| 3 | + defmodule Node do |
| 4 | + defstruct [:path, :type, line_count: 0, children: %{}] |
| 5 | + |
| 6 | + def deep_map(%Node{type: :file}=node, func) do |
| 7 | + func.(node) |
| 8 | + end |
| 9 | + def deep_map(%Node{type: :directory, children: children}=node, func) do |
| 10 | + new_children = for {k, n} <- children, into: %{}, do: {k, deep_map(n, func)} |
| 11 | + func.(%{node | children: new_children}) |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + @path "../turtle/turtle-api/lib" |
| 16 | + @whitelist ~w(.ex .exs .rb) |
| 17 | + |
| 18 | + def run do |
| 19 | + report = get_report(@path) |
| 20 | + IO.puts(format_report(report)) |
| 21 | + end |
| 22 | + |
| 23 | + def format_report(node), do: format_report(node, 0) |
| 24 | + def format_report(%Node{path: path, line_count: line_count, children: children}, depth) do |
| 25 | + [ |
| 26 | + String.duplicate(" ", depth), |
| 27 | + Path.basename(path), " (", to_string(line_count), ")", |
| 28 | + format_report(Map.values(children), depth + 1) |
| 29 | + ] |
| 30 | + end |
| 31 | + def format_report(nodes, depth) when is_list(nodes) do |
| 32 | + nodes |
| 33 | + |> Enum.sort_by(fn n -> -n.line_count end) |
| 34 | + |> Enum.map(fn n -> ["\n", format_report(n, depth + 1)] end) |
| 35 | + end |
| 36 | + |
| 37 | + def get_report(path) do |
| 38 | + get_tree(path) |
| 39 | + |> Node.deep_map(&calculate_line_count/1) |
| 40 | + |> Node.deep_map(&normalize_path(&1, path)) |
| 41 | + |> Node.deep_map(&aggregate/1) |
| 42 | + end |
| 43 | + |
| 44 | + def normalize_path(%Node{path: path}=node, root) do |
| 45 | + %{node | path: Path.relative_to(path, root)} |
| 46 | + end |
| 47 | + |
| 48 | + def calculate_line_count(%Node{type: :file, path: path}=node) do |
| 49 | + %{node | line_count: line_count(path)} |
| 50 | + end |
| 51 | + def calculate_line_count(%Node{type: :directory}=node), do: node |
| 52 | + |
| 53 | + def aggregate(%Node{type: :file}=node), do: node |
| 54 | + def aggregate(%Node{type: :directory, children: children}=node) do |
| 55 | + line_count = children |> Map.values() |> Enum.map(&Map.get(&1, :line_count)) |> Enum.sum() |
| 56 | + %{node | line_count: line_count} |
| 57 | + end |
| 58 | + |
| 59 | + def get_tree(path) do |
| 60 | + cond do |
| 61 | + File.regular?(path) -> |
| 62 | + %Node{type: :file, path: path} |
| 63 | + File.dir?(path) -> |
| 64 | + children = for subpath <- get_filtered_subpaths(path), into: %{} do |
| 65 | + {Path.basename(subpath), get_tree(subpath)} |
| 66 | + end |
| 67 | + %Node{type: :directory, path: path, children: children} |
| 68 | + true -> nil |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + def line_count(filepath) do |
| 73 | + File.stream!(filepath) |
| 74 | + |> Stream.reject(&Regex.match?(~r/^\s*$/, &1)) |
| 75 | + |> Enum.count() |
| 76 | + end |
| 77 | + |
| 78 | + def get_filtered_subpaths(path) do |
| 79 | + path |
| 80 | + |> File.ls!() |
| 81 | + |> Enum.map(&Path.join(path, &1)) |
| 82 | + |> Enum.reject(&is_dotfile?/1) |
| 83 | + |> Enum.filter(fn p -> |
| 84 | + File.dir?(p) || has_ext?(p, @whitelist) |
| 85 | + end) |
| 86 | + end |
| 87 | + |
| 88 | + defp is_dotfile?(path) do |
| 89 | + case Path.basename(path) do |
| 90 | + "." <> _ -> true |
| 91 | + _ -> false |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + defp has_ext?(path, extensions) do |
| 96 | + Enum.member?(extensions, Path.extname(path)) |
| 97 | + end |
| 98 | + |
| 99 | +end |
0 commit comments