Beispiel #1
0
        public static void GenerateClientAPIs(string webRootPath, CodeGenSettings settings, Fonlow.Web.Meta.WebApiDescription[] apiDescriptions)
        {
            if (!string.IsNullOrWhiteSpace(settings.ClientApiOutputs.ClientLibraryProjectFolderName))
            {
                string clientProjectDir = System.IO.Path.Combine(webRootPath, "..", settings.ClientApiOutputs.ClientLibraryProjectFolderName);
                if (!System.IO.Directory.Exists(clientProjectDir))
                {
                    throw new CodeGenException("ClientLibraryProjectFolderNotExist")
                          {
                              Description = $"{clientProjectDir} not exist."
                          }
                }
                ;

                var path = System.IO.Path.Combine(clientProjectDir, "WebApiClientAuto.cs");
                var gen  = new Fonlow.CodeDom.Web.Cs.ControllersClientApiGen(settings);
                gen.ForBothAsyncAndSync = settings.ClientApiOutputs.GenerateBothAsyncAndSync;
                gen.CreateCodeDom(apiDescriptions);
                gen.Save(path);
            }


            Func <string, string, string> CreateTsPath = (folder, fileName) =>
            {
                if (!string.IsNullOrEmpty(folder))
                {
                    string theFolder;
                    try
                    {
                        theFolder = System.IO.Path.IsPathRooted(folder) ?
                                    folder : System.IO.Path.Combine(webRootPath, folder);
                    }
                    catch (System.ArgumentException e)
                    {
                        System.Diagnostics.Trace.TraceWarning(e.Message);
                        throw new CodeGenException("InvalidTypeScriptFolder")
                              {
                                  Description = $"Invalid TypeScriptFolder {folder}"
                              };
                    }

                    if (!System.IO.Directory.Exists(theFolder))
                    {
                        throw new CodeGenException("TypeScriptFolderNotExist")
                              {
                                  Description = $"TypeScriptFolder {theFolder} not exist."
                              };
                    }
                    return(System.IO.Path.Combine(theFolder, fileName));
                }
                ;

                return(null);
            };


            var jQueryPath = CreateTsPath(settings.ClientApiOutputs.TypeScriptJQFolder, settings.ClientApiOutputs.TypeScriptJQFile);

            if (!String.IsNullOrEmpty(jQueryPath))
            {
                var jQueryOutput = new JSOutput(settings, jQueryPath, false);
                var tsGen        = new Fonlow.CodeDom.Web.Ts.ControllersTsClientApiGen(jQueryOutput);
                tsGen.CreateCodeDom(apiDescriptions);
                tsGen.Save();
            }

            var ng2Path = CreateTsPath(settings.ClientApiOutputs.TypeScriptNG2Folder, settings.ClientApiOutputs.TypeScriptNG2File);

            if (!String.IsNullOrEmpty(ng2Path))
            {
                var ng2Output = new JSOutput(settings, ng2Path, true);
                var tsGen     = new Fonlow.CodeDom.Web.Ts.ControllersTsNG2ClientApiGen(ng2Output);
                tsGen.CreateCodeDom(apiDescriptions);
                tsGen.Save();
            }
        }
    }
