Skip to content

Commit 1159296

Browse files
committed
overhaul progress
1 parent 39dbd5e commit 1159296

5 files changed

Lines changed: 64 additions & 92 deletions

File tree

build.roc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ roc_version! = |roc_cmd|
4747
info!("Checking provided roc; executing `${roc_cmd} version`:")?
4848

4949
Cmd.exec!(roc_cmd, ["version"])
50-
|> Result.map_err(RocVersionCheckFailed)
5150

5251
get_os_and_arch! : {} => Result OSAndArch _
5352
get_os_and_arch! = |{}|
@@ -78,7 +77,6 @@ build_stub_app_lib! = |roc_cmd, stub_lib_path|
7877
info!("Building stubbed app shared library ...")?
7978

8079
Cmd.exec!(roc_cmd, ["build", "--lib", "platform/libapp.roc", "--output", stub_lib_path, "--optimize"])
81-
|> Result.map_err(ErrBuildingAppStub)
8280

8381
stub_file_extension : OSAndArch -> Str
8482
stub_file_extension = |os_and_arch|
@@ -130,7 +128,6 @@ cargo_build_host! = |debug_mode|
130128
args = cargo_build_args!({})?
131129

132130
Cmd.exec!("cargo", args)
133-
|> Result.map_err(ErrBuildingHostBinaries)
134131

135132
copy_host_lib! : OSAndArch, Str => Result {} _
136133
copy_host_lib! = |os_and_arch, rust_target_folder|
@@ -142,7 +139,6 @@ copy_host_lib! = |os_and_arch, rust_target_folder|
142139
info!("Moving the prebuilt binary from ${host_build_path} to ${host_dest_path} ...")?
143140

144141
Cmd.exec!("cp", [host_build_path, host_dest_path])
145-
|> Result.map_err(ErrMovingPrebuiltLegacyBinary)
146142

147143
preprocess_host! : Str, Str, Str => Result {} _
148144
preprocess_host! = |roc_cmd, stub_lib_path, rust_target_folder|
@@ -152,7 +148,6 @@ preprocess_host! = |roc_cmd, stub_lib_path, rust_target_folder|
152148
surgical_build_path = "${rust_target_folder}host"
153149

154150
Cmd.exec!(roc_cmd, ["preprocess-host", surgical_build_path, "platform/main.roc", stub_lib_path])
155-
|> Result.map_err(ErrPreprocessingSurgicalBinary)
156151

157152
info! : Str => Result {} _
158153
info! = |msg|

examples/command.roc

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
app [main!] { pf: platform "../platform/main.roc" }
22

33
import pf.Stdout
4-
import pf.Stderr
54
import pf.Cmd
65
import pf.Arg exposing [Arg]
76

@@ -13,84 +12,43 @@ main! : List Arg => Result {} _
1312
main! = |_args|
1413

1514
# Simplest way to execute a command (prints to your terminal).
16-
Cmd.exec!("echo", ["Hello"]) ? |err| EchoHelloFailed(err)
15+
Cmd.exec!("echo", ["Hello"])?
1716

1817
# To execute and capture the output (stdout, stderr, and exit code) without inheriting your terminal.
19-
output_example!({}) ? |err| OutputExampleFailed(err)
20-
21-
# To run a command with an environment variable.
22-
env_example!({}) ? |err| EnvExampleFailed(err)
23-
24-
# To execute and just get the exit code (prints to your terminal).
25-
status_example!({}) ? |err| StatusExampleFailed(err)
26-
27-
Ok({})
28-
29-
# Execute command and capture the output (stdout, stderr, and exit code)
30-
output_example! : {} => Result {} _
31-
output_example! = |{}|
32-
3318
cmd_output =
3419
Cmd.new("echo")
3520
|> Cmd.args(["Hi"])
36-
|> Cmd.output!
37-
38-
print_output!(cmd_output)
39-
40-
41-
print_output! : Cmd.Output => Result {} _
42-
print_output! = |cmd_output|
21+
|> Cmd.exec_output!()?
4322

23+
Stdout.line!(
24+
"""
25+
stdout:${cmd_output.stdout_utf8}
26+
stderr:${cmd_output.stderr_utf8_lossy}
27+
"""
28+
)?
4429

