Current section
Files
Jump to
Current section
Files
src/atproto_openapi@cli.erl
-module(atproto_openapi@cli).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/atproto_openapi/cli.gleam").
-export([run/3, main/0]).
-export_type([flags/0, load_outcome/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(false).
-type flags() :: {flags, binary(), binary(), list(binary())}.
-type load_outcome() :: {loaded, binary(), atproto_lexicon@ast:lexicon_doc()} |
{skipped_decode, binary(), binary()}.
-file("src/atproto_openapi/cli.gleam", 35).
?DOC(false).
-spec default_flags() -> flags().
default_flags() ->
{flags, <<"atproto"/utf8>>, <<"0.0.0"/utf8>>, []}.
-file("src/atproto_openapi/cli.gleam", 55).
?DOC(false).
-spec usage_error() -> nil.
usage_error() ->
gleam_stdlib:println_error(
<<"usage: gleam run -m atproto_openapi -- <lexicons-dir> <out-file> [--title T] [--version V] [--server URL]"/utf8>>
),
erlang:halt(1).
-file("src/atproto_openapi/cli.gleam", 115).
?DOC(false).
-spec report_summary(atproto_openapi@emit:document(), binary()) -> nil.
report_summary(Document, Out_file) ->
gleam_stdlib:println(
<<<<<<<<(erlang:integer_to_binary(erlang:element(3, Document)))/binary,
" operation(s), "/utf8>>/binary,
(erlang:integer_to_binary(erlang:element(4, Document)))/binary>>/binary,
" schema(s) written to "/utf8>>/binary,
Out_file/binary>>
),
gleam@list:each(
erlang:element(5, Document),
fun(Nsid) ->
gleam_stdlib:println(
<<"skipped subscription (WSS, not representable as a path): "/utf8,
Nsid/binary>>
)
end
).
-file("src/atproto_openapi/cli.gleam", 105).
?DOC(false).
-spec write_document(atproto_openapi@emit:document(), binary()) -> nil.
write_document(Document, Out_file) ->
case simplifile:write(
Out_file,
gleam@json:to_string(erlang:element(2, Document))
) of
{ok, _} ->
report_summary(Document, Out_file);
{error, Reason} ->
gleam_stdlib:println_error(
<<<<Out_file/binary, ": write failed: "/utf8>>/binary,
(gleam@string:inspect(Reason))/binary>>
),
erlang:halt(1)
end.
-file("src/atproto_openapi/cli.gleam", 161).
?DOC(false).
-spec report_load_outcome(load_outcome()) -> nil.
report_load_outcome(Outcome) ->
case Outcome of
{loaded, _, _} ->
nil;
{skipped_decode, _, Message} ->
gleam_stdlib:println_error(Message)
end.
-file("src/atproto_openapi/cli.gleam", 145).
?DOC(false).
-spec load_one(binary()) -> load_outcome().
load_one(Path) ->
case simplifile:read(Path) of
{error, Reason} ->
{skipped_decode,
Path,
<<<<Path/binary, ": read failed: "/utf8>>/binary,
(gleam@string:inspect(Reason))/binary>>};
{ok, Source} ->
case atproto_lexicon@decoding:decode_json(Source) of
{ok, Doc} ->
{loaded, Path, Doc};
{error, Errors} ->
{skipped_decode,
Path,
<<<<Path/binary, ": decode failed: "/utf8>>/binary,
(gleam@string:inspect(Errors))/binary>>}
end
end.
-file("src/atproto_openapi/cli.gleam", 137).
?DOC(false).
-spec source_files(binary()) -> {ok, list(binary())} | {error, binary()}.
source_files(Src_dir) ->
_pipe = simplifile:get_files(Src_dir),
_pipe@1 = gleam@result:map(
_pipe,
fun(_capture) ->
gleam@list:filter(
_capture,
fun(_capture@1) ->
gleam_stdlib:string_ends_with(_capture@1, <<".json"/utf8>>)
end
)
end
),
gleam@result:map_error(
_pipe@1,
fun(E) ->
<<"could not read directory: "/utf8,
(gleam@string:inspect(E))/binary>>
end
).
-file("src/atproto_openapi/cli.gleam", 130).
?DOC(false).
-spec load_tree(binary()) -> list(load_outcome()).
load_tree(Src_dir) ->
case source_files(Src_dir) of
{ok, Files} ->
gleam@list:map(Files, fun load_one/1);
{error, Reason} ->
[{skipped_decode,
Src_dir,
<<<<Src_dir/binary, ": "/utf8>>/binary, Reason/binary>>}]
end.
-file("src/atproto_openapi/cli.gleam", 84).
?DOC(false).
-spec run(binary(), binary(), flags()) -> nil.
run(Src_dir, Out_file, Flags) ->
Outcomes = load_tree(Src_dir),
gleam@list:each(Outcomes, fun report_load_outcome/1),
Docs = gleam@list:filter_map(Outcomes, fun(Outcome) -> case Outcome of
{loaded, _, Doc} ->
{ok, Doc};
{skipped_decode, _, _} ->
{error, nil}
end end),
case atproto_openapi@emit:assemble(
Docs,
erlang:element(2, Flags),
erlang:element(3, Flags),
erlang:element(4, Flags)
) of
{ok, Document} ->
write_document(Document, Out_file);
{error, Ref_error} ->
gleam_stdlib:println_error(
<<"generation failed: "/utf8,
(atproto_openapi@refs:describe(Ref_error))/binary>>
),
erlang:halt(1)
end.
-file("src/atproto_openapi/cli.gleam", 64).
?DOC(false).
-spec parse_flags_loop(list(binary()), flags()) -> {ok, flags()} | {error, nil}.
parse_flags_loop(Args, Acc) ->
case Args of
[] ->
{ok, Acc};
[<<"--title"/utf8>>, Value | Rest] ->
parse_flags_loop(
Rest,
{flags, Value, erlang:element(3, Acc), erlang:element(4, Acc)}
);
[<<"--version"/utf8>>, Value@1 | Rest@1] ->
parse_flags_loop(
Rest@1,
{flags, erlang:element(2, Acc), Value@1, erlang:element(4, Acc)}
);
[<<"--server"/utf8>>, Value@2 | Rest@2] ->
parse_flags_loop(
Rest@2,
{flags,
erlang:element(2, Acc),
erlang:element(3, Acc),
lists:append(erlang:element(4, Acc), [Value@2])}
);
_ ->
{error, nil}
end.
-file("src/atproto_openapi/cli.gleam", 60).
?DOC(false).
-spec parse_flags(list(binary())) -> {ok, flags()} | {error, nil}.
parse_flags(Args) ->
parse_flags_loop(Args, default_flags()).
-file("src/atproto_openapi/cli.gleam", 44).
?DOC(false).
-spec main() -> nil.
main() ->
case erlang:element(4, argv:load()) of
[Src_dir, Out_file | Rest] ->
case parse_flags(Rest) of
{ok, Flags} ->
run(Src_dir, Out_file, Flags);
{error, _} ->
usage_error()
end;
_ ->
usage_error()
end.