Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to snake_case builtins #305

Merged
merged 13 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 55 additions & 55 deletions build.roc
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,47 @@ import cli.Env
## Check basic-cli-build-steps.png for a diagram that shows what the code does.
##
main! : _ => Result {} _
main! = \_ ->
main! = \_args ->

roc_cmd = Env.var! "ROC" |> Result.withDefault "roc"
roc_cmd = Env.var!("ROC") |> Result.with_default("roc")

debug_mode =
when Env.var! "DEBUG" is
Ok str if !(Str.isEmpty str) -> Debug
when Env.var!("DEBUG") is
Ok(str) if !(Str.is_empty(str)) -> Debug
_ -> Release

try roc_version! roc_cmd
roc_version!(roc_cmd)?

os_and_arch = try get_os_and_arch! {}
os_and_arch = get_os_and_arch!({})?

stub_lib_path = "platform/libapp.$(stub_file_extension os_and_arch)"
stub_lib_path = "platform/libapp.$(stub_file_extension(os_and_arch))"

try build_stub_app_lib! roc_cmd stub_lib_path
build_stub_app_lib!(roc_cmd, stub_lib_path)?

try cargo_build_host! debug_mode
cargo_build_host!(debug_mode)?

rust_target_folder = try get_rust_target_folder! debug_mode
rust_target_folder = get_rust_target_folder!(debug_mode)?

try copy_host_lib! os_and_arch rust_target_folder
copy_host_lib!(os_and_arch, rust_target_folder)?

try preprocess_host! roc_cmd stub_lib_path rust_target_folder
preprocess_host!(roc_cmd, stub_lib_path, rust_target_folder)?

try info! "Successfully built platform files!"
info!("Successfully built platform files!")?

Ok {}
Ok({})

roc_version! : Str => Result {} _
roc_version! = \roc_cmd ->
try info! "Checking provided roc; executing `$(roc_cmd) version`:"
info!( "Checking provided roc; executing `$(roc_cmd) version`:")?

roc_cmd
|> Cmd.exec! ["version"]
|> Result.mapErr RocVersionCheckFailed
Cmd.exec!(roc_cmd, ["version"])
|> Result.map_err(RocVersionCheckFailed)

get_os_and_arch! : {} => Result OSAndArch _
get_os_and_arch! = \{} ->
try info! "Getting the native operating system and architecture ..."
info!( "Getting the native operating system and architecture ...")?

{ os, arch } = Env.platform! {}

convert_os_and_arch!! { os, arch }
convert_os_and_arch!(Env.platform!({}))

