8000 Miscelaneous minor improvements by kzu · Pull Request #100 · devlooped/stunts · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
SLEET_CONNECTION: ${{ secrets.SLEET_CONNECTION }}
if: matrix.os == 'ubuntu-latest' && env.SLEET_CONNECTION != ''
run: |
dotnet tool install -g --version 3.2.0 sleet
dotnet tool install -g --version 4.0.18 sleet
sleet push bin --config none -f --verbose -p "SLEET_FEED_CONTAINER=nuget" -p "SLEET_FEED_CONNECTIONSTRING=${{ secrets.SLEET_CONNECTION }}" -p "SLEET_FEED_TYPE=azure"

acceptance:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-notes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
ConvertFrom-Json |
select -ExpandProperty id

$notes = (Get-Content .\changelog.md | where { !($_ -like '\*') } | %{ $_.replace('\#', '\\#') }) -join '\n'
$notes = (Get-Content .\changelog.md | where { !($_ -like '\*') } | %{ $_.replace('\', '\\') }) -join '\n'
$headers = @{ 'Accept'='application/vnd.github.v3+json;charset=utf-8'; 'Authorization' = "bearer $env:GITHUB_TOKEN" }
$body = '{ "body":"' + $notes + '" }'

Expand Down
4 changes: 2 additions & 2 deletions .netconfig
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
weak
[file ".github/workflows/build.yml"]
url = https://github.com/devlooped/oss/blob/main/.github/workflows/build.yml
etag = 337666cc9f17f0cf395df950370967381f6de5134cc215f61035d36f38fed3a3
etag = 3ece28919da5fd43d3e264c775cb64b9f2f4a40dd8a02ec5a60617e203ecf97f
weak
[file ".github_changelog_generator"]
url = https://github.com/devlooped/oss/blob/main/.github_changelog_generator
Expand Down Expand Up @@ -122,5 +122,5 @@
weak
[file ".github/workflows/release-notes.yml"]
url = https://github.com/devlooped/oss/blob/main/.github/workflows/release-notes.yml
etag = e997e6e7bfd4d850a3554d4232562075a89cf7713a8cec4aef483f2bd6f3c10a
etag = c987e5b93b002c5a2a63304f677eb969b7b6e274b77ef4392aeef4d9ca7e10dd
weak
10 changes: 6 additions & 4 deletions src/Avatar.Package/Avatar.Package.msbuildproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Avatar</PackageId>
<Title>Avatar</Title>
<Description>Avatars blend with the Na'vi seamlessly, and you can control their behavior precisely ('drive' them) with a psionic link.
Just like a dynamic proxy, with behavior driven by code.

IMyService service = Avatar.Of&lt;IMyService&gt;();
<Description>Avatar is a modern interception library which implements the proxy pattern
and runs everywhere, even where run-time code generation (Reflection.Emit) is forbidden or
limitted, like physical iOS devices and game consoles, through compile-time code generation.
The proxy behavior is configured in code using what we call a behavior pipeline.

IMyService service = Avatar.Of&lt;IMyService&gt;();
service.AddBehavior(...);
</Description>
<PackageTags>dotnet proxy runtime dynamic</PackageTags>
Expand Down
84 changes: 80 additions & 4 deletions src/Avatar.StaticProxy/AvatarGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,28 @@ void OnExecute(ProcessorContext context)
syntax = processor.Process(syntax, context);

var code = syntax.NormalizeWhitespace().ToFullString();
var shouldEmit = false;

// Additional pretty-printing when emitting generated files, improves whitespace handling for C#
if (context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.EmitCompilerGeneratedFiles", out var emitSources) &&
bool.TryParse(emitSources, out shouldEmit) &&
shouldEmit &&
// NOTE: checking for C# last, since the Debugger.Attached section below would depend on
// the proper initialization of shouldEmit too, regardless of language
context.Language == LanguageNames.CSharp)
{
syntax = CSharpSyntaxTree.ParseText(code, (CSharpParseOptions)context.ParseOptions).GetRoot();
syntax = new CSharpFormatter().Visit(syntax);
code = syntax.GetText().ToString();
}

avatars.Add(name);
context.AddSource(name, SourceText.From(code, Encoding.UTF8));

#if DEBUG
if (Debugger.IsAttached)
{
if (context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.EmitCompilerGeneratedFiles", out var emitSources) &&
bool.TryParse(emitSources, out var shouldEmit) &&
shouldEmit &&
if (shouldEmit &&
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.IntermediateOutputPath", out var intermediateDir) &&
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.MSBuildProjectDirectory", out var projectDir))
{
Expand All @@ -244,7 +257,7 @@ void OnExecute(ProcessorContext context)

var filePath = Path.Combine(targetDir, name + (context.Language == LanguageNames.CSharp ? ".cs" : ".vb"));
File.WriteAllText(filePath, code);
Debugger.Log(0, "", "Avatar Generated: " + filePath);
Debugger.Log(0, "", "Avatar Generated: " + filePath + Environment.NewLine);
}

Debugger.Log(0, "", string.Join(
Expand All @@ -256,6 +269,69 @@ void OnExecute(ProcessorContext context)
}
}

class CSharpFormatter : CSharpSyntaxRewriter
{
static SyntaxTrivia NewLine => SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, "\n");
static SyntaxTrivia Tab => SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " ");

public override SyntaxNode? VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
// Ctor is always replaced, so it needs whitespace
=> base.VisitConstructorDeclaration(node)!
.WithTrailingTrivia(node.GetTrailingTrivia().Add(NewLine));

public override SyntaxNode? VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{
var canRead = node.AccessorList?.Accessors.Any(SyntaxKind.GetAccessorDeclaration) == true;
var canWrite = node.AccessorList?.Accessors.Any(SyntaxKind.SetAccessorDeclaration) == true;

// If there's get+set, the property declaration is preserved from scaffold
if (canRead && canWrite)
return base.VisitPropertyDeclaration(node);

return base.VisitPropertyDeclaration(node)!
.WithTrailingTrivia(node.GetTrailingTrivia().Add(NewLine));
}

public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax node)
{
// ref/out already get proper whitespace from scaffold
if (!node.ParameterList.Parameters.Any(x => x.IsRefOut()))
return base.VisitMethodDeclaration(node)!
.WithTrailingTrivia(node.GetTrailingTrivia().Add(NewLine));

return base.VisitMethodDeclaration(node);
}

public override SyntaxNode? VisitFieldDeclaration(FieldDeclarationSyntax node)
=> base.VisitFieldDeclaration(node)!
.WithTrailingTrivia(node.GetTrailingTrivia().Add(NewLine));

SyntaxTriviaList? indent;

public override SyntaxNode? VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
{
indent = node.Initializer?.GetLeadingTrivia();

return base.VisitObjectCreationExpression(node);
}

public override SyntaxNode? VisitInitializerExpression(InitializerExpressionSyntax node)
{
if (node.Kind() == SyntaxKind.ComplexElementInitializerExpression)
return base.VisitInitializerExpression(node)!.WithLeadingTrivia(indent?.Insert(0, NewLine).Add(Tab));

if (node.Kind() == SyntaxKind.CollectionInitializerExpression)
{
var last = node.Expressions.Count - 1;
return base.VisitInitializerExpression(node
.WithExpressions(SyntaxFactory.SeparatedList(
node.Expressions.Select((e, i) => i != last ? e : e.WithTrailingTrivia(indent?.Insert(0, NewLine))))));
}

return base.VisitInitializerExpression(node);
}
}

static bool CanGenerateFor(INamedTypeSymbol? symbol)
{
if (symbol == null)
Expand Down
36 changes: 23 additions & 13 deletions src/Avatar.StaticProxy/Processors/CSharpRewrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class CSharpRewriteVisitor : CSharpSyntaxRewriter

node = node.WithExpressionBody(ArrowExpressionClause(body))
.WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
.WithSemicolon();

return base.VisitConstructorDeclaration(node);
}
Expand Down Expand Up @@ -186,7 +186,9 @@ StatementSyntax InitLocal(ParameterSyntax parameter) =>
InvocationExpression(
MemberAccessExpression(
MemberAccessExpression("m", nameof(IMethodInvocation.Arguments)),
GenericName("Get", parameter.Type!)),
parameter.Type!.Kind() == SyntaxKind.NullableType ?
GenericName(nameof(ArgumentCollectionExtensions.GetNullable), parameter.Type!) :
GenericName(nameof(ArgumentCollectionExtensions.Get), parameter.Type!)),
Argument(
LiteralExpression(parameter.Identifier.ToString())))));

