Skip to content

Commit

Permalink
Add auto-generated support for the code base.
Browse files Browse the repository at this point in the history
Add analyzers to give consistent results, including headers
  • Loading branch information
glennawatson committed Sep 22, 2020
1 parent 7005ccf commit b463b0c
Show file tree
Hide file tree
Showing 31 changed files with 602 additions and 137 deletions.
13 changes: 0 additions & 13 deletions .idea/.idea.ReactiveMarbles.ObservableEvents/.idea/.gitignore

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/.idea.ReactiveMarbles.ObservableEvents/.idea/misc.xml

This file was deleted.

7 changes: 0 additions & 7 deletions .idea/.idea.ReactiveMarbles.ObservableEvents/riderModule.iml

This file was deleted.

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 ReactiveUI Association, Inc

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
63 changes: 63 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<Project>
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591;1701;1702;1705;VSX1000</NoWarn>
<Platform>AnyCPU</Platform>
<IsTestProject>$(MSBuildProjectName.Contains('Tests'))</IsTestProject>
<DebugType>embedded</DebugType>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)analyzers.ruleset</CodeAnalysisRuleSet>

<Authors>Glenn Watson</Authors>
<Copyright>Copyright (c) 2019 Glenn Watson</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/reactivemarbles/PropertyChanged</PackageProjectUrl>
<PackageDescription>Allows to get an observables for property changed events.</PackageDescription>
<Owners>glennawatson</Owners>
<PackageTags>system.reactive;propertychanged;inpc;reactive;functional</PackageTags>
<PackageReleaseNotes>https://github.com/reactivemarbles/PropertyChanged/releases</PackageReleaseNotes>
<RepositoryUrl>https://github.com/reactivemarbles/PropertyChanged</RepositoryUrl>
<RepositoryType>git</RepositoryType>

<!-- Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<!-- Embed source files that are not tracked by the source control manager in the PDB -->
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<!-- Include PDB in the built .nupkg -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>

<ItemGroup Condition="$(IsTestProject)">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.console" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Xunit.StaFact" Version="1.0.37" />
<PackageReference Include="Shouldly" Version="4.0.0-beta0003" />
<PackageReference Include="PublicApiGenerator" Version="10.2.0" />
</ItemGroup>

<ItemGroup Condition="'$(IsTestProject)' != 'true'">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

<PropertyGroup>
<SolutionDir Condition="'$(SolutionDir)' == ''">$(MSBuildThisFileDirectory)</SolutionDir>
</PropertyGroup>

<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)..\LICENSE" Pack="true" PackagePath="LICENSE" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Nerdbank.GitVersioning" Version="3.2.31" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="stylecop.analyzers" Version="1.2.0-beta.164" PrivateAssets="all" />
<PackageReference Include="Roslynator.Analyzers" Version="3.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" Link="stylecop.json" />
</ItemGroup>
</Project>
30 changes: 30 additions & 0 deletions src/Directory.build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project>
<PropertyGroup>
<Product>$(AssemblyName) ($(TargetFramework))</Product>
</PropertyGroup>

<PropertyGroup Condition="$(TargetFramework.StartsWith('net4'))">
<DefineConstants>$(DefineConstants);NET_45;XAML</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith('uap'))">
<DefineConstants>$(DefineConstants);NETFX_CORE;XAML;WINDOWS_UWP</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith('Xamarin.iOS'))">
<DefineConstants>$(DefineConstants);MONO;UIKIT;COCOA</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith('Xamarin.Mac'))">
<DefineConstants>$(DefineConstants);MONO;COCOA</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith('Xamarin.TVOS'))">
<DefineConstants>$(DefineConstants);MONO;UIKIT;COCOA</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith('Xamarin.WatchOS'))">
<DefineConstants>$(DefineConstants);MONO;UIKIT;COCOA</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith('MonoAndroid'))">
<DefineConstants>$(DefineConstants);MONO;ANDROID</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith('tizen'))">
<DefineConstants>$(DefineConstants);TIZEN</DefineConstants>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
// Copyright (c) 2020 ReactiveUI Association Inc. All rights reserved.
// ReactiveUI Association Inc licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand All @@ -17,10 +21,14 @@

namespace ReactiveMarbles.ObservableEvents.SourceGenerator
{
/// <summary>
/// Generates Observables from events in specified types and namespaces.
/// </summary>
[Generator]
public class EventGeneratorHook : ISourceGenerator
{
private const string AttributeText = @"using System;
private const string AttributeText = @"// <auto-generated />
using System;
namespace ReactiveMarbles.ObservableEvents
{
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct)]
Expand All @@ -47,11 +55,11 @@ public TypeEventsToObservablesAttribute(Type type)
/// <inheritdoc />
public void Execute(GeneratorExecutionContext context)
{
// add the attribute text
// add the attribute text.
context.AddSource("EventsToObservablesAttribute.SourceGenerated.cs", SourceText.From(AttributeText, Encoding.UTF8));

// retreive the populated receiver
if (!(context.SyntaxReceiver is SyntaxReceiver receiver))
// retreive the populated receiver.
if (context.SyntaxReceiver is not SyntaxReceiver receiver)
{
return;
}
Expand All @@ -73,6 +81,13 @@ public void Execute(GeneratorExecutionContext context)
}
}

