Skip to content

Commit b6d6e1c

Browse files
committed
v1.3.0
1 parent 013f51e commit b6d6e1c

File tree

7 files changed

+55
-46
lines changed

7 files changed

+55
-46
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
build
44
erl_crash.dump
55
/gleescript
6+
/tmp

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v1.3.0 - 2024-06-24
4+
5+
- The `--out` flag can be used to write the escript to a specified directory.
6+
37
## v1.2.0 - 2024-05-24
48

59
- Generated escript files are now executable by default.

README.md

+16-5
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ gleam run -m gleescript
2424
# Hello from your_project!
2525
```
2626

27-
### Configuration
28-
29-
You can optionally configure the target directory of the executable by adding `--target-dir my_directory`, where `my_directory` is where you want to store the executable.
30-
3127
The escript can run on any computer that has the Erlang VM installed. Older
3228
versions of the virtual machine may not support the newer bytecode contained in
3329
the escript. Typically being within a couple major versions of the version used
@@ -37,4 +33,19 @@ See the [Erlang escript documentation][1] for more information.
3733

3834
[1]: https://www.erlang.org/doc/man/escript.html
3935

40-
Havefun!
36+
37+
By default the executable is created in the current directory. The `--out` flag
38+
can be used to write to a different directory.
39+
40+
```sh
41+
gleam run -m gleescript -- --out=./some/directory
42+
# Compiling your_project
43+
# Compiled in 0.26s
44+
# Running gleescript.main
45+
# Generated ./some/directory/your_project
46+
47+
./some/directory/your_project
48+
# Hello from your_project!
49+
```
50+
51+
Have fun!

gleam.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "gleescript"
2-
version = "1.2.0"
2+
version = "1.3.0"
33
description = "Bundle your Gleam-on-Erlang project into an escript, a single executable file!"
44
licences = ["Apache-2.0"]
55
repository = { type = "github", user = "lpil", repo = "gleescript" }

src/gleescript.gleam

+19-19
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,7 @@ pub fn main() {
2626
}
2727

2828
type Config {
29-
Config(package_name: String, target_dir: String)
30-
}
31-
32-
pub fn get_target_dir(args: List(String)) -> String {
33-
let target = case args {
34-
["--target-dir", folder] -> folder
35-
_ -> "./"
36-
}
37-
case string.last(target) {
38-
Ok("/") -> target
39-
_ -> target <> "/"
40-
}
29+
Config(package_name: String, out_dir: String)
4130
}
4231

4332
pub fn run() -> snag.Result(Nil) {
@@ -69,14 +58,14 @@ pub fn run() -> snag.Result(Nil) {
6958
)
7059

7160
use _ <- result.try(
72-
simplifile.create_directory_all(config.target_dir)
61+
simplifile.create_directory_all(config.out_dir)
7362
|> snag_inspect_error
74-
|> snag.context("Failed to create " <> config.target_dir <> " directory"),
63+
|> snag.context("Failed to create " <> config.out_dir <> " directory"),
7564
)
7665

7766
let result =
7867
erlang_escript_create(
79-
charlist.from_string(config.target_dir <> config.package_name),
68+
charlist.from_string(config.out_dir <> config.package_name),
8069
[
8170
Shebang,
8271
Comment(charlist.from_string("")),
@@ -92,7 +81,7 @@ pub fn run() -> snag.Result(Nil) {
9281
|> snag.context("Failed to build escript"),
9382
)
9483

95-
let name = config.target_dir <> config.package_name
84+
let name = config.out_dir <> config.package_name
9685
use _ <- result.try(
9786
simplifile.set_permissions_octal(name, 0o777)
9887
|> snag_inspect_error
@@ -101,7 +90,7 @@ pub fn run() -> snag.Result(Nil) {
10190

10291
io.println(
10392
" \u{001b}[95mGenerated\u{001b}[0m "
104-
<> config.target_dir
93+
<> config.out_dir
10594
<> config.package_name,
10695
)
10796

@@ -125,7 +114,7 @@ fn locate_beam_files() -> snag.Result(List(String)) {
125114
}
126115

127116
fn load_config() -> snag.Result(Config) {
128-
let target_dir = get_target_dir(argv.load().arguments)
117+
let out_dir = get_out_dir(argv.load().arguments)
129118

130119
use text <- result.try(
131120
simplifile.read("gleam.toml")
@@ -145,7 +134,7 @@ fn load_config() -> snag.Result(Config) {
145134
|> snag.context("Failed to get package name from gleam.toml"),
146135
)
147136

148-
Ok(Config(package_name: package_name, target_dir: target_dir))
137+
Ok(Config(package_name: package_name, out_dir: out_dir))
149138
}
150139

151140
fn snag_inspect_error(result: Result(t, e)) -> snag.Result(t) {
@@ -172,3 +161,14 @@ fn erlang_escript_create(
172161

173162
@external(erlang, "init", "stop")
174163
fn shutdown(status: Int) -> a
164+
165+
fn get_out_dir(args: List(String)) -> String {
166+
let out = case args {
167+
["--out", folder] | ["--out=" <> folder] -> folder
168+
_ -> "./"
169+
}
170+
case string.last(out) {
171+
Ok("/") -> out
172+
_ -> out <> "/"
173+
}
174+
}

test.sh

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
#!/bin/sh
22

3-
DIRECTORY=test-dir
3+
set -eu
44

5-
rm -rf $DIRECTORY
5+
# Reset out artefacts
6+
rm -rf gleescript ./tmp
67

7-
gleam run -- --target-dir $DIRECTORY
8+
# No arguments
9+
gleam run -m gleescript
10+
./gleescript
811

9-
if [ ! -d $DIRECTORY ]; then
10-
echo "Directory $DIRECTORY was not created"
11-
exit 1
12-
fi
12+
# --out tmp/without-equals
13+
gleam run -m gleescript -- --out tmp/without-equals
14+
./tmp/without-equals/gleescript
1315

14-
echo "Test passed!"
16+
# --out=tmp/with-equals
17+
gleam run -m gleescript -- --out=tmp/with-equals
18+
./tmp/with-equals/gleescript
19+
20+
echo "Tests passed!"

test/gleescript_test.gleam

-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
import gleescript
21
import gleeunit
3-
import gleeunit/should
42

53
pub fn main() {
64
gleeunit.main()
75
}
8-
9-
// gleeunit test functions end in `_test`
10-
pub fn arguments_parsed_correctly_test() {
11-
["--target-dir", "test"]
12-
|> gleescript.get_target_dir
13-
|> should.equal("test/")
14-
15-
[]
16-
|> gleescript.get_target_dir
17-
|> should.equal("./")
18-
}

0 commit comments

Comments
 (0)