open Fixgen open Cmdliner let parse (s : string) = let lexbuf = Lexing.from_string s in let ast = Parser.prog Lexer.read lexbuf in ast 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 | "csv" -> Fixture.csv_of_generated_fixtures | "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.map (fun (name, file) -> let result = generate_output file in name, result) |> save_output | None -> print_endline "error" let file = let doc = "Fixgen content" in Arg.(required & pos 0 (some string) None & info [] ~docv:"PROGRAM" ~doc) 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 $ output_folder) let cmd = Cmd.v (Cmd.info "fixgen") fixgen_t let () = exit (Cmd.eval cmd)