aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/build/packages/adglent/src/adglent@init.erl
blob: fb281014ac117df7d51de61d157099cda79f0ec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
-module(adglent@init).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).

-export([main/0]).

-spec main() -> {ok, binary()} | {error, binary()}.
main() ->
    Year = priv@prompt:value(<<"Year"/utf8>>, <<"2023"/utf8>>, false),
    Session = priv@prompt:value(<<"Session Cookie"/utf8>>, <<""/utf8>>, false),
    Use_showtime = priv@prompt:confirm(<<"Use showtime"/utf8>>, false),
    Aoc_toml_file = <<"aoc.toml"/utf8>>,
    Overwrite = case simplifile:create_file(Aoc_toml_file) of
        {ok, _} ->
            true;

        {error, eexist} ->
            priv@prompt:confirm(<<"aoc.toml exits - overwrite"/utf8>>, false);

        _ ->
            erlang:error(#{gleam_error => panic,
                    message => <<"Could not create aoc.toml"/utf8>>,
                    module => <<"adglent/init"/utf8>>,
                    function => <<"main"/utf8>>,
                    line => 29})
    end,
    _pipe@3 = case Overwrite of
        true ->
            _pipe@1 = priv@template:render(
                <<"
version = {{ version }}
year = \"{{ year }}\"
session = \"{{ session }}\"
showtime = {{ showtime }}
"/utf8>>,
                [{<<"version"/utf8>>, <<"2"/utf8>>},
                    {<<"year"/utf8>>, Year},
                    {<<"session"/utf8>>, Session},
                    {<<"showtime"/utf8>>,
                        begin
                            _pipe = gleam@bool:to_string(Use_showtime),
                            gleam@string:lowercase(_pipe)
                        end}]
            ),
            _pipe@2 = simplifile:write(Aoc_toml_file, _pipe@1),
            priv@errors:map_messages(
                _pipe@2,
                <<"aoc.toml - written"/utf8>>,
                <<"Error when writing aoc.toml"/utf8>>
            );

        false ->
            {ok, <<"aoc.toml - skipped"/utf8>>}
    end,
    priv@errors:print_result(_pipe@3),
    Gleam_toml = begin
        _pipe@4 = simplifile:read(<<"gleam.toml"/utf8>>),
        _pipe@5 = priv@errors:map_error(
            _pipe@4,
            <<"Could not read gleam.toml"/utf8>>
        ),
        _pipe@6 = priv@errors:print_error(_pipe@5),
        priv@errors:assert_ok(_pipe@6)
    end,
    Name = begin
        _pipe@7 = priv@toml:get_string(Gleam_toml, [<<"name"/utf8>>]),
        _pipe@8 = priv@errors:map_error(
            _pipe@7,
            <<"Could not read \"name\" from gleam.toml"/utf8>>
        ),
        _pipe@9 = priv@errors:print_error(_pipe@8),
        priv@errors:assert_ok(_pipe@9)
    end,
    Test_main_file = <<<<"test/"/utf8, Name/binary>>/binary,
        "_test.gleam"/utf8>>,
    _pipe@12 = case Use_showtime of
        true ->
            _pipe@10 = priv@template:render(
                <<"
import showtime

pub fn main() {
  showtime.main()
}
"/utf8>>,
                []
            ),
            _pipe@11 = simplifile:write(Test_main_file, _pipe@10),
            priv@errors:map_messages(
                _pipe@11,
                <<"Wrote "/utf8, Test_main_file/binary>>,
                <<"Could not write to "/utf8, Test_main_file/binary>>
            );

        false ->
            {ok, <<"Using existing (gleeunit) "/utf8, Test_main_file/binary>>}
    end,
    _pipe@13 = priv@errors:print_result(_pipe@12),
    priv@errors:assert_ok(_pipe@13),
    _pipe@17 = case simplifile:is_file(<<".gitignore"/utf8>>) of
        true ->
            gleam@result:'try'(
                begin
                    _pipe@14 = simplifile:read(<<".gitignore"/utf8>>),
                    gleam@result:map_error(
                        _pipe@14,
                        fun(Err) ->
                            <<"Could not read .gitignore: "/utf8,
                                (gleam@string:inspect(Err))/binary>>
                        end
                    )
                end,
                fun(Gitignore) ->
                    Aoc_toml_ignored = begin
                        _pipe@15 = gleam@string:split(Gitignore, <<"\n"/utf8>>),
                        gleam@list:find(
                            _pipe@15,
                            fun(Line) -> Line =:= <<"aoc.toml"/utf8>> end
                        )
                    end,
                    case Aoc_toml_ignored of
                        {error, _} ->
                            _pipe@16 = simplifile:append(
                                <<".gitignore"/utf8>>,
                                <<"\naoc.toml"/utf8>>
                            ),
                            priv@errors:map_messages(
                                _pipe@16,
                                <<".gitignore written"/utf8>>,
                                <<"Error when writing .gitignore"/utf8>>
                            );

                        {ok, _} ->
                            {ok,
                                <<".gitignore - skipped (already configured)"/utf8>>}
                    end
                end
            );

        false ->
            {error, <<"Could not find .gitignore"/utf8>>}
    end,
    priv@errors:print_result(_pipe@17).