Skip to content

Commit

Permalink
Merge pull request #81 from max-ieremenko/release/1.4.6
Browse files Browse the repository at this point in the history
Release/1.4.6
  • Loading branch information
max-ieremenko authored Apr 3, 2022
2 parents 5258b07 + 997d0fe commit 5c2d54d
Show file tree
Hide file tree
Showing 33 changed files with 527 additions and 321 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void BeforeAllTests()

protected override MethodInfo GetClientInstanceMethod(string name)
{
return base.GetClientInstanceMethod("ServiceModel.Grpc.TestApi.Domain.IGenericContract<System.Int32,System.String>." + name);
return base.GetClientInstanceMethod("global::ServiceModel.Grpc.TestApi.Domain.IGenericContract<System.Int32,System.String>." + name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public void BeforeAllTests()

protected override MethodInfo GetClientInstanceMethod(string name)
{
return base.GetClientInstanceMethod(typeof(IContract).FullName + "." + name);
return base.GetClientInstanceMethod("global::" + typeof(IContract).FullName + "." + name);
}

protected override MethodInfo GetClientInstanceMethod(string name, params Type[] parameters)
{
return base.GetClientInstanceMethod(typeof(IContract).FullName + "." + name, parameters);
return base.GetClientInstanceMethod("global::" + typeof(IContract).FullName + "." + name, parameters);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
// </copyright>

using System;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

namespace ServiceModel.Grpc.DesignTime.Generator
{
Expand Down Expand Up @@ -46,11 +48,25 @@ public void Execute(GeneratorExecutionContext context)

var outputContext = new GeneratorContext(
context.Compilation,
context.ReportDiagnostic,
context.CancellationToken,
context.AddSource);
new ExecutionContext(context));
new CSharpSourceGenerator().Execute(outputContext, candidates);
}
}

private sealed class ExecutionContext : IExecutionContext
{
private readonly GeneratorExecutionContext _context;

public ExecutionContext(GeneratorExecutionContext context)
{
_context = context;
}

public CancellationToken CancellationToken => _context.CancellationToken;

public void ReportDiagnostic(Diagnostic diagnostic) => _context.ReportDiagnostic(diagnostic);

public void AddSource(string hintName, SourceText sourceText) => _context.AddSource(hintName, sourceText);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
// </copyright>

using System.Collections.Immutable;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;

namespace ServiceModel.Grpc.DesignTime.Generator
{
Expand Down Expand Up @@ -54,12 +56,26 @@ private static void Execute(

var outputContext = new GeneratorContext(
source.Compilation,
context.ReportDiagnostic,
context.CancellationToken,
context.AddSource);
new ExecutionContext(context));
new CSharpSourceGenerator().Execute(outputContext, source.Candidates);
}
}
}

