Skip to content

Commit

Permalink
Merge pull request #161 from Cratis/fix/order-proxy-import
Browse files Browse the repository at this point in the history
Order the proxy import statements
  • Loading branch information
einari authored Sep 2, 2024
2 parents 74a42f5 + 1fd0f6b commit 8ea747b
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 19 deletions.
6 changes: 3 additions & 3 deletions Samples/eCommerce/Basic/Web/API/Carts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

/* eslint-disable sort-imports */
// eslint-disable-next-line header/header
export * from './CartItem';
export * from './CartForCurrentUser';
export * from './ObserveCartForCurrentUser';
export * from './AddItem';
export * from './Cart';
export * from './CartForCurrentUser';
export * from './CartItem';
export * from './ObserveCartForCurrentUser';
8 changes: 4 additions & 4 deletions Samples/eCommerce/Basic/Web/API/Products/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

/* eslint-disable sort-imports */
// eslint-disable-next-line header/header
export * from './Product';
export * from './SetStockForProduct';
export * from './SetPrice';
export * from './AddProduct';
export * from './ObserveAllProducts';
export * from './AllProducts';
export * from './ObserveAllProducts';
export * from './Product';
export * from './SetPrice';
export * from './SetStockForProduct';
2 changes: 1 addition & 1 deletion Source/DotNET/Tools/ProxyGenerator/CommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static CommandDescriptor ToCommandDescriptor(this MethodInfo method, stri
route.MakeRouteTemplate(),
method.Name,
properties,
imports,
imports.OrderBy(_ => _.Module),
method.GetArgumentDescriptors(),
hasResponse,
responseModel,
Expand Down
7 changes: 5 additions & 2 deletions Source/DotNET/Tools/ProxyGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ public static async Task<bool> Generate(string assemblyFile, string outputPath,
var directoriesWithContent = directories.Distinct().Select(_ => new DirectoryInfo(_));
foreach (var directory in directoriesWithContent)
{
var exports = directory.GetFiles("*.ts").Select(_ => $"./{Path.GetFileNameWithoutExtension(_.Name)}");
var exports = directory
.GetFiles("*.ts")
.Select(_ => $"./{Path.GetFileNameWithoutExtension(_.Name)}")
.OrderBy(_ => _.Split('/')[^1]);
var descriptor = new IndexDescriptor(exports);
var content = TemplateTypes.Index(descriptor);
await File.WriteAllTextAsync(Path.Join(directory.FullName, "index.ts"), content);
Expand All @@ -99,4 +102,4 @@ public static async Task<bool> Generate(string assemblyFile, string outputPath,

return true;
}
}
}
2 changes: 1 addition & 1 deletion Source/DotNET/Tools/ProxyGenerator/QueryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static QueryDescriptor ToQueryDescriptor(this MethodInfo method, string t
responseModel.Constructor,
responseModel.IsEnumerable,
responseModel.IsObservable,
imports,
imports.ToOrderedImports(),
method.GetArgumentDescriptors(),
propertyDescriptors,
[.. typesInvolved, .. additionalTypesInvolved]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public record CommandDescriptor(
string RouteTemplate,
string Name,
IEnumerable<PropertyDescriptor> Properties,
IEnumerable<ImportStatement> Imports,
IOrderedEnumerable<ImportStatement> Imports,
IEnumerable<RequestArgumentDescriptor> Arguments,
bool HasResponse,
ModelDescriptor ResponseType,
Expand Down
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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ namespace Cratis.Applications.ProxyGenerator.Templates;
/// Describes what should be available in the index file for a module.
/// </summary>
/// <param name="Exports">Files to export.</param>
public record IndexDescriptor(IEnumerable<string> Exports);
public record IndexDescriptor(IOrderedEnumerable<string> Exports);
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public record ModelDescriptor(
string Constructor,
bool IsEnumerable,
bool IsObservable,
IEnumerable<ImportStatement> Imports)
IOrderedEnumerable<ImportStatement> Imports)
{
/// <summary>
/// Represents an empty <see cref="ModelDescriptor"/>.
/// </summary>
public static readonly ModelDescriptor Empty = new(null!, string.Empty, string.Empty, false, false, []);
public static readonly ModelDescriptor Empty = new(null!, string.Empty, string.Empty, false, false, Enumerable.Empty<ImportStatement>().OrderBy(_ => _.Module));
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public record QueryDescriptor(
string Constructor,
bool IsEnumerable,
bool IsObservable,
IEnumerable<ImportStatement> Imports,
IOrderedEnumerable<ImportStatement> Imports,
IEnumerable<RequestArgumentDescriptor> Arguments,
IEnumerable<PropertyDescriptor> Properties,
IEnumerable<Type> TypesInvolved) : IDescriptor;
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public record TypeDescriptor(
Type Type,
string Name,
IEnumerable<PropertyDescriptor> Properties,
IEnumerable<ImportStatement> Imports,
IOrderedEnumerable<ImportStatement> Imports,
IEnumerable<Type> TypesInvolved) : IDescriptor;
4 changes: 2 additions & 2 deletions Source/DotNET/Tools/ProxyGenerator/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public static TypeDescriptor ToTypeDescriptor(this Type type, string targetPath,
type,
type.GetTargetType().Type,
propertyDescriptors,
imports,
imports.ToOrderedImports(),
typesInvolved);
}

Expand Down Expand Up @@ -343,7 +343,7 @@ public static ModelDescriptor ToModelDescriptor(this Type type)
targetType.Constructor,
isEnumerable,
isSubject || isAsyncEnumerable,
[]);
Enumerable.Empty<ImportStatement>().ToOrderedImports());
}

/// <summary>
Expand Down

0 comments on commit 8ea747b

Please sign in to comment.