45-
when cmd_output.status is
46-
Ok(0) ->
47-
stdout_utf8 = Str.from_utf8(cmd_output.stdout)?
48-
Stdout.line!("Command output: ${stdout_utf8}")
49-
50-
Ok(exit_code) ->
51-
stdout_utf8 = Str.from_utf8_lossy(cmd_output.stdout)
52-
stderr_utf8 = Str.from_utf8_lossy(cmd_output.stderr)
53-
err_data =
54-
"""
55-
Command failed:
56-
- exit code: ${Num.to_str(exit_code)}
57-
- stdout: ${stdout_utf8}
58-
- stderr: ${stderr_utf8}
59-
"""
60-
61-
Stderr.line!(err_data)
62-
63-
Err(err) ->
64-
Stderr.line!("Failed to get exit code for command, error: ${Inspect.to_str(err)}")
65-
66-
67-
# Run command with an environment variable
68-
env_example! : {} => Result {} _
69-
env_example! = |{}|
70-
71-
cmd_output =
30+
# To run a command with environment variables.
31+
cmd_output_env =
7232
Cmd.new("env")
7333
|> Cmd.clear_envs # You probably don't need to clear all other environment variables, this is just an example.
7434
|> Cmd.env("FOO", "BAR")
7535
|> Cmd.envs([("BAZ", "DUCK"), ("XYZ", "ABC")]) # Set multiple environment variables at once with `envs`
7636
|> Cmd.args(["-v"])
77-
|> Cmd.output!
37+
|> Cmd.exec_output!()?
7838

79-
print_output!(cmd_output)
39+
Stdout.line!(
40+
"""
41+
stdout:${cmd_output_env.stdout_utf8}
42+
stderr:${cmd_output_env.stderr_utf8_lossy}
43+
"""
44+
)?
8045

81-
# Execute command and capture the exit code
82-
status_example! : {} => Result {} _
83-
status_example! = |{}|
84-
cmd_result =
46+
# To execute and just get the exit code (prints to your terminal).
47+
exit_code =
8548
Cmd.new("echo")
8649
|> Cmd.args(["Yo"])
87-
|> Cmd.status!
50+
|> Cmd.exec_exit_code!()?
8851

89-
when cmd_result is
90-
Ok(0) -> Ok({})
52+
Stdout.line!("Exit code: ${Num.to_str(exit_code)}")?
9153

92-
Ok(exit_code) ->
93-
Stderr.line!("Command failed with exit code: ${Num.to_str(exit_code)}")
94-
95-
Err(err) ->
96-
Stderr.line!("Failed to get exit code for command, error: ${Inspect.to_str(err)}")
54+
Ok({})

flake.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

platform/Cmd.roc

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ module [
99
exec_output!,
1010
exec!,
1111
exec_cmd!,
12+
exec_exit_code!,
1213
]
1314

14-
import InternalCmd
15+
import InternalCmd exposing [to_str]
1516
import InternalIOErr exposing [IOErr]
1617
import Host
1718

@@ -22,9 +23,9 @@ Cmd := InternalCmd.Command
2223
## If you want to capture the output, use [exec_output!].
2324
## ```
2425
## # Call echo to print "hello world"
25-
## Cmd.exec!("echo", ["hello world"]) ? |err| CmdEchoFailed(err)
26+
## Cmd.exec!("echo", ["hello world"])?
2627
## ```
27-
exec! : Str, List Str => Result {} [ExecFailed Str (List Str) I32, FailedToGetExitCode IOErr]
28+
exec! : Str, List Str => Result {} [ExecFailed Str I32, FailedToGetExitCode Str IOErr]
2829
exec! = |cmd_name, arguments|
2930
exit_code =
3031
new(cmd_name)
@@ -34,7 +35,8 @@ exec! = |cmd_name, arguments|
3435
if exit_code == 0i32 then
3536
Ok({})
3637
else
37-
Err(ExecFailed(cmd_name, arguments, exit_code))
38+
cmd_str = "${cmd_name} ${Str.join_with(arguments, " ")}"
39+
Err(ExecFailed(cmd_str, exit_code))
3840

3941
## Execute a Cmd while inheriting stdin, stdout and stderr from parent.
4042
## You should prefer using `exec!`, only use this if you want to use [env], [envs] or [clear_envs].
@@ -46,15 +48,15 @@ exec! = |cmd_name, arguments|
4648
## |> Cmd.env("RUST_BACKTRACE", "1")
4749
## |> Cmd.exec_cmd!()?
4850
## ```
49-
exec_cmd! : Cmd => Result {} [ExecCmdFailed Cmd I32, FailedToGetExitCode IOErr]
50-
exec_cmd! = |cmd|
51+
exec_cmd! : Cmd => Result {} [ExecCmdFailed Str I32, FailedToGetExitCode Str IOErr]
52+
exec_cmd! = |@Cmd(cmd)|
5153
exit_code =
52-
exec_exit_code!(cmd)?
54+
exec_exit_code!(@Cmd(cmd))?
5355

