meta/lib/mix/tasks/generate_opennebula_records.ex

44 lines
1.3 KiB
Elixir
Raw Normal View History

defmodule Mix.Tasks.GenerateOpenNebulaRecords do
use Mix.Task
@moduledoc """
Generate erlang records from OpenNebula's XSD templates. Inspired from
http://codesinger.blogspot.com/2015/12/elixir-erlang-records-and-erlsom-xml.html
"""
@opennebula_release "5.12"
@shortdoc "Generate erlang records from OpenNebula's XSD templates"
def run(_) do
priv_dir = to_string(:code.priv_dir(:meta))
xsd_dir = Path.join([priv_dir, "opennebula", @opennebula_release, "xsd-src"])
hrl_dir = Path.join([priv_dir, "opennebula", @opennebula_release, "xsd-records"])
files = xsd_dir
|> File.ls!()
|> Enum.filter(fn filename -> String.ends_with?(filename, ".xsd") end)
|> Enum.sort()
# Delete existing hrl headers.
if File.exists?(hrl_dir) do
File.rm_rf!(hrl_dir)
end
File.mkdir_p(hrl_dir)
for xsd_filename <- files do
hrl_filename = String.replace_trailing(xsd_filename, ".xsd", ".hrl")
xsd_file = Path.join([xsd_dir, xsd_filename]) |> String.to_charlist
hrl_file = Path.join([hrl_dir, hrl_filename]) |> String.to_charlist
IO.write "> Processing #{xsd_filename}... "
try do
:erlsom.write_xsd_hrl_file(xsd_file, hrl_file, [])
IO.puts "OK"
catch
e -> IO.puts "Error: #{inspect(e)}"
end
end
end
end