Skip to content

Commit

Permalink
Source generator samples (#511)
Browse files Browse the repository at this point in the history
* Add initial source generator samples
- Add samples
- Add demo consumption project
- Add readme
- Update global.json
  • Loading branch information
chsienki authored Apr 28, 2020
1 parent ea2ad80 commit a560eb2
Show file tree
Hide file tree
Showing 14 changed files with 573 additions and 3 deletions.
17 changes: 17 additions & 0 deletions Samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "VisualBasicToCSharpConverte
EndProject
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "VisualBasicToCSharpConverter.UnitTests", "samples\VisualBasic\VisualBasicToCSharpConverter\VisualBasicToCSharpConverter.Test\VisualBasicToCSharpConverter.UnitTests.vbproj", "{5B7D7569-B5EE-4C01-9AFA-BC1958588160}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SourceGenerators", "SourceGenerators", "{14D18F51-6B59-49D5-9AB7-08B38417A459}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGeneratorSamples", "samples\CSharp\SourceGenerators\SourceGeneratorSamples\SourceGeneratorSamples.csproj", "{2ADE5CFA-5DF4-44A9-BD67-E884BCFBA045}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GeneratedDemo", "samples\CSharp\SourceGenerators\GeneratedDemo\GeneratedDemo.csproj", "{EC4DB63B-C2B4-4D06-AF98-15253035C6D5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -269,6 +275,14 @@ Global
{5B7D7569-B5EE-4C01-9AFA-BC1958588160}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B7D7569-B5EE-4C01-9AFA-BC1958588160}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B7D7569-B5EE-4C01-9AFA-BC1958588160}.Release|Any CPU.Build.0 = Release|Any CPU
{2ADE5CFA-5DF4-44A9-BD67-E884BCFBA045}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2ADE5CFA-5DF4-44A9-BD67-E884BCFBA045}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2ADE5CFA-5DF4-44A9-BD67-E884BCFBA045}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2ADE5CFA-5DF4-44A9-BD67-E884BCFBA045}.Release|Any CPU.Build.0 = Release|Any CPU
{EC4DB63B-C2B4-4D06-AF98-15253035C6D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EC4DB63B-C2B4-4D06-AF98-15253035C6D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC4DB63B-C2B4-4D06-AF98-15253035C6D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC4DB63B-C2B4-4D06-AF98-15253035C6D5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -325,6 +339,9 @@ Global
{8E1C9AEC-6EF1-43A8-A378-52C5C0E40532} = {CDA94F62-E35A-4913-8045-D9D42416513C}
{ECB83742-8023-4609-B139-D7B78DD66ED9} = {8E1C9AEC-6EF1-43A8-A378-52C5C0E40532}
{5B7D7569-B5EE-4C01-9AFA-BC1958588160} = {8E1C9AEC-6EF1-43A8-A378-52C5C0E40532}
{14D18F51-6B59-49D5-9AB7-08B38417A459} = {C3FB27E9-C8EE-4F76-B0AA-7CD67A7E652B}
{2ADE5CFA-5DF4-44A9-BD67-E884BCFBA045} = {14D18F51-6B59-49D5-9AB7-08B38417A459}
{EC4DB63B-C2B4-4D06-AF98-15253035C6D5} = {14D18F51-6B59-49D5-9AB7-08B38417A459}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B849838B-3D7A-4B6B-BE07-285DCB1588F4}
Expand Down
6 changes: 3 additions & 3 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"tools": {
"dotnet": "3.0.101",
"dotnet": "3.1.300-preview-015115",
"vs": {
"version": "16.3"
"version": "16.6"
},
"xcopy-msbuild": "16.3.0-alpha"
},
"msbuild-sdks": {
"Microsoft.DotNet.Arcade.Sdk": "5.0.0-beta.20117.3"
"Microsoft.DotNet.Arcade.Sdk": "5.0.0-beta.20224.11"
}
}
24 changes: 24 additions & 0 deletions samples/CSharp/SourceGenerators/GeneratedDemo/GeneratedDemo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>preview</LangVersion>
</PropertyGroup>

