Skip to content

Commit bd222d0

Browse files
authored
Merge pull request #5554 from kc284/private/konstantin1/sdk-ps
[OIL] Templatization of PS Remove-Xen* cmdlets. Refactoring of HTTP_actions.mustache
2 parents 0c65928 + 1f80e89 commit bd222d0

File tree

5 files changed

+262
-134
lines changed

5 files changed

+262
-134
lines changed

ocaml/sdk-gen/csharp/friendly_error_names.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let _ =
1616
)
1717
]
1818
(fun x -> raise (Arg.Bad ("Found anonymous argument " ^ x)))
19-
"Generates C# bindings for the XenAPI. See -help."
19+
"Generates the C# SDK for the XenAPI. See -help."
2020

2121
let sr_xml = !sr_xml'
2222

ocaml/sdk-gen/csharp/gen_csharp_binding.ml

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -162,57 +162,39 @@ and gen_http_actions () =
162162
| Varargs_query_arg ->
163163
"args"
164164
in
165-
let delegate_type = function
166-
| Get ->
167-
"DataCopiedDelegate"
168-
| Put ->
169-
"UpdateProgressDelegate"
170-
| _ ->
171-
failwith "Unimplemented HTTP method"
172-
in
173-
let delegate_name = function
174-
| Get ->
175-
"dataCopiedDelegate"
176-
| Put ->
177-
"progressDelegate"
178-
| _ ->
179-
failwith "Unimplemented HTTP method"
180-
in
181-
let http_method = function
182-
| Get ->
183-
"Get"
184-
| Put ->
185-
"Put"
186-
| _ ->
187-
failwith "Unimplemented HTTP method"
188-
in
189-
let action_json (name, (meth, uri, _, sdkargs, _, _)) =
190-
let enhanced_args =
191-
[String_query_arg "task_id"; String_query_arg "session_id"] @ sdkargs
192-
in
193-
`O
194-
[
195-
("name", `String name)
196-
; ("delegate_type", `String (delegate_type meth))
197-
; ("delegate_name", `String (delegate_name meth))
198-
; ("http_method", `String (http_method meth))
199-
; ("uri", `String uri)
200-
; ( "sdkargs_decl"
201-
, `String
202-
(enhanced_args |> List.map decl_of_sdkarg |> String.concat ", ")
203-
)
204-
; ( "sdkargs"
205-
, `String (enhanced_args |> List.map use_of_sdkarg |> String.concat ", ")
206-
)
207-
]
208-
in
209165
let filtered_actions =
210166
http_actions |> List.filter (fun (_, (_, _, sdk, _, _, _)) -> sdk)
211167
in
212168
`O
213169
[
214-
("licence", `String Licence.bsd_two_clause)
215-
; ("http_actions", `A (List.map action_json filtered_actions))
170+
( "http_actions"
171+
, `A
172+
(List.map
173+
(fun (name, (meth, uri, _, sdkargs, _, _)) ->
174+
`O
175+
[
176+
("name", `String name)
177+
; ("isPut", `Bool (meth == Put))
178+
; ("isGet", `Bool (meth == Get))
179+
; ("uri", `String uri)
180+
; ( "args"
181+
, `A
182+
(List.map
183+
(fun x ->
184+
`O
185+
[
186+
("arg_decl", `String (decl_of_sdkarg x))
187+
; ("arg_use", `String (use_of_sdkarg x))
188+
]
189+
)
190+
sdkargs
191+
)
192+
)
193+
]
194+
)
195+
filtered_actions
196+
)
197+
)
216198
]
217199

218200
(* ------------------- category: classes *)

ocaml/sdk-gen/csharp/templates/HTTP_actions.mustache

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
1-
{{{licence}}}
1+
/*
2+
* Copyright (c) Cloud Software Group, Inc.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* 1) Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* 2) Redistributions in binary form must reproduce the above
12+
* copyright notice, this list of conditions and the following
13+
* disclaimer in the documentation and/or other materials
14+
* provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27+
* OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*/
229

3-
using System;
4-
using System.Text;
530
using System.Net;
631

732
namespace XenAPI
@@ -21,11 +46,11 @@ namespace XenAPI
2146
}
2247

2348
{{#http_actions}}
24-
public static void {{name}}(HTTP.{{delegate_type}} {{delegate_name}}, HTTP.FuncBool cancellingDelegate, int timeout_ms,
25-
string hostname, IWebProxy proxy, string path, {{{sdkargs_decl}}})
49+
public static void {{name}}(HTTP.{{#isPut}}UpdateProgressDelegate progressDelegate{{/isPut}}{{#isGet}}DataCopiedDelegate dataCopiedDelegate{{/isGet}}, HTTP.FuncBool cancellingDelegate, int timeout_ms,
50+
string hostname, IWebProxy proxy, string path, string task_id = null, string session_id = null{{#args}}, {{{arg_decl}}}{{/args}})
2651
{
27-
{{http_method}}({{delegate_name}}, cancellingDelegate, timeout_ms, hostname, "{{uri}}", proxy, path,
28-
{{{sdkargs}}});
52+
{{#isPut}}Put{{/isPut}}{{#isGet}}Get{{/isGet}}({{#isPut}}progressDelegate{{/isPut}}{{#isGet}}dataCopiedDelegate{{/isGet}}, cancellingDelegate, timeout_ms, hostname, "{{uri}}", proxy, path,
53+
"task_id", task_id, "session_id", session_id{{#args}}, {{{arg_use}}}{{/args}});
2954
}
3055

3156
{{/http_actions}}

ocaml/sdk-gen/powershell/gen_powershell_binding.ml

Lines changed: 28 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ let rec main () =
9797
|> List.filter (fun (_, (_, _, sdk, _, _, _)) -> sdk)
9898
|> List.iter gen_http_action ;
9999

100-
let cmdlets =
101-
classes |> List.filter generated |> List.map gen_cmdlets |> List.concat
102-
in
103-
List.iter (fun x -> write_file x.filename x.content) cmdlets
100+
let filtered_classes = List.filter generated classes in
101+
let cmdlets = List.concat_map gen_cmdlets filtered_classes in
102+
103+
List.iter (fun x -> write_file x.filename x.content) cmdlets ;
104+
105+
filtered_classes |> List.iter gen_destructor
104106

105107
(****************)
106108
(* Http actions *)
@@ -178,11 +180,6 @@ and gen_cmdlets obj =
178180
; content=
179181
gen_constructor obj classname (List.filter is_constructor messages)
180182
}
181-
; {
182-
filename= sprintf "Remove-Xen%s.cs" stem
183-
; content=
184-
gen_destructor obj classname (List.filter is_destructor messages)
185-
}
186183
; {
187184
filename= sprintf "Remove-Xen%sProperty.cs" stem
188185
; content= gen_remover obj classname (List.filter is_remover messages)
@@ -598,84 +595,35 @@ and convert_from_hashtable fname ty =
598595
(************************************)
599596
(* Print function for Remove-XenFoo *)
600597
(************************************)
601-
and gen_destructor obj classname messages =
602-
match messages with
598+
599+
and gen_destructor obj =
600+
let {name= classname; messages; _} = obj in
601+
let destructors = List.filter is_destructor messages in
602+
match destructors with
603603
| [] ->
604-
""
604+
()
605605
| [x] ->
606-
let cut_message_name x = cut_msg_name (pascal_case x.msg_name) "Remove" in
607-
let asyncMessages =
608-
List.map cut_message_name (List.filter (fun x -> x.msg_async) messages)
606+
let json =
607+
`O
608+
[
609+
("type", `String (qualified_class_name classname))
610+
; ("wire_class_name", `String (exposed_class_name classname))
611+
; ("class_name", `String (ocaml_class_to_csharp_class classname))
612+
; ("property", `String (ocaml_class_to_csharp_property classname))
613+
; ("type_local", `String (ocaml_class_to_csharp_local_var classname))
614+
; ("async", `Bool x.msg_async)
615+
; ("has_uuid", `Bool (has_uuid obj))
616+
; ("has_name", `Bool (has_name obj))
617+
]
609618
in
610-
sprintf
611-
"%s\n\n\
612-
using System;\n\
613-
using System.Collections;\n\
614-
using System.Collections.Generic;\n\
615-
using System.Management.Automation;\n\
616-
using XenAPI;\n\n\
617-
namespace Citrix.XenServer.Commands\n\
618-
{\n\
619-
\ [Cmdlet(VerbsCommon.Remove, \"Xen%s\", SupportsShouldProcess = \
620-
true)]\n\
621-
\ [OutputType(typeof(%s))]%s\n\
622-
\ [OutputType(typeof(void))]\n\
623-
\ public class RemoveXen%s : XenServerCmdlet\n\
624-
\ {\n\
625-
\ #region Cmdlet Parameters\n\n\
626-
\ [Parameter]\n\
627-
\ public SwitchParameter PassThru { get; set; }\n\
628-
%s%s\n\
629-
\ #endregion\n\n\
630-
\ #region Cmdlet Methods\n\n\
631-
\ protected override void ProcessRecord()\n\
632-
\ {\n\
633-
\ GetSession();\n\n\
634-
\ string %s = Parse%s();\n\n\
635-
\ %s\n\n\
636-
\ UpdateSessions();\n\
637-
\ }\n\n\
638-
\ #endregion\n\n\
639-
\ #region Private Methods\n\
640-
%s%s\n\
641-
\ #endregion\n\
642-
\ }\n\
643-
}\n"
644-
Licence.bsd_two_clause
645-
(ocaml_class_to_csharp_class classname)
646-
(qualified_class_name classname)
647-
( if asyncMessages <> [] then
648-
"\n [OutputType(typeof(XenAPI.Task))]"
649-
else
650-
""
651-
)
652-
(ocaml_class_to_csharp_class classname)
653-
(print_xenobject_params obj classname true true true)
654-
( if asyncMessages <> [] then
655-
sprintf
656-
"\n\
657-
\ protected override bool GenerateAsyncParam\n\
658-
\ {\n\
659-
\ get { return true; }\n\
660-
\ }\n"
661-
else
662-
""
663-
)
664-
(ocaml_class_to_csharp_local_var classname)
665-
(ocaml_class_to_csharp_property classname)
666-
(print_cmdlet_methods_remover classname x)
667-
(print_parse_xenobject_private_method obj classname true)
668-
(print_process_record_private_methods classname messages "Remove"
669-
"asyncpassthru"
619+
render_file
620+
( "Remove-XenObject.mustache"
621+
, sprintf "Remove-Xen%s.cs" (ocaml_class_to_csharp_class classname)
670622
)
623+
json templdir destdir
671624
| _ ->
672625
assert false
673626

674-
and print_cmdlet_methods_remover classname message =
675-
let localVar = ocaml_class_to_csharp_local_var classname in
676-
let cut_message_name x = cut_msg_name (pascal_case x.msg_name) "Remove" in
677-
sprintf "ProcessRecord%s(%s);" (cut_message_name message) localVar
678-
679627
(*****************************************)
680628
(* Print function for Remove-XenFoo -Bar *)
681629
(*****************************************)

0 commit comments

Comments
 (0)