Skip to content

Commit

Permalink
upgrade to PNC, update flake back to roc
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewilliamboswell committed Jan 9, 2025
1 parent 89dc0fe commit b1c5ed4
Show file tree
Hide file tree
Showing 56 changed files with 1,089 additions and 1,004 deletions.
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.with_default "roc"
roc_cmd = Env.var!("ROC") |> Result.with_default("roc")

debug_mode =
when Env.var! "DEBUG" is
Ok str if !(Str.is_empty 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.map_err 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.map_err 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.is_empty 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.to_str 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.map_err 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.map_err 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.map_err 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)")
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.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`")
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.to_str exit_code)"
Err err -> Stdout.line! "Error executing command: $(Inspect.to_str 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.from_utf8 output.stdout |> Result.with_default "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.to_str |> \s -> "$(s)...")
_ = Stdin.line! {}
tick! (n - 1)
Stdout.line!((n |> Num.to_str |> \s -> "$(s)..."))?
_ = Stdin.line!({})
tick!((n - 1))
26 changes: 14 additions & 12 deletions examples/dir.roc
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,34 @@ import pf.Path
main! = \_args ->

# Create a directory
try Dir.create! "dirExampleE"
Dir.create!("dirExampleE")?

# Create a directory and its parents
try Dir.create_all! "dirExampleA/b/c/child"
Dir.create_all!("dirExampleA/b/c/child")?

# Create a child directory
try Dir.create! "dirExampleA/child"
Dir.create!("dirExampleA/child")?

# List the contents of a directory
paths_as_str =
Dir.list! "dirExampleA"
|> Result.map \paths -> List.map paths Path.display
Dir.list!("dirExampleA")
|> Result.map(\paths -> List.map(paths, Path.display))
|> try

# Check the contents of the directory
expect (Set.from_list paths_as_str) == (Set.from_list ["dirExampleA/b", "dirExampleA/child"])
expect (Set.from_list(paths_as_str)) == (Set.from_list(["dirExampleA/b", "dirExampleA/child"]))

# Try to create a directory without a parent (should fail, ignore error)
when Dir.create! "dirExampleD/child" is
Ok {} -> {}
Err _ -> {}
when Dir.create!("dirExampleD/child") is
Ok({}) -> {}
Err(_) -> {}

# Delete an empty directory
try Dir.delete_empty! "dirExampleE"
Dir.delete_empty!("dirExampleE")?

# Delete all directories recursively
try Dir.delete_all! "dirExampleA"
Dir.delete_all!("dirExampleA")?

Stdout.line! "Success!"
Stdout.line!("Success!")?

Ok({})
Loading

0 comments on commit b1c5ed4

Please sign in to comment.