<ItemGroup>
<AdditionalFiles Include="MainSettings.xmlsettings" />
<None Include="MainSettings.xmlsettings">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Analyzer Include="$(OutDir)\..\..\..\SourceGeneratorSamples\$(Configuration)\netstandard2.0\SourceGeneratorSamples.dll" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SourceGeneratorSamples\SourceGeneratorSamples.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<Settings name="Main">
<Setting name="FirstRun" type="bool">false</Setting>
<Setting name="CacheSize" type="int">1234</Setting>
</Settings>
20 changes: 20 additions & 0 deletions samples/CSharp/SourceGenerators/GeneratedDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace GeneratedDemo
{
class Program
{
static void Main(string[] args)
{
// Run the various scenarios
Console.WriteLine("Running HelloWorld:\n");
UseHelloWorldGenerator.Run();

Console.WriteLine("\n\nRunning AutoNotify:\n");
UseAutoNotifyGenerator.Run();

Console.WriteLine("\n\nRunning XmlSettings:\n");
UseXmlSettingsGenerator.Run();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using AutoNotify;

namespace GeneratedDemo
{
// The view model we'd like to augment
public partial class ExampleViewModel
{
[AutoNotify]
private string _text = "private field text";

[AutoNotify(PropertyName = "Count")]
private int _amount = 5;
}

public static class UseAutoNotifyGenerator
{
public static void Run()
{
ExampleViewModel vm = new ExampleViewModel();

// we didn't explicitly create the 'Text' property, it was generated for us
string text = vm.Text;
Console.WriteLine($"Text = {text}");

// Properties can have differnt names generated based on the PropertyName argument of the attribute
int count = vm.Count;
Console.WriteLine($"Count = {count}");

// the viewmodel will automatically implement INotifyPropertyChanged
vm.PropertyChanged += (o, e) => Console.WriteLine($"Property {e.PropertyName} was changed");
vm.Text = "abc";
vm.Count = 123;

// Try adding fields to the ExampleViewModel class above and tagging them with the [AutoNotify] attribute
// You'll see the matching generated properties visibile in IntelliSense in realtime
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace GeneratedDemo
{
public static class UseHelloWorldGenerator
{
public static void Run()
{
// The static call below is generated at build time, and will list the syntax trees used in the compilation
HelloWorldGenerated.HelloWorld.SayHello();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using AutoSettings;

namespace GeneratedDemo
{
public static class UseXmlSettingsGenerator
{
public static void Run()
{
// This XmlSettings generator makes a static property in the XmlSettings class for each .xmlsettings file

// here we have the 'Main' settings file from MainSettings.xmlsettings
// the name is determined by the 'name' attribute of the root settings element
XmlSettings.MainSettings main = XmlSettings.Main;
Console.WriteLine($"Reading settings from {main.GetLocation()}");

// settings are strongly typed and can be read directly from the static instance
bool firstRun = XmlSettings.Main.FirstRun;
Console.WriteLine($"Setting firstRun = {firstRun}");

int cacheSize = XmlSettings.Main.CacheSize;
Console.WriteLine($"Setting cacheSize = {cacheSize}");

// Try adding some keys to the settings file and see the settings become available to read from
}
}
}
35 changes: 35 additions & 0 deletions samples/CSharp/SourceGenerators/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
🚧 Work In Progress
========

These samples are for an in-progress feature of Roslyn. As such they may change or break as the feature is developed, and no level of support is implied.

For more infomation on the Source Generators feature, see the [design document](https://github.com/dotnet/roslyn/blob/master/docs/features/source-generators.md).

Prerequisites
-----

These samples require Visual Studio 16.6 or higher.

Building the samples
-----
Open `SourceGenerators.sln` in Visual Studio or run `dotnet build` from the `\SourceGenerators` directory.

Running the samples
-----

The generators must be run as part of another build, as they inject source into the project being built. This repo contains a sample project `GeneratorDemo` that relies of the sample generators to add code to it's compilation.

Run `GeneratedDemo` in Visual studio or run `dotnet run` from the `GeneratorDemo` directory.

Using the samples in your project
-----

You can add the sample generators to your own project by adding an item group containing an analyzer reference:

```xml
<ItemGroup>
<Analyzer Include="path\to\SourceGeneratorSamples.dll">
</ItemGroup>
```

You may need to close and reopen the solution in Visual Studio for the change to take effect.
Loading

0 comments on commit a560eb2

Please sign in to comment.