Esempio n. 1
0
        protected override async Task <string> RunIsolatedAsync(AssemblyLoader.AssemblyLoader assemblyLoader)
        {
            var document       = new OpenApiDocument();
            var generator      = new JsonSchemaGenerator(Settings);
            var schemaResolver = new OpenApiSchemaResolver(document, Settings);

#if FullNet
            var assemblies = PathUtilities.ExpandFileWildcards(AssemblyPaths)
                             .Select(path => Assembly.LoadFrom(path)).ToArray();
#else
            var currentDirectory = DynamicApis.DirectoryGetCurrentDirectory();
            var assemblies       = PathUtilities.ExpandFileWildcards(AssemblyPaths)
                                   .Select(path => assemblyLoader.Context.LoadFromAssemblyPath(PathUtilities.MakeAbsolutePath(path, currentDirectory))).ToArray();
#endif

            var allExportedClassNames = assemblies.SelectMany(a => a.ExportedTypes).Select(t => t.FullName).ToList();
            var matchedClassNames     = ClassNames
                                        .SelectMany(n => PathUtilities.FindWildcardMatches(n, allExportedClassNames, '.'))
                                        .Distinct();

            foreach (var className in matchedClassNames)
            {
                var type = assemblies.Select(a => a.GetType(className)).FirstOrDefault(t => t != null);
                generator.Generate(type, schemaResolver);
            }

            return(document.ToJson(OutputType));
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        public static OpenApiDocument GetOpenApiDocument(JsonSchemaGeneratorSettings settings, params string[] assemblyDllPaths)
        {
            string currentDirectory = DynamicApis.DirectoryGetCurrentDirectory();

            Assembly[] assemblies = PathUtilities.ExpandFileWildcards(assemblyDllPaths)
                                    .Select(path => Assembly.LoadFrom(PathUtilities.MakeAbsolutePath(path, currentDirectory))).ToArray();
            return(GetOpenApiDocument(settings, assemblies));
        }
Esempio n. 4
0
        protected Assembly[] LoadAssemblies(IEnumerable <string> assemblyPaths, AssemblyLoader.AssemblyLoader assemblyLoader)
        {
#if NETFRAMEWORK
            var assemblies = PathUtilities.ExpandFileWildcards(assemblyPaths)
                             .Select(path => Assembly.LoadFrom(path)).ToArray();
#else
            var currentDirectory = DynamicApis.DirectoryGetCurrentDirectory();
            var assemblies       = PathUtilities.ExpandFileWildcards(assemblyPaths)
                                   .Select(path => assemblyLoader.Context.LoadFromAssemblyPath(PathUtilities.MakeAbsolutePath(path, currentDirectory)))
                                   .ToArray();
#endif
            return(assemblies);
        }
        protected override async Task <string[]> RunIsolatedAsync(AssemblyLoader.AssemblyLoader assemblyLoader)
        {
#if FullNet
            return(PathUtilities.ExpandFileWildcards(AssemblyPaths)
                   .Select(Assembly.LoadFrom)
#else
            var currentDirectory = DynamicApis.DirectoryGetCurrentDirectory());

            return(PathUtilities.ExpandFileWildcards(AssemblyPaths)
                   .Select(p => assemblyLoader.Context.LoadFromAssemblyPath(PathUtilities.MakeAbsolutePath(p, currentDirectory)))
#endif
                   .SelectMany(WebApiOpenApiDocumentGenerator.GetControllerClasses)
                   .Select(t => t.FullName)
                   .OrderBy(c => c)
                   .ToArray());
        }
Esempio n. 6
0
        protected override Task <string[]> RunIsolatedAsync(AssemblyLoader.AssemblyLoader assemblyLoader)
        {
#if NETFRAMEWORK
            var result = PathUtilities.ExpandFileWildcards(AssemblyPaths)
                         .Select(Assembly.LoadFrom)
#else
            var currentDirectory = DynamicApis.DirectoryGetCurrentDirectory();
            var result = PathUtilities.ExpandFileWildcards(AssemblyPaths)
                         .Select(p => assemblyLoader.Context.LoadFromAssemblyPath(PathUtilities.MakeAbsolutePath(p, currentDirectory)))
#endif
                         .SelectMany(a => a.ExportedTypes)
                         .Select(t => t.FullName)
                         .OrderBy(c => c)
                         .ToArray();

            return(Task.FromResult(result));
        }
Esempio n. 7
0
        private Assembly TryLoadByPath(string assemblyName, string file)
        {
            try
            {
                if (!file.EndsWith("/refs/" + assemblyName + ".dll") &&
                    !file.EndsWith("\\refs\\" + assemblyName + ".dll"))
                {
                    var currentDirectory = DynamicApis.DirectoryGetCurrentDirectory();
                    return(LoadFromAssemblyPath(PathUtilities.MakeAbsolutePath(file, currentDirectory)));
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine("NSwag: AssemblyLoader exception when loading assembly by file '" + file + "': \n" + exception);
            }

            return(null);
        }
Esempio n. 8
0
        private static string[] GetNSwagPath(string[] args)
        {
            var files = new List <string>();

            Queue <string> queue = new Queue <string>(args);

            while (queue.Any())
            {
                var arg = queue.Dequeue();
                if (arg.StartsWith("-"))
                {
                    if (arg.Equals("-c", StringComparison.OrdinalIgnoreCase))
                    {
                        while (true)
                        {
                            if (!queue.Any())
                            {
                                break;
                            }

                            arg = queue.Dequeue();
                            if (arg.StartsWith("-"))
                            {
                                break;
                            }

                            var tmpPath = arg;
                            if (Path.IsPathRooted(tmpPath))
                            {
                                files.Add(tmpPath);
                                continue;
                            }

                            if (arg.StartsWith('.'))
                            {
                                tmpPath = Path.Combine(Directory.GetCurrentDirectory(), arg);
                                files.Add(tmpPath);
                                continue;
                            }

                            tmpPath = Path.Combine(Directory.GetCurrentDirectory(), arg);
                            files.Add(tmpPath);
                        }
                    }
                }
            }

            if (files.Any())
            {
                return(files.ToArray());
            }

            var currentDirectory = DynamicApis.DirectoryGetCurrentDirectory();

            Console.WriteLine(currentDirectory);
            files = DynamicApis.DirectoryGetFiles(currentDirectory, "*.nswag").ToList();
            if (files.Any())
            {
                return(files.ToArray());
            }

            currentDirectory = AppContext.BaseDirectory;
            Console.WriteLine(currentDirectory);
            files = DynamicApis.DirectoryGetFiles(currentDirectory, "*.nswag").ToList();
            if (files.Any())
            {
                return(files.ToArray());
            }

            currentDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            Console.WriteLine(currentDirectory);
            files = DynamicApis.DirectoryGetFiles(currentDirectory, "*.nswag").ToList();
            if (files.Any())
            {
                return(files.ToArray());
            }

            return(files.ToArray());
        }
Esempio n. 9
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);
            }
        }