aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/Program.cs
blob: 501058016df77724f45a055e6dfc7636546296ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Pro
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.CodeGeneration;
using Tango.Core.Helpers;
using Tango.Transport.Web;
using Tango.Web.Security;
using Tango.Web.Controllers;

namespace Tango.WebClientGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            //Generate PPC client.
            //GenerateWebClient<PPC.Common.Web.LoginRequest, PPC.Common.Web.LoginResponse, MachineService.Controllers.PPCController>("Tango.PPC.Common.Web", "PPCWebClientBase", PathHelper.GetSolutionFolder() + @"\PPC\Tango.PPC.Common\Web");

            //Generate Machine Studio client.
            //GenerateWebClient<MachineStudio.Common.Web.LoginRequest, MachineStudio.Common.Web.LoginResponse, MachineService.Controllers.MachineStudioController>("Tango.MachineStudio.Common.Web", "MachineStudioWebClientBase", PathHelper.GetSolutionFolder() + @"\MachineStudio\Tango.MachineStudio.Common\Web");

            //Generate FSE client.
            GenerateWebClientV2<FSE.Web.Messages.LoginRequest, FSE.Web.Messages.LoginResponse, MachineService.Controllers.FSEController>("Tango.FSE.BL.Web", "FSEWebClientBase", PathHelper.GetSolutionFolder() + @"\FSE\Tango.FSE.BL\Web");

            Console.WriteLine("Done");
        }

        private static void GenerateWebClient<TLoginRequest, TLoginResponse, TController>(String nameSpace, String name, String path) where TLoginRequest : WebRequestMessage where TLoginResponse : WebTokenResponse where TController : TangoController
        {
            TangoWebClientCodeFile model = new TangoWebClientCodeFile();
            model.Namespace = nameSpace;
            model.ControllerName = typeof(TController).Name.Replace("Controller", "");
            model.Name = name;
            model.LoginRequest = typeof(TLoginRequest).FullName;
            model.LoginResponse = typeof(TLoginResponse).FullName;

            foreach (var action in typeof(TController).GetMethods().Where(x => typeof(WebResponseMessage).IsAssignableFrom(x.ReturnType) && x.Name != "Login"))
            {
                model.Actions.Add(new TangoWebClientCodeFile.ControllerAction()
                {
                    Name = action.Name,
                    Request = action.GetParameters()[0].ParameterType.FullName,
                    Response = action.ReturnType.FullName,
                });
            }

            String code = model.GenerateCode();
            File.WriteAllText(Path.Combine(path, name + ".cs"), code);
        }

        private static void GenerateWebClientV2<TLoginRequest, TLoginResponse, TController>(String nameSpace, String name, String path) where TLoginRequest : WebRequestMessage where TLoginResponse : WebTokenResponse where TController : TangoController
        {
            TangoWebClientv2CodeFile model = new TangoWebClientv2CodeFile();
            model.Namespace = nameSpace;
            model.ControllerName = typeof(TController).Name.Replace("Controller", "");
            model.Name = name;
            model.LoginRequest = typeof(TLoginRequest).FullName;
            model.LoginResponse = typeof(TLoginResponse).FullName;

            foreach (var action in typeof(TController).GetMethods().Where(x => typeof(WebResponseMessage).IsAssignableFrom(x.ReturnType) && x.Name != "Login"))
            {
                model.Actions.Add(new TangoWebClientv2CodeFile.ControllerAction()
                {
                    Name = action.Name,
                    Request = action.GetParameters()[0].ParameterType.FullName,
                    Response = action.ReturnType.FullName,
                });
            }

            String code = model.GenerateCode();
            File.WriteAllText(Path.Combine(path, name + ".cs"), code);
        }
    }
}