From bb1cd190006eb2f62868fbda12ad9bbbdfaded32 Mon Sep 17 00:00:00 2001 From: jutty Date: Sat, 19 Apr 2025 19:09:16 -0300 Subject: [PATCH] OCaml: Apply formatting --- ocaml/.justfile | 65 ++++++++++++++++++++++++++--- ocaml/.ocamlformat | 2 + ocaml/.ocamlformat-ignore | 1 + ocaml/bin/main.ml | 2 +- ocaml/lib/parsers/argument.ml | 26 +++++++----- ocaml/lib/schema/schema.ml | 16 +++---- ocaml/lib/system/file.ml | 3 +- ocaml/lib/system/package.ml | 40 ++++++++++-------- ocaml/lib/system/process/command.ml | 15 ++++--- ocaml/lib/system/process/fork.ml | 11 +++-- ocaml/lib/system/process/reader.ml | 37 ++++++++-------- ocaml/test/test_tori.ml | 1 - 12 files changed, 145 insertions(+), 74 deletions(-) create mode 100644 ocaml/.ocamlformat-ignore diff --git a/ocaml/.justfile b/ocaml/.justfile index 6cdc060..8ef36ed 100644 --- a/ocaml/.justfile +++ b/ocaml/.justfile @@ -1,10 +1,5 @@ set unstable -dependencies := \ - require('dune') && \ - require('entr') && \ - require('bisect-ppx-report') - _default: @just --list @@ -105,10 +100,61 @@ alias f := format # Check formatting without changing files [group('checks')] format-check: - dune fmt --preview + #!/usr/bin/env sh + find . \ + \( -name '*.ml' -o -name '*.mli' \) \ + \( -path './lib/*' -o -path './bin/*' \) | + xargs ocamlformat --check alias fck := format-check +# Format specific files +[group('checks')] +[no-cd] +format-file *args: + ocamlformat --inplace -- {{ args }} + +alias ff := format-file + +# Check formatting on specific files +[group('checks')] +[no-exit-message] +[no-cd] +format-check-file *args: + #!/usr/bin/env sh + files=$(printf '%s' "{{ args }}" | sed 's/ /\n/g') + for file in $files; do + if ocamlformat --check -- $file; then + echo " [ OK ] $file" + else + echo " [ !! ] $file" + extension=$(printf '%s' "$file" | rev | cut -d . -f 1 | rev) + formatted="$(basename $file .$extension).fmt.$extension" + ocamlformat "$file" > "$formatted" + delta "$file" $formatted + fi + done + +alias ffck := format-check-file + +# Cleanup formatting temporary files +[group('checks')] +[no-cd] +format-file-cleanup: + #!/usr/bin/env sh + files=$(find . -regex '.*\.fmt\.[a-zA-Z0-9]+$') + if [ -n "$files" ]; then + printf '%s:\n%s\n\n%s\n%s\n > ' \ + 'Files found' \ + "$files" \ + '[RETURN] Remove all' '[Ctrl-C] Abort' + read _ + rm -v $files + else + echo 'No temporary formatting files found' + fi + +alias ffcl := format-file-cleanup # UNGROUPED @@ -121,3 +167,10 @@ info: @echo OS/Arch: {{ os() }} {{ arch() }} @echo GCC Triplet: $(gcc -dumpmachine) @echo Shell: {{ env('SHELL') }} + +dependencies := \ + require('dune') && \ + require('ocamlformat') && \ + require('delta') && \ + require('entr') && \ + require('bisect-ppx-report') diff --git a/ocaml/.ocamlformat b/ocaml/.ocamlformat index c8c3e93..92b82b2 100644 --- a/ocaml/.ocamlformat +++ b/ocaml/.ocamlformat @@ -1,4 +1,6 @@ +profile = default version = 0.27.0 + margin-check = true extension-indent = 4 function-indent = 4 diff --git a/ocaml/.ocamlformat-ignore b/ocaml/.ocamlformat-ignore new file mode 100644 index 0000000..683c15d --- /dev/null +++ b/ocaml/.ocamlformat-ignore @@ -0,0 +1 @@ +lib/schema/schema.ml diff --git a/ocaml/bin/main.ml b/ocaml/bin/main.ml index 81d2626..8184ae5 100644 --- a/ocaml/bin/main.ml +++ b/ocaml/bin/main.ml @@ -3,7 +3,7 @@ open Tori.Utilities.Aliases let () = match Array.to_list Sys.argv with | _ :: tail -> - let future = (Tori.Parsers.Argument.interpret Tori.Schema.seed tail) in + let future = Tori.Parsers.Argument.interpret Tori.Schema.seed tail in if future.output.main <> "" then print_endline future.output.main; if future.output.log <> "" then elog future.output.log; exit future.meta.status diff --git a/ocaml/lib/parsers/argument.ml b/ocaml/lib/parsers/argument.ml index 98956d3..c849686 100644 --- a/ocaml/lib/parsers/argument.ml +++ b/ocaml/lib/parsers/argument.ml @@ -1,9 +1,11 @@ -let interpret (past: Schema.schema) (input: string list): Schema.schema = +let interpret (past : Schema.schema) (input : string list) : Schema.schema = + let future : Schema.schema = + { past with output = { past.output with main = "" } } + in - let future: Schema.schema = { past with output = { past.output with main = "" } } in - - let say (message: string): Schema.schema = - { future with output = { future.output with main = message }} in + let say (message : string) : Schema.schema = + { future with output = { future.output with main = message } } + in (* TODO: return a schema with orders, instead of calling side-effects @@ -18,12 +20,14 @@ let interpret (past: Schema.schema) (input: string list): Schema.schema = say (Schema.format_version future.meta.version) | ("help" | "-h" | "--help") :: _ -> say future.meta.help.long | head :: _ -> - { future with - output = { - future.output with main = - ("Unrecognized command: " ^ head ^ "\n" - ^ future.meta.help.short) + { + future with + output = + { + future.output with + main = + "Unrecognized command: " ^ head ^ "\n" ^ future.meta.help.short; }; - meta = { future.meta with status = 1 }; + meta = { future.meta with status = 1 }; } | _ -> future diff --git a/ocaml/lib/schema/schema.ml b/ocaml/lib/schema/schema.ml index 7863036..5c325e6 100644 --- a/ocaml/lib/schema/schema.ml +++ b/ocaml/lib/schema/schema.ml @@ -1,17 +1,17 @@ open Utilities.Aliases -type version = { major: int; minor: int; patch: int } -type help = { short: string; long: string } -type meta = { version: version; help: help; status: int } +type version = { major : int; minor : int; patch : int } +type help = { short : string; long : string } +type meta = { version : version; help : help; status : int } -type output = { main: string; log: string } +type output = { main : string; log : string } type os = Unknown | FreeBSD | Void | Alpine -type host = { os: os; name: string } +type host = { os : os; name : string } -type schema = { meta: meta; output: output; host: host } +type schema = { meta : meta; output : output; host : host } -let seed: schema = { +let seed : schema = { meta = { version = { major = 0; @@ -36,7 +36,7 @@ let seed: schema = { }; } -let format_version (version: version): string = +let format_version (version : version) : string = "v" ^ str_int version.major ^ "." ^ str_int version.minor ^ "." ^ str_int version.patch diff --git a/ocaml/lib/system/file.ml b/ocaml/lib/system/file.ml index efbf033..970fd0f 100644 --- a/ocaml/lib/system/file.ml +++ b/ocaml/lib/system/file.ml @@ -6,8 +6,7 @@ let read_channel channel = Buffer.add_char buffer '\n'; read () in - try read () with - End_of_file -> Buffer.contents buffer + try read () with End_of_file -> Buffer.contents buffer let read path = let channel = open_in path in diff --git a/ocaml/lib/system/package.ml b/ocaml/lib/system/package.ml index 228858f..849ff75 100644 --- a/ocaml/lib/system/package.ml +++ b/ocaml/lib/system/package.ml @@ -1,30 +1,34 @@ -let merge (schema: Schema.schema) (packages: string list): Schema.schema = - +let merge (schema : Schema.schema) (packages : string list) : Schema.schema = match packages with - | [] -> { schema with output = { - schema.output with main = "No packages provided" } - } + | [] -> + { + schema with + output = { schema.output with main = "No packages provided" }; + } | _ -> - - let commands: Process.Command.command list = [ - { + let commands : Process.Command.command list = + [ + { name = "doas"; - arguments = ["doas"; "apk"; "-i"; "add"] @ packages; + arguments = [ "doas"; "apk"; "-i"; "add" ] @ packages; status = Unevaluated; - }; - { + }; + { name = "doas"; - arguments = ["doas"; "apk"; "-i"; "del"] @ packages; + arguments = [ "doas"; "apk"; "-i"; "del" ] @ packages; status = Unevaluated; - } - ] in + }; + ] + in let ran = Process.Fork.run_many commands in let formatted_ran = Process.Command.format_many ran in { - schema with output = { - schema.output with log = - "Done:\n" ^ (String.concat "\n" formatted_ran) - } + schema with + output = + { + schema.output with + log = "Done:\n" ^ String.concat "\n" formatted_ran; + }; } diff --git a/ocaml/lib/system/process/command.ml b/ocaml/lib/system/process/command.ml index bc245cd..6ed623d 100644 --- a/ocaml/lib/system/process/command.ml +++ b/ocaml/lib/system/process/command.ml @@ -1,13 +1,16 @@ open Utilities.Aliases type status = Exit of int | Unevaluated -type command = { name: string; arguments: string list; status: status } +type command = { name : string; arguments : string list; status : status } -let format (command: command): string = - command.name ^ - " with arguments: " ^ (String.concat " " command.arguments) ^ - " and result " ^ match command.status with +let format (command : command) : string = + command.name ^ " with arguments: " + ^ String.concat " " command.arguments + ^ " and result " + ^ + match command.status with | Exit n -> str_int n | Unevaluated -> "Not evaluated" -let format_many (commands: command list): string list = List.map format commands +let format_many (commands : command list) : string list = + List.map format commands diff --git a/ocaml/lib/system/process/fork.ml b/ocaml/lib/system/process/fork.ml index d581096..649d1cd 100644 --- a/ocaml/lib/system/process/fork.ml +++ b/ocaml/lib/system/process/fork.ml @@ -1,9 +1,12 @@ -let run (command: Command.command): Command.command = +let run (command : Command.command) : Command.command = match Unix.fork () with | 0 -> Unix.execvp command.name (Array.of_list command.arguments) - | pid -> let (_, status) = Unix.waitpid [] pid in + | pid -> ( + let _, status = Unix.waitpid [] pid in match status with - | WSTOPPED n | WSIGNALED n | WEXITED n -> { command with status = Exit n } + | WSTOPPED n | WSIGNALED n | WEXITED n -> + { command with status = Exit n } + ) -let run_many (commands: Command.command list): Command.command list = +let run_many (commands : Command.command list) : Command.command list = List.map run commands diff --git a/ocaml/lib/system/process/reader.ml b/ocaml/lib/system/process/reader.ml index f256121..2762dbd 100644 --- a/ocaml/lib/system/process/reader.ml +++ b/ocaml/lib/system/process/reader.ml @@ -1,15 +1,14 @@ open Utilities.Aliases -type output = { output: string; error: string; status: string; } +type output = { output : string; error : string; status : string } -let handle_exit_status (status: Unix.process_status): string = +let handle_exit_status (status : Unix.process_status) : string = match status with | Unix.WEXITED n -> "Exit " ^ str_int n | Unix.WSIGNALED n -> "Kill " ^ str_int n | Unix.WSTOPPED n -> "Stopped " ^ str_int n -let read (env: string array) (command: string): output = - +let read (env : string array) (command : string) : output = let stdout, stdin, stderr = Unix.open_process_full command env in let in_buffer = Buffer.create 4096 in let err_buffer = Buffer.create 4096 in @@ -20,25 +19,29 @@ let read (env: string array) (command: string): output = Buffer.add_char in_buffer '\n'; read_in () in - try read_in () with End_of_file -> (); + try read_in () + with End_of_file -> ( + (); - let rec read_err () = - let err_line = input_line stderr in - Buffer.add_string err_buffer err_line; - Buffer.add_char err_buffer '\n'; - read_err () - in - try read_err () with - End_of_file -> let exit_status = + let rec read_err () = + let err_line = input_line stderr in + Buffer.add_string err_buffer err_line; + Buffer.add_char err_buffer '\n'; + read_err () + in + try read_err () + with End_of_file -> + let exit_status = handle_exit_status (Unix.close_process_full (stdout, stdin, stderr)) in { - output = String.trim (Buffer.contents in_buffer); - error = Buffer.contents err_buffer; - status = exit_status; + output = String.trim (Buffer.contents in_buffer); + error = Buffer.contents err_buffer; + status = exit_status; } + ) -let format (output: output): string = +let format (output : output) : string = match output with | { output = o; error = _; status = "Exit 0" } -> o | { output = ""; error = e; status = s } -> "[" ^ s ^ "]" ^ " " ^ e diff --git a/ocaml/test/test_tori.ml b/ocaml/test/test_tori.ml index 4a03b32..270ac1c 100644 --- a/ocaml/test/test_tori.ml +++ b/ocaml/test/test_tori.ml @@ -2,7 +2,6 @@ module Reader = Tori.System.Process.Reader module File = Tori.System.File let smoke () = - (* Executing echo should return the same string on output *) let result = Reader.read [||] "echo 0x70121" in assert (Reader.format result = "0x70121");