Skip to content

Commit

Permalink
🎉 Hello, Joe!
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri committed Aug 5, 2024
0 parents commit 614a385
Show file tree
Hide file tree
Showing 13 changed files with 3,788 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: test

on:
push:
branches:
- master
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
with:
otp-version: "26.0.2"
gleam-version: "1.4.0-rc1"
rebar3-version: "3"
# elixir-version: "1.15.4"
- run: gleam deps download
- run: gleam test
- run: gleam format --check src test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# squirrel

[![Package Version](https://img.shields.io/hexpm/v/squirrel)](https://hex.pm/packages/squirrel)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/squirrel/)

```gleam
import squirrel
pub fn main() {
// TODO: An example of the project in use
}
```

Further documentation can be found at <https://hexdocs.pm/squirrel>.

## Development

```sh
gleam run # Run the project
gleam test # Run the tests
```
21 changes: 21 additions & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name = "squirrel"
version = "0.1.0"
description = "🐿️ Type safe SQL in Gleam"
licences = ["Apache-2.0"]
repository = { type = "github", user = "giacomocavalieri", repo = "squirrel" }

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
simplifile = ">= 2.0.1 and < 3.0.0"
eval = ">= 1.0.0 and < 2.0.0"
gleam_json = ">= 1.0.0 and < 2.0.0"
mug = ">= 1.1.0 and < 2.0.0"
glam = ">= 2.0.1 and < 3.0.0"
justin = ">= 1.0.1 and < 2.0.0"
filepath = ">= 1.0.0 and < 2.0.0"
gleam_community_ansi = ">= 1.4.0 and < 2.0.0"
term_size = ">= 1.0.1 and < 2.0.0"
glint = ">= 1.0.1 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
35 changes: 35 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "eval", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "eval", source = "hex", outer_checksum = "264DAF4B49DF807F303CA4A4E4EBC012070429E40BE384C58FE094C4958F9BDA" },
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
{ name = "glam", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glam", source = "hex", outer_checksum = "66EC3BCD632E51EED029678F8DF419659C1E57B1A93D874C5131FE220DFAD2B2" },
{ name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "FE79E08BF97009729259B6357EC058315B6FBB916FAD1C2FF9355115FEB0D3A4" },
{ name = "gleam_community_colour", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "795964217EBEDB3DA656F5EB8F67D7AD22872EB95182042D3E7AFEF32D3FD2FE" },
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" },
{ name = "gleam_stdlib", version = "0.39.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "2D7DE885A6EA7F1D5015D1698920C9BAF7241102836CE0C3837A4F160128A9C4" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "glint", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "369C8A289017F73581D6B9FE2F5748169EB6FA021FFAA34FA7A49EE2094C73B3" },
{ name = "justin", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "justin", source = "hex", outer_checksum = "7FA0C6DB78640C6DC5FBFD59BF3456009F3F8B485BF6825E97E1EB44E9A1E2CD" },
{ name = "mug", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "mug", source = "hex", outer_checksum = "85A61E67A7A8C25F4460D9CBEF1C09C68FC06ABBC6FF893B0A1F42AE01CBB546" },
{ name = "simplifile", version = "2.0.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "5FFEBD0CAB39BDD343C3E1CCA6438B2848847DC170BA2386DF9D7064F34DF000" },
{ name = "snag", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "54D32E16E33655346AA3E66CBA7E191DE0A8793D2C05284E3EFB90AD2CE92BCC" },
{ name = "term_size", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "term_size", source = "hex", outer_checksum = "D00BD2BC8FB3EBB7E6AE076F3F1FF2AC9D5ED1805F004D0896C784D06C6645F1" },
{ name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" },
]

[requirements]
eval = { version = ">= 1.0.0 and < 2.0.0" }
filepath = { version = ">= 1.0.0 and < 2.0.0" }
glam = { version = ">= 2.0.1 and < 3.0.0" }
gleam_community_ansi = { version = ">= 1.4.0 and < 2.0.0" }
gleam_json = { version = ">= 1.0.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
glint = { version = ">= 1.0.1 and < 2.0.0" }
justin = { version = ">= 1.0.1 and < 2.0.0" }
mug = { version = ">= 1.1.0 and < 2.0.0" }
simplifile = { version = ">= 2.0.1 and < 3.0.0" }
term_size = { version = ">= 1.0.1 and < 2.0.0" }
97 changes: 97 additions & 0 deletions src/squirrel.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import filepath
import glam/doc
import gleam/int
import gleam/io
import gleam/list
import gleam/result
import gleam_community/ansi
import glint
import simplifile
import squirrel/internal/database/postgres
import squirrel/internal/error.{type Error, CannotWriteToFile, FileWithNoQueries}
import squirrel/internal/query.{type TypedQuery}
import term_size

const clear_screen = "\u{001B}[H\u{001B}[J"

// TODO LIST:
// - [ ] How does one decide where the query goes?
// - [ ] How do I deal with duplicate names then?
// - [ ] Make a nice CLI tool
// - [ ] How does one connect to the db?
// - [ ] Somehow it treats int[][] as int[]

/// Entry point for the `squirrel` CLI.
///
pub fn main() {
let in_file = todo
let out_file = todo

io.print(clear_screen)

let res = {
use queries <- result.try(query.from_file(in_file))

let connection =
postgres.PgConnectionData(
..postgres.default_connection(),
user: "giacomocavalieri",
database: "prova",
)
use queries <- result.try(postgres.main(queries, connection))

use _ <- result.try(case queries {
[] -> Error(FileWithNoQueries(in_file))
_ -> Ok(Nil)
})
write_queries(queries, to: out_file)
}

let message_width = term_size.columns() |> result.unwrap(80)
case res {
Error(error) -> {
error.to_doc(error)
|> doc.to_string(message_width)
|> io.println
io.println("")
}

Ok(n) ->
{
"🐿️ Generated code for "
<> int.to_string(n)
<> " "
<> pluralise(n, "query", "queries")
}
|> ansi.green
|> io.println
}
}

fn write_queries(
queries: List(TypedQuery),
to file: String,
) -> Result(Int, Error) {
let directory = filepath.directory_name(file)
let _ = simplifile.create_directory_all(directory)

let imports = "import gleam/pgo\nimport decode\n"
let #(count, code) = {
use #(count, code), query <- list.fold(queries, #(0, imports))
#(count + 1, code <> "\n" <> query.generate_code(query))
}

let try_write =
simplifile.write(code, to: file)
|> result.map_error(CannotWriteToFile(file, _))

use _ <- result.try(try_write)
Ok(count)
}

fn pluralise(count: Int, singular: String, plural: String) -> String {
case count {
1 -> singular
_ -> plural
}
}
Loading

0 comments on commit 614a385

Please sign in to comment.