Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json code gen skeleton #5

Merged
merged 9 commits into from
Jul 16, 2020
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
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}") = "codegenerator", "codegenerator\codegenerator.csproj", "{08FDD841-6001-46AE-87A4-9DB2C7BAAC8C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "codegenerator.UnitTests", "codegenerator.UnitTests\codegenerator.UnitTests.csproj", "{37E5E8FB-1220-49B3-BE0E-CFFC079DA040}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "codegenerator.Tests", "codegenerator.Tests\codegenerator.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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Xunit;
kevinwkt marked this conversation as resolved.
Show resolved Hide resolved

namespace System.Text.Json.CodeGenerator.Tests
{
// TODO(@kevinwkt): Temporary end2end tests to use the generated code using codegen.
kevinwkt marked this conversation as resolved.
Show resolved Hide resolved
public class JsonSerializerSouceGeneratorTests
{
// Temporary test to make sure code was generated.
[Fact]
public static void TestGeneratedCode()
{
Assert.Equal("Hello", HelloWorldGenerated.HelloWorld.SayHello());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

namespace System.Text.Json.CodeGenerator.Tests
{
// TODO(@kevinwkt): Populate the folder with sample classes we want to source generate for.
public partial class SampleClass
kevinwkt marked this conversation as resolved.
Show resolved Hide resolved
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\codegenerator\codegenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
<Compile Include="JsonSerializerSourceGeneratorTests.cs" />
<Compile Include="TestClasses\SampleClass.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,14 @@
using Xunit;

namespace System.Text.Json.CodeGenerator.UnitTests
{
// TODO(@kevinwkt) Temporary test for CodeGenerator.
public class CodeGeneratorTests
{
[Fact]
public static void GenerateFromType()
{
CodeGenerator generator = new CodeGenerator();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Xunit;

namespace System.Text.Json.CodeGenerator.UnitTests
{
// TODO(@kevinwkt): Temporary unit tests for Source Generator.
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,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
</PropertyGroup>

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

<ItemGroup>
<Compile Include="CodeGeneratorTests.cs" />
<Compile Include="JsonSerializerSourceGeneratorTests.cs" />
</ItemGroup>

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

</Project>
9 changes: 9 additions & 0 deletions src/libraries/System.Text.Json/codegenerator/CodeGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

namespace System.Text.Json.CodeGenerator
{
// TODO(@kevinwkt): Logic involving types found through attributer to code transformation will mostly be here.
public class CodeGenerator
kevinwkt marked this conversation as resolved.
Show resolved Hide resolved
{
public CodeGenerator(){ }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Generic;

namespace System.Text.Json.CodeGenerator
{
// TODO(@kevinwkt): Base JsonSerializerSourceGenerator. This class will invoke CodeGenerator within Execute
// to generate wanted output code for JsonSerializers.
[Generator]
public class JsonSerializerSourceGenerator : ISourceGenerator
{
public void Execute(SourceGeneratorContext context)
{
// TODO(@kevinwkt): Foreach type found, call code generator.
// Temporary simple HelloWorld class.
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());
}

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

public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
// TODO(@kevinwkt): For now get all the type decl in all syntax tree.
if (syntaxNode is TypeDeclarationSyntax tds)
{
GeneratorInputTypes.Add(tds);
}
}
}

}
}
25 changes: 25 additions & 0 deletions src/libraries/System.Text.Json/codegenerator/codegenerator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
ericstj marked this conversation as resolved.
Show resolved Hide resolved

<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="CodeGenerator.cs" />
<Compile Include="JsonSerializerSourceGenerator.cs" />
</ItemGroup>

</Project>
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="..\codegenerator\codegenerator.csproj" SkipPackageFileCheck="true" HarvestDependencies="false" />
ericstj marked this conversation as resolved.
Show resolved Hide resolved
kevinwkt marked this conversation as resolved.
Show resolved Hide resolved
<!-- 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>