|
| 1 | +# Copyright (c) 2014 Paul Schoenfelder |
| 2 | +# From the conform library: https://github.com/bitwalker/conform |
| 3 | +defmodule Mix.Tasks.Compile.Peg do |
| 4 | + @moduledoc """ |
| 5 | + Compiles Erlang Parsing Expression Grammars (PEGs). |
| 6 | + """ |
| 7 | + @shortdoc "Compiles Erlang Parsing Expression Grammars (PEGs)." |
| 8 | + |
| 9 | + use Mix.Task |
| 10 | + |
| 11 | + @recursive true |
| 12 | + @manifest ".compile.peg" |
| 13 | + |
| 14 | + def run(_) do |
| 15 | + manifest = load_manifest!() |
| 16 | + get_sources() |
| 17 | + |> Enum.filter(&(compile?(&1, manifest))) |
| 18 | + |> Enum.map(&compile_peg/1) |
| 19 | + |> Enum.reduce([], &build_manifest/2) |
| 20 | + |> write_manifest! |
| 21 | + end |
| 22 | + |
| 23 | + def clean do |
| 24 | + get_sources() |> Enum.map(&do_clean/1) |
| 25 | + end |
| 26 | + |
| 27 | + defp do_clean(source_path) do |
| 28 | + erl = erl_source_path(source_path) |
| 29 | + case File.exists?(erl) do |
| 30 | + true -> File.rm!(erl) |
| 31 | + false -> :ok |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + defp get_sources, |
| 36 | + do: Mix.Project.config[:erlc_paths] |> Mix.Utils.extract_files([:peg]) |
| 37 | + |
| 38 | + defp compile?(source_path, manifest) do |
| 39 | + erl = source_path |> erl_source_path |> Path.expand |
| 40 | + peg = source_path |> Path.expand |
| 41 | + # If the compiled file doesn't exist, then compile |
| 42 | + case File.exists?(erl) do |
| 43 | + true -> |
| 44 | + # If the manifest doesn't contain an entry for the peg file, then compile |
| 45 | + case Map.get(manifest, source_path) do |
| 46 | + nil -> true |
| 47 | + last_compilation -> |
| 48 | + # Otherwise, compare the modified time of the peg file against the |
| 49 | + # last compilation time (in seconds) of that file, if modifications |
| 50 | + # have been made, then compile |
| 51 | + {:ok, %File.Stat{mtime: mtime}} = File.stat(peg) |
| 52 | + :calendar.datetime_to_gregorian_seconds(mtime) > last_compilation |
| 53 | + end |
| 54 | + false -> true |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + defp compile_peg(source_path) do |
| 59 | + peg = Path.expand(source_path) |
| 60 | + relative_path = Path.relative_to_cwd(source_path) |
| 61 | + info "Compiling #{relative_path}" |
| 62 | + case :neotoma.file('#{peg}') do |
| 63 | + :ok -> :ok |
| 64 | + {:error, reason} -> |
| 65 | + error "Failed to compile #{relative_path}: #{reason}" |
| 66 | + exit(:normal) |
| 67 | + end |
| 68 | + case File.stat(peg) do |
| 69 | + {:ok, %File.Stat{mtime: last_compilation}} -> |
| 70 | + {source_path, last_compilation |> :calendar.datetime_to_gregorian_seconds} |
| 71 | + {:error, :enoent} -> |
| 72 | + error "Cannot stat #{relative_path}!" |
| 73 | + exit(:normal) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + defp build_manifest({source_path, compilation_time}, manifest) do |
| 78 | + [<<source_path :: binary, ?\t, ?\t, "#{compilation_time}" :: binary>> | manifest] |
| 79 | + end |
| 80 | + |
| 81 | + defp write_manifest!([]), do: :ok |
| 82 | + defp write_manifest!(manifest) when is_list(manifest) do |
| 83 | + serialized = manifest |> Enum.join(<<?\n>>) |
| 84 | + output_path = manifest_path() |
| 85 | + case File.write!(output_path, serialized) do |
| 86 | + :ok -> info "PEG manifest updated" |
| 87 | + {:error, reason} -> error "Unable to save PEG manifest: #{reason}" |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + defp erl_source_path(source_path), do: String.replace(source_path, ".peg", ".erl") |
| 92 | + defp manifest_path, do: Mix.Project.app_path |> Path.join(@manifest) |> Path.expand |
| 93 | + |
| 94 | + defp load_manifest! do |
| 95 | + manifest = manifest_path() |
| 96 | + case File.exists?(manifest_path()) do |
| 97 | + true -> |
| 98 | + # Create resource from manifest |
| 99 | + res = Stream.resource( |
| 100 | + fn -> File.open!(manifest) end, |
| 101 | + fn file -> |
| 102 | + case IO.read(file, :line) do |
| 103 | + data when is_binary(data) -> {[data], file} |
| 104 | + _ -> {:halt, file} |
| 105 | + end |
| 106 | + end, |
| 107 | + fn file -> File.close(file) end |
| 108 | + ) |
| 109 | + # For each line in the manifest, split on \t, where the |
| 110 | + # first part is the source file path, and the second part |
| 111 | + # is the time at which that file was last compiled |
| 112 | + res |
| 113 | + |> Stream.map(fn line -> |
| 114 | + case line |> String.split(<<?\t, ?\t>>, [parts: 2, trim: true]) do |
| 115 | + [path, mtime] -> |
| 116 | + {mtime, _} = Integer.parse(mtime) |
| 117 | + {path, mtime} |
| 118 | + _ -> |
| 119 | + nil |
| 120 | + end |
| 121 | + end) |
| 122 | + |> Stream.filter(fn meta -> meta != nil end) |
| 123 | + |> Enum.into(%{}) |
| 124 | + false -> |
| 125 | + %{} |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + defp info(message), do: print(message) |
| 130 | + defp error(message), do: print(message, IO.ANSI.red) |
| 131 | + defp print(message, color \\ nil) do |
| 132 | + has_colors? = IO.ANSI.enabled? |
| 133 | + cond do |
| 134 | + color == nil -> message |
| 135 | + has_colors? -> color <> message <> IO.ANSI.reset |
| 136 | + true -> message |
| 137 | + end |> IO.puts |
| 138 | + end |
| 139 | + |
| 140 | +end |
0 commit comments