Skip to content

Commit

Permalink
CP-48666: use dune rule to get the destination dir for the generated …
Browse files Browse the repository at this point in the history
…files

Signed-off-by: Luca Zhang <[email protected]>
  • Loading branch information
duobei committed Apr 10, 2024
1 parent 32e165a commit 05d07ed
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
3 changes: 2 additions & 1 deletion ocaml/sdk-gen/go/dune
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
CommonFunctions
mustache
xapi-datamodel
xapi-stdext-unix
gen_go_helper
)
)
Expand All @@ -26,7 +27,7 @@
(:x gen_go_binding.exe)
(source_tree templates)
)
(action (run %{x}))
(action (run %{x} --destdir autogen))
)

(test
Expand Down
27 changes: 20 additions & 7 deletions ocaml/sdk-gen/go/gen_go_binding.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
open CommonFunctions
open Gen_go_helper

let render_api_messages_and_errors () =
let render_api_messages_and_errors destdir =
let obj =
`O
[
Expand All @@ -30,11 +30,11 @@ let render_api_messages_and_errors () =
let messages_rendered =
header ^ render_template "APIMessages.mustache" obj ^ "\n"
in
generate_file error_rendered "api_errors.go" ;
generate_file messages_rendered "api_messages.go"
generate_file error_rendered destdir "api_errors.go" ;
generate_file messages_rendered destdir "api_messages.go"

let main () =
render_api_messages_and_errors () ;
let main destdir =
render_api_messages_and_errors destdir ;
let objects = Json.xenapi objects in
List.iter
(fun (name, obj) ->
Expand All @@ -43,8 +43,21 @@ let main () =
let record_rendered = render_template "Record.mustache" obj in
let rendered = header_rendered ^ enums_rendered ^ record_rendered in
let output_file = name ^ ".go" in
generate_file rendered output_file
generate_file rendered destdir output_file
)
objects

let _ = main ()
let _ =
let destdir = ref "." in
Arg.parse
[
( "--destdir"
, Arg.Set_string destdir
, "the destination directory for the generated files"
)
]
(fun x -> Printf.fprintf stderr "Ignoring unknown parameter: %s\n%!" x)
"Generates Go SDK." ;
let destdir = !destdir // "src" in
Xapi_stdext_unix.Unixext.mkdir_rec destdir 0o755 ;
main destdir
8 changes: 2 additions & 6 deletions ocaml/sdk-gen/go/gen_go_helper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@
open Datamodel_types
open CommonFunctions

let dest_dir = "autogen"

let templates_dir = "templates"

let ( // ) = Filename.concat

let src_dir = dest_dir // "src"

let snake_to_camel (s : string) : string =
String.split_on_char '_' s
|> List.map (fun s -> String.split_on_char '-' s)
Expand All @@ -37,8 +33,8 @@ let render_template template_file json =
in
Mustache.render templ json

let generate_file rendered output_file =
let out_chan = open_out (src_dir // output_file) in
let generate_file rendered destdir output_file =
let out_chan = open_out (destdir // output_file) in
Fun.protect
(fun () -> output_string out_chan rendered)
~finally:(fun () -> close_out out_chan)
Expand Down
2 changes: 1 addition & 1 deletion ocaml/sdk-gen/go/gen_go_helper.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ val snake_to_camel : string -> string

val render_template : string -> Mustache.Json.t -> string

val generate_file : string -> string -> unit
val generate_file : string -> string -> string -> unit

module Json : sig
val xenapi : Datamodel_types.obj list -> (string * Mustache.Json.t) list
Expand Down

0 comments on commit 05d07ed

Please sign in to comment.