Skip to content

Commit

Permalink
Merge branch 'master' into users/svegira/make-context-mockable
Browse files Browse the repository at this point in the history
  • Loading branch information
WhitWaldo authored Oct 30, 2024
2 parents 763e91e + 03038fa commit 0665a76
Show file tree
Hide file tree
Showing 21 changed files with 1,090 additions and 436 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" Version="1.1.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
<PackageVersion Include="Microsoft.DurableTask.Client.Grpc" Version="1.3.0" />
<PackageVersion Include="Microsoft.DurableTask.Worker.Grpc" Version="1.3.0" />
Expand Down
41 changes: 24 additions & 17 deletions examples/GeneratedActor/ActorClient/ActorClient.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6</TargetFramework>
<LangVersion>10.0</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6</TargetFramework>
<LangVersion>10.0</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<ItemGroup>
<ProjectReference Include="..\ActorCommon\ActorCommon.csproj" />
<ProjectReference Include="..\..\..\src\Dapr.Actors\Dapr.Actors.csproj" />
</ItemGroup>
<!-- Persist the source generator (and other) files to disk -->
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<!-- 👇 The "base" path for the source generators -->
<!--<GeneratedFolder>Generated</GeneratedFolder>-->
<!-- 👇 Write the output for each target framework to a different sub-folder -->
<!--<CompilerGeneratedFilesOutputPath>$(GeneratedFolder)\$(TargetFramework)</CompilerGeneratedFilesOutputPath>-->
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Dapr.Actors.Generators\Dapr.Actors.Generators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ActorCommon\ActorCommon.csproj" />
<ProjectReference Include="..\..\..\src\Dapr.Actors\Dapr.Actors.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Dapr.Actors.Generators\Dapr.Actors.Generators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion examples/GeneratedActor/ActorClient/IClientActor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// Copyright 2023 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
454 changes: 221 additions & 233 deletions src/Dapr.Actors.Generators/ActorClientGenerator.cs

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/Dapr.Actors.Generators/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Release 1.14

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|--------------------
DAPR0001| Usage | Error | Cancellation tokens must be the last argument
DAPR0002| Usage | Error | Only methods with a single argument or a single argument followed by a cancellation token are supported
3 changes: 3 additions & 0 deletions src/Dapr.Actors.Generators/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

38 changes: 38 additions & 0 deletions src/Dapr.Actors.Generators/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Dapr.Actors.Generators
{
/// <summary>
/// Constants used by the code generator.
/// </summary>
internal static class Constants
{
/// <summary>
/// The namespace used by the generated code.
/// </summary>
public const string GeneratorsNamespace = "Dapr.Actors.Generators";

/// <summary>
/// The name of the attribute used to mark actor interfaces.
/// </summary>
public const string ActorMethodAttributeTypeName = "ActorMethodAttribute";

/// <summary>
/// The full type name of the attribute used to mark actor interfaces.
/// </summary>
public const string ActorMethodAttributeFullTypeName = GeneratorsNamespace + "." + ActorMethodAttributeTypeName;

/// <summary>
/// The name of the attribute used to mark actor interfaces.
/// </summary>
public const string GenerateActorClientAttributeTypeName = "GenerateActorClientAttribute";

/// <summary>
/// The full type name of the attribute used to mark actor interfaces.
/// </summary>
public const string GenerateActorClientAttributeFullTypeName = GeneratorsNamespace + "." + GenerateActorClientAttributeTypeName;

/// <summary>
/// Actor proxy type name.
/// </summary>
public const string ActorProxyTypeName = "Dapr.Actors.Client.ActorProxy";
}
}
64 changes: 37 additions & 27 deletions src/Dapr.Actors.Generators/Dapr.Actors.Generators.csproj
Original file line number Diff line number Diff line change
@@ -1,45 +1,55 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsRoslynComponent>true</IsRoslynComponent>
</PropertyGroup>

<PropertyGroup>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<PropertyGroup>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="all" />
</ItemGroup>

<!--
<!--
Source generators are built and packaged as analyzers and not "normal" NuGet libraries.
-->

<PropertyGroup>
<!-- Generators must target netstandard2.0. -->
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks></TargetFrameworks>
<PropertyGroup>
<!-- Generators must target netstandard2.0. -->
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks></TargetFrameworks>

<!-- Do not include the generator as a lib dependency -->
<IncludeBuildOutput>false</IncludeBuildOutput>
<!-- Do not include the generator as a lib dependency -->
<IncludeBuildOutput>false</IncludeBuildOutput>

<!-- Suppress false-positive error NU5128 when packing analyzers with no lib/ref files. -->
<SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
<!-- Suppress false-positive error NU5128 when packing analyzers with no lib/ref files. -->
<SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>

