Skip to content

Commit

Permalink
Merge pull request #75 from max-ieremenko/release/1.4.4
Browse files Browse the repository at this point in the history
Release/1.4.4
  • Loading branch information
max-ieremenko authored Mar 12, 2022
2 parents ee20cb6 + 0869408 commit 7a83bd5
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Sources/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<CodeAnalysisRuleSet>..\StyleCope.ruleset</CodeAnalysisRuleSet>

<LangVersion>8.0</LangVersion>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
25 changes: 12 additions & 13 deletions Sources/ServiceModel.Grpc.DesignTime.Generator.Test/GrpcServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@

using ServiceModel.Grpc.TestApi.Domain;

namespace ServiceModel.Grpc.DesignTime.Generator.Test
namespace ServiceModel.Grpc.DesignTime.Generator.Test;

[ImportGrpcService(typeof(IMultipurposeService))]
[ExportGrpcService(typeof(IMultipurposeService))]
[ImportGrpcService(typeof(IContract))]
[ImportGrpcService(typeof(IGenericContract<int, string>))]
[ImportGrpcService(typeof(IGenericContract<string, int>))]
[ImportGrpcService(typeof(IInvalidContract))]
[ImportGrpcService(typeof(ISomeService))]
[ImportGrpcService(typeof(IFilteredService))]
[ExportGrpcService(typeof(TrackedFilteredService), GenerateAspNetExtensions = true, GenerateSelfHostExtensions = true)]
public static partial class GrpcServices
{
[ImportGrpcService(typeof(IMultipurposeService))]
[ExportGrpcService(typeof(IMultipurposeService))]
[ImportGrpcService(typeof(IContract))]
[ImportGrpcService(typeof(IGenericContract<int, string>))]
[ImportGrpcService(typeof(IGenericContract<string, int>))]
[ImportGrpcService(typeof(IInvalidContract))]
[ImportGrpcService(typeof(ISomeService))]
[ImportGrpcService(typeof(IFilteredService))]
[ExportGrpcService(typeof(TrackedFilteredService), GenerateAspNetExtensions = true, GenerateSelfHostExtensions = true)]
public static partial class GrpcServices
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using ServiceModel.Grpc.DesignTime.Generator.Internal.CSharp;

Expand Down Expand Up @@ -93,17 +94,15 @@ private void DeclareClass(ClassDeclarationSyntax node)
{
var owners = ImmutableArray<string>.Empty;

foreach (var ancestor in node.Ancestors())
foreach (var ancestor in node.AncestorMembers())
{
switch (ancestor)
if (ancestor.Kind == SyntaxKind.NamespaceDeclaration)
{
case NamespaceDeclarationSyntax ns:
owners = owners.Insert(0, "namespace " + ns.Name.WithoutTrivia());
break;

case ClassDeclarationSyntax c:
owners = owners.Insert(0, "partial class " + c.Identifier.WithoutTrivia());
break;
owners = owners.Insert(0, "namespace " + ancestor.Name);
}
else
{
owners = owners.Insert(0, "partial class " + ancestor.Name);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// <copyright>
// Copyright 2020 Max Ieremenko
// Copyright 2020-2022 Max Ieremenko
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,9 +15,12 @@
// </copyright>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace ServiceModel.Grpc.DesignTime.Generator
Expand All @@ -27,25 +30,10 @@ internal static class SyntaxFactoryExtensions
public static string GetFullName(this ClassDeclarationSyntax node)
{
var result = new StringBuilder(node.Identifier.WithoutTrivia().ToString());
foreach (var ancestor in node.Ancestors())
foreach (var ancestor in node.AncestorMembers())
{
string? name = null;
switch (ancestor)
{
case NamespaceDeclarationSyntax ns:
name = ns.Name.WithoutTrivia().ToString();
break;

case ClassDeclarationSyntax c:
name = c.Identifier.WithoutTrivia().ToString();
break;
}

if (!string.IsNullOrEmpty(name))
{
result.Insert(0, ".");
result.Insert(0, name);
}
result.Insert(0, ".");
result.Insert(0, ancestor.Name);
}

return result.ToString();
Expand All @@ -55,5 +43,48 @@ public static bool IsStatic(this ClassDeclarationSyntax node)
{
return node.Modifiers.Any(i => "static".Equals(i.ToString(), StringComparison.Ordinal));
}

public static IEnumerable<(SyntaxKind Kind, string Name)> AncestorMembers(this ClassDeclarationSyntax node)
{
foreach (var ancestor in node.Ancestors().OfType<MemberDeclarationSyntax>())
{
string? name = null;
var kind = default(SyntaxKind);

if (ancestor is NamespaceDeclarationSyntax ns)
{
kind = SyntaxKind.NamespaceDeclaration;
name = ns.Name.WithoutTrivia().ToString();
}
else if (ancestor is ClassDeclarationSyntax c)
{
kind = SyntaxKind.ClassDeclaration;
name = c.Identifier.WithoutTrivia().ToString();
}
else
{
var type = ancestor.GetType();

// see #73, more elegant fix requires roslyn4.0
// roslyn4.0 does not work on pure .netcore 3.1 sdk
// TODO: support roslyn4.0 and roslyn3.11
if ("FileScopedNamespaceDeclarationSyntax".Equals(type.Name, StringComparison.Ordinal))
{
var nameSyntax = (NameSyntax)ancestor
.GetType()
.GetProperty(nameof(NamespaceDeclarationSyntax.Name), BindingFlags.Public | BindingFlags.Instance)!
.GetValue(ancestor);

kind = SyntaxKind.NamespaceDeclaration;
name = nameSyntax.WithoutTrivia().ToString();
}
}

if (name != null)
{
yield return (kind, name);
}
}
}
}
}
2 changes: 1 addition & 1 deletion Sources/Versions.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<ServiceModelGrpcVersion>1.4.3</ServiceModelGrpcVersion>
<ServiceModelGrpcVersion>1.4.4</ServiceModelGrpcVersion>

<GrpcCoreVersion>2.43.0</GrpcCoreVersion>
<GrpcNetVersion>2.42.0</GrpcNetVersion>
Expand Down

0 comments on commit 7a83bd5

Please sign in to comment.