Skip to content

Commit

Permalink
Add support for Windows
Browse files Browse the repository at this point in the history
Let's do the validation in remote CI (GHA)
  • Loading branch information
paulo-ferraz-oliveira committed Jul 13, 2023
1 parent 24a9486 commit 90938d1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
32 changes: 22 additions & 10 deletions src/rebar3_checkshell_arch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,39 @@ do(Files, State) ->
do(get_archs(), Files, State).

-spec do(Archs, Files, State) -> Result when
Archs :: {IsMacOS :: boolean(), IsLinux :: boolean(), IsWindows :: boolean()},
Archs ::
{IsMacOS :: true, IsLinux :: false, IsWindows :: false}
| {IsMacOS :: false, IsLinux :: true, IsWindows :: false}
| {IsMacOS :: false, IsLinux :: false, IsWindows :: true},
Files :: string(),
Result :: {ok, State} | {error, rebar3_checkshell_utils:str()}.
do({true = _IsMacOS, false = _IsLinux, false = _IsWindows}, Files, State) ->
do_unix_like(darwin, Files, State);
execute(darwin, Files, State);
do({false = _IsMacOS, true = _IsLinux, false = _IsWindows}, Files, State) ->
do_unix_like(linux, Files, State);
do({false = _IsMacOS, false = _IsLinux, true = _IsWindows}, _Files, _State) ->
{error, "checkshell: no support for Windows yet"}.
execute(linux, Files, State);
do({false = _IsMacOS, false = _IsLinux, true = _IsWindows}, Files, State) ->
execute(windows, Files, State).

-spec do_unix_like(Arch, Files, State) -> Result when
Arch :: darwin | linux,
-spec execute(Arch, Files, State) -> Result when
Arch :: darwin | linux | windows,
Files :: string(),
Result :: {ok, State} | {error, rebar3_checkshell_utils:str()}.
do_unix_like(Arch, Files, State) ->
Exec =
rebar3_checkshell_utils:priv_dir() ++ "/" ++ atom_to_list(Arch) ++ ".x86_64/shellcheck",
execute(Arch, Files, State) ->
ArchDir = filename:join(rebar3_checkshell_utils:priv_dir(), atom_to_list(Arch) ++ ".x86_64"),
Ext = extension_for(Arch),
Exec = filename:join(ArchDir, "shellcheck" ++ Ext),
OpenPortCmd = {spawn, Exec ++ args(Files, State)},
OpenPortOpts = [exit_status],
result(port_loop(erlang:open_port(OpenPortCmd, OpenPortOpts), ""), State).

-spec extension_for(Arch) -> Ext when
Arch :: darwin | linux | windows,
Ext :: rebar3_checkshell_utils:str().
extension_for(windows) ->
".exe";
extension_for(_Other) ->
"".

-spec result({ExitCode, Analysis}, State) -> Result when
ExitCode :: non_neg_integer(),
Analysis :: string(),
Expand Down
24 changes: 14 additions & 10 deletions src/rebar3_checkshell_prv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ init(State) ->
Result :: {error, rebar3_checkshell_utils:str()} | {ok, State}.
do(State) ->
Files = get_arg(files, State),
case Files of
undefined ->
{error, "checkshell: missing --files"};
_ ->
_ = rebar_log:log(
info, "rebar3_checkshell analysis starting, this may take a while...", []
),
rebar3_checkshell_arch:do(Files, State)
end.
do_for(Files, State).

-spec do_for(Files, State) -> Result when
Files :: undefined | string(),
Result :: {ok, State} | {error, rebar3_checkshell_utils:str()}.
do_for(undefined = _Files, _State) ->
{error, "checkshell: missing --files"};
do_for(Files, State) ->
_ = rebar_log:log(
info, "rebar3_checkshell analysis starting, this may take a while...", []
),
rebar3_checkshell_arch:do(Files, State).

-spec format_error(Reason) -> Result when
Reason :: term(),
Expand All @@ -49,7 +52,8 @@ format_error(Reason) ->
-spec version() -> Result when
Result :: rebar3_checkshell_utils:str().
version() ->
{ok, Version} = file:read_file(rebar3_checkshell_utils:priv_dir() ++ "/VERSION"),
VersionFile = filename:join(rebar3_checkshell_utils:priv_dir(), "VERSION"),
{ok, Version} = file:read_file(VersionFile),
binary_to_list(Version).

-spec get_arg(Arg, State) -> Result when
Expand Down

0 comments on commit 90938d1

Please sign in to comment.