OSAndArch : [
MacosArm64,
Expand All @@ -70,19 +67,18 @@ OSAndArch : [
convert_os_and_arch! : _ => Result OSAndArch _
convert_os_and_arch! = \{ os, arch } ->
when (os, arch) is
(MACOS, AARCH64) -> Ok MacosArm64
(MACOS, X64) -> Ok MacosX64
(LINUX, AARCH64) -> Ok LinuxArm64
(LINUX, X64) -> Ok LinuxX64
_ -> Err (UnsupportedNative os arch)
(MACOS, AARCH64) -> Ok(MacosArm64)
(MACOS, X64) -> Ok(MacosX64)
(LINUX, AARCH64) -> Ok(LinuxArm64)
(LINUX, X64) -> Ok(LinuxX64)
_ -> Err(UnsupportedNative(os, arch))

build_stub_app_lib! : Str, Str => Result {} _
build_stub_app_lib! = \roc_cmd, stub_lib_path ->
try info! "Building stubbed app shared library ..."
info!( "Building stubbed app shared library ...")?

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

stub_file_extension : OSAndArch -> Str
stub_file_extension = \os_and_arch ->
Expand All @@ -106,53 +102,57 @@ get_rust_target_folder! = \debug_mode ->

debug_or_release = if debug_mode == Debug then "debug" else "release"

when Env.var! "CARGO_BUILD_TARGET" is
Ok target_env_var ->
if Str.isEmpty target_env_var then
Ok "target/$(debug_or_release)/"
when Env.var!("CARGO_BUILD_TARGET") is
Ok(target_env_var) ->
if Str.is_empty(target_env_var) then
Ok("target/$(debug_or_release)/")
else
Ok "target/$(target_env_var)/$(debug_or_release)/"
Ok("target/$(target_env_var)/$(debug_or_release)/")

Err e ->
try info! "Failed to get env var CARGO_BUILD_TARGET with error $(Inspect.toStr e). Assuming default CARGO_BUILD_TARGET (native)..."
Err(e) ->
info!( "Failed to get env var CARGO_BUILD_TARGET with error $(Inspect.to_str(e)). Assuming default CARGO_BUILD_TARGET (native)...")?

Ok "target/$(debug_or_release)/"
Ok("target/$(debug_or_release)/")

cargo_build_host! : [Debug, Release] => Result {} _
cargo_build_host! = \debug_mode ->
cargo_build_args =

cargo_build_args! = \{} ->
when debug_mode is
Debug -> Result.map (info! "Building rust host in debug mode...") \_ -> ["build"]
Release -> Result.map (info! "Building rust host ...") \_ -> ["build", "--release"]
Debug ->
info!("Building rust host in debug mode...")?
Ok(["build"])
Release ->
info!("Building rust host ...")?
Ok(["build", "--release"])

args = cargo_build_args!({})?

"cargo"
|> Cmd.exec! (try cargo_build_args)
|> Result.mapErr ErrBuildingHostBinaries
Cmd.exec!("cargo", args)
|> Result.map_err(ErrBuildingHostBinaries)

copy_host_lib! : OSAndArch, Str => Result {} _
copy_host_lib! = \os_and_arch, rust_target_folder ->

host_build_path = "$(rust_target_folder)libhost.a"

host_dest_path = "platform/$(prebuilt_static_lib_file os_and_arch)"
host_dest_path = "platform/$(prebuilt_static_lib_file(os_and_arch))"

try info! "Moving the prebuilt binary from $(host_build_path) to $(host_dest_path) ..."
info!( "Moving the prebuilt binary from $(host_build_path) to $(host_dest_path) ...")?

"cp"
|> Cmd.exec! [host_build_path, host_dest_path]
|> Result.mapErr ErrMovingPrebuiltLegacyBinary
Cmd.exec!("cp", [host_build_path, host_dest_path])
|> Result.map_err(ErrMovingPrebuiltLegacyBinary)

preprocess_host! : Str, Str, Str => Result {} _
preprocess_host! = \roc_cmd, stub_lib_path, rust_target_folder ->

try info! "Preprocessing surgical host ..."
info!( "Preprocessing surgical host ...")?

surgical_build_path = "$(rust_target_folder)host"

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

info! : Str => Result {} _
info! = \msg ->
Stdout.line! "\u(001b)[34mINFO:\u(001b)[0m $(msg)"
Stdout.line!("\u(001b)[34mINFO:\u(001b)[0m $(msg)")
27 changes: 0 additions & 27 deletions ci/expect_scripts/http-get-json.exp
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to roc-json in lukewilliamboswell/roc-json#42

This file was deleted.

2 changes: 1 addition & 1 deletion ci/rust_http_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use hyper::service::{make_service_fn, service_fn};
use std::convert::Infallible;

async fn handle_request(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
// Encode.toBytes {foo: "Hello Json!"} Json.utf8
// Encode.to_bytes {foo: "Hello Json!"} Json.utf8
lukewilliamboswell marked this conversation as resolved.
Show resolved Hide resolved
let json_bytes: Vec<u8> = vec![123, 34, 102, 111, 111, 34, 58, 34, 72, 101, 108, 108, 111, 32, 74, 115, 111, 110, 33, 34, 125];

let response = Response::builder()
Expand Down
12 changes: 6 additions & 6 deletions examples/args.roc
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import pf.Arg exposing [Arg]
main! : List Arg => Result {} _
main! = \raw_args ->

args = List.map raw_args Arg.display
args = List.map(raw_args, Arg.display)

# get the second argument, the first is the executable's path
when List.get args 1 |> Result.mapErr (\_ -> ZeroArgsGiven) is
Err ZeroArgsGiven ->
Err (Exit 1 "Error ZeroArgsGiven:\n\tI expected one argument, but I got none.\n\tRun the app like this: `roc main.roc -- input.txt`")
when List.get(args, 1) |> Result.map_err(\_ -> ZeroArgsGiven) is
Err(ZeroArgsGiven) ->
Err(Exit(1, "Error ZeroArgsGiven:\n\tI expected one argument, but I got none.\n\tRun the app like this: `roc main.roc -- input.txt`"))

Ok first_arg ->
Stdout.line! "received argument: $(first_arg)"
Ok(first_arg) ->
Stdout.line!("received argument: $(first_arg)")
32 changes: 16 additions & 16 deletions examples/command.roc
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,45 @@ import pf.Stdout
import pf.Cmd

main! = \_args ->
try status_example! {}
status_example!({})?

try output_example! {}
output_example!({})?

try exec_example! {}
exec_example!({})?

Ok {}
Ok({})

exec_example! : {} => Result {} _
exec_example! = \{} -> Cmd.exec! "echo" ["EXEC"]
exec_example! = \{} -> Cmd.exec!("echo", ["EXEC"])

# Run "env" with verbose option, clear all environment variables, and pass in
# "FOO" and "BAZ".
status_example! : {} => Result {} _
status_example! = \{} ->
result =
Cmd.new "env"
|> Cmd.arg "-v"
Cmd.new("env")
|> Cmd.arg("-v")
|> Cmd.clear_envs
|> Cmd.envs [("FOO", "BAR"), ("BAZ", "DUCK")]
|> Cmd.envs([("FOO", "BAR"), ("BAZ", "DUCK")])
|> Cmd.status!

when result is
Ok exit_code if exit_code == 0 -> Ok {}
Ok exit_code -> Stdout.line! "Child exited with non-zero code: $(Num.toStr exit_code)"
Err err -> Stdout.line! "Error executing command: $(Inspect.toStr err)"
Ok(exit_code) if exit_code == 0 -> Ok({})
Ok(exit_code) -> Stdout.line!("Child exited with non-zero code: $(Num.to_str(exit_code))")
Err(err) -> Stdout.line!("Error executing command: $(Inspect.to_str(err))")

# Run "env" with verbose option, clear all environment variables, and pass in
# only as an environment variable "FOO"
output_example! : {} => Result {} _
output_example! = \{} ->

output =
Cmd.new "env"
Cmd.new("env")
|> Cmd.clear_envs
|> Cmd.env "FOO" "BAR"
|> Cmd.args ["-v"]
|> Cmd.env("FOO", "BAR")
|> Cmd.args(["-v"])
|> Cmd.output!

msg = Str.fromUtf8 output.stdout |> Result.withDefault "Failed to decode stdout"
msg = Str.from_utf8(output.stdout) |> Result.with_default("Failed to decode stdout")

Stdout.write! msg
Stdout.write!(msg)
16 changes: 8 additions & 8 deletions examples/countdown.roc
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import pf.Stdin
import pf.Stdout

main! = \_args ->
try Stdout.line! "\nLet's count down from 3 together - all you have to do is press <ENTER>."
_ = Stdin.line! {}
tick! 3
Stdout.line!("\nLet's count down from 3 together - all you have to do is press <ENTER>.")?
_ = Stdin.line!({})
tick!(3)

tick! = \n ->
if n == 0 then
try Stdout.line! "🎉 SURPRISE! Happy Birthday! 🎂"
Ok {}
Stdout.line!("🎉 SURPRISE! Happy Birthday! 🎂")?
Ok({})
else
try Stdout.line! (n |> Num.toStr |> \s -> "$(s)...")
_ = Stdin.line! {}
tick! (n - 1)
Stdout.line!((n |> Num.to_str |> \s -> "$(s)..."))?
_ = Stdin.line!({})
tick!((n - 1))
Loading
Loading