aboutsummaryrefslogtreecommitdiff
path: root/bin/main.ml
blob: 43ce56b4e8b770c1593f5ba9c89c9963d2748fb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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)


(* 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 $ output_folder)
let cmd = Cmd.v (Cmd.info "fixgen") fixgen_t
let () = exit (Cmd.eval cmd)