Skip to content

Commit

Permalink
Improve output and QoL
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Grondin committed Apr 11, 2020
1 parent 70922ac commit ebc26f2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SHELL := /usr/bin/env bash

build:
opam exec -- dune build src/cli/strings.exe
cp _build/default/src/cli/strings.exe .
strip strings.exe

clean:
opam exec -- dune clean
37 changes: 29 additions & 8 deletions src/cli/strings.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
open Core_kernel

let version = "1.0.2"
let header = sprintf "/* Generated by okTurtles/strings v%s */\n\n" version

let () = Lwt.async_exception_hook := (fun ex ->
let open Lwt in
let p1 = Lwt_io.write_line Lwt_io.stderr (sprintf "💀 PLEASE REPORT THIS BUG. UNCAUGHT EXCEPTION: %s" (Utils.Exception.human ex)) in
Expand All @@ -20,7 +23,10 @@ let process_file ~root (strings, count) filename =
let%lwt parsed = Lwt_io.with_file ~mode:Input ~flags:read_flags filename (Vue.parse filename) in
Queue.iter parsed ~f:(fun string ->
let data = String.chop_prefix filename ~prefix:root |> Option.value ~default:filename in
String.Table.add_multi strings ~key:string ~data
String.Table.update strings string ~f:(function
| None -> String.Set.add String.Set.empty data
| Some set -> String.Set.add set data
)
);
Lwt.return_unit
)
Expand Down Expand Up @@ -49,10 +55,14 @@ let write_english english count =
let%lwt () =
Lwt_io.with_file ~flags:write_flags ~mode:Output path_strings (fun oc_strings ->
Lwt_io.with_file ~flags:write_flags ~mode:Output path_json (fun oc_json ->
let%lwt () = Lwt_io.write_char oc_json '{' in
let%lwt () = Lwt.join [
Lwt_io.write oc_strings header;
Lwt_io.write_char oc_json '{';
]
in
let%lwt () = String.Table.fold english ~init:Lwt.return_unit ~f:(fun ~key ~data acc ->
let fmt_key = fmt key in
let output_strings = sprintf "/* %s */\n%s = %s;\n\n" (String.concat ~sep:", " data) fmt_key fmt_key in
let output_strings = sprintf "/* %s */\n%s = %s;\n\n" data fmt_key fmt_key in
let output_json = json_pair fmt_key fmt_key first in
let%lwt () = acc in
(Lwt_io.write oc_strings output_strings) <&> (Lwt_io.write oc_json output_json)
Expand All @@ -73,10 +83,14 @@ let write_other ~language english other =
let%lwt n_left, n_right, n_both =
Lwt_io.with_file ~flags:write_flags ~mode:Output path_strings (fun oc_strings ->
Lwt_io.with_file ~flags:write_flags ~mode:Output path_json (fun oc_json ->
let%lwt () = Lwt_io.write_char oc_json '{' in
let%lwt () = Lwt.join [
Lwt_io.write oc_strings header;
Lwt_io.write_char oc_json '{';
]
in
let missing_translation key x =
let fmt_key = fmt key in
let line_strings = sprintf "/* MISSING TRANSLATION - %s */\n%s = %s;\n\n" (String.concat ~sep:", " x) fmt_key fmt_key in
let line_strings = sprintf "/* MISSING TRANSLATION - %s */\n%s = %s;\n\n" x fmt_key fmt_key in
let line_json = json_pair fmt_key fmt_key first in
Some (`Left, Lwt_io.write oc_strings line_strings <&> Lwt_io.write oc_json line_json)
in
Expand All @@ -86,7 +100,7 @@ let write_other ~language english other =
| `Both (x, y) ->
let fmt_key = fmt key in
let fmt_y = fmt y in
let line_strings = sprintf "/* %s */\n%s = %s;\n\n" (String.concat x) fmt_key fmt_y in
let line_strings = sprintf "/* %s */\n%s = %s;\n\n" x fmt_key fmt_y in
let line_json = json_pair fmt_key fmt_y first in
Some (`Both, Lwt_io.write oc_strings line_strings <&> Lwt_io.write oc_json line_json)
| `Right y when String.(key = y) -> None
Expand Down Expand Up @@ -124,6 +138,9 @@ let directory_exists path =
let main args =
let t0 = Time_now.nanoseconds_since_unix_epoch () in
let%lwt directories = begin match args with
| _::"-v"::[] | _::"--version"::[] ->
let%lwt () = Lwt_io.write_line Lwt_io.stdout (sprintf "Version %s" version) in
exit 0
| _::[] -> failwith "At least one argument is required"
| _::x -> Lwt.return x
| _ -> failwith "Expected Unix calling convention"
Expand All @@ -145,13 +162,17 @@ let main args =
in
(* English *)
let%lwt english =
let english = String.Table.create () in
let english_list = String.Table.create () in
let count = ref 0 in
let%lwt () = Lwt_list.iter_p (fun directory ->
let root = (String.chop_suffix ~suffix:"/" directory |> Option.value ~default:directory) in
traverse ~root:(sprintf "%s/" root) (english, count) root
traverse ~root:(sprintf "%s/" root) (english_list, count) root
) directories
in
let english = String.Table.map english_list ~f:(fun set ->
String.Set.to_array set |> String.concat_array ~sep:", "
)
in
let%lwt () = write_english english !count in
Lwt.return english
in
Expand Down

0 comments on commit ebc26f2

Please sign in to comment.