Beispiel #1
0
            private async Task <string> FromAssemblyTypeAsync(string[] classNames, string settingsData)
            {
                var document = new SwaggerDocument();
                var settings = JsonConvert.DeserializeObject <AssemblyTypeToSwaggerGeneratorSettings>(settingsData);

                RegisterReferencePaths(GetAllReferencePaths(settings));

                var generator      = new JsonSchemaGenerator(settings);
                var schemaResolver = new SwaggerSchemaResolver(document, settings);

#if FullNet
                var assemblies = PathUtilities.ExpandFileWildcards(settings.AssemblySettings.AssemblyPaths)
                                 .Select(path => Assembly.LoadFrom(path)).ToArray();
#else
                var currentDirectory = await DynamicApis.DirectoryGetCurrentDirectoryAsync().ConfigureAwait(false);

                var assemblies = PathUtilities.ExpandFileWildcards(settings.AssemblySettings.AssemblyPaths)
                                 .Select(path => 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);
                    await generator.GenerateAsync(type, schemaResolver).ConfigureAwait(false);
                }

                return(document.ToJson());
            }
        protected override async Task <string> RunIsolatedAsync(AssemblyLoader.AssemblyLoader assemblyLoader)
        {
            var document       = new SwaggerDocument();
            var generator      = new JsonSchemaGenerator(Settings);
            var schemaResolver = new SwaggerSchemaResolver(document, Settings);

#if FullNet
            var assemblies = PathUtilities.ExpandFileWildcards(AssemblyPaths)
                             .Select(path => Assembly.LoadFrom(path)).ToArray();
#else
            var currentDirectory = await DynamicApis.DirectoryGetCurrentDirectoryAsync().ConfigureAwait(false);

            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);
                await generator.GenerateAsync(type, schemaResolver).ConfigureAwait(false);
            }

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

                if (hasNSwagJson)
                {
                    await ExecuteDocumentAsync(host, "nswag.json");
                }

                var currentDirectory = await DynamicApis.DirectoryGetCurrentDirectoryAsync().ConfigureAwait(false);

                var files = await DynamicApis.DirectoryGetFilesAsync(currentDirectory, "*.nswag").ConfigureAwait(false);

                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);
        }
Beispiel #4
0
        private Assembly TryLoadByName(List <string> allReferencePaths, string assemblyName)
        {
            foreach (var path in allReferencePaths)
            {
                var files = Directory.GetFiles(path, assemblyName + ".dll", SearchOption.TopDirectoryOnly);
                foreach (var file in files)
                {
                    try
                    {
#if FullNet
                        return(Assembly.LoadFrom(file));
#else
                        var currentDirectory = DynamicApis.DirectoryGetCurrentDirectoryAsync().GetAwaiter().GetResult();
                        return(Context.LoadFromAssemblyPath(PathUtilities.MakeAbsolutePath(file, currentDirectory)));
#endif
                    }
                    catch (Exception exception)
                    {
                        Debug.WriteLine("NSwag: AssemblyLoader exception when loading assembly by file '" + file + "': \n" + exception);
                    }
                }
            }

            return(null);
        }
Beispiel #5
0
        protected void RegisterReferencePaths(IEnumerable <string> referencePaths)
        {
#if FullNet
            var allReferencePaths = new List <string>(GetAllDirectories(AppDomain.CurrentDomain.SetupInformation.ApplicationBase));
#else
            var allReferencePaths = new List <string>();
#endif

            foreach (var path in referencePaths.Where(p => !string.IsNullOrWhiteSpace(p)))
            {
                allReferencePaths.Add(path);
                allReferencePaths.AddRange(GetAllDirectories(path));
            }

            // Add path to nswag directory
            allReferencePaths.Add(Path.GetDirectoryName(typeof(AssemblyLoader).GetTypeInfo().Assembly.CodeBase.Replace("file:///", string.Empty)));
            allReferencePaths = allReferencePaths.Distinct().ToList();

#if FullNet
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
#else
            Context.Resolving += (context, args) =>
#endif
            {
                var separatorIndex = args.Name.IndexOf(",", StringComparison.Ordinal);
                var assemblyName   = separatorIndex > 0 ? args.Name.Substring(0, separatorIndex) : args.Name;

#if FullNet
                var existingAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == assemblyName);
                if (existingAssembly != null)
                {
                    return(existingAssembly);
                }
#endif

                foreach (var path in allReferencePaths)
                {
                    var files = Directory.GetFiles(path, assemblyName + ".dll", SearchOption.TopDirectoryOnly);
                    foreach (var file in files)
                    {
                        try
                        {
#if FullNet
                            return(Assembly.LoadFrom(file));
#else
                            var currentDirectory = DynamicApis.DirectoryGetCurrentDirectoryAsync().GetAwaiter().GetResult();
                            return(Context.LoadFromAssemblyPath(PathUtilities.MakeAbsolutePath(file, currentDirectory)));
#endif
                        }
                        catch (Exception exception)
                        {
                            Debug.WriteLine("AssemblyLoader.AssemblyResolve exception when loading DLL '" + file + "': \n" + exception.ToString());
                        }
                    }
                }

                return(null);
            };
        }
            internal string[] GetExportedClassNames(string assemblyPath, IEnumerable <string> referencePaths)
            {
                RegisterReferencePaths(referencePaths);

#if FullNet
                var assembly = Assembly.LoadFrom(assemblyPath);
#else
                var currentDirectory = DynamicApis.DirectoryGetCurrentDirectoryAsync().GetAwaiter().GetResult();
                var assembly         = Context.LoadFromAssemblyPath(PathUtilities.MakeAbsolutePath(assemblyPath, currentDirectory));
#endif

                return(GetExportedClassNames(assembly));
            }
        protected async Task <Assembly[]> LoadAssembliesAsync(IEnumerable <string> assemblyPaths, AssemblyLoader.AssemblyLoader assemblyLoader)
        {
#if FullNet
            var assemblies = PathUtilities.ExpandFileWildcards(assemblyPaths)
                             .Select(path => Assembly.LoadFrom(path)).ToArray();
#else
            var currentDirectory = await DynamicApis.DirectoryGetCurrentDirectoryAsync().ConfigureAwait(false);

            var assemblies = PathUtilities.ExpandFileWildcards(assemblyPaths)
                             .Select(path => assemblyLoader.Context.LoadFromAssemblyPath(PathUtilities.MakeAbsolutePath(path, currentDirectory)))
                             .ToArray();
#endif
            return(assemblies);
        }
        private string[] GetControllerNames(AssemblyLoader.AssemblyLoader assemblyLoader)
        {
#if FullNet
            return(PathUtilities.ExpandFileWildcards(AssemblyPaths)
                   .Select(Assembly.LoadFrom)
#else
            var currentDirectory = DynamicApis.DirectoryGetCurrentDirectoryAsync().GetAwaiter().GetResult());

            return(PathUtilities.ExpandFileWildcards(AssemblyPaths)
                   .Select(p => assemblyLoader.Context.LoadFromAssemblyPath(PathUtilities.MakeAbsolutePath(p, currentDirectory)))
#endif
                   .SelectMany(WebApiToSwaggerGenerator.GetControllerClasses)
                   .Select(t => t.FullName)
                   .OrderBy(c => c)
                   .ToArray());
        }