Beispiel #2
0
        public static void GenerateClientAPIs(string webRootPath, CodeGenSettings settings, WebApiDescription[] webApiDescriptions)
        {
            if (webRootPath == null)            //Run the .net core web through dotnet may have IHostingEnvironment.WebRootPath==null
            {
                webRootPath = "";
            }

            var currentDir = System.IO.Directory.GetCurrentDirectory();

            if (!string.IsNullOrWhiteSpace(settings.ClientApiOutputs.ClientLibraryProjectFolderName))
            {
                string csharpClientProjectDir = System.IO.Path.IsPathRooted(settings.ClientApiOutputs.ClientLibraryProjectFolderName) ?
                                                settings.ClientApiOutputs.ClientLibraryProjectFolderName : System.IO.Path.Combine(webRootPath, settings.ClientApiOutputs.ClientLibraryProjectFolderName);

                if (!System.IO.Directory.Exists(csharpClientProjectDir))
                {
                    throw new CodeGenException("Client Library Project Folder Not Exist")
                          {
                              Description = $"{csharpClientProjectDir} not exist while current directory is {currentDir}"
                          }
                }
                ;

                var path = System.IO.Path.Combine(csharpClientProjectDir, settings.ClientApiOutputs.FileName);
                var gen  = new Cs.ControllersClientApiGen(settings);
                gen.ForBothAsyncAndSync = settings.ClientApiOutputs.GenerateBothAsyncAndSync;
                gen.CreateCodeDom(webApiDescriptions);
                gen.Save(path);
            }


            Func <string, string, string> CreateTsPath = (folder, fileName) =>
            {
                if (!string.IsNullOrEmpty(folder))
                {
                    string theFolder;
                    try
                    {
                        theFolder = System.IO.Path.IsPathRooted(folder) ?
                                    folder : System.IO.Path.Combine(webRootPath, folder);
                    }
                    catch (ArgumentException e)
                    {
                        System.Diagnostics.Trace.TraceWarning(e.Message);
                        throw new CodeGenException("Invalid TypeScript Folder")
                              {
                                  Description = $"Invalid TypeScriptFolder {folder} while current directory is {currentDir}"
                              };
                    }

                    if (!System.IO.Directory.Exists(theFolder))
                    {
                        throw new CodeGenException("TypeScript Folder Not Exist")
                              {
                                  Description = $"TypeScriptFolder {theFolder} not exist while current directory is {currentDir}"
                              };
                    }
                    return(System.IO.Path.Combine(theFolder, fileName));
                }
                ;

                return(null);
            };

            if (settings.ClientApiOutputs.Plugins != null)
            {
                foreach (var plugin in settings.ClientApiOutputs.Plugins)
                {
                    var jsOutput = new JSOutput
                    {
                        CamelCase      = settings.ClientApiOutputs.CamelCase,
                        JSPath         = CreateTsPath(plugin.TargetDir, plugin.TSFile),
                        AsModule       = plugin.AsModule,
                        ContentType    = plugin.ContentType,
                        StringAsString = settings.ClientApiOutputs.StringAsString,

                        ApiSelections             = settings.ApiSelections,
                        ClientNamespaceSuffix     = plugin.ClientNamespaceSuffix,
                        ContainerNameSuffix       = plugin.ContainerNameSuffix,
                        DataAnnotationsToComments = plugin.DataAnnotationsToComments,
                    };

                    var tsGen = PluginFactory.CreateImplementationsFromAssembly(plugin.AssemblyName, jsOutput);
                    if (tsGen != null)
                    {
                        tsGen.CreateCodeDom(webApiDescriptions);
                        tsGen.Save();
                    }
                    else
                    {
                        var s = $"Cannot instantiate plugin {plugin.AssemblyName}. Please check if the plugin assembly is in place.";
                        System.Diagnostics.Trace.TraceError(s);
                        throw new CodeGenException(s);
                    }
                }
            }
        }
    }
