-module(glipt@cmd@add).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glipt/cmd/add.gleam").
-export([execute/1]).
-file("src/glipt/cmd/add.gleam", 37).
-spec parse_package_spec(binary()) -> {binary(), binary()}.
parse_package_spec(Spec) ->
case gleam_stdlib:contains_string(Spec, <<"@"/utf8>>) of
true ->
case gleam@string:split_once(Spec, <<"@"/utf8>>) of
{ok, {Name, Ver}} ->
{Name, glipt@parser:expand_shorthand_version(Ver)};
{error, nil} ->
{Spec, <<""/utf8>>}
end;
false ->
case gleam@string:split_once(Spec, <<" "/utf8>>) of
{ok, {Name@1, Constraint}} ->
{Name@1, Constraint};
{error, nil} ->
{Spec, <<""/utf8>>}
end
end.
-file("src/glipt/cmd/add.gleam", 8).
-spec execute(list(binary())) -> nil.
execute(Args) ->
case Args of
[Package_spec, File_path] ->
{Name, Constraint} = parse_package_spec(Package_spec),
case Constraint of
<<""/utf8>> ->
gleam_stdlib:println_error(
<<"Error: version is required in v0.1. Use: glipt add package@1.0.0 file.gleam"/utf8>>
);
_ ->
Source = begin
_pipe = simplifile:read(File_path),
gleam@result:unwrap(_pipe, <<""/utf8>>)
end,
Meta = glipt@parser:parse(Source),
Updated = glipt@parser:update_dep(Meta, Name, Constraint),
New_content = glipt@internal@script:reassemble(
Updated,
Source
),
case simplifile:write(File_path, New_content) of
{ok, nil} ->
gleam_stdlib:println(
<<<<<<<<<<"Added "/utf8, Name/binary>>/binary,
" "/utf8>>/binary,
Constraint/binary>>/binary,
" to "/utf8>>/binary,
File_path/binary>>
);
{error, E} ->
gleam_stdlib:println_error(
<<"Error writing file: "/utf8,
(gleam@string:inspect(E))/binary>>
)
end
end;
_ ->
gleam_stdlib:println_error(
<<"Usage: glipt add <package@version> <file.gleam>"/utf8>>
)
end.