OCaml: Apply formatting

This commit is contained in:
Juno Takano 2025-04-19 19:09:16 -03:00
commit bb1cd19000
12 changed files with 145 additions and 74 deletions

View file

@ -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')

View file

@ -1,4 +1,6 @@
profile = default
version = 0.27.0
margin-check = true
extension-indent = 4
function-indent = 4

View file

@ -0,0 +1 @@
lib/schema/schema.ml

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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;
};
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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");