Packages

A fun, friendly, and type-safe ORM for Gleam! Compose type-safe SQL queries, generate decoders and types from schemas, and with a fun CLI!

Current section

Files

Jump to
funsies src funsies@cli_project@project.erl
Raw

src/funsies@cli_project@project.erl

-module(funsies@cli_project@project).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([name/0, src/0, find_schema_files/0, create_files/1, work/0]).
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/cli_project/project.gleam", 31).
-spec try_nil(
{ok, LOE} | {error, any()},
fun((LOE) -> {ok, LOI} | {error, nil})
) -> {ok, LOI} | {error, nil}.
try_nil(Result, Do) ->
gleam@result:'try'(gleam@result:replace_error(Result, nil), Do).
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/cli_project/project.gleam", 54).
-spec find_root(binary()) -> binary().
find_root(Path) ->
Toml = filepath:join(Path, <<"gleam.toml"/utf8>>),
case simplifile_erl:is_file(Toml) of
{ok, false} ->
find_root(filepath:join(<<".."/utf8>>, Path));
{error, _} ->
find_root(filepath:join(<<".."/utf8>>, Path));
{ok, true} ->
Path
end.
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/cli_project/project.gleam", 50).
-spec root() -> binary().
root() ->
find_root(<<"."/utf8>>).
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/cli_project/project.gleam", 22).
-spec name() -> {ok, binary()} | {error, nil}.
name() ->
Configuration_path = filepath:join(root(), <<"gleam.toml"/utf8>>),
try_nil(
simplifile:read(Configuration_path),
fun(Configuration) ->
try_nil(
tom:parse(Configuration),
fun(Toml) ->
try_nil(
tom:get_string(Toml, [<<"name"/utf8>>]),
fun(Name) -> {ok, Name} end
)
end
)
end
).
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/cli_project/project.gleam", 43).
-spec src() -> binary().
src() ->
filepath:join(root(), <<"src"/utf8>>).
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/cli_project/project.gleam", 65).
-spec find_schema_files() -> {ok, gleam@dict:dict(binary(), binary())} |
{error, nil}.
find_schema_files() ->
Current_root = root(),
gleam@io:println(<<"Current root: "/utf8, Current_root/binary>>),
_assert_subject = simplifile:current_directory(),
{ok, Cwd} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"funsies/cli_project/project"/utf8>>,
function => <<"find_schema_files"/utf8>>,
line => 69})
end,
_assert_subject@1 = globlin:new_pattern(
filepath:join(Cwd, <<"/src/schema/*.gleam"/utf8>>)
),
{ok, Pattern} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"funsies/cli_project/project"/utf8>>,
function => <<"find_schema_files"/utf8>>,
line => 71})
end,
case globlin_fs:glob(Pattern, regular_files) of
{ok, Files} ->
gleam@io:println(
<<"Files found: "/utf8,
(begin
_pipe = Files,
gleam@string:join(_pipe, <<", "/utf8>>)
end)/binary>>
),
_pipe@1 = Files,
_pipe@5 = gleam@list:map(
_pipe@1,
fun(File) ->
File_names = begin
_pipe@2 = File,
filepath:base_name(_pipe@2)
end,
File_paths = <<"schema/"/utf8, File_names/binary>>,
{File_paths,
begin
_pipe@3 = File,
_pipe@4 = filepath:base_name(_pipe@3),
filepath:strip_extension(_pipe@4)
end}
end
),
_pipe@6 = maps:from_list(_pipe@5),
{ok, _pipe@6};
{error, Err} ->
gleam@io:debug(Err),
{error, nil}
end.
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/cli_project/project.gleam", 100).
-spec create_files(gleam@dict:dict(binary(), binary())) -> {ok, nil} |
{error, simplifile:file_error()}.
create_files(Input) ->
Values = gleam@dict:values(Input),
simplifile_erl:create_directory(<<(src())/binary, "/funs/"/utf8>>),
Code = <<<<<<<<<<<<<<<<<<<<<<"// code to be run to generate the types and decoders"/utf8,
"\n// DO NOT MODIFY"/utf8>>/binary,
"\n // generated by `funsies`"/utf8>>/binary,
"\n\nimport funsies/decoder"/utf8>>/binary,
"\nimport funsies/orm_gen"/utf8>>/binary,
"\n\n"/utf8>>/binary,
(gleam@string:concat(
gleam@list:map(
Values,
fun(Val) ->
<<<<"import schema/"/utf8, Val/binary>>/binary,
"\n"/utf8>>
end
)
))/binary>>/binary,
"\n"/utf8>>/binary,
"\n"/utf8>>/binary,
"pub fn main() {"/utf8>>/binary,
(gleam@string:concat(
gleam@list:map(
Values,
fun(Val@1) ->
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"decoder.generate_row_type("/utf8,
Val@1/binary>>/binary,
"."/utf8>>/binary,
Val@1/binary>>/binary,
"()"/utf8>>/binary,
")"/utf8>>/binary,
"\n"/utf8>>/binary,
"decoder.generate_decoder_code("/utf8>>/binary,
Val@1/binary>>/binary,
"."/utf8>>/binary,
Val@1/binary>>/binary,
"()"/utf8>>/binary,
")"/utf8>>/binary,
"\n"/utf8>>/binary,
"orm_gen.create_insert("/utf8>>/binary,
"\""/utf8>>/binary,
"src/funs/insert.gleam"/utf8>>/binary,
"\", "/utf8>>/binary,
Val@1/binary>>/binary,
"."/utf8>>/binary,
Val@1/binary>>/binary,
"())"/utf8>>/binary,
"\n"/utf8>>
end
)
))/binary>>/binary,
"}"/utf8>>,
simplifile:write(<<(src())/binary, "/funs/generate.gleam"/utf8>>, Code).
-file("/Users/ashercohen/Desktop/ASHERSOPRO/funsies/src/funsies/cli_project/project.gleam", 150).
-spec work() -> {ok, nil} | {error, nil}.
work() ->
_assert_subject = simplifile:current_directory(),
{ok, Cwd} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"funsies/cli_project/project"/utf8>>,
function => <<"work"/utf8>>,
line => 151})
end,
case shellout:command(
<<"gleam"/utf8>>,
[<<"run"/utf8>>, <<"-m"/utf8>>, <<"funs/generate"/utf8>>],
<<Cwd/binary, "/"/utf8>>,
[]
) of
{ok, Res} ->
gleam@io:println(
<<"Successfully generated types and decoders."/utf8>>
),
{ok, nil};
{error, E} ->
gleam@io:debug(<<"Error generating types and decoders"/utf8>>),
{error, nil}
end.