Beispiel #1
0
        public async Task <object> RunAsync(CommandLineProcessor processor, IConsoleHost host)
        {
            if (!string.IsNullOrEmpty(Input) && !Input.StartsWith("/") && !Input.StartsWith("-"))
            {
                await ExecuteDocumentAsync(host, Input);
            }
            else
            {
                var hasNSwagJson = DynamicApis.FileExists("nswag.json");
                if (hasNSwagJson)
                {
                    await ExecuteDocumentAsync(host, "nswag.json");
                }

                var currentDirectory = DynamicApis.DirectoryGetCurrentDirectory();
                var files            = DynamicApis.DirectoryGetFiles(currentDirectory, "*.nswag");
                if (files.Any())
                {
                    foreach (var file in files)
                    {
                        await ExecuteDocumentAsync(host, file);
                    }
                }
                else if (!hasNSwagJson)
                {
                    host.WriteMessage("Current directory does not contain any .nswag files.");
                }
            }
            return(null);
        }
        public static Task <bool> TryWriteFileOutputAsync(this IOutputCommand command, string path, IConsoleHost host, NewLineBehavior newLineBehavior, Func <string> generator)
        {
            if (!string.IsNullOrEmpty(path))
            {
                var directory = DynamicApis.PathGetDirectoryName(path);
                if (!string.IsNullOrEmpty(directory) && DynamicApis.DirectoryExists(directory) == false)
                {
                    DynamicApis.DirectoryCreateDirectory(directory);
                }

                var data = generator();

                data = data?.Replace("\r", "") ?? "";
                data = newLineBehavior == NewLineBehavior.Auto ? data.Replace("\n", Environment.NewLine) :
                       newLineBehavior == NewLineBehavior.CRLF ? data.Replace("\n", "\r\n") : data;

                if (!DynamicApis.FileExists(path) || DynamicApis.FileReadAllText(path) != data)
                {
                    DynamicApis.FileWriteAllText(path, data);

                    host?.WriteMessage("Code has been successfully written to file.\n");
                }
                else
                {
                    host?.WriteMessage("Code has been successfully generated but not written to file (no change detected).\n");
                }
                return(Task.FromResult(true));
            }
            return(Task.FromResult(false));
        }
Beispiel #3
0
        public async Task <SwaggerDocument> RunAsync()
        {
            return(await Task.Run(() =>
            {
                if (!string.IsNullOrEmpty(DocumentTemplate))
                {
                    if (DynamicApis.FileExists(DocumentTemplate))
                    {
                        Settings.DocumentTemplate = DynamicApis.FileReadAllText(DocumentTemplate);
                    }
                    else
                    {
                        Settings.DocumentTemplate = DocumentTemplate;
                    }
                }
                else
                {
                    Settings.DocumentTemplate = null;
                }

                var generator = CreateGenerator();
                var controllerNames = ControllerNames.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList();
                if (!controllerNames.Any() && Settings.AssemblyPaths?.Length > 0)
                {
                    controllerNames = generator.GetControllerClasses().ToList();
                }

                var document = generator.GenerateForControllers(controllerNames);

                if (ServiceHost == ".")
                {
                    document.Host = string.Empty;
                }
                else if (!string.IsNullOrEmpty(ServiceHost))
                {
                    document.Host = ServiceHost;
                }

                if (ServiceSchemes != null && ServiceSchemes.Any())
                {
                    document.Schemes = ServiceSchemes.Select(s => (SwaggerSchema)Enum.Parse(typeof(SwaggerSchema), s, true)).ToList();
                }

                if (!string.IsNullOrEmpty(ServiceBasePath))
                {
                    document.BasePath = ServiceBasePath;
                }

                return document;
            }));
        }
        public async Task <string> RunAsync()
        {
            return(await Task.Run(() =>
            {
                var additionalCode = ExtensionCode ?? string.Empty;
                if (DynamicApis.FileExists(additionalCode))
                {
                    additionalCode = DynamicApis.FileReadAllText(additionalCode);
                }
                Settings.TypeScriptGeneratorSettings.ExtensionCode = additionalCode;

                var clientGenerator = new SwaggerToTypeScriptClientGenerator(InputSwaggerDocument, Settings);
                return clientGenerator.GenerateFile();
            }));
        }
Beispiel #5
0
        private static XDocument TryGetXmlDocsDocument(AssemblyName assemblyName, string pathToXmlFile)
        {
            if (!Cache.ContainsKey(assemblyName.FullName))
            {
                if (DynamicApis.FileExists(pathToXmlFile) == false)
                {
                    Cache[assemblyName.FullName] = null;
                    return(null);
                }

                Cache[assemblyName.FullName] = XDocument.Load(pathToXmlFile, LoadOptions.PreserveWhitespace);
            }

            return(Cache[assemblyName.FullName]);
        }
        public async Task <object> RunAsync(CommandLineProcessor processor, IConsoleHost host)
        {
            if (!DynamicApis.FileExists("nswag.json"))
            {
                await CreateDocumentAsync("nswag.json");

                host.WriteMessage("nswag.json file created.");
            }
            else
            {
                host.WriteMessage("nswag.json already exists.");
            }

            return(null);
        }
        public async Task <string> RunAsync()
        {
            var additionalCode = ExtensionCode ?? string.Empty;

            if (DynamicApis.FileExists(additionalCode))
            {
                additionalCode = DynamicApis.FileReadAllText(additionalCode);
            }

            Settings.TypeScriptGeneratorSettings.ExtensionCode = additionalCode;

            var document = await GetInputSwaggerDocument().ConfigureAwait(false);

            var clientGenerator = new TypeScriptClientGenerator(document, Settings);

            return(clientGenerator.GenerateFile());
        }
