Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added wildcard support #232

Merged
merged 1 commit into from
Aug 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/NSwag.AssemblyLoader/NSwag.AssemblyLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="SwaggerGenerators\AssemblyTypeToSwaggerGeneratorSettings.cs" />
<Compile Include="SwaggerGenerators\WebApi\WebApiAssemblyToSwaggerGenerator.cs" />
<Compile Include="SwaggerGenerators\WebApi\WebApiAssemblyToSwaggerGeneratorSettings.cs" />
<Compile Include="Utilities\PathUtilities.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\NJsonSchema\src\NJsonSchema\NJsonSchema.csproj">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Reflection;
using Newtonsoft.Json;
using NSwag.CodeGeneration.Infrastructure;
using NSwag.CodeGeneration.Utilities;

namespace NSwag.CodeGeneration.SwaggerGenerators.WebApi
{
Expand Down Expand Up @@ -124,7 +125,7 @@ internal string[] GetControllerClasses(string[] assemblyPaths, IEnumerable<strin
{
RegisterReferencePaths(referencePaths);

return assemblyPaths
return PathUtilities.ExpandFileWildcards(assemblyPaths)
.Select(Assembly.LoadFrom)
.SelectMany(WebApiToSwaggerGenerator.GetControllerClasses)
.Select(t => t.FullName)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string> ExpandWildcards(string path)
{
return ExpandWildcards(new[] { path });
}

public static IEnumerable<string> ExpandWildcards(IEnumerable<string> paths)
{
var allFiles = new List<string>();
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);
}

/// <exception cref="ArgumentException">The path of the two files doesn't have any common base.</exception>
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 file="PathUtilities.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace NSwag.CodeGeneration.Utilities
{
/// <summary>Provides file path utility methods.</summary>
public static class PathUtilities
{
// TODO: Move to MyToolkit

/// <summary>Expands the given wildcards (** or *) in the path.</summary>
/// <param name="path">The file path with wildcards.</param>
/// <returns>All expanded file paths.</returns>
public static IEnumerable<string> ExpandFileWildcards(string path)
{
return ExpandFileWildcards(new[] { path });
}

/// <summary>Expands the given wildcards (** or *) in the paths.</summary>
/// <param name="paths">The files path with wildcards.</param>
/// <returns>All expanded file paths.</returns>
public static IEnumerable<string> ExpandFileWildcards(IEnumerable<string> paths)
{
var allFiles = new List<string>();
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();
}

/// <summary>Converts a relative path to an absolute path.</summary>
/// <param name="relativePath">The relative path.</param>
/// <param name="relativeTo">The current directory.</param>
/// <returns>The absolute path.</returns>
public static string MakeAbsolutePath(string relativePath, string relativeTo)
{
var absolutePath = Path.Combine(relativePath, relativeTo);
return Path.GetFullPath(new Uri(absolutePath).LocalPath);
}

/// <summary>Converts an absolute path to a relative path if possible.</summary>
/// <param name="absolutePath">The absolute path.</param>
/// <param name="relativeTo">The current directory.</param>
/// <returns>The relative path.</returns>
/// <exception cref="ArgumentException">The path of the two files doesn't have any common base.</exception>
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();
}
}
}
14 changes: 4 additions & 10 deletions src/NSwag.Tests/Commands/WildcardTests.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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")));
}
}
}
4 changes: 4 additions & 0 deletions src/NSwag.Tests/NSwag.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
<Project>{7B7A2E32-E808-4A19-98B1-37E766580F8C}</Project>
<Name>NJsonSchema</Name>
</ProjectReference>
<ProjectReference Include="..\NSwag.AssemblyLoader\NSwag.AssemblyLoader.csproj">
<Project>{46634C60-BA7D-43E6-9049-6AD461488C39}</Project>
<Name>NSwag.AssemblyLoader</Name>
</ProjectReference>
<ProjectReference Include="..\NSwag.CodeGeneration\NSwag.CodeGeneration.csproj">
<Project>{75B3F91D-687E-4FB3-AD45-CCFA3C406DB4}</Project>
<Name>NSwag.CodeGeneration</Name>
Expand Down
1 change: 0 additions & 1 deletion src/NSwag/NSwag.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
<Compile Include="NSwagDocument.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utilities\PathUtilities.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="App.config">
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag/NSwagDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using NJsonSchema;
using NSwag.Commands;
using NSwag.Commands.Base;
using NSwag.Utilities;
using NSwag.CodeGeneration.Utilities;

namespace NSwag
{
Expand Down