5456
if exit_code == 0i32 then
5557
Ok({})
5658
else
57-
Err(ExecCmdFailed(cmd, exit_code))
59+
Err(ExecCmdFailed(to_str(cmd), exit_code))
5860

5961
## Execute command and capture stdout, stderr and the exit code in [Output].
6062
##
@@ -66,16 +68,16 @@ exec_output! : Cmd =>
6668
Result
6769
{stdout_utf8 : Str, stderr_utf8_lossy : Str}
6870
[
69-
StdoutContainsInvalidUtf8([BadUtf8 { index : U64, problem : Str.Utf8Problem }]),
70-
NonZeroExitCode({exit_code: I32, stdout_utf8_lossy: Str, stderr_utf8_lossy: Str}),
71-
FailedToGetExitCode(IOErr)
71+
StdoutContainsInvalidUtf8({ cmd : Str, err: [BadUtf8 { index : U64, problem : Str.Utf8Problem }]}),
72+
NonZeroExitCode({cmd : Str, exit_code: I32, stdout_utf8_lossy: Str, stderr_utf8_lossy: Str}),
73+
FailedToGetExitCode({ cmd : Str, err : IOErr})
7274
]
7375
exec_output! = |@Cmd(cmd)|
7476
exec_res = Host.command_exec_output!(cmd)
7577

7678
when exec_res is
7779
Ok({stdout_bytes, stderr_bytes}) ->
78-
stdout_utf8 = Str.from_utf8(stdout_bytes) ? |err| StdoutContainsInvalidUtf8(err)
80+
stdout_utf8 = Str.from_utf8(stdout_bytes) ? |err| StdoutContainsInvalidUtf8({ cmd: to_str(cmd), err})
7981
stderr_utf8_lossy = Str.from_utf8_lossy(stderr_bytes)
8082

8183
Ok({stdout_utf8, stderr_utf8_lossy})
@@ -85,20 +87,20 @@ exec_output! = |@Cmd(cmd)|
8587
stdout_utf8_lossy = Str.from_utf8_lossy(stdout_bytes)
8688
stderr_utf8_lossy = Str.from_utf8_lossy(stderr_bytes)
8789

88-
Err(NonZeroExitCode({exit_code, stdout_utf8_lossy, stderr_utf8_lossy}))
90+
Err(NonZeroExitCode({cmd : to_str(cmd), exit_code, stdout_utf8_lossy, stderr_utf8_lossy}))
8991
Err(err) ->
9092
Err(InternalIOErr.handle_err(err))
91-
|> Result.map_err(FailedToGetExitCode)
93+
|> Result.map_err(|er| FailedToGetExitCode({ cmd : to_str(cmd), err : er }))
9294

9395
# TODO exec_output_bytes
9496

9597
## Execute command and inherit stdin, stdout and stderr from parent. Returns the exit code.
9698
## Helper function.
97-
exec_exit_code! : Cmd => Result I32 [FailedToGetExitCode IOErr]
99+
exec_exit_code! : Cmd => Result I32 [FailedToGetExitCode Str IOErr]
98100
exec_exit_code! = |@Cmd(cmd)|
99101
Host.command_exec_exit_code!(cmd)
100102
|> Result.map_err(InternalIOErr.handle_err)
101-
|> Result.map_err(FailedToGetExitCode)
103+
|> Result.map_err(|err| FailedToGetExitCode(to_str(cmd), err))
102104

103105
# This hits a compiler bug: Alias `6.IdentId(11)` not registered in delayed aliases! ...
104106
# ## Converts output into a utf8 string. Invalid utf8 sequences in stderr are ignored.

platform/InternalCmd.roc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module [
22
Command,
33
OutputFromHostSuccess,
44
OutputFromHostFailure,
5+
to_str,
56
]
67

78
Command : {
@@ -20,4 +21,20 @@ OutputFromHostFailure : {
2021
exit_code : I32,
2122
stdout_bytes : List U8,
2223
stderr_bytes : List U8,
23-
}
24+
}
25+
26+
to_str : Command -> Str
27+
to_str = |cmd|
28+
envs_str =
29+
cmd.envs
30+
#|> List.map(|(key, value)| "${key}=${value}")
31+
|> Str.join_with(" ")
32+
33+
clear_envs_str = if cmd.clear_envs then "true" else "false"
34+
35+
"""
36+
cmd: ${cmd.program}
37+
args: ${Str.join_with(cmd.args, " ")}
38+
envs: ${envs_str}
39+
clear_envs: ${clear_envs_str}
40+
"""

0 commit comments

Comments
 (0)