Skip to content

Commit

Permalink
Merge pull request #5554 from kc284/private/konstantin1/sdk-ps
Browse files Browse the repository at this point in the history
[OIL] Templatization of PS Remove-Xen* cmdlets. Refactoring of HTTP_actions.mustache
  • Loading branch information
kc284 authored Jul 16, 2024
2 parents 0c65928 + 1f80e89 commit bd222d0
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 134 deletions.
2 changes: 1 addition & 1 deletion ocaml/sdk-gen/csharp/friendly_error_names.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let _ =
)
]
(fun x -> raise (Arg.Bad ("Found anonymous argument " ^ x)))
"Generates C# bindings for the XenAPI. See -help."
"Generates the C# SDK for the XenAPI. See -help."

let sr_xml = !sr_xml'

Expand Down
74 changes: 28 additions & 46 deletions ocaml/sdk-gen/csharp/gen_csharp_binding.ml
Original file line number Diff line number Diff line change
Expand Up @@ -162,57 +162,39 @@ and gen_http_actions () =
| Varargs_query_arg ->
"args"
in
let delegate_type = function
| Get ->
"DataCopiedDelegate"
| Put ->
"UpdateProgressDelegate"
| _ ->
failwith "Unimplemented HTTP method"
in
let delegate_name = function
| Get ->
"dataCopiedDelegate"
| Put ->
"progressDelegate"
| _ ->
failwith "Unimplemented HTTP method"
in
let http_method = function
| Get ->
"Get"
| Put ->
"Put"
| _ ->
failwith "Unimplemented HTTP method"
in
let action_json (name, (meth, uri, _, sdkargs, _, _)) =
let enhanced_args =
[String_query_arg "task_id"; String_query_arg "session_id"] @ sdkargs
in
`O
[
("name", `String name)
; ("delegate_type", `String (delegate_type meth))
; ("delegate_name", `String (delegate_name meth))
; ("http_method", `String (http_method meth))
; ("uri", `String uri)
; ( "sdkargs_decl"
, `String
(enhanced_args |> List.map decl_of_sdkarg |> String.concat ", ")
)
; ( "sdkargs"
, `String (enhanced_args |> List.map use_of_sdkarg |> String.concat ", ")
)
]
in
let filtered_actions =
http_actions |> List.filter (fun (_, (_, _, sdk, _, _, _)) -> sdk)
in
`O
[
("licence", `String Licence.bsd_two_clause)
; ("http_actions", `A (List.map action_json filtered_actions))
( "http_actions"
, `A
(List.map
(fun (name, (meth, uri, _, sdkargs, _, _)) ->
`O
[
("name", `String name)
; ("isPut", `Bool (meth == Put))
; ("isGet", `Bool (meth == Get))
; ("uri", `String uri)
; ( "args"
, `A
(List.map
(fun x ->
`O
[
("arg_decl", `String (decl_of_sdkarg x))
; ("arg_use", `String (use_of_sdkarg x))
]
)
sdkargs
)
)
]
)
filtered_actions
)
)
]

(* ------------------- category: classes *)
Expand Down
39 changes: 32 additions & 7 deletions ocaml/sdk-gen/csharp/templates/HTTP_actions.mustache
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
{{{licence}}}
/*
* Copyright (c) Cloud Software Group, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Text;
using System.Net;

namespace XenAPI
Expand All @@ -21,11 +46,11 @@ namespace XenAPI
}

{{#http_actions}}
public static void {{name}}(HTTP.{{delegate_type}} {{delegate_name}}, HTTP.FuncBool cancellingDelegate, int timeout_ms,
string hostname, IWebProxy proxy, string path, {{{sdkargs_decl}}})
public static void {{name}}(HTTP.{{#isPut}}UpdateProgressDelegate progressDelegate{{/isPut}}{{#isGet}}DataCopiedDelegate dataCopiedDelegate{{/isGet}}, HTTP.FuncBool cancellingDelegate, int timeout_ms,
string hostname, IWebProxy proxy, string path, string task_id = null, string session_id = null{{#args}}, {{{arg_decl}}}{{/args}})
{
{{http_method}}({{delegate_name}}, cancellingDelegate, timeout_ms, hostname, "{{uri}}", proxy, path,
{{{sdkargs}}});
{{#isPut}}Put{{/isPut}}{{#isGet}}Get{{/isGet}}({{#isPut}}progressDelegate{{/isPut}}{{#isGet}}dataCopiedDelegate{{/isGet}}, cancellingDelegate, timeout_ms, hostname, "{{uri}}", proxy, path,
"task_id", task_id, "session_id", session_id{{#args}}, {{{arg_use}}}{{/args}});
}

{{/http_actions}}
Expand Down
108 changes: 28 additions & 80 deletions ocaml/sdk-gen/powershell/gen_powershell_binding.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ let rec main () =
|> List.filter (fun (_, (_, _, sdk, _, _, _)) -> sdk)
|> List.iter gen_http_action ;

let cmdlets =
classes |> List.filter generated |> List.map gen_cmdlets |> List.concat
in
List.iter (fun x -> write_file x.filename x.content) cmdlets
let filtered_classes = List.filter generated classes in
let cmdlets = List.concat_map gen_cmdlets filtered_classes in

List.iter (fun x -> write_file x.filename x.content) cmdlets ;

filtered_classes |> List.iter gen_destructor

(****************)
(* Http actions *)
Expand Down Expand Up @@ -178,11 +180,6 @@ and gen_cmdlets obj =
; content=
gen_constructor obj classname (List.filter is_constructor messages)
}
; {
filename= sprintf "Remove-Xen%s.cs" stem
; content=
gen_destructor obj classname (List.filter is_destructor messages)
}
; {
filename= sprintf "Remove-Xen%sProperty.cs" stem
; content= gen_remover obj classname (List.filter is_remover messages)
Expand Down Expand Up @@ -598,84 +595,35 @@ and convert_from_hashtable fname ty =
(************************************)
(* Print function for Remove-XenFoo *)
(************************************)
and gen_destructor obj classname messages =
match messages with

and gen_destructor obj =
let {name= classname; messages; _} = obj in
let destructors = List.filter is_destructor messages in
match destructors with
| [] ->
""
()
| [x] ->
let cut_message_name x = cut_msg_name (pascal_case x.msg_name) "Remove" in
let asyncMessages =
List.map cut_message_name (List.filter (fun x -> x.msg_async) messages)
let json =
`O
[
("type", `String (qualified_class_name classname))
; ("wire_class_name", `String (exposed_class_name classname))
; ("class_name", `String (ocaml_class_to_csharp_class classname))
; ("property", `String (ocaml_class_to_csharp_property classname))
; ("type_local", `String (ocaml_class_to_csharp_local_var classname))
; ("async", `Bool x.msg_async)
; ("has_uuid", `Bool (has_uuid obj))
; ("has_name", `Bool (has_name obj))
]
in
sprintf
"%s\n\n\
using System;\n\
using System.Collections;\n\
using System.Collections.Generic;\n\
using System.Management.Automation;\n\
using XenAPI;\n\n\
namespace Citrix.XenServer.Commands\n\
{\n\
\ [Cmdlet(VerbsCommon.Remove, \"Xen%s\", SupportsShouldProcess = \
true)]\n\
\ [OutputType(typeof(%s))]%s\n\
\ [OutputType(typeof(void))]\n\
\ public class RemoveXen%s : XenServerCmdlet\n\
\ {\n\
\ #region Cmdlet Parameters\n\n\
\ [Parameter]\n\
\ public SwitchParameter PassThru { get; set; }\n\
%s%s\n\
\ #endregion\n\n\
\ #region Cmdlet Methods\n\n\
\ protected override void ProcessRecord()\n\
\ {\n\
\ GetSession();\n\n\
\ string %s = Parse%s();\n\n\
\ %s\n\n\
\ UpdateSessions();\n\
\ }\n\n\
\ #endregion\n\n\
\ #region Private Methods\n\
%s%s\n\
\ #endregion\n\
\ }\n\
}\n"
Licence.bsd_two_clause
(ocaml_class_to_csharp_class classname)
(qualified_class_name classname)
( if asyncMessages <> [] then
"\n [OutputType(typeof(XenAPI.Task))]"
else
""
)
(ocaml_class_to_csharp_class classname)
(print_xenobject_params obj classname true true true)
( if asyncMessages <> [] then
sprintf
"\n\
\ protected override bool GenerateAsyncParam\n\
\ {\n\
\ get { return true; }\n\
\ }\n"
else
""
)
(ocaml_class_to_csharp_local_var classname)
(ocaml_class_to_csharp_property classname)
(print_cmdlet_methods_remover classname x)
(print_parse_xenobject_private_method obj classname true)
(print_process_record_private_methods classname messages "Remove"
"asyncpassthru"
render_file
( "Remove-XenObject.mustache"
, sprintf "Remove-Xen%s.cs" (ocaml_class_to_csharp_class classname)
)
json templdir destdir
| _ ->
assert false

and print_cmdlet_methods_remover classname message =
let localVar = ocaml_class_to_csharp_local_var classname in
let cut_message_name x = cut_msg_name (pascal_case x.msg_name) "Remove" in
sprintf "ProcessRecord%s(%s);" (cut_message_name message) localVar

(*****************************************)
(* Print function for Remove-XenFoo -Bar *)
(*****************************************)
Expand Down
Loading

0 comments on commit bd222d0

Please sign in to comment.