src/glipt@cmd@project.erl

-module(glipt@cmd@project).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glipt/cmd/project.gleam").
-export([execute/1]).

-file("src/glipt/cmd/project.gleam", 9).
-spec execute(binary()) -> nil.
execute(File_path) ->
    case simplifile:read(File_path) of
        {error, E} ->
            gleam_stdlib:println_error(
                <<"Error reading file: "/utf8,
                    (gleam@string:inspect(E))/binary>>
            );

        {ok, Source} ->
            Meta = glipt@parser:parse(Source),
            Project_name = glipt@internal@path:drop_extension(
                glipt@internal@path:basename(File_path)
            ),
            Dir = Project_name,
            Src_dir = <<Dir/binary, "/src"/utf8>>,
            case simplifile_erl:is_directory(Dir) of
                {ok, true} ->
                    gleam_stdlib:println_error(
                        <<<<"Error: directory '"/utf8, Dir/binary>>/binary,
                            "' already exists"/utf8>>
                    );

                _ ->
                    Toml_content = glipt@internal@toml:generate_project_toml(
                        Project_name,
                        Meta
                    ),
                    Result = begin
                        gleam@result:'try'(
                            simplifile:create_directory_all(Src_dir),
                            fun(_) ->
                                gleam@result:'try'(
                                    simplifile:write(
                                        <<Dir/binary, "/gleam.toml"/utf8>>,
                                        Toml_content
                                    ),
                                    fun(_) ->
                                        simplifile:copy(
                                            File_path,
                                            <<<<Src_dir/binary, "/"/utf8>>/binary,
                                                (glipt@internal@path:basename(
                                                    File_path
                                                ))/binary>>
                                        )
                                    end
                                )
                            end
                        )
                    end,
                    case Result of
                        {ok, _} ->
                            gleam_stdlib:println(
                                <<<<<<"Created project '"/utf8,
                                            Project_name/binary>>/binary,
                                        "/' from "/utf8>>/binary,
                                    File_path/binary>>
                            );

                        {error, E@1} ->
                            gleam_stdlib:println_error(
                                <<"Error: "/utf8,
                                    (gleam@string:inspect(E@1))/binary>>
                            )
                    end
            end
    end.