From 05d07ed0687dbcb0f1bc663332dd21b9627da45e Mon Sep 17 00:00:00 2001 From: Luca Zhang Date: Wed, 10 Apr 2024 14:46:35 +0800 Subject: [PATCH] CP-48666: use dune rule to get the destination dir for the generated files Signed-off-by: Luca Zhang --- ocaml/sdk-gen/go/dune | 3 ++- ocaml/sdk-gen/go/gen_go_binding.ml | 27 ++++++++++++++++++++------- ocaml/sdk-gen/go/gen_go_helper.ml | 8 ++------ ocaml/sdk-gen/go/gen_go_helper.mli | 2 +- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/ocaml/sdk-gen/go/dune b/ocaml/sdk-gen/go/dune index 00d835053bf..311ff35f4fd 100644 --- a/ocaml/sdk-gen/go/dune +++ b/ocaml/sdk-gen/go/dune @@ -6,6 +6,7 @@ CommonFunctions mustache xapi-datamodel + xapi-stdext-unix gen_go_helper ) ) @@ -26,7 +27,7 @@ (:x gen_go_binding.exe) (source_tree templates) ) - (action (run %{x})) + (action (run %{x} --destdir autogen)) ) (test diff --git a/ocaml/sdk-gen/go/gen_go_binding.ml b/ocaml/sdk-gen/go/gen_go_binding.ml index a5b68c86825..3fe2a59ef15 100644 --- a/ocaml/sdk-gen/go/gen_go_binding.ml +++ b/ocaml/sdk-gen/go/gen_go_binding.ml @@ -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 [ @@ -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) -> @@ -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 diff --git a/ocaml/sdk-gen/go/gen_go_helper.ml b/ocaml/sdk-gen/go/gen_go_helper.ml index 6fd9746f9bf..1e07903c0ba 100644 --- a/ocaml/sdk-gen/go/gen_go_helper.ml +++ b/ocaml/sdk-gen/go/gen_go_helper.ml @@ -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) @@ -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) diff --git a/ocaml/sdk-gen/go/gen_go_helper.mli b/ocaml/sdk-gen/go/gen_go_helper.mli index e624a99e0a9..74504f0ff13 100644 --- a/ocaml/sdk-gen/go/gen_go_helper.mli +++ b/ocaml/sdk-gen/go/gen_go_helper.mli @@ -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