<!-- Suppress generation of symbol package (.snupkg). -->
<IncludeSymbols>false</IncludeSymbols>
<!-- Suppress generation of symbol package (.snupkg). -->
<IncludeSymbols>false</IncludeSymbols>

<!-- Additional NuGet package properties. -->
<Description>This package contains source generators for interacting with Actor services using Dapr.</Description>
<PackageTags>$(PackageTags);Actors</PackageTags>
</PropertyGroup>
<!-- Additional NuGet package properties. -->
<Description>This package contains source generators for interacting with Actor services using Dapr.</Description>
<PackageTags>$(PackageTags);Actors</PackageTags>
</PropertyGroup>

<ItemGroup>
<!-- Package the generator in the analyzer directory of the NuGet package -->
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>
<ItemGroup>
<!-- Package the generator in the analyzer directory of the NuGet package -->
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="AnalyzerReleases.Shipped.md" />
<AdditionalFiles Include="AnalyzerReleases.Unshipped.md" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="$(AssemblyName).Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b1f597635c44597fcecb493e2b1327033b29b1a98ac956a1a538664b68f87d45fbaada0438a15a6265e62864947cc067d8da3a7d93c5eb2fcbb850e396c8684dba74ea477d82a1bbb18932c0efb30b64ff1677f85ae833818707ac8b49ad8062ca01d2c89d8ab1843ae73e8ba9649cd28666b539444dcdee3639f95e2a099bb2"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.CodeAnalysis;

namespace Dapr.Actors.Generators.Diagnostics
{
internal static class CancellationTokensMustBeTheLastArgument
{
public const string DiagnosticId = "DAPR0001";
public const string Title = "Invalid method signature";
public const string MessageFormat = "Cancellation tokens must be the last argument";
public const string Category = "Usage";

private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
DiagnosticId,
Title,
MessageFormat,
Category,
DiagnosticSeverity.Error,
isEnabledByDefault: true);

internal static Diagnostic CreateDiagnostic(ISymbol symbol) => Diagnostic.Create(
Rule,
symbol.Locations.First(),
symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.CodeAnalysis;

namespace Dapr.Actors.Generators.Diagnostics
{
internal static class MethodMustOnlyHaveASingleArgumentOptionallyFollowedByACancellationToken
{
public const string DiagnosticId = "DAPR0002";
public const string Title = "Invalid method signature";
public const string MessageFormat = "Only methods with a single argument or a single argument followed by a cancellation token are supported";
public const string Category = "Usage";

private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
DiagnosticId,
Title,
MessageFormat,
Category,
DiagnosticSeverity.Error,
isEnabledByDefault: true);

internal static Diagnostic CreateDiagnostic(ISymbol symbol) => Diagnostic.Create(
Rule,
symbol.Locations.First(),
symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat));
}
}
25 changes: 25 additions & 0 deletions src/Dapr.Actors.Generators/DiagnosticsException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.CodeAnalysis;

namespace Dapr.Actors.Generators
{
/// <summary>
/// Exception thrown when diagnostics are encountered during code generation.
/// </summary>
internal sealed class DiagnosticsException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="DiagnosticsException"/> class.
/// </summary>
/// <param name="diagnostics">List of diagnostics generated.</param>
public DiagnosticsException(IEnumerable<Diagnostic> diagnostics)
: base(string.Join("\n", diagnostics.Select(d => d.ToString())))
{
this.Diagnostics = diagnostics.ToArray();
}

/// <summary>
/// Diagnostics encountered during code generation.
/// </summary>
public ICollection<Diagnostic> Diagnostics { get; }
}
}
34 changes: 34 additions & 0 deletions src/Dapr.Actors.Generators/Extensions/IEnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace Dapr.Actors.Generators.Extensions
{
internal static class IEnumerableExtensions
{
/// <summary>
/// Returns the index of the first item in the sequence that satisfies the predicate. If no item satisfies the predicate, -1 is returned.
/// </summary>
/// <typeparam name="T">The type of objects in the <see cref="IEnumerable{T}"/>.</typeparam>
/// <param name="source"><see cref="IEnumerable{T}"/> in which to search.</param>
/// <param name="predicate">Function performed to check whether an item satisfies the condition.</param>
/// <returns>Return the zero-based index of the first occurrence of an element that satisfies the condition, if found; otherwise, -1.</returns>
internal static int IndexOf<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}

int index = 0;

foreach (var item in source)
{
if (predicate(item))
{
return index;
}

index++;
}

return -1;
}
}
}
Loading

0 comments on commit 0665a76

Please sign in to comment.