-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from Cratis/fix/order-proxy-import
Order the proxy import statements
- Loading branch information
Showing
12 changed files
with
69 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
Source/DotNET/Tools/ProxyGenerator/Templates/ImportStatementExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Cratis. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Cratis.Applications.ProxyGenerator.Templates; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="ImportStatement"/>. | ||
/// </summary> | ||
public static class ImportStatementExtensions | ||
{ | ||
/// <summary> | ||
/// Converts the enumerable of <see cref="ImportStatement"/> to an ordered enumerable. | ||
/// </summary> | ||
/// <param name="imports">The imports.</param> | ||
/// <returns>The ordered enumerable.</returns> | ||
public static IOrderedEnumerable<ImportStatement> ToOrderedImports(this IEnumerable<ImportStatement> imports) => | ||
imports.Order(new ImportStatementComparer()); | ||
|
||
sealed class ImportStatementComparer : IComparer<ImportStatement> | ||
{ | ||
public int Compare(ImportStatement? x, ImportStatement? y) | ||
{ | ||
var moduleLeft = x?.Module; | ||
var moduleRight = y?.Module; | ||
|
||
if (moduleLeft == null || moduleRight == null) | ||
{ | ||
return 0; | ||
} | ||
|
||
if (!IsPathImport(moduleLeft) && !IsPathImport(moduleRight)) | ||
{ | ||
return string.Compare(moduleLeft, moduleLeft, StringComparison.OrdinalIgnoreCase); | ||
} | ||
if (!IsPathImport(moduleLeft)) | ||
{ | ||
return -1; | ||
} | ||
return !IsPathImport(moduleRight) | ||
? 1 | ||
: string.Compare(GetPathImportFilename(moduleLeft), GetPathImportFilename(moduleRight), StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
static bool IsPathImport(string module) => module.StartsWith("./") || module.StartsWith("../"); | ||
static string GetPathImportFilename(string module) => module.Split('/')[^1]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters