Skip to content

Commit

Permalink
Merge pull request dotnet#5 from kevinwkt/JsonCodeGenSkeleton
Browse files Browse the repository at this point in the history
Json code gen skeleton
  • Loading branch information
ericstj authored Jul 16, 2020
2 parents b83a26c + 5ed4f65 commit 8a17046
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 5 deletions.
4 changes: 4 additions & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
<optimizationwindows_ntx64IBCCoreCLRVersion>99.99.99-master-20200228.3</optimizationwindows_ntx64IBCCoreCLRVersion>
<optimizationlinuxx64IBCCoreCLRVersion>99.99.99-master-20200228.3</optimizationlinuxx64IBCCoreCLRVersion>
<optimizationPGOCoreCLRVersion>99.99.99-master-20200228.3</optimizationPGOCoreCLRVersion>
<!-- Roslyn dependencies for source generation -->
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>3.6.0-3.20207.2</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
<MicrosoftCodeAnalysisAnalyzersVersion>3.0.0-beta2.final</MicrosoftCodeAnalysisAnalyzersVersion>
<MicrosoftCodeAnalysisVersion>3.6.0.0</MicrosoftCodeAnalysisVersion>
<!-- Not auto-updated. -->
<MicrosoftDiaSymReaderNativeVersion>1.7.0</MicrosoftDiaSymReaderNativeVersion>
<SystemCommandLineVersion>2.0.0-beta1.20253.1</SystemCommandLineVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;

