Skip to content

Commit 8ff5a48

Browse files
committed
✨ Config file support
1 parent 7427eb2 commit 8ff5a48

File tree

7 files changed

+52
-10
lines changed

7 files changed

+52
-10
lines changed

gleam.toml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ glam = ">= 2.0.0 and < 3.0.0"
2121
dedent = ">= 1.0.0 and < 2.0.0"
2222
filepath = ">= 1.0.0 and < 2.0.0"
2323
simplifile = ">= 1.7.0 and < 2.0.0"
24+
tom = ">= 0.3.0 and < 1.0.0"
2425

2526
[dev-dependencies]
2627
gleeunit = "~> 1.0"

manifest.toml

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ packages = [
1515
{ name = "pprint", version = "1.0.2", build_tools = ["gleam"], requirements = ["glam", "gleam_stdlib"], otp_app = "pprint", source = "hex", outer_checksum = "3EB2A7A8F72322F73EEF342374D0354AE39E7F9C64678B960BE8B2DC1B564AE1" },
1616
{ name = "simplifile", version = "1.7.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "1D5DFA3A2F9319EC85825F6ED88B8E449F381B0D55A62F5E61424E748E7DDEB0" },
1717
{ name = "thoas", version = "1.2.0", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "540C8CB7D9257F2AD0A14145DC23560F91ACDCA995F0CCBA779EB33AF5D859D1" },
18+
{ name = "tom", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "0831C73E45405A2153091226BF98FB485ED16376988602CC01A5FD086B82D577" },
1819
]
1920

2021
[requirements]
@@ -28,3 +29,4 @@ gleeunit = { version = "~> 1.0" }
2829
hug = { version = "~> 1.0" }
2930
pprint = { version = "~> 1.0" }
3031
simplifile = { version = ">= 1.7.0 and < 2.0.0" }
32+
tom = { version = ">= 0.3.0 and < 1.0.0"}

src/spark.gleam

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import filepath
22
import gleam/io
33
import gleam/result.{try}
44
import gleam/string
5+
import gleam_community/ansi
56
import spark/file
67
import spark/project
7-
import gleam_community/ansi
88

99
pub fn main() {
1010
let result = {
11-
use project <- try(project.from("TestProject", "examples/TestProject"))
11+
use project <- try(project.from("examples/TestProject"))
1212
build(project)
1313
}
1414

@@ -24,15 +24,16 @@ fn build(project: project.Project) -> Result(Nil, String) {
2424
let build_dir = filepath.join(project.dir, "build")
2525

2626
report("Compiling", "Spark")
27-
use template <- try(project.from("Spark", "./templates"))
27+
use template <- try(project.from("./templates"))
2828
use _ <- try(project.compile(template, to: build_dir))
2929

30-
report("Compiling", project.name)
30+
report("Compiling", project.config.name)
3131
use _ <- try(project.compile(project, to: build_dir))
3232

3333
report("Creating", "index file")
3434
let path = filepath.join(project.dir, "build/index.mjs")
35-
let contents = "import { main } from './" <> project.name <> ".mjs';\nmain();"
35+
let contents =
36+
"import { main } from './" <> project.config.name <> ".mjs';\nmain();"
3637
use _ <- try(file.write_all(path, contents))
3738

3839
report("Compiled", "successfully")

src/spark/error.gleam

+4
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,7 @@ fn parse_error_to_string(error, pos: Span, ctx_stack) {
8787

8888
#("syntax error" <> ctx, hint, pos)
8989
}
90+
91+
pub fn simple_error(message: String) -> String {
92+
ansi.red("error") <> ansi.dim(": ") <> ansi.bold(message)
93+
}

src/spark/file.gleam

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import filepath
22
import gleam/result.{try}
3-
import gleam_community/ansi
43
import simplifile
4+
import spark/error
55

66
/// Write a file, creating directories as needed.
77
///
@@ -37,5 +37,5 @@ pub fn get_files(dir: String) -> Result(List(String), String) {
3737
}
3838

3939
fn file_error(message, path) -> String {
40-
ansi.red("I was unable to " <> message <> ": ") <> path
40+
error.simple_error("I was unable to " <> message <> ": " <> path)
4141
}

src/spark/project.gleam

+36-3
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,37 @@ import spark/lex
99
import spark/module.{type Module}
1010
import spark/parse
1111
import spark/util
12+
import tom
1213

1314
// ---- TYPES ------------------------------------------------------------------
1415

1516
pub type Project {
16-
Project(name: String, modules: List(Module), extra_files: List(String), dir: String)
17+
Project(
18+
config: Config,
19+
modules: List(Module),
20+
extra_files: List(String),
21+
dir: String,
22+
)
23+
}
24+
25+
pub type Config {
26+
Config(name: String)
1727
}
1828

1929
// ---- CONSTRUCTOR ------------------------------------------------------------
2030

21-
pub fn from(name: String, dir: String) -> Result(Project, String) {
31+
pub fn from(dir: String) -> Result(Project, String) {
32+
use config_file <- try(
33+
file.read(filepath.join(dir, "spark.toml"))
34+
|> result.replace_error(error.simple_error(
35+
"`spark.toml` config file is missing",
36+
)),
37+
)
38+
use config <- try(parse_config(config_file))
2239
use #(modules, extra_files) <- try(
2340
scan_project_files(filepath.join(dir, "src")),
2441
)
25-
Ok(Project(name, modules, extra_files, dir))
42+
Ok(Project(config, modules, extra_files, dir))
2643
}
2744

2845
fn scan_project_files(
@@ -40,6 +57,22 @@ fn scan_project_files(
4057
|> Ok
4158
}
4259

60+
fn parse_config(config_file: String) -> Result(Config, String) {
61+
use parsed <- try(
62+
tom.parse(config_file)
63+
|> result.replace_error(error.simple_error(
64+
"I was unable to parse the config file as valid TOML",
65+
)),
66+
)
67+
use name <- try(
68+
tom.get_string(parsed, ["name"])
69+
|> result.replace_error(error.simple_error(
70+
"Config is missing the `name` field or it is not a string",
71+
)),
72+
)
73+
Ok(Config(name))
74+
}
75+
4376
// ---- FUNCTIONS --------------------------------------------------------------
4477

4578
/// Compile a project to the given build directory.

templates/spark.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
name = "Spark"

0 commit comments

Comments
 (0)