private sealed class ExecutionContext : IExecutionContext
{
private readonly SourceProductionContext _context;

public ExecutionContext(SourceProductionContext context)
{
_context = context;
}

public CancellationToken CancellationToken => _context.CancellationToken;

public void ReportDiagnostic(Diagnostic diagnostic) => _context.ReportDiagnostic(diagnostic);

public void AddSource(string hintName, SourceText sourceText) => _context.AddSource(hintName, sourceText);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ private static IEnumerable<int> GetNonBuildInMessages(ContractDescription contra

private static void AddPropertiesCount(SortedSet<int> target, MessageDescription? message)
{
// see ServiceModel.Grpc.Channel.Message.tt
var length = message?.Properties.Length ?? 0;
if (length > 3)
if (message != null && !message.IsBuiltIn)
{
target.Add(length);
target.Add(message.Properties.Length);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using ServiceModel.Grpc.Channel;
using ServiceModel.Grpc.Configuration;

namespace ServiceModel.Grpc.DesignTime.Generator
{
Expand Down Expand Up @@ -54,26 +50,11 @@ public void Execute(GeneratorContext context, IEnumerable<ClassDeclarationSyntax
}
}

private static ICollection<string> CreateDefaultUsing()
{
return new HashSet<string>(StringComparer.Ordinal)
{
typeof(Func<>).Namespace,
typeof(IEnumerable<>).Namespace,
typeof(CancellationToken).Namespace,
typeof(Task).Namespace,
"Grpc.Core",
typeof(IMarshallerFactory).Namespace,
typeof(Message).Namespace
};
}

private void InvokeGenerator(GeneratorContext context, ICodeGeneratorFactory factory, ClassDeclarationSyntax node)
{
var generatedCount = 0;

CompilationUnit unit = default;
ICollection<string> imports = null!;

foreach (var generator in factory.GetGenerators())
{
Expand All @@ -84,14 +65,12 @@ private void InvokeGenerator(GeneratorContext context, ICodeGeneratorFactory fac
if (generatedCount == 0)
{
unit = new CompilationUnit(node);
imports = CreateDefaultUsing();
}
else
{
unit.Output.AppendLine();
}

generator.AddUsing(imports);
generator.GenerateMemberDeclaration(unit.Output);

generatedCount++;
Expand All @@ -101,7 +80,7 @@ private void InvokeGenerator(GeneratorContext context, ICodeGeneratorFactory fac
context.CancellationToken.ThrowIfCancellationRequested();
if (generatedCount > 0)
{
var source = unit.GetSourceText(imports);
var source = unit.GetSourceText();
context.AddOutput(node, factory.GetHintName(), source);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public bool TryResolve(
return false;
}

var fullName = SyntaxTools.GetFullName(attributeData.AttributeClass);
var fullName = attributeData.AttributeClass.ToDisplayString(NullableFlowState.None);
var isImport = false;
if ("ServiceModel.Grpc.DesignTime.ImportGrpcServiceAttribute".Equals(fullName, StringComparison.Ordinal))
{
Expand Down
37 changes: 23 additions & 14 deletions Sources/ServiceModel.Grpc.DesignTime.Shared/CompilationUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,32 @@ namespace ServiceModel.Grpc.DesignTime.Generator
{
internal readonly ref struct CompilationUnit
{
private readonly IList<IDisposable> _indentation;
private readonly IDisposable[] _indentation;

public CompilationUnit(ClassDeclarationSyntax node)
{
Output = new CodeStringBuilder();
_indentation = new List<IDisposable>();

DeclareClass(node);
AddComment(Output);
AddUsing(Output);
_indentation = DeclareClass(node, Output);
}

public CodeStringBuilder Output { get; }

public string GetSourceText(IEnumerable<string> imports)
public string GetSourceText()
{
for (var i = 0; i < _indentation.Count; i++)
for (var i = 0; i < _indentation.Length; i++)
{
_indentation[i].Dispose();
Output.AppendLine("}");
}

var text = Output.AsStringBuilder();

InsertImports(text, imports);
InsertComment(text);

return text.ToString();
}

private static void InsertComment(StringBuilder text)
private static void AddComment(CodeStringBuilder output)
{
var comment = @"// ------------------------------------------------------------------------------
// <auto-generated>
Expand All @@ -71,7 +68,16 @@ private static void InsertComment(StringBuilder text)
.Replace("\r\n", "\n")
.Replace("\n", Environment.NewLine);

text.Insert(0, comment);
output.Append(comment);
}

private static void AddUsing(CodeStringBuilder output)
{
output.AppendLine("using System;");
output.AppendLine("using System.Collections.Generic;");
output.AppendLine("using System.Threading;");
output.AppendLine("using System.Threading.Tasks;");
output.AppendLine();
}

private static void InsertImports(StringBuilder text, IEnumerable<string> imports)
Expand All @@ -84,7 +90,7 @@ private static void InsertImports(StringBuilder text, IEnumerable<string> import
}
}

private void DeclareClass(ClassDeclarationSyntax node)
private static IDisposable[] DeclareClass(ClassDeclarationSyntax node, CodeStringBuilder output)
{
var owners = ImmutableArray<string>.Empty;

Expand All @@ -102,14 +108,17 @@ private void DeclareClass(ClassDeclarationSyntax node)

owners = owners.Add("partial class " + node.Identifier.WithoutTrivia());

var result = new IDisposable[owners.Length];
for (var i = 0; i < owners.Length; i++)
{
Output
output
.AppendLine(owners[i])
.AppendLine("{");

_indentation.Add(Output.Indent());
result[i] = output.Indent();
}

return result;
}
}
}
17 changes: 6 additions & 11 deletions Sources/ServiceModel.Grpc.DesignTime.Shared/GeneratorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,29 @@ namespace ServiceModel.Grpc.DesignTime.Generator
{
internal sealed class GeneratorContext
{
private readonly Action<Diagnostic> _reportDiagnostic;
private readonly Action<string, SourceText> _addSource;
private readonly IExecutionContext _executionContext;
private readonly HashSet<string> _generatedNames;

public GeneratorContext(
Compilation compilation,
Action<Diagnostic> reportDiagnostic,
CancellationToken cancellationToken,
Action<string, SourceText> addSource)
IExecutionContext executionContext)
{
_executionContext = executionContext;
Compilation = compilation;
CancellationToken = cancellationToken;
_reportDiagnostic = reportDiagnostic;
_addSource = addSource;
_generatedNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
}

public CancellationToken CancellationToken { get; }
public CancellationToken CancellationToken => _executionContext.CancellationToken;

public Compilation Compilation { get; }

public void ReportDiagnostic(Diagnostic diagnostic) => _reportDiagnostic(diagnostic);
public void ReportDiagnostic(Diagnostic diagnostic) => _executionContext.ReportDiagnostic(diagnostic);

public void AddOutput(ClassDeclarationSyntax node, string hintName, string source)
{
var fileName = GetOutputFileName(node, hintName);
var sourceText = SourceText.From(source, Encoding.UTF8);
_addSource(fileName, sourceText);
_executionContext.AddSource(fileName, sourceText);
}

internal string GetOutputFileName(ClassDeclarationSyntax node, string hintName)
Expand Down
31 changes: 31 additions & 0 deletions Sources/ServiceModel.Grpc.DesignTime.Shared/IExecutionContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// <copyright>
// Copyright 2020-2021 Max Ieremenko
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

namespace ServiceModel.Grpc.DesignTime.Generator
{
internal interface IExecutionContext
{
CancellationToken CancellationToken { get; }

void ReportDiagnostic(Diagnostic diagnostic);

void AddSource(string hintName, SourceText sourceText);
}
}
Loading

0 comments on commit 5c2d54d

Please sign in to comment.