Beispiel #3
0
        public static void GenerateClientAPIs(Settings settings, OpenApiPaths paths, OpenApiComponents components, string outputBasePath)
        {
            string currentDir = System.IO.Directory.GetCurrentDirectory();

            if (settings.ClientLibraryProjectFolderName != null)
            {
                string csharpClientProjectDir = System.IO.Path.IsPathRooted(settings.ClientLibraryProjectFolderName) ?
                                                settings.ClientLibraryProjectFolderName : System.IO.Path.Combine(outputBasePath, settings.ClientLibraryProjectFolderName);

                if (!System.IO.Directory.Exists(csharpClientProjectDir))
                {
                    if (settings.CreateFolder)
                    {
                        System.IO.Directory.CreateDirectory(csharpClientProjectDir);
                    }
                    else
                    {
                        string msg = $"{csharpClientProjectDir} not exist while current directory is {currentDir}";
                        throw new CodeGenException(msg);
                    }
                }

                string path = System.IO.Path.Combine(csharpClientProjectDir, settings.ClientLibraryFileName);
                ControllersClientApiGen gen = new ControllersClientApiGen(settings);
                gen.CreateCodeDom(paths, components);
                gen.Save(path);
            }

            string CreateTsPath(string folder, string fileName)
            {
                if (folder != null)
                {
                    string theFolder;
                    try
                    {
                        theFolder = System.IO.Path.IsPathRooted(folder) ?
                                    folder : System.IO.Path.Combine(outputBasePath, folder);

                        if (!System.IO.Directory.Exists(theFolder))
                        {
                            if (settings.CreateFolder)
                            {
                                System.IO.Directory.CreateDirectory(theFolder);
                            }
                            else
                            {
                                string msg = $"{theFolder} not exist while current directory is {currentDir}";
                                throw new CodeGenException(msg);
                            }
                        }
                    }
                    catch (ArgumentException e)
                    {
                        Trace.TraceWarning(e.Message);
                        string msg = $"Invalid TypeScriptFolder {folder} while current directory is {currentDir}";
                        throw new CodeGenException(msg);
                    }

                    if (!System.IO.Directory.Exists(theFolder))
                    {
                        string msg = $"TypeScriptFolder {theFolder} not exist while current directory is {currentDir}";
                        throw new CodeGenException(msg);
                    }
                    return(System.IO.Path.Combine(theFolder, fileName));
                }
                ;

                return(null);
            }

            if (settings.Plugins != null)
            {
                string exeDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                foreach (JSPlugin plugin in settings.Plugins)
                {
                    JSOutput jsOutput = new JSOutput
                    {
                        JSPath      = CreateTsPath(plugin.TargetDir, plugin.TSFile),
                        AsModule    = plugin.AsModule,
                        ContentType = plugin.ContentType,
                    };

                    string assemblyFilePath = System.IO.Path.Combine(exeDir, plugin.AssemblyName + ".dll");
                    Ts.ControllersTsClientApiGenBase tsGen = PluginFactory.CreateImplementationsFromAssembly(assemblyFilePath, settings, jsOutput);
                    if (tsGen != null)
                    {
                        Trace.TraceInformation($"Generate codes with {tsGen.ProductName} ......");
                        tsGen.CreateCodeDom(paths, components);
                        tsGen.Save();
                    }
                    else
                    {
                        Trace.TraceWarning($"Not done with plugin {plugin.AssemblyName}");
                    }
                }
            }
        }
        /// <summary>
        /// Load the first ICommand type found in the assembly and instantiate it.
        /// </summary>
        /// <param name="assemblyName">The assembly must have a concrete class derived from ICommand, and generally from CommandBase, CommandWithOptions or CommandWithParametersAndOptions;
        /// and the class must have a constructor without parameter that calls a base constructor with proper options type and parameters type.</param>
        /// <param name="jsOutput"></param>
        /// <returns>ICommand object. Null if not found</returns>
        public static ControllersTsClientApiGenBase CreateImplementationsFromAssembly(string assemblyName, JSOutput jsOutput)
        {
            Assembly assembly = null;

            try
            {
                assembly = Assembly.Load(assemblyName);
                Trace.TraceInformation("Assembly {0} is loaded for type {1}.", assemblyName, "ICommand");
            }
            catch (System.IO.FileLoadException e)
            {
                Trace.TraceWarning(String.Format("When loading plugin {0}, FileLoadException: {1}", assemblyName, e.Message));
                return(null);
            }
            catch (BadImageFormatException e)
            {
                Trace.TraceWarning(String.Format("When loading plugin {0}, BadImageFormatException: {1}", assemblyName, e.Message));
                //when file is a win32 dll.
                return(null);
            }
            catch (System.IO.FileNotFoundException e)
            {
                Trace.TraceWarning(String.Format("When loading plugin {0}, FileNotFoundException: {1}", assemblyName, e.Message));
                return(null);
            }
            catch (ArgumentException e)
            {
                Trace.TraceWarning(String.Format("When loading plugin {0}, ArgumentException: {1}", assemblyName, e.Message));
                return(null);
            }

            ControllersTsClientApiGenBase controllersTsClientApiGen = null;

            try
            {
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.IsClass && type.IsSubclassOf(typeof(ControllersTsClientApiGenBase)))
                    {
                        controllersTsClientApiGen = (ControllersTsClientApiGenBase)Activator.CreateInstance(type, jsOutput);
                        break;
                    }
                }

                if (controllersTsClientApiGen == null)
                {
                    throw new ArgumentException($"Cannot find derived class of ControllersTsClientApiGenBase from {assemblyName}");
                }

                return(controllersTsClientApiGen);
            }
            catch (ReflectionTypeLoadException e)
            {
                foreach (Exception ex in e.LoaderExceptions)
                {
                    Trace.TraceWarning(String.Format("When loading plugin {0}, GetTypes errors occur: {1}", assemblyName, ex.Message));
                }
            }
            catch (TargetInvocationException e)
            {
                Trace.TraceWarning(String.Format("When loading plugin {0}, GetTypes errors occur: {1}", assemblyName, e.Message + "~~" + e.InnerException.Message));
            }

            return(null);
        }