namespace System.Text.Json.SourceGeneration.Tests
{
public class JsonSerializerSouceGeneratorTests
{
[Fact]
public static void TestGeneratedCode()
{
Assert.Equal("Hello", HelloWorldGenerated.HelloWorld.SayHello());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
<Compile Include="JsonSourceGeneratorTests.cs" />
</ItemGroup>

<Target Name="FixIncrementalCoreCompileWithAnalyzers" BeforeTargets="CoreCompile">
<ItemGroup>
<CustomAdditionalCompileInputs Include="@(Analyzer)" />
</ItemGroup>
</Target>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Xunit;

namespace System.Text.Json.SourceGeneration.UnitTests
{
public static class GeneratorTests
{
[Fact]
public static void SourceGeneratorInitializationPass()
{
}

[Fact]
public static void SourceGeneratorInitializationFail()
{
}

[Fact]
public static void SourceGeneratorExecutionPass()
{
}

[Fact]
public static void SourceGeneratorExecutionFail()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion)" PrivateAssets="all" />
<ProjectReference Include="..\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="JsonSourceGeneratorTests.cs" />
</ItemGroup>

<Target Name="FixIncrementalCoreCompileWithAnalyzers" BeforeTargets="CoreCompile">
<ItemGroup>
<CustomAdditionalCompileInputs Include="@(Analyzer)" />
</ItemGroup>
</Target>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

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

namespace System.Text.Json.SourceGeneration
{
/// <summary>
/// Base JsonSerializerSourceGenerator. This class will invoke CodeGenerator within Execute
/// to generate wanted output code for JsonSerializers.
/// </summary>
[Generator]
public class JsonSerializerSourceGenerator : ISourceGenerator
{
public void Execute(SourceGeneratorContext context)
{
// Foreach type found, call code generator.
StringBuilder sourceBuilder = new StringBuilder(@"
using System;
namespace HelloWorldGenerated
{
public static class HelloWorld
{
public static string SayHello()
{
return ""Hello"";
");

sourceBuilder.Append(@"
}
}
}");

context.AddSource("helloWorldGenerated", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8));
}

public void Initialize(InitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new JsonSerializableSyntaxReceiver());
}

// Temporary function for now that reads all types. Should search types with attribute JsonSerializable.
internal class JsonSerializableSyntaxReceiver : ISyntaxReceiver
{
public List<TypeDeclarationSyntax> GeneratorInputTypes = new List<TypeDeclarationSyntax>();

public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
// Get all the type decl in all syntax tree.
if (syntaxNode is TypeDeclarationSyntax tds)
{
GeneratorInputTypes.Add(tds);
}
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<CLSCompliant>false</CLSCompliant>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisCSharpWorkspacesVersion)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
<PackageReference Include="Microsoft.DotNet.Build.Tasks.Packaging" Version="$(MicrosoftDotNetBuildTasksPackagingVersion)" PrivateAssets="all" />

<!-- update dependencies brought in transitively from CodeAnalysis to avoid referencing 1.x packages -->
<PackageReference Include="NETStandard.Library" Version="2.0.3" ExcludeAssets="All" />
<PackageReference Include="System.Composition" Version="1.4.1" />

<PackageDestination Include="analyzers\dotnet\cs" />
</ItemGroup>

<ItemGroup>
<Compile Include="JsonSourceGenerator.cs" />
</ItemGroup>

</Project>
27 changes: 23 additions & 4 deletions src/libraries/System.Text.Json/System.Text.Json.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27213.1

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30204.135
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json.Tests", "tests\System.Text.Json.Tests.csproj", "{5F553243-042C-45C0-8E49-C739131E11C3}"
ProjectSection(ProjectDependencies) = postProject
Expand All @@ -20,7 +21,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E107E9C1-E89
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{2E666815-2EDB-464B-9DF6-380BF4789AD4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestUtilities", "..\Common\tests\TestUtilities\TestUtilities.csproj", "{C42D5CBB-DAC5-4892-8E69-ED3183A6409E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUtilities", "..\Common\tests\TestUtilities\TestUtilities.csproj", "{C42D5CBB-DAC5-4892-8E69-ED3183A6409E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json.SourceGeneration", "System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.csproj", "{08FDD841-6001-46AE-87A4-9DB2C7BAAC8C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json.SourceGeneration.UnitTests", "System.Text.Json.SourceGeneration.UnitTests\System.Text.Json.SourceGeneration.UnitTests.csproj", "{37E5E8FB-1220-49B3-BE0E-CFFC079DA040}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Text.Json.SourceGeneration.Tests", "System.Text.Json.SourceGeneration.Tests\System.Text.Json.SourceGeneration.Tests.csproj", "{D6287A51-AE21-4680-BBF3-BE4BA4AFF311}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -44,6 +51,18 @@ Global
{C42D5CBB-DAC5-4892-8E69-ED3183A6409E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C42D5CBB-DAC5-4892-8E69-ED3183A6409E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C42D5CBB-DAC5-4892-8E69-ED3183A6409E}.Release|Any CPU.Build.0 = Release|Any CPU
{08FDD841-6001-46AE-87A4-9DB2C7BAAC8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{08FDD841-6001-46AE-87A4-9DB2C7BAAC8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{08FDD841-6001-46AE-87A4-9DB2C7BAAC8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{08FDD841-6001-46AE-87A4-9DB2C7BAAC8C}.Release|Any CPU.Build.0 = Release|Any CPU
{37E5E8FB-1220-49B3-BE0E-CFFC079DA040}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{37E5E8FB-1220-49B3-BE0E-CFFC079DA040}.Debug|Any CPU.Build.0 = Debug|Any CPU
{37E5E8FB-1220-49B3-BE0E-CFFC079DA040}.Release|Any CPU.ActiveCfg = Release|Any CPU
{37E5E8FB-1220-49B3-BE0E-CFFC079DA040}.Release|Any CPU.Build.0 = Release|Any CPU
{D6287A51-AE21-4680-BBF3-BE4BA4AFF311}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D6287A51-AE21-4680-BBF3-BE4BA4AFF311}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D6287A51-AE21-4680-BBF3-BE4BA4AFF311}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D6287A51-AE21-4680-BBF3-BE4BA4AFF311}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 2 additions & 1 deletion src/libraries/System.Text.Json/pkg/System.Text.Json.pkgproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<ProjectReference Include="..\src\System.Text.Json.csproj">
<SupportedFramework>net461;netcoreapp2.0;uap10.0.16299;$(AllXamarinFrameworks)</SupportedFramework>
</ProjectReference>
<ProjectReference Include="..\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.csproj" />
<!-- Since UAP and .NETCoreApp are package based we still want to enable
OOBing libraries that happen to overlap with their framework package.
This avoids us having to lock the API in our NuGet packages just
Expand All @@ -15,4 +16,4 @@
</ValidatePackageSuppression>
</ItemGroup>
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.targets))" />
</Project>
</Project>

0 comments on commit 8a17046

Please sign in to comment.