Beispiel #8
0
        /// <exception cref="ArgumentException">The argument 'Input' was empty.</exception>
        protected async Task <JsonSchema> GetJsonSchemaAsync()
        {
            var input = Input.ToString();

            if (string.IsNullOrEmpty(input))
            {
                throw new ArgumentException("The argument 'Input' was empty.");
            }

            if (IsJson(input))
            {
                return(await JsonSchema.FromJsonAsync(input).ConfigureAwait(false));
            }

            if (DynamicApis.FileExists(input))
            {
                return(await JsonSchema.FromFileAsync(input).ConfigureAwait(false));
            }

            return(await JsonSchema.FromUrlAsync(input).ConfigureAwait(false));
        }
Beispiel #9
0
        private static CachingXDocument?TryGetXmlDocsDocument(AssemblyName assemblyName, string?pathToXmlFile)
        {
            if (Cache.TryGetValue(assemblyName.FullName, out var document))
            {
                return(document);
            }

            if (pathToXmlFile is null)
            {
                return(null);
            }

            if (DynamicApis.FileExists(pathToXmlFile) == false)
            {
                Cache[assemblyName.FullName] = null;
                return(null);
            }

            document = new CachingXDocument(pathToXmlFile);
            Cache[assemblyName.FullName] = document;

            return(document);
        }
Beispiel #10
0
        public static async Task <bool> TryWriteFileOutputAsync(this IOutputCommand command, string path, IConsoleHost host, Func <string> generator)
        {
            if (!string.IsNullOrEmpty(path))
            {
                var directory = DynamicApis.PathGetDirectoryName(path);
                if (!string.IsNullOrEmpty(directory) && DynamicApis.DirectoryExists(directory) == false)
                {
                    DynamicApis.DirectoryCreateDirectory(directory);
                }

                var data = generator();
                if (!DynamicApis.FileExists(path) || DynamicApis.FileReadAllText(path) != data)
                {
                    DynamicApis.FileWriteAllText(path, data);
                    host?.WriteMessage("Code has been successfully written to file.\n");
                }
                else
                {
                    host?.WriteMessage("Code has been successfully generated but not written to file (no change detected).\n");
                }
                return(true);
            }
            return(false);
        }
Beispiel #11
0
        private static string GetXmlDocsPath(dynamic assembly)
        {
            string path;

            try
            {
                if (assembly == null)
                {
                    return(null);
                }

                var assemblyName = assembly.GetName();
                if (string.IsNullOrEmpty(assemblyName.Name))
                {
                    return(null);
                }

                if (Cache.ContainsKey(assemblyName.FullName))
                {
                    return(null);
                }

                if (!string.IsNullOrEmpty(assembly.Location))
                {
                    var assemblyDirectory = DynamicApis.PathGetDirectoryName((string)assembly.Location);
                    path = DynamicApis.PathCombine(assemblyDirectory, (string)assemblyName.Name + ".xml");
                    if (DynamicApis.FileExists(path))
                    {
                        return(path);
                    }
                }

                if (ObjectExtensions.HasProperty(assembly, "CodeBase"))
                {
                    var codeBase = (string)assembly.CodeBase;
                    if (!string.IsNullOrEmpty(codeBase))
                    {
                        path = DynamicApis.PathCombine(DynamicApis.PathGetDirectoryName(codeBase
                                                                                        .Replace("file:///", string.Empty)), assemblyName.Name + ".xml")
                               .Replace("file:\\", string.Empty);

                        if (DynamicApis.FileExists(path))
                        {
                            return(path);
                        }
                    }
                }

                var currentDomain = Type.GetType("System.AppDomain")?.GetRuntimeProperty("CurrentDomain").GetValue(null);
                if (currentDomain?.HasProperty("BaseDirectory") == true)
                {
                    var baseDirectory = currentDomain.TryGetPropertyValue("BaseDirectory", "");
                    if (!string.IsNullOrEmpty(baseDirectory))
                    {
                        path = DynamicApis.PathCombine(baseDirectory, assemblyName.Name + ".xml");
                        if (DynamicApis.FileExists(path))
                        {
                            return(path);
                        }

                        return(DynamicApis.PathCombine(baseDirectory, "bin\\" + assemblyName.Name + ".xml"));
                    }
                }

                var currentDirectory = DynamicApis.DirectoryGetCurrentDirectory();
                path = DynamicApis.PathCombine(currentDirectory, assembly.GetName().Name + ".xml");
                if (DynamicApis.FileExists(path))
                {
                    return(path);
                }

                path = DynamicApis.PathCombine(currentDirectory, "bin\\" + assembly.GetName().Name + ".xml");
                if (DynamicApis.FileExists(path))
                {
                    return(path);
                }

                return(null);
            }
            catch
            {
                return(null);
            }
        }