Beispiel #9
0
        protected override async Task <string[]> RunIsolatedAsync(AssemblyLoader.AssemblyLoader assemblyLoader)
        {
#if FullNet
            return(PathUtilities.ExpandFileWildcards(AssemblyPaths)
                   .Select(Assembly.LoadFrom)
#else
            var currentDirectory = DynamicApis.DirectoryGetCurrentDirectoryAsync().GetAwaiter().GetResult());

            return(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());
        }
        private async Task <IEnumerable <Type> > GetControllerTypesAsync(IEnumerable <string> controllerNames, AssemblyLoader.AssemblyLoader assemblyLoader)
#pragma warning restore 1998
        {
            if (AssemblyPaths == null || AssemblyPaths.Length == 0)
            {
                throw new InvalidOperationException("No assembly paths have been provided.");
            }

#if FullNet
            var assemblies = PathUtilities.ExpandFileWildcards(AssemblyPaths)
                             .Select(path => Assembly.LoadFrom(path)).ToArray();
#else
            var currentDirectory = await DynamicApis.DirectoryGetCurrentDirectoryAsync().ConfigureAwait(false);

            var assemblies = PathUtilities.ExpandFileWildcards(AssemblyPaths)
                             .Select(path => assemblyLoader.Context.LoadFromAssemblyPath(PathUtilities.MakeAbsolutePath(path, currentDirectory))).ToArray();
#endif

            var allExportedNames       = assemblies.SelectMany(a => a.ExportedTypes).Select(t => t.FullName).ToList();
            var matchedControllerNames = controllerNames
                                         .SelectMany(n => PathUtilities.FindWildcardMatches(n, allExportedNames, '.'))
                                         .Distinct();

            var controllerNamesWithoutWildcard = controllerNames.Where(n => !n.Contains("*")).ToArray();
            if (controllerNamesWithoutWildcard.Any(n => !matchedControllerNames.Contains(n)))
            {
                throw new TypeLoadException("Unable to load type for controllers: " + string.Join(", ", controllerNamesWithoutWildcard));
            }

            var controllerTypes = new List <Type>();
            foreach (var className in matchedControllerNames)
            {
                var controllerType = assemblies.Select(a => a.GetType(className)).FirstOrDefault(t => t != null);
                if (controllerType != null)
                {
                    controllerTypes.Add(controllerType);
                }
                else
                {
                    throw new TypeLoadException("Unable to load type for controller: " + className);
                }
            }

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

            return(null);
        }
Beispiel #12
0
            internal string[] GetExportedClassNames(string[] assemblyPaths, IEnumerable <string> referencePaths)
            {
                RegisterReferencePaths(referencePaths);

#if FullNet
                return(PathUtilities.ExpandFileWildcards(assemblyPaths)
                       .Select(Assembly.LoadFrom)
#else
                var currentDirectory = DynamicApis.DirectoryGetCurrentDirectoryAsync().GetAwaiter().GetResult());

                return(PathUtilities.ExpandFileWildcards(assemblyPaths)
                       .Select(p => Context.LoadFromAssemblyPath(PathUtilities.MakeAbsolutePath(p, currentDirectory)))
#endif
                       .SelectMany(a => a.ExportedTypes)
                       .Select(t => t.FullName)
                       .OrderBy(c => c)
                       .ToArray());
            }