/// <inheritdoc />
public void Initialize(GeneratorInitializationContext context)
{
// Register a syntax receiver that will be created for each generation pass
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
}

private static void GetAvailableTypes(SyntaxReceiver receiver, Compilation compilation, out SortedDictionary<string, List<INamedTypeSymbol>> instanceNamespaceList, out SortedDictionary<string, List<INamedTypeSymbol>> staticNamespaceList)
{
instanceNamespaceList = new SortedDictionary<string, List<INamedTypeSymbol>>();
Expand Down Expand Up @@ -110,13 +125,6 @@ private static void GetAvailableTypes(SyntaxReceiver receiver, Compilation compi
}
}

/// <inheritdoc />
public void Initialize(GeneratorInitializationContext context)
{
// Register a syntax receiver that will be created for each generation pass
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
}

private static void GenerateEvents(GeneratorExecutionContext context, IEventSymbolGenerator symbolGenerator, string namespaceName, string suffixName, List<INamedTypeSymbol> symbols)
{
var namespaceSyntax = symbolGenerator.Generate(namespaceName, symbols);
Expand All @@ -126,9 +134,12 @@ private static void GenerateEvents(GeneratorExecutionContext context, IEventSymb
return;
}

var compilationUnit = CompilationUnit().WithMembers(SingletonList<MemberDeclarationSyntax>(namespaceSyntax));
var compilationUnit = CompilationUnit().WithMembers(SingletonList<MemberDeclarationSyntax>(namespaceSyntax))
.WithLeadingTrivia(
XmlSyntaxFactory.GenerateDocumentationString(
"<auto-generated />"));

var name = $"SourceClass{namespaceName}{suffixName}.SourceGenerated.cs";
var name = $"SourceClass{namespaceName}{suffixName}.SourceGenerated.Designer.cs";

var sourceText = compilationUnit.NormalizeWhitespace().ToFullString();
context.AddSource(
Expand All @@ -138,36 +149,4 @@ private static void GenerateEvents(GeneratorExecutionContext context, IEventSymb
File.WriteAllText(Path.Combine("C:/Temp", name), sourceText);
}
}

/// <summary>
/// Created on demand before each generation pass
/// </summary>
internal class SyntaxReceiver : ISyntaxReceiver
{
public List<AttributeSyntax> CandidateAttributes { get; } = new List<AttributeSyntax>();

public List<TypeDeclarationSyntax> CandidateTypes { get; } = new List<TypeDeclarationSyntax>();

/// <summary>
/// Called for every syntax node in the compilation, we can inspect the nodes and save any information useful for generation
/// </summary>
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
// any field with at least one attribute is a candidate for property generation
if (syntaxNode is AttributeSyntax attributeSyntax)
{
var attributeName = attributeSyntax.Name.ToFullString();
if (attributeName == "EventsToObservablesAttribute" || attributeName == "EventsToObservables")
{
CandidateAttributes.Add(attributeSyntax);
}
}

if (syntaxNode is TypeDeclarationSyntax typeAttributeSyntax
&& typeAttributeSyntax.AttributeLists.Count > 0)
{
CandidateTypes.Add(typeAttributeSyntax);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Copyright (c) 2020 ReactiveUI Association Inc. All rights reserved.
// ReactiveUI Association Inc licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Copyright (c) 2020 ReactiveUI Association Inc. All rights reserved.
// ReactiveUI Association Inc licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Collections.Generic;
// Copyright (c) 2020 ReactiveUI Association Inc. All rights reserved.
// ReactiveUI Association Inc licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System.Collections.Generic;

namespace ReactiveMarbles.ObservableEvents.SourceGenerator.EventGenerators
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Copyright (c) 2020 ReactiveUI Association Inc. All rights reserved.
// ReactiveUI Association Inc licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Copyright (c) 2020 ReactiveUI Association Inc. All rights reserved.
// ReactiveUI Association Inc licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Copyright (c) 2020 ReactiveUI Association Inc. All rights reserved.
// ReactiveUI Association Inc licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System.Collections.Generic;
Expand Down Expand Up @@ -61,7 +60,7 @@ private static (ArrowExpressionClauseSyntax ArrowClause, TypeSyntax EventArgsTyp
ArgumentListSyntax methodParametersArgumentList;
TypeSyntax eventArgsType;

var invokeMethod = ((INamedTypeSymbol)(eventSymbol.OriginalDefinition).Type).DelegateInvokeMethod;
var invokeMethod = ((INamedTypeSymbol)eventSymbol.OriginalDefinition.Type).DelegateInvokeMethod;

if (invokeMethod == null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Copyright (c) 2020 ReactiveUI Association Inc. All rights reserved.
// ReactiveUI Association Inc licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Copyright (c) 2020 ReactiveUI Association Inc. All rights reserved.
// ReactiveUI Association Inc licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System.Collections.Generic;
Expand Down Expand Up @@ -31,7 +30,7 @@ internal class InstanceEventGenerator : EventGeneratorBase

members.Add(GenerateStaticClass(namespaceName, orderedTypeDeclarations));

members.AddRange(orderedTypeDeclarations.Select(x => GenerateEventWrapperClass(x)).Where(x => x != null));
members.AddRange(orderedTypeDeclarations.Select(GenerateEventWrapperClass).Where(x => x != null));

if (members.Count > 0)
{
Expand Down
Loading

0 comments on commit b463b0c

Please sign in to comment.