Exemplo n.º 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();
            }
        }
    }
Exemplo n.º 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);
                    }
                }
            }
        }
    }