From b826340d7536c2777fdcafb3bfcc6ebc8d8053ee Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Tue, 26 Dec 2023 11:15:33 -0600 Subject: Support output file --- bin/main.ml | 39 ++++++++++++++++++++++++++++++++++----- lib/ast_types.ml | 21 +++------------------ 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index d644133..43ce56b 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -7,7 +7,26 @@ let parse (s : string) = ast -let fixgen content output_type () = +let print_stdout = function + | [ (_, content) ] -> print_endline content + | list -> + list + |> List.iter (fun (name, content) -> + print_endline ("FILE NAME: " ^ name); + print_endline content) + + +let file_name name file_format = name ^ "." ^ file_format + +let save_files file_format output_folder list = + list + |> List.iter (fun (name, content) -> + let file = open_out (output_folder ^ "/" ^ file_name name file_format) in + output_string file content; + close_out file) + + +let fixgen content output_type output_folder = let result = parse content in let generate_output = match output_type with @@ -15,13 +34,18 @@ let fixgen content output_type () = | "json" -> Fixture.json_of_generated_fixtures | _ -> failwith "Unsupported output type, supported types are: json, csv" in + let save_output = + match output_folder with + | "STDOUT" -> print_stdout + | s -> save_files output_type s + in match result with | Some ast -> Ast_types.compile ast - |> List.iter (fun (name, file) -> + |> List.map (fun (name, file) -> let result = generate_output file in - print_endline ("FILE NAME: " ^ name); - print_endline result) + name, result) + |> save_output | None -> print_endline "error" @@ -31,12 +55,17 @@ let file = (* TODO: Support option to set output folder *) +let output_folder = + let doc = "Set output folder" in + Arg.( + value & opt string "STDOUT" & info [ "o"; "output-folder" ] ~docv:"OUTPUT FOLDER" ~doc) + let output_type = let doc = "Set output file type, supported=csv" in Arg.(value & opt string "csv" & info [ "f"; "file-type" ] ~docv:"FILE TYPE" ~doc) -let fixgen_t = Term.(const fixgen $ file $ output_type $ const ()) +let fixgen_t = Term.(const fixgen $ file $ output_type $ output_folder) let cmd = Cmd.v (Cmd.info "fixgen") fixgen_t let () = exit (Cmd.eval cmd) diff --git a/lib/ast_types.ml b/lib/ast_types.ml index c192c60..7638d3b 100644 --- a/lib/ast_types.ml +++ b/lib/ast_types.ml @@ -59,6 +59,8 @@ type ast_table_relation = | PTable of string * int * Fixture.t list | CTable of string * string * string * int * (string list -> Fixture.t list) (** parent, row, child_name, row *) + (* TODO: Support multiple relations *) + let table_name = function | PTable (n,_,_) -> n @@ -99,7 +101,6 @@ let rec ast_table_to_tables ast = it can be used as input for the next entry *) match ast with | Table (name, amount, r, t) -> - print_endline ("Converting table: " ^ name); ast_table_to_table name amount (ast_row_to_fixtures r) :: ast_table_to_tables t | End -> [] @@ -110,6 +111,7 @@ let%test "ast_table_to_tables" = List.length tables == 2 let generated_fixtures = ref (Hashtbl.create 10) +(** Used to not regenerate a different fixture for the same table. Also theoretically speeds up the compilation process by not generating the same fixture twice. I do not know what kind of monstrosity of a fixture you'd need for it to matter, but neat regardless. *) let rec resolve_fixtures tables name = (* Verify if we have already generated fixtures for this table *) @@ -129,12 +131,7 @@ let rec resolve_fixtures tables name = let (_, parent_fixtures) = resolve_fixtures tables p in (* store the generated fixtures for this table *) Hashtbl.add !generated_fixtures name parent_fixtures; - print_endline ("Resolving fixtures for " ^ c); - for i = 0 to List.length parent_fixtures - 1 do - print_endline ("Parent fixture: " ^ Fixture.csv_of_string_list (List.nth parent_fixtures i)); - done; let generated_ids = Fixture.find_entries_for_header parent_fixtures pr in - print_endline ("Generated ids: " ^ String.concat ", " (Result.get_ok generated_ids)); match generated_ids with | Ok ids -> (c, Fixture.compile ~amount (create_fixtures ids)) | Error e -> failwith e @@ -148,12 +145,6 @@ let%test "resolve_fixtures" = ] in let (_, user_fixtures) = resolve_fixtures tables "user" in let (_, purchase_fixtures) = resolve_fixtures tables "posts" in - for i = 0 to List.length user_fixtures - 1 do - print_endline ("User fixture: " ^ Fixture.csv_of_string_list (List.nth user_fixtures i)); - done; - for i = 0 to List.length purchase_fixtures - 1 do - print_endline ("Purchase fixture: " ^ Fixture.csv_of_string_list (List.nth purchase_fixtures i)); - done; match (user_fixtures,purchase_fixtures) with | ([["id";_];[u;_]], [["user_id"]; [v]]) -> v = u | _ -> @@ -170,26 +161,20 @@ let show_tables tables = let compile ast = let tables = ast_table_to_tables ast in - print_endline ("Table length: " ^ string_of_int (List.length tables)); - print_endline (show_tables tables); let resolve_fixtures' = function | PTable (n, amount, l) -> let (n,_,resolved) = (n, amount, Fixture.compile l ~amount) in - (* store the generated fixtures for this table *) Hashtbl.add !generated_fixtures n resolved; (n,resolved) | CTable (p,pr,c, amount, create_fixtures) -> let (_, parent_fixtures) = resolve_fixtures tables p in - (* store the generated fixtures for this table *) Hashtbl.add !generated_fixtures c parent_fixtures; - print_endline ("Resolving fixtures for " ^ c); let generated_ids = Fixture.find_entries_for_header parent_fixtures pr in match generated_ids with | Ok ids -> (c, Fixture.compile ~amount (create_fixtures ids)) | Error e -> failwith e in let result = List.map resolve_fixtures' tables in - print_endline ("Result names: " ^ String.concat ", " (List.map fst result)); result -- cgit v1.2.3