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

Added VB (specific/focused) SourceGenerators Sample (Try #2) #692

Merged
merged 1 commit into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions samples/VisualBasic/SourceGenerators/GeneratedDemo/Cars.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Brand, Model, Year, cc, Favorite
Fiat, Punto, 2008, 12.3, No
Ford, Wagon, 1956, 20.3, No
BMW, "335", 2014, 20.3, Yes
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<AdditionalFiles Include="People.csv" CsvLoadType="Startup" />
<AdditionalFiles Include="Cars.csv" CsvLoadType="OnDemand" CacheObjects="true" />

<AdditionalFiles Include="MainSettings.xmlsettings" CopyToOutputDirectory="PreserveNewest" />
<None Include="MainSettings.xmlsettings" CopyToOutputDirectory="PreserveNewest" /> <!-- TODO: remove this when AdditionalFiles supports CopyToOutputDirectory -->
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SourceGeneratorSamples\SourceGeneratorSamples.vbproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<!-- Manually reference the generator props because we locally reference the generator. When added via NuGet this happens automatically -->
<Import Project="..\SourceGeneratorSamples\CsvGenerator.props" />

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<Settings name="Main">
<Setting name="FirstRun" type="Boolean">False</Setting>
<Setting name="CacheSize" type="Integer">1234</Setting>
<Setting name="TwitchPhrase" type="String">Hello World!</Setting>
</Settings>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Name, address, 11Age
"Luca Bol", "23 Bell Street", 90
"john doe", "32 Carl street", 45
39 changes: 39 additions & 0 deletions samples/VisualBasic/SourceGenerators/GeneratedDemo/Program.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Option Explicit On
Option Strict On
Option Infer On

Module Program

Public Sub Main()

Console.WriteLine("Running HelloWorld:
")
UseHelloWorldGenerator.Run()

Console.WriteLine("

Running AutoNotify:
")
UseAutoNotifyGenerator.Run()

Console.WriteLine("

Running XmlSettings:
")
UseXmlSettingsGenerator.Run()

Console.WriteLine("

Running CsvGenerator:
")
UseCsvGenerator.Run()

Console.WriteLine("

Running MustacheGenerator:
")
UseMustacheGenerator.Run()

End Sub

End Module
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Option Explicit On
Option Strict On
Option Infer On

Imports AutoNotify

' The view model we'd like to augment
Partial Public Class ExampleViewModel

<AutoNotify>
Private _text As String = "private field text"

<AutoNotify(PropertyName:="Count")>
Private _amount As Integer = 5

End Class

Public Module UseAutoNotifyGenerator

Public Sub Run()

Dim vm As New ExampleViewModel()

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

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

' the viewmodel will automatically implement INotifyPropertyChanged
AddHandler vm.PropertyChanged, Sub(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

End Sub

End Module
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Option Explicit On
Option Strict On
Option Infer On

Imports CSV

Friend Class UseCsvGenerator

Public Shared Sub Run()

Console.WriteLine("## CARS")
Cars.All.ToList().ForEach(Sub(c) Console.WriteLine(c.Brand & vbTab & c.Model & vbTab & c.Year & vbTab & c.Cc & vbTab & c.Favorite))
Console.WriteLine(vbCr & "## PEOPLE")
People.All.ToList().ForEach(Sub(p) Console.WriteLine(p.Name & vbTab & p.Address & vbTab & p._11Age))

End Sub

End Class
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Public Module UseHelloWorldGenerator

Public Sub Run()
' The static call below is generated at build time, and will list the syntax trees used in the compilation
HelloWorldGenerated.HelloWorld.SayHello()
End Sub

End Module
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
Option Explicit On
Option Strict On
Option Infer On

Imports GeneratedDemo.UseMustacheGenerator

<Assembly: Mustache("Lottery", t1, h1)>
<Assembly: Mustache("HR", t2, h2)>
<Assembly: Mustache("HTML", t3, h3)>
<Assembly: Mustache("Section", t4, h4)>
<Assembly: Mustache("NestedSection", t5, h5)>

Friend Class UseMustacheGenerator

Public Shared Sub Run()
Console.WriteLine(Mustache.Constants.Lottery)
Console.WriteLine(Mustache.Constants.HR)
Console.WriteLine(Mustache.Constants.HTML)
Console.WriteLine(Mustache.Constants.Section)
Console.WriteLine(Mustache.Constants.NestedSection)
End Sub

' Mustache templates and hashes from the manual at https://mustache.github.io/mustache.1.html...
Public Const t1 As String = "
Hello {{name}}
You have just won {{value}} dollars!
{{#in_ca}}
Well, {{taxed_value}} dollars, after taxes.
{{/in_ca}}
"
Public Const h1 As String = "
{
""name"": ""Chris"",
""value"": 10000,
""taxed_value"": 5000,
""in_ca"": true
}
"
Public Const t2 As String = "
* {{name}}
* {{age}}
* {{company}}
* {{{company}}}
"
Public Const h2 As String = "
{
""name"": ""Chris"",
""company"": ""<b>GitHub</b>""
}
"
Public Const t3 As String = "
Shown
{{#person}}
Never shown!
{{/person}}
"
Public Const h3 As String = "
{
""person"": false
}
"
Public Const t4 As String = "
{{#repo}}
<b>{{name}}</b>
{{/repo}}
"
Public Const h4 As String = "
{
""repo"": [
{ ""name"": ""resque"" },
{ ""name"": ""hub"" },
{ ""name"": ""rip"" }
]
}
"
Public Const t5 As String = "
{{#repo}}
<b>{{name}}</b>
{{#nested}}
NestedName: {{name}}
{{/nested}}
{{/repo}}
"
Public Const h5 As String = "
{
""repo"": [
{ ""name"": ""resque"", ""nested"":[{""name"":""nestedResque""}] },
{ ""name"": ""hub"" },
{ ""name"": ""rip"" }
]
}
"

End Class
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Imports AutoSettings

Public Module UseXmlSettingsGenerator

Public Sub 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
Dim main As XmlSettings.MainSettings = XmlSettings.Main
Console.WriteLine($"Reading settings from {main.GetLocation()}")

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

Dim cacheSize As Integer = 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

End Sub

End Module
35 changes: 35 additions & 0 deletions samples/VisualBasic/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 information 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.9.0 Preview 2.0** 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 will most likely need to close and reopen the solution in Visual Studio for any changes made to the generators to take effect.
Loading