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));
        }
Beispiel #2
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());
            }
Beispiel #3
0
            /// <exception cref="InvalidOperationException">No assembly paths have been provided.</exception>
            private IEnumerable <Type> GetControllerTypes(IEnumerable <string> controllerClassNames, WebApiAssemblyToSwaggerGeneratorSettings settings)
            {
                if (settings.AssemblyPaths == null || settings.AssemblyPaths.Length == 0)
                {
                    throw new InvalidOperationException("No assembly paths have been provided.");
                }

#if FullNet
                var assemblies = PathUtilities.ExpandFileWildcards(settings.AssemblyPaths)
                                 .Select(path => Assembly.LoadFrom(path)).ToArray();
#else
                var assemblies = PathUtilities.ExpandFileWildcards(settings.AssemblyPaths)
                                 .Select(path => Context.LoadFromAssemblyPath(path)).ToArray();
#endif

                var controllerTypes = new List <Type>();
                foreach (var className in controllerClassNames)
                {
                    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);
            }
Beispiel #4
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));
        }
Beispiel #5
0
        //[Fact]
        public void When_path_has_wildcards_then_they_are_expanded_correctly()
        {
            //// Arrange


            //// Act
            var files = PathUtilities.ExpandFileWildcards("../../**/NSwag.*.dll").ToList();

            //// Assert
            Assert.True(files.Any(f => f.Contains("bin\\Debug")) || files.Any(f => f.Contains("bin\\Release")));
        }
            internal string[] GetControllerClasses(string[] assemblyPaths, IEnumerable <string> referencePaths)
            {
                RegisterReferencePaths(referencePaths);

                return(PathUtilities.ExpandFileWildcards(assemblyPaths)
                       .Select(Context.LoadFromAssemblyPath)
                       .SelectMany(WebApiToSwaggerGenerator.GetControllerClasses)
                       .Select(t => t.FullName)
                       .OrderBy(c => c)
                       .ToArray());
            }
Beispiel #7
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 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());
        }
        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());
        }
Beispiel #11
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);
        }
Beispiel #13
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());
            }
Beispiel #14
0
        protected async Task <TResult> RunIsolatedAsync(string configurationFile)
        {
            var assemblyDirectory = AssemblyPaths.Any() ? Path.GetDirectoryName(Path.GetFullPath(PathUtilities.ExpandFileWildcards(AssemblyPaths).First())) : configurationFile;
            var bindingRedirects  = GetBindingRedirects();
            var assemblies        = GetAssemblies(assemblyDirectory);

            if (UseNuGetCache)
            {
                var defaultNugetPackages = LoadDefaultNugetCache();
                ReferencePaths = ReferencePaths.Concat(defaultNugetPackages).ToArray();
            }

            using (var isolated = new AppDomainIsolation <IsolatedCommandAssemblyLoader>(assemblyDirectory, AssemblyConfig, bindingRedirects, assemblies))
            {
                return(await Task.Run(() => isolated.Object.Run(GetType().FullName, JsonConvert.SerializeObject(this), AssemblyPaths, ReferencePaths)).ConfigureAwait(false));
            }
        }