-
Notifications
You must be signed in to change notification settings - Fork 256
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
Source generator samples #511
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f3a533f
Add initial source generator samples
chsienki e7b4ef5
Add hello world generator, and clean up samples
chsienki 0e8d3eb
Add WIP Readme
chsienki 09053bb
Clean up project + remove warning
chsienki 6ebd7b7
Expand samples:
chsienki 61d81ab
Whitespace
chsienki 8c4859a
Add SourceGenerators.sln
chsienki c41a496
Add generated project to main samples SLN
chsienki c034ef8
Downgrade generated demo to netcoreapp3.0
chsienki af2f5c2
Update global.json
chsienki b15fee6
Bump arcade version too
chsienki 5985a80
Restore netcoreapp3.1
chsienki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
samples/CSharp/SourceGenerators/GeneratedDemo/GeneratedDemo.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
5 changes: 5 additions & 0 deletions
5
samples/CSharp/SourceGenerators/GeneratedDemo/MainSettings.xmlsettings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
samples/CSharp/SourceGenerators/GeneratedDemo/UseAutoNotifyGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
samples/CSharp/SourceGenerators/GeneratedDemo/UseHelloWorldGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
samples/CSharp/SourceGenerators/GeneratedDemo/UseXmlSettingsGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it might help if I added that file to the repo huh? |
||
|
||
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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This appears to be missing the consumption project.