diff --git a/src/NSwag.AssemblyLoader/NSwag.AssemblyLoader.csproj b/src/NSwag.AssemblyLoader/NSwag.AssemblyLoader.csproj index f479253f25..c730db46ae 100644 --- a/src/NSwag.AssemblyLoader/NSwag.AssemblyLoader.csproj +++ b/src/NSwag.AssemblyLoader/NSwag.AssemblyLoader.csproj @@ -57,6 +57,7 @@ + diff --git a/src/NSwag.AssemblyLoader/SwaggerGenerators/WebApi/WebApiAssemblyToSwaggerGenerator.cs b/src/NSwag.AssemblyLoader/SwaggerGenerators/WebApi/WebApiAssemblyToSwaggerGenerator.cs index 70ea7c2bf7..929ebb0603 100644 --- a/src/NSwag.AssemblyLoader/SwaggerGenerators/WebApi/WebApiAssemblyToSwaggerGenerator.cs +++ b/src/NSwag.AssemblyLoader/SwaggerGenerators/WebApi/WebApiAssemblyToSwaggerGenerator.cs @@ -13,6 +13,7 @@ using System.Reflection; using Newtonsoft.Json; using NSwag.CodeGeneration.Infrastructure; +using NSwag.CodeGeneration.Utilities; namespace NSwag.CodeGeneration.SwaggerGenerators.WebApi { @@ -124,7 +125,7 @@ internal string[] GetControllerClasses(string[] assemblyPaths, IEnumerable t.FullName) diff --git a/src/NSwag/Utilities/PathUtilities.cs b/src/NSwag.AssemblyLoader/Utilities/PathUtilities.cs similarity index 50% rename from src/NSwag/Utilities/PathUtilities.cs rename to src/NSwag.AssemblyLoader/Utilities/PathUtilities.cs index 70c178ab7c..05be06e3a3 100644 --- a/src/NSwag/Utilities/PathUtilities.cs +++ b/src/NSwag.AssemblyLoader/Utilities/PathUtilities.cs @@ -1,109 +1,134 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Text.RegularExpressions; - -namespace NSwag.Utilities -{ - public static class PathUtilities - { - // TODO: Move to MyToolkit - - public static IEnumerable ExpandWildcards(string path) - { - return ExpandWildcards(new[] { path }); - } - - public static IEnumerable ExpandWildcards(IEnumerable paths) - { - var allFiles = new List(); - foreach (var path in paths) - { - if (path.Contains("*")) - { - var starIndex = path.IndexOf("*", StringComparison.InvariantCulture); - - var rootIndex = path.IndexOf("\\", 0, starIndex, StringComparison.InvariantCulture); - if (rootIndex == -1) - rootIndex = path.IndexOf("/", 0, starIndex, StringComparison.InvariantCulture); - - var rootPath = rootIndex >= 0 ? path.Substring(0, rootIndex + 1) : Directory.GetCurrentDirectory(); - var files = Directory.GetFiles(rootPath, "*", SearchOption.AllDirectories); - - var regex = new Regex( - Regex.Escape(path.Substring(rootIndex + 1) - .Replace("/", "__del__") - .Replace("\\", "__del__") - .Replace("**", "__starstar__") - .Replace("*", "__star__")) - .Replace("__del__", "[\\\\/]") - .Replace("__starstar__", "(.*?)") - .Replace("__star__", "([^\\/]*?)")); - - allFiles.AddRange(files - .Where(f => regex.Match(f).Success) - .Select(Path.GetFullPath)); - } - else - allFiles.Add(path); - } - - return allFiles.Distinct(); - } - - public static string MakeAbsolutePath(string relativePath, string relTo) - { - var absolutePath = System.IO.Path.Combine(relativePath, relTo); - return System.IO.Path.GetFullPath(new Uri(absolutePath).LocalPath); - } - - /// The path of the two files doesn't have any common base. - public static string MakeRelativePath(string absPath, string relTo) - { - string[] absParts = absPath.Split(System.IO.Path.DirectorySeparatorChar); - string[] relParts = relTo.Split(System.IO.Path.DirectorySeparatorChar); - - // Get the shortest of the two paths - int len = absParts.Length < relParts.Length ? absParts.Length : relParts.Length; - - // Use to determine where in the loop we exited - int lastCommonRoot = -1; - int index; - - // Find common root - for (index = 0; index < len; index++) - { - if (absParts[index].Equals(relParts[index], StringComparison.OrdinalIgnoreCase)) - lastCommonRoot = index; - else - break; - } - - // If we didn't find a common prefix then throw - if (lastCommonRoot == -1) - return absPath; - - // Build up the relative path - var relativePath = new StringBuilder(); - - // Add on the .. - for (index = lastCommonRoot + 1; index < relParts.Length; index++) - { - relativePath.Append(".."); - relativePath.Append(System.IO.Path.DirectorySeparatorChar); - } - - // Add on the folders - for (index = lastCommonRoot + 1; index < absParts.Length - 1; index++) - { - relativePath.Append(absParts[index]); - relativePath.Append(System.IO.Path.DirectorySeparatorChar); - } - relativePath.Append(absParts[absParts.Length - 1]); - - return relativePath.ToString(); - } - } -} +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/NSwag/NSwag/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace NSwag.CodeGeneration.Utilities +{ + /// Provides file path utility methods. + public static class PathUtilities + { + // TODO: Move to MyToolkit + + /// Expands the given wildcards (** or *) in the path. + /// The file path with wildcards. + /// All expanded file paths. + public static IEnumerable ExpandFileWildcards(string path) + { + return ExpandFileWildcards(new[] { path }); + } + + /// Expands the given wildcards (** or *) in the paths. + /// The files path with wildcards. + /// All expanded file paths. + public static IEnumerable ExpandFileWildcards(IEnumerable paths) + { + var allFiles = new List(); + foreach (var path in paths) + { + if (path.Contains("*")) + { + var starIndex = path.IndexOf("*", StringComparison.InvariantCulture); + + var rootIndex = path.Substring(0, starIndex).LastIndexOf("\\", StringComparison.InvariantCulture); + if (rootIndex == -1) + rootIndex = path.Substring(0, starIndex).LastIndexOf("/", StringComparison.InvariantCulture); + + var rootPath = rootIndex >= 0 ? path.Substring(0, rootIndex + 1) : Directory.GetCurrentDirectory(); + var files = Directory.GetFiles(rootPath, "*", SearchOption.AllDirectories); + + var regex = new Regex( + "^" + + Regex.Escape(path//.Substring(rootIndex + 1) + .Replace("**/", "__starstar__") + .Replace("**\\", "__starstar__") + .Replace("/", "__del__") + .Replace("\\", "__del__") + .Replace("*", "__star__")) + .Replace("__del__", "([\\\\/])") + .Replace("__starstar__", "((.*?)[/\\\\])") + .Replace("__star__", "([^\\/]*?)") + "$"); + + allFiles.AddRange(files + .Where(f => regex.Match(f).Success) + .Select(Path.GetFullPath)); + } + else + allFiles.Add(path); + } + + return allFiles.Distinct(); + } + + /// Converts a relative path to an absolute path. + /// The relative path. + /// The current directory. + /// The absolute path. + public static string MakeAbsolutePath(string relativePath, string relativeTo) + { + var absolutePath = Path.Combine(relativePath, relativeTo); + return Path.GetFullPath(new Uri(absolutePath).LocalPath); + } + + /// Converts an absolute path to a relative path if possible. + /// The absolute path. + /// The current directory. + /// The relative path. + /// The path of the two files doesn't have any common base. + public static string MakeRelativePath(string absolutePath, string relativeTo) + { + string[] absParts = absolutePath.Split(System.IO.Path.DirectorySeparatorChar); + string[] relParts = relativeTo.Split(System.IO.Path.DirectorySeparatorChar); + + // Get the shortest of the two paths + int len = absParts.Length < relParts.Length ? absParts.Length : relParts.Length; + + // Use to determine where in the loop we exited + int lastCommonRoot = -1; + int index; + + // Find common root + for (index = 0; index < len; index++) + { + if (absParts[index].Equals(relParts[index], StringComparison.OrdinalIgnoreCase)) + lastCommonRoot = index; + else + break; + } + + // If we didn't find a common prefix then throw + if (lastCommonRoot == -1) + return absolutePath; + + // Build up the relative path + var relativePath = new StringBuilder(); + + // Add on the .. + for (index = lastCommonRoot + 1; index < relParts.Length; index++) + { + relativePath.Append(".."); + relativePath.Append(System.IO.Path.DirectorySeparatorChar); + } + + // Add on the folders + for (index = lastCommonRoot + 1; index < absParts.Length - 1; index++) + { + relativePath.Append(absParts[index]); + relativePath.Append(System.IO.Path.DirectorySeparatorChar); + } + relativePath.Append(absParts[absParts.Length - 1]); + + return relativePath.ToString(); + } + } +} diff --git a/src/NSwag.Tests/Commands/WildcardTests.cs b/src/NSwag.Tests/Commands/WildcardTests.cs index 03407f3560..39701fa133 100644 --- a/src/NSwag.Tests/Commands/WildcardTests.cs +++ b/src/NSwag.Tests/Commands/WildcardTests.cs @@ -1,11 +1,6 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; -using NSwag.Utilities; +using NSwag.CodeGeneration.Utilities; namespace NSwag.Tests.Commands { @@ -19,11 +14,10 @@ public void When_path_has_wildcards_then_they_are_expanded_correctly() //// Act - var x = Directory.GetCurrentDirectory(); - var files = PathUtilities.ExpandWildcards("../../**/*.dll"); + var files = PathUtilities.ExpandFileWildcards("../../**/NSwag.*.dll").ToList(); //// Assert - + Assert.IsTrue(files.Any(f => f.Contains("bin\\Debug")) || files.Any(f => f.Contains("bin\\Release"))); } } } diff --git a/src/NSwag.Tests/NSwag.Tests.csproj b/src/NSwag.Tests/NSwag.Tests.csproj index 039ddeb468..362bb86c85 100644 --- a/src/NSwag.Tests/NSwag.Tests.csproj +++ b/src/NSwag.Tests/NSwag.Tests.csproj @@ -80,6 +80,10 @@ {7B7A2E32-E808-4A19-98B1-37E766580F8C} NJsonSchema + + {46634C60-BA7D-43E6-9049-6AD461488C39} + NSwag.AssemblyLoader + {75B3F91D-687E-4FB3-AD45-CCFA3C406DB4} NSwag.CodeGeneration diff --git a/src/NSwag/NSwag.csproj b/src/NSwag/NSwag.csproj index b78f21d3d2..339ebe3e1e 100644 --- a/src/NSwag/NSwag.csproj +++ b/src/NSwag/NSwag.csproj @@ -71,7 +71,6 @@ - diff --git a/src/NSwag/NSwagDocument.cs b/src/NSwag/NSwagDocument.cs index 4e01c36b2a..ecac39f8f5 100644 --- a/src/NSwag/NSwagDocument.cs +++ b/src/NSwag/NSwagDocument.cs @@ -16,7 +16,7 @@ using NJsonSchema; using NSwag.Commands; using NSwag.Commands.Base; -using NSwag.Utilities; +using NSwag.CodeGeneration.Utilities; namespace NSwag {