|
| 1 | +defmodule Mix.Tasks.Compile.Sql do |
| 2 | + @moduledoc """ |
| 3 | + A SQL compiler for sql files. |
| 4 | +
|
| 5 | + You must add it to your `mix.exs` as: |
| 6 | +
|
| 7 | + compilers: [:sql] ++ Mix.compilers() |
| 8 | +
|
| 9 | + """ |
| 10 | + use Mix.Task.Compiler |
| 11 | + |
| 12 | + @doc false |
| 13 | + def run(_args) do |
| 14 | + Mix.Task.Compiler.after_compiler(:elixir, fn |
| 15 | + {:noop, diagnostics} -> |
| 16 | + {:noop, diagnostics} |
| 17 | + {status, diagnostics} -> |
| 18 | + compile() |
| 19 | + {status, diagnostics} |
| 20 | + end) |
| 21 | + end |
| 22 | + |
| 23 | + def compile(files \\ files(), sql_files \\ []) |
| 24 | + def compile([path|rest], sql_files) do |
| 25 | + case {File.dir?(path), String.ends_with?(path, ".sql")} do |
| 26 | + {true, false} -> compile(rest, compile(files(path), sql_files)) |
| 27 | + {false, true} -> compile(rest, [{path, do_compile(path)}|sql_files]) |
| 28 | + {false, false} -> compile(rest, sql_files) |
| 29 | + end |
| 30 | + end |
| 31 | + def compile([], sql_files), do: sql_files |
| 32 | + |
| 33 | + def do_compile(path) do |
| 34 | + {:ok, context, tokens} = SQL.Lexer.lex(File.read!(path), path) |
| 35 | + {:ok, context, tokens} = SQL.Parser.parse(tokens, context) |
| 36 | + SQL.format(tokens, context, {nil, nil, 0, [file: Path.relative_to_cwd(path), line: 1]}) |
| 37 | + end |
| 38 | + |
| 39 | + def files(path \\ File.cwd!) do |
| 40 | + for file <- File.ls!(path), do: Path.join(path, file) |
| 41 | + end |
| 42 | +end |
0 commit comments