Skip to content

Instantly share code, notes, and snippets.

@zkessin
Created March 6, 2014 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zkessin/9392821 to your computer and use it in GitHub Desktop.
Save zkessin/9392821 to your computer and use it in GitHub Desktop.
-module(rebar_yang).
-compile(export_all).
%% Add to rebar.config
%% {plugins, [rebar_yang]}.
pre_compile(_Config,AppFile) ->
rebar_log:log(info, "rebar_yang: ~p~n", [rebar_utils:get_cwd()]),
scan_files(rebar_utils:get_cwd()),
ok.
pre_rebar_yang(_,_) ->
ok.
post_rebar_yang(_,_) ->
ok.
is_base_dir() ->
rebar_utils:get_cwd() == rebar_config:get_global(base_dir, undefined).
last_mod(File) ->
calendar:datetime_to_gregorian_seconds(filelib:last_modified(File)).
make_hrl_file(YangFile,Dir) ->
BaseName = filename:basename(YangFile, ".yang"),
HRLFile = filename:flatten([Dir,"/", BaseName, ".hrl"]),
HRLFile.
yang_needs_build(YangFile, Dir) ->
HRLFile = make_hrl_file(YangFile,Dir),
rebar_log:log(info, "COMPILE ~s -> ~s ~n", [YangFile,HRLFile]),
case filelib:is_file(HRLFile) of
false ->
true;
true ->
last_mod(YangFile) > last_mod(HRLFile)
end.
scan_files(Dir) ->
ToBuild = filelib:fold_files(Dir ++ "/src/",
"\.yang$",
false,
fun(YangFile, Acc) ->
case yang_needs_build(YangFile, Dir) of
true ->
[YangFile|Acc];
false -> Acc
end
end,
[]),
[build_yang(File, Dir) || File <- ToBuild].
build_yang(File,Dir) ->
rebar_log:log(info, "Building Yang File ~s~n", [File]),
case yang:deep_parse_file(File) of
{ok, Yang} ->
Ts = yang:typespec(Yang),
HRLFile = make_hrl_file(File,Dir),
file:write_file(HRLFile, yang_typespec:hrl(Ts));
{error, Error} ->
rebar_log:log(error, "Unable to build Yang file ~s due to ~s",[File, Error])
end,
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment