Current section
Files
Jump to
Current section
Files
lib/mix/tasks/ex_okf.format.ex
defmodule Mix.Tasks.ExOkf.Format do
@shortdoc "Rewrite an OKF bundle with normalized frontmatter"
@moduledoc """
Loads a bundle and writes it back with normalized YAML frontmatter.
mix ex_okf.format path/to/bundle
mix ex_okf.format path/to/bundle --out path/to/normalized
"""
use Mix.Task
@impl Mix.Task
def run(args) do
{opts, paths, _} = OptionParser.parse(args, strict: [out: :string])
path = List.first(paths) || Mix.raise("Usage: mix ex_okf.format PATH [--out DEST]")
dest = opts[:out] || path
{:ok, bundle} = ExOKF.load(path)
{status, diags} = ExOKF.validate(bundle)
if status == :error do
Mix.shell().error(ExOKF.Diagnostics.format(diags))
Mix.raise("Refusing to format a non-conformant bundle")
end
:ok = ExOKF.write(bundle, dest)
Mix.shell().info("Wrote normalized bundle to #{dest}")
:ok
end
end