Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OIL] Templatization of PS Remove-Xen* cmdlets. Refactoring of HTTP_actions.mustache #5554

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
)
)
Comment on lines 168 to +197
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe below is little better:

let of_arg arg =
    `O
      [
        ("arg_decl", `String (decl_of_sdkarg arg))
      ; ("arg_use", `String (use_of_sdkarg arg))
      ]
  let of_action (name, (meth, uri, _, sdkargs, _, _)) =
    `O
      [
        ("name", `String name)
      ; ("isPut", `Bool (meth == Put))
      ; ("isGet", `Bool (meth == Get))
      ; ("uri", `String uri)
      ; ("args", `A (List.map of_arg sdkargs))
      ]
   in
  `O [( "http_actions", `A (List.map of_action 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
102 changes: 25 additions & 77 deletions ocaml/sdk-gen/powershell/gen_powershell_binding.ml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ let rec main () =
let cmdlets =
classes |> List.filter generated |> List.map gen_cmdlets |> List.concat
in
List.iter (fun x -> write_file x.filename x.content) cmdlets
List.iter (fun x -> write_file x.filename x.content) cmdlets ;

classes |> List.filter generated |> List.iter gen_destructor
Comment on lines 100 to +105
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let cmdlets =
classes |> List.filter generated |> List.map gen_cmdlets |> List.concat
in
List.iter (fun x -> write_file x.filename x.content) cmdlets
List.iter (fun x -> write_file x.filename x.content) cmdlets ;
classes |> List.filter generated |> List.iter gen_destructor
let classes_filterd = List.filter generated classes in
let cmdlets =
classes_filterd |> List.map gen_cmdlets |> List.concat
in
List.iter (fun x -> write_file x.filename x.content) cmdlets ;
List.iter gen_destructor classes_filterd

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List.concat_map can be used to generate cmdlets as well


(****************)
(* 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 List.length asyncMessages > 0 then
"\n [OutputType(typeof(XenAPI.Task))]"
else
""
)
(ocaml_class_to_csharp_class classname)
(print_xenobject_params obj classname true true true)
( if List.length asyncMessages > 0 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