Expand Down Expand Up @@ -296,7 +298,9 @@ StatementSyntax InitLocal(ParameterSyntax parameter) =>
MemberAccessExpression(
prefix + "result",
nameof(IMethodReturn.Outputs)),
GenericName("Get", x.Type!)),
x.Type!.Kind() == SyntaxKind.NullableType ?
GenericName(nameof(ArgumentCollectionExtensions.GetNullable), x.Type!) :
GenericName(nameof(ArgumentCollectionExtensions.Get), x.Type!)),
Argument(
LiteralExpression(x.Identifier.ToString()))))))
.ToArray());
Expand Down Expand Up @@ -350,7 +354,7 @@ StatementSyntax InitLocal(ParameterSyntax parameter) =>
method = method
.WithBody(null)
.WithExpressionBody(ArrowExpressionClause(body))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
.WithSemicolon();
}

return base.VisitMethodDeclaration(method);
Expand Down Expand Up @@ -378,7 +382,7 @@ StatementSyntax InitLocal(ParameterSyntax parameter) =>
node = node
.WithExpressionBody(ArrowExpressionClause(Execute(
node.Type, Enumerable.Empty<ParameterSyntax>(), baseCall)))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
.WithSemicolon();
}
else
{
Expand All @@ -388,7 +392,7 @@ StatementSyntax InitLocal(ParameterSyntax parameter) =>
node = node.AddAccessorListAccessors(AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
.WithExpressionBody(ArrowExpressionClause(Execute(
node.Type, Enumerable.Empty<ParameterSyntax>(), baseCall)))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
.WithSemicolon());
}
if (canWrite)
{
Expand All @@ -398,15 +402,17 @@ StatementSyntax InitLocal(ParameterSyntax parameter) =>
baseCall = baseCall?.WithRight(InvocationExpression(
MemberAccessExpression(
MemberAccessExpression("m", nameof(IMethodInvocation.Arguments)),
GenericName("Get", node.Type)),
node.Type.Kind() == SyntaxKind.NullableType ?
GenericName(nameof(ArgumentCollectionExtensions.GetNullable), node.Type) :
GenericName(nameof(ArgumentCollectionExtensions.Get), node.Type)),
Argument(
LiteralExpression("value"))));

node = node.AddAccessorListAccessors(AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
.WithExpressionBody(ArrowExpressionClause(
// NOTE: we always append the implicit "value" parameter for setters.
Execute(null, new[] { Parameter(Identifier("value")).WithType(node.Type) }, baseCall)))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
.WithSemicolon());
}
}

Expand Down Expand Up @@ -445,7 +451,7 @@ StatementSyntax InitLocal(ParameterSyntax parameter) =>
(ElementAccessExpressionSyntax?)GetBaseCall(
prop,
SyntaxKind.GetAccessorDeclaration)))))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
.WithSemicolon();
}
else
{
Expand All @@ -459,7 +465,7 @@ StatementSyntax InitLocal(ParameterSyntax parameter) =>
(ElementAccessExpressionSyntax?)GetBaseCall(
prop,
SyntaxKind.GetAccessorDeclaration)))))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
.WithSemicolon());
}

if (canWrite)
Expand All @@ -471,15 +477,17 @@ StatementSyntax InitLocal(ParameterSyntax parameter) =>
.WithRight(InvocationExpression(
MemberAccessExpression(
MemberAccessExpression("m", nameof(IMethodInvocation.Arguments)),
GenericName("Get", node.Type)),
node.Type.Kind() == SyntaxKind.NullableType ?
GenericName(nameof(ArgumentCollectionExtensions.GetNullable), node.Type) :
GenericName(nameof(ArgumentCollectionExtensions.Get), node.Type)),
Argument(
LiteralExpression("value"))));

node = node.AddAccessorListAccessors(AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
.WithExpressionBody(ArrowExpressionClause(
Execute(null, node.ParameterList.Parameters.Concat(new[] { Parameter(Identifier("value")).WithType(node.Type) }),
baseCall)))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
.WithSemicolon());
}
}

Expand Down Expand Up @@ -545,7 +553,9 @@ ArrowExpressionClauseSyntax body(SyntaxKind kind)
InvocationExpression(
MemberAccessExpression(
MemberAccessExpression("m", nameof(IMethodInvocation.Arguments)),
GenericName("Get", prm.Type!)),
prm.Type!.Kind() == SyntaxKind.NullableType ?
GenericName(nameof(ArgumentCollectionExtensions.GetNullable), prm.Type) :
GenericName(nameof(ArgumentCollectionExtensions.Get), prm.Type!)),
Argument(
Literal(prm.Identifier.ToString()))))));

Expand Down
27 changes: 0 additions & 27 deletions src/Avatar.StaticProxy/Processors/CSharpSyntaxExtensions.cs

This file was deleted.

Loading
0