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

Move source gen samples to the incremental model and source to C# 10 #985

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>10.0</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
35 changes: 17 additions & 18 deletions samples/CSharp/SourceGenerators/GeneratedDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
using System;

namespace GeneratedDemo
namespace GeneratedDemo;

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

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

Console.WriteLine("\n\nRunning XmlSettings:\n");
UseXmlSettingsGenerator.Run();
Console.WriteLine("\n\nRunning XmlSettings:\n");
UseXmlSettingsGenerator.Run();

Console.WriteLine("\n\nRunning CsvGenerator:\n");
UseCsvGenerator.Run();
Console.WriteLine("\n\nRunning CsvGenerator:\n");
UseCsvGenerator.Run();

Console.WriteLine("\n\nRunning MustacheGenerator:\n");
UseMustacheGenerator.Run();
Console.WriteLine("\n\nRunning MustacheGenerator:\n");
UseMustacheGenerator.Run();

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

namespace GeneratedDemo
namespace GeneratedDemo;

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

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

public static class UseAutoNotifyGenerator
public static class UseAutoNotifyGenerator
{
public static void Run()
{
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
}
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
Expand Up @@ -5,16 +5,15 @@
using static System.Console;
using CSV;

namespace GeneratedDemo
namespace GeneratedDemo;

class UseCsvGenerator
{
class UseCsvGenerator
public static void Run()
{
public static void Run()
{
WriteLine("## CARS");
Cars.All.ToList().ForEach(c => WriteLine($"{c.Brand}\t{c.Model}\t{c.Year}\t{c.Cc}"));
WriteLine("\n## PEOPLE");
People.All.ToList().ForEach(p => WriteLine($"{p.Name}\t{p.Address}\t{p._11Age}"));
}
WriteLine("## CARS");
Cars.All.ToList().ForEach(c => WriteLine($"{c.Brand}\t{c.Model}\t{c.Year}\t{c.Cc}"));
WriteLine("\n## PEOPLE");
People.All.ToList().ForEach(p => WriteLine($"{p.Name}\t{p.Address}\t{p._11Age}"));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace GeneratedDemo
namespace GeneratedDemo;

public static class UseHelloWorldGenerator
{
public static class UseHelloWorldGenerator
public static void Run()
{
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();
}
// 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
@@ -1,15 +1,14 @@
using static System.Console;
using Maths;

namespace GeneratedDemo
namespace GeneratedDemo;

public static class UseMathsGenerator
{
public static class UseMathsGenerator
public static void Run()
{
public static void Run()
{
WriteLine($"The area of a (10, 5) rectangle is: {Formulas.AreaRectangle(10, 5)}");
WriteLine($"The area of a (10) square is: {Formulas.AreaSquare(10)}");
WriteLine($"The GoldHarmon of 3 is: {Formulas.GoldHarm(3)}");
}
WriteLine($"The area of a (10, 5) rectangle is: {Formulas.AreaRectangle(10, 5)}");
WriteLine($"The area of a (10) square is: {Formulas.AreaSquare(10)}");
WriteLine($"The GoldHarmon of 3 is: {Formulas.GoldHarm(3)}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,64 @@
[assembly: Mustache("Section", t4, h4)]
[assembly: Mustache("NestedSection", t5, h5)]

namespace GeneratedDemo
namespace GeneratedDemo;

class UseMustacheGenerator
{
class UseMustacheGenerator
public static void Run()
{
public static void Run()
{
WriteLine(Mustache.Constants.Lottery);
WriteLine(Mustache.Constants.HR);
WriteLine(Mustache.Constants.HTML);
WriteLine(Mustache.Constants.Section);
WriteLine(Mustache.Constants.NestedSection);
}
WriteLine(Mustache.Constants.Lottery);
WriteLine(Mustache.Constants.HR);
WriteLine(Mustache.Constants.HTML);
WriteLine(Mustache.Constants.Section);
WriteLine(Mustache.Constants.NestedSection);
}

// Mustache templates and hashes from the manual at https://mustache.github.io/mustache.1.html...
public const string t1 = @"
// Mustache templates and hashes from the manual at https://mustache.github.io/mustache.1.html...
public const string t1 = @"
Hello {{name}}
You have just won {{value}} dollars!
{{#in_ca}}
Well, {{taxed_value}} dollars, after taxes.
{{/in_ca}}
";
public const string h1 = @"
public const string h1 = @"
{
""name"": ""Chris"",
""value"": 10000,
""taxed_value"": 5000,
""in_ca"": true
}
";
public const string t2 = @"
public const string t2 = @"
* {{name}}
* {{age}}
* {{company}}
* {{{company}}}
";
public const string h2 = @"
public const string h2 = @"
{
""name"": ""Chris"",
""company"": ""<b>GitHub</b>""
}
";
public const string t3 = @"
public const string t3 = @"
Shown
{{#person}}
Never shown!
{{/person}}
";
public const string h3 = @"
public const string h3 = @"
{
""person"": false
}
";
public const string t4 = @"
public const string t4 = @"
{{#repo}}
<b>{{name}}</b>
{{/repo}}
";
public const string h4 = @"
public const string h4 = @"
{
""repo"": [
{ ""name"": ""resque"" },
Expand All @@ -78,15 +78,15 @@ Never shown!
]
}
";
public const string t5 = @"
public const string t5 = @"
{{#repo}}
<b>{{name}}</b>
{{#nested}}
NestedName: {{name}}
{{/nested}}
{{/repo}}
";
public const string h5 = @"
public const string h5 = @"
{
""repo"": [
{ ""name"": ""resque"", ""nested"":[{""name"":""nestedResque""}] },
Expand All @@ -96,5 +96,4 @@ Never shown!
}
";

}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
using System;
using AutoSettings;

namespace GeneratedDemo
namespace GeneratedDemo;

public static class UseXmlSettingsGenerator
{
public static class UseXmlSettingsGenerator
public static void Run()
{
public static void Run()
{
// This XmlSettings generator makes a static property in the XmlSettings class for each .xmlsettings file
// 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()}");
// 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}");
// 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}");
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
}
// Try adding some keys to the settings file and see the settings become available to read from
}
}
2 changes: 1 addition & 1 deletion samples/CSharp/SourceGenerators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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/main/docs/features/source-generators.md).
For more information on the Source Generators feature, see the [design document](https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.md).

Prerequisites
-----
Expand Down
Loading