Skip to content

Commit

Permalink
Format using erlfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Feb 1, 2021
1 parent 54a3596 commit f2a2ac0
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 98 deletions.
25 changes: 8 additions & 17 deletions src/cargo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@

-type result() :: cargo_cmd:output().


-spec init(file:name_all()) -> cargo_opts:t() | no_return().
init(Path) ->
init(Path, #{}).


-spec init(file:name_all(), #{ atom() => _ }) -> cargo_opts:t() | no_return().
-spec init(file:name_all(), #{atom() => _}) -> cargo_opts:t() | no_return().
init(Path, Opts) ->
cargo_opts:new(Opts#{ path => Path }).
cargo_opts:new(Opts#{path => Path}).

-spec metadata(cargo_opts:t()) -> #{ binary() => _ } | no_return().
-spec metadata(cargo_opts:t()) -> #{binary() => _} | no_return().
metadata(Opts0) ->
% --release is invalid for metadata, skip
Opts1 = cargo_opts:release(Opts0, false),
Expand All @@ -45,17 +43,15 @@ metadata(Opts0) ->
),
Metadata.


-spec build_raw(cargo_opts:t(), cargo_util:maybe_package()) -> result() | no_return().
build_raw(Opts, MaybePackage) ->
cargo_cmd:run_with_flags(
Opts,
"build",
["--message-format=json-diagnostic-short"] ++
cargo_util:package_flag(MaybePackage)
cargo_util:package_flag(MaybePackage)
).


-spec test_all(cargo_opts:t()) -> result() | no_return().
test_all(Opts) ->
do_test(Opts, false).
Expand All @@ -69,11 +65,10 @@ do_test(Opts, MaybePackage) ->
Opts,
"test",
["--message-format=json-diagnostic-short"] ++
cargo_util:package_flag(MaybePackage) ++
["--", "-Z", "unstable-options", "--format=json"]
cargo_util:package_flag(MaybePackage) ++
["--", "-Z", "unstable-options", "--format=json"]
).


-spec clean(cargo_opts:t()) -> ok | no_return().
clean(Opts) ->
cargo_cmd:run_with_flags(
Expand All @@ -83,8 +78,7 @@ clean(Opts) ->
),
ok.


-spec get_package_versions(cargo_opts:t()) -> #{ atom() => _ }.
-spec get_package_versions(cargo_opts:t()) -> #{atom() => _}.
get_package_versions(Opts) ->
#{<<"packages">> := Packages} = metadata(Opts),
maps:from_list([
Expand All @@ -98,23 +92,20 @@ get_package_versions(Opts) ->
|| M <- Packages
]).


-spec build_all(cargo_opts:t()) -> [cargo_artifact:t()].
build_all(Opts) ->
do_build(Opts, false).


-spec build(cargo_opts:t(), cargo_util:to_binary()) -> [cargo_artifact:t()].
build(Opts, Package) ->
do_build(Opts, {true, Package}).


do_build(Opts, MaybePackage) ->
Packages = get_package_versions(Opts),
Outputs = build_raw(Opts, MaybePackage),

lists:filtermap(
fun (Entry) ->
fun(Entry) ->
case cargo_artifact:from_json(Entry) of
{ok, Artifact} ->
PackageId = cargo_artifact:package_id(Artifact),
Expand Down
34 changes: 17 additions & 17 deletions src/cargo_artifact.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
executable/1
]).


-record(artifact, {
name :: binary(),
% crate :: binary(), from package_id?
Expand All @@ -26,7 +25,9 @@
}).

-type kind() :: bin | cdylib | other.

-opaque t() :: #artifact{}.

-export_type([t/0]).

-spec from_json(jsx:json_term()) -> {ok, t()} | error.
Expand All @@ -36,12 +37,13 @@ from_json(Entry) ->
#{
<<"package_id">> := PackageId,
<<"target">> := Target
} = Entry,
} = Entry,
Kind = kind_to_atom(maps:get(<<"kind">>, Target)),
Executable = case maps:get(<<"executable">>, Entry, undefined) of
null -> undefined;
V -> V
end,
Executable =
case maps:get(<<"executable">>, Entry, undefined) of
null -> undefined;
V -> V
end,
{ok, #artifact{
name = maps:get(<<"name">>, Target),
filenames = maps:get(<<"filenames">>, Entry),
Expand All @@ -54,32 +56,30 @@ from_json(Entry) ->
error
end.


-spec name(t()) -> binary().
name(#artifact{name=C}) -> C.
name(#artifact{name = C}) -> C.

-spec version(t()) -> binary() | undefined.
version(#artifact{version=V}) -> V.
version(#artifact{version = V}) -> V.

-spec version(t(), binary()) -> t().
version(#artifact{} = A, V) -> A#artifact{version=V}.
version(#artifact{} = A, V) -> A#artifact{version = V}.

-spec package_id(t()) -> binary().
package_id(#artifact{package_id=P}) -> P.
package_id(#artifact{package_id = P}) -> P.

-spec fresh(t()) -> boolean().
fresh(#artifact{fresh=F}) -> F.
fresh(#artifact{fresh = F}) -> F.

-spec kind(t()) -> kind().
kind(#artifact{kind=K}) -> K.
kind(#artifact{kind = K}) -> K.

-spec filenames(t()) -> [binary()].
filenames(#artifact{filenames=F}) -> F.
filenames(#artifact{filenames = F}) -> F.

-spec executable(t()) -> binary() | undefined.
executable(#artifact{executable=E}) -> E.

executable(#artifact{executable = E}) -> E.

kind_to_atom([<<"bin">>]) -> bin;
kind_to_atom([<<"cdylib">>]) -> cdylib;
kind_to_atom(_) -> other.
kind_to_atom(_) -> other.
87 changes: 43 additions & 44 deletions src/cargo_cmd.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,53 @@

-spec run_with_flags(cargo_opts:t(), iolist(), [iolist()]) -> output().
run_with_flags(Opts, Cmd, Flags) ->

Inserts =
lists:foldl(fun({release, true}, Acc) ->
["--release" | Acc];
({target, Target}, Acc) when is_binary(Target) ->
[io_lib:format("--target=~s", [Target]) | Acc];
({target_dir, TargetDir}, Acc) when is_binary(TargetDir) ->
[io_lib:format("--target-dir=~s", [TargetDir]) | Acc];
(_, Acc) ->
Acc
end,
[],
[{release, cargo_opts:release(Opts)},
{target, cargo_opts:target(Opts)},
{target_dir, cargo_opts:target_dir(Opts)}]),
lists:foldl(
fun
({release, true}, Acc) ->
["--release" | Acc];
({target, Target}, Acc) when is_binary(Target) ->
[io_lib:format("--target=~s", [Target]) | Acc];
({target_dir, TargetDir}, Acc) when is_binary(TargetDir) ->
[io_lib:format("--target-dir=~s", [TargetDir]) | Acc];
(_, Acc) ->
Acc
end,
[],
[
{release, cargo_opts:release(Opts)},
{target, cargo_opts:target(Opts)},
{target_dir, cargo_opts:target_dir(Opts)}
]
),

Cmd1 = [Cmd] ++ Inserts ++ Flags,

run(Opts, Cmd1).


-spec run(cargo_opts:t(), [iolist()]) -> output().
run(Opts, Args) ->
C0 = ["cargo"],
C1 =
case cargo_opts:toolchain(Opts) of
undefined ->
C0;
Toolchain ->
C0 ++ [io_lib:format("+~s", [Toolchain])]
end,

Cmd = lists:flatten(lists:join(
" ",
[binary_to_list(cargo_util:ensure_binary(I)) || I <- C1 ++ Args]
)),
case cargo_opts:toolchain(Opts) of
undefined ->
C0;
Toolchain ->
C0 ++ [io_lib:format("+~s", [Toolchain])]
end,

Cmd = lists:flatten(
lists:join(
" ",
[binary_to_list(cargo_util:ensure_binary(I)) || I <- C1 ++ Args]
)
),

exec(Cmd, cargo_opts:path(Opts)).


% Code derived from rebar3
-spec exec(string(), file:filename_all()) -> output().
exec(Command, Path) ->

Options1 = [
in,
binary,
Expand All @@ -84,14 +87,13 @@ exec(Command, Path) ->
case loop(Port, []) of
{ok, Output} ->
Output;
{error, {_Rc, _Output}=Err} ->
{error, {_Rc, _Output} = Err} ->
error({cargo_error, Err})
end
after
port_close(Port)
end.


-spec loop(port(), [T | {incomplete, _}]) -> {ok, [T]} | {error, _}.
loop(Port, Acc) ->
receive
Expand All @@ -107,7 +109,6 @@ loop(Port, Acc) ->
end
end.


-spec patch_on_windows(string()) -> string().
patch_on_windows(Cmd) ->
case os:type() of
Expand All @@ -117,19 +118,20 @@ patch_on_windows(Cmd) ->
Cmd
end.


-spec env() -> [{string(), string()}].
env() ->
case os:type() of
{unix, darwin} ->
% https://github.com/rust-lang/cargo/issues/3287
% https://github.com/rust-lang/rust/pull/36574
[{"RUSTFLAGS", "--codegen link-arg=-flat_namespace --codegen link-arg=-undefined --codegen link-arg=suppress"}];
[
{"RUSTFLAGS",
"--codegen link-arg=-flat_namespace --codegen link-arg=-undefined --codegen link-arg=suppress"}
];
_ ->
[]
end.


% Ignore dialyzer warnings for finalize and handle_output as the spec
% for jsx:decode/2 is broken (does not include incomplete and with_tail)
-dialyzer([
Expand All @@ -139,28 +141,25 @@ env() ->

finalize([{incomplete, _Decode} | _Acc]) ->
error(incomplete_json);

finalize(Acc) ->
lists:reverse(Acc).

handle_output("", Acc) ->
Acc;

handle_output(<<"">>, Acc) ->
Acc;

handle_output(Line, Acc0) ->
{DecodeRes, Acc} =
case Acc0 of
[{incomplete, Decode} | Acc1] ->
{Decode(Line), Acc1};
Acc2 ->
{jsx:decode(Line, [return_maps, stream, return_tail]), Acc2}
end,
case Acc0 of
[{incomplete, Decode} | Acc1] ->
{Decode(Line), Acc1};
Acc2 ->
{jsx:decode(Line, [return_maps, stream, return_tail]), Acc2}
end,

case DecodeRes of
{with_tail, Res, Tail} ->
handle_output(Tail, [Res | Acc]);
Else ->
[Else | Acc]
end.
end.
25 changes: 12 additions & 13 deletions src/cargo_opts.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,37 @@ new(Opts) ->
TargetDir = maps:get(target_dir, Opts, undefined),

#opts{
path=Path,
release=Release,
toolchain=Toolchain,
target=Target,
target_dir=TargetDir
path = Path,
release = Release,
toolchain = Toolchain,
target = Target,
target_dir = TargetDir
}.

-spec toolchain(t()) -> binary() | undefined.
toolchain(#opts{toolchain=Toolchain}) ->
toolchain(#opts{toolchain = Toolchain}) ->
Toolchain.

-spec release(t()) -> boolean().
release(#opts{release=Release}) ->
release(#opts{release = Release}) ->
Release.

-spec release(t(), boolean()) -> t().
release(O, Release) ->
O#opts{release=Release}.
O#opts{release = Release}.

-spec path(t()) -> file:filename_all().
path(#opts{path=Path}) ->
path(#opts{path = Path}) ->
Path.

-spec target(t()) -> binary() | undefined.
target(#opts{target=Target}) ->
target(#opts{target = Target}) ->
Target.

-spec target_dir(t()) -> binary() | undefined.
target_dir(#opts{target_dir=TargetDir}) ->
target_dir(#opts{target_dir = TargetDir}) ->
TargetDir.

-spec target_dir(t(), binary()) -> t().
target_dir(O, TargetDir) ->
O#opts{target_dir=TargetDir}.

O#opts{target_dir = TargetDir}.
Loading

0 comments on commit f2a2ac0

Please sign in to comment.