Skip to content

Commit

Permalink
Added --output/-o, refactoring, optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
SGrondin committed Jan 21, 2023
1 parent d68e2e6 commit 4e87c08
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 178 deletions.
2 changes: 1 addition & 1 deletion dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(data_only_dirs node_modules)
(data_only_dirs node_modules quickjs)
(env
(dev
(flags (:standard -warn-error -A))
Expand Down
261 changes: 137 additions & 124 deletions src/cli/strings.ml

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/quickjs/quickjs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ external stub_init_contexts : int -> (unit, string) Result.t = "stub_init_contex
external stub_extract : int -> string -> fn_name:string -> (string array * string array, string) Result.t
= "stub_extract"

let num_threads = 1
let num_threads = 4

let init_time = ref Int63.zero

let init_contexts =
lazy
(let time = Utils.time () in
(let time = Utils.Timing.start () in
Lwt_preemptive.detach (fun () -> stub_init_contexts num_threads) () >>= function
| Ok () ->
let time = time () in
let time = time `Stop in
init_time := time;
Lwt_io.printlf
!"✅ [%{Int63}ms] Initialized %d JS runtimes for TS and/or Pug processing"
Expand All @@ -38,6 +38,7 @@ let js_contexts =
incr ctr;
Lwt.return i)

(* Re-indent from 0 if base indent is greater than 0 *)
let clean_pug code =
let rec next i =
match String.index_from code i '\n' with
Expand All @@ -53,7 +54,7 @@ let clean_pug code =
match String.lfindi s ~f:(fun _i c -> Char.(c <> ' ')) with
| None as x -> x
| Some indent ->
let is_comment = String.( = ) (String.sub code ~pos:indent ~len:2) "//" in
let is_comment = String.is_substring_at code ~pos:indent ~substring:"//" in
Some (indent, is_comment, s)))
|> Sequence.fold_until ~init:None ~finish:(Option.value ~default:0) ~f:(fun in_comment data ->
match in_comment, data with
Expand Down
30 changes: 30 additions & 0 deletions src/utils/collector.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
open! Core

type t = {
path: string;
strings: string Queue.t;
possible_scripts: string Queue.t;
file_errors: string Queue.t;
}
[@@deriving sexp]

let create ~path =
{ path; strings = Queue.create (); possible_scripts = Queue.create (); file_errors = Queue.create () }

let render_errors { file_errors; path; _ } =
match Queue.length file_errors with
| 0 -> None
| 1 ->
let buf = Buffer.create 256 in
bprintf buf "\n❌ 1 error in %s: %s" path (Queue.get file_errors 0);
Some (Buffer.contents buf)
| len ->
let buf = Buffer.create 256 in
bprintf buf "\n❌ %d errors in %s:\n" len path;
Queue.iter file_errors ~f:(bprintf buf "- %s\n");
Some (Buffer.contents buf)

let blit_transfer ~src ~dst =
Queue.blit_transfer ~src:src.strings ~dst:dst.strings ();
Queue.blit_transfer ~src:src.possible_scripts ~dst:dst.possible_scripts ();
Queue.blit_transfer ~src:src.file_errors ~dst:dst.file_errors ()
10 changes: 10 additions & 0 deletions src/utils/exception.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
open! Core

let human = function
| Failure msg -> msg
| Lwt.Canceled -> "Timed out."
| Core_unix.Unix_error (c, n, p) ->
sprintf {s|System Error "%s" during '%s("%s")'|s} (String.uppercase (Core_unix.Error.message c)) n p
| unknown -> Exn.to_string unknown

let full ex = sprintf !"%s\n%{Backtrace}" (human ex) (Backtrace.get ())
33 changes: 33 additions & 0 deletions src/utils/io.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
open! Core
open Lwt.Infix
open Lwt.Syntax

let read_flags = Core_unix.[ O_RDONLY; O_NONBLOCK ]

let write_flags = Core_unix.[ O_WRONLY; O_NONBLOCK; O_TRUNC; O_CREAT ]

let directory_exists path =
Lwt.try_bind
(fun () -> Lwt_unix.stat path)
(function
| { st_kind = S_DIR; _ } -> Lwt.return_true
| { st_kind = _; _ } -> failwithf "%s already exists, but is not a directory" path ())
(fun _ -> Lwt.return_false)

let mkdir_p ~dir_name ~perms =
let+ (_ : string) =
Filename.parts dir_name
|> Lwt_list.fold_left_s
(fun acc part ->
match acc with
| "" -> Lwt.return part
| acc -> (
let path = Filename.concat acc part in
Lwt_unix.file_exists path >>= function
| true -> Lwt.return path
| false ->
let+ () = Lwt_unix.mkdir path perms in
path))
""
in
()
7 changes: 7 additions & 0 deletions src/utils/timing.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
open! Core

let start () =
let t0 = Time_now.nanoseconds_since_unix_epoch () in
fun `Stop ->
let t1 = Time_now.nanoseconds_since_unix_epoch () in
Int63.((t1 - t0) / of_int 1_000_000)
49 changes: 0 additions & 49 deletions src/utils/utils.ml

This file was deleted.

0 comments on commit 4e87c08

Please sign in to comment.