-
Notifications
You must be signed in to change notification settings - Fork 517
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Rgen] Use a roslyn code generator to help generate the flag parsing …
…of all attributes. The old Xamarin API used A LOT of attributes as flags to indicate different generating methods. All this attributes do not have any data and can be used as a feature flag. For example, if we wanted to generated a static class we can do: ```csharp [Static] interface MyStaticClass {} ``` In this commit we are relaying in a custom roslyn code generator that parses an attribute and generates the flag properties for our data model. All the code is very repetitive and specially error prone. The generator allows use just to focus on the name of the attribute and the targets to which the attribute can be applied. When you do the review you'll notice that the targets used in the AttributeNames in the transformer do not match the ones in the bgen/Attributes.cs file. The mismatch is because people back in the day were careless and used the default flag, which is All. That is a mistake, so I went throgh the trouble of using the correct target and add a document string explaining the use of the flag and therefore the reasoning of the updated target. A sample of the generated code can be found [here](https://gist.github.com/mandel-macaque/ed4277457a3afa6606af0f78ee9bf07e)
- Loading branch information
1 parent
b50532b
commit d4be53b
Showing
11 changed files
with
571 additions
and
93 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Generated |
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
56 changes: 56 additions & 0 deletions
56
src/rgen/Microsoft.Macios.Generator/TabbedStringBuilder.Availability.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,56 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.Macios.Generator.Availability; | ||
using Xamarin.Utils; | ||
|
||
namespace Microsoft.Macios.Generator; | ||
|
||
partial class TabbedStringBuilder { | ||
|
||
public TabbedStringBuilder AppendMemberAvailability (in SymbolAvailability allPlatformsAvailability) | ||
{ | ||
foreach (var availability in allPlatformsAvailability.PlatformAvailabilities) { | ||
var platformName = availability.Platform.AsString ().ToLower (); | ||
if (availability.SupportedVersion is not null) { | ||
var versionStr = (PlatformAvailability.IsDefaultVersion (availability.SupportedVersion)) | ||
? string.Empty | ||
: availability.SupportedVersion.ToString (); | ||
AppendLine ($"[SupportedOSPlatform (\"{platformName}{versionStr}\")]"); | ||
} | ||
|
||
// loop over the unsupported versions of the platform | ||
foreach (var (version, message) in availability.UnsupportedVersions) { | ||
var versionStr = (PlatformAvailability.IsDefaultVersion (version)) ? string.Empty : version.ToString (); | ||
if (message is null) { | ||
AppendLine ($"[UnsupportedOSPlatform (\"{platformName}{versionStr}\")]"); | ||
} else { | ||
AppendLine ($"[UnsupportedOSPlatform (\"{platformName}{versionStr}\", \"{message}\")]"); | ||
} | ||
} | ||
|
||
// loop over the obsolete versions of the platform | ||
foreach (var (version, obsoleteInfo) in availability.ObsoletedVersions) { | ||
var versionStr = (PlatformAvailability.IsDefaultVersion (version)) ? string.Empty : version.ToString (); | ||
|
||
switch (obsoleteInfo) { | ||
case (null, null): | ||
AppendLine ($"[ObsoletedOSPlatform (\"{platformName}{versionStr}\")]"); | ||
break; | ||
case (not null, null): | ||
AppendLine ($"[ObsoletedOSPlatform (\"{platformName}{versionStr}\", \"{obsoleteInfo.Message}\")]"); | ||
break; | ||
case (null, not null): | ||
AppendLine ($"[ObsoletedOSPlatform (\"{platformName}{versionStr}\", Url=\"{obsoleteInfo.Url}\")]"); | ||
break; | ||
case (not null, not null): | ||
AppendLine ( | ||
$"[ObsoletedOSPlatform (\"{platformName}{versionStr}\", \"{obsoleteInfo.Message}\", Url=\"{obsoleteInfo.Url}\")]"); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return this; | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
...ator/Microsoft.Macios.Transformer.Generator/Microsoft.Macios.Transformer.Generator.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,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>latest</LangVersion> | ||
|
||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
<IsRoslynComponent>true</IsRoslynComponent> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
|
||
<RootNamespace>Microsoft.Macios.Transformer.Generator</RootNamespace> | ||
<PackageId>Microsoft.Macios.Transformer.Generator</PackageId> | ||
<!-- There is a bug in the roslyn analyzer for roslyn analyzers.... --> | ||
<NoWarn>RS2007;RS1041;APL0003</NoWarn> | ||
|
||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\..\Microsoft.Macios.Generator\TabbedStringBuilder.cs"> | ||
<Link>TabbedStringBuilder.cs</Link> | ||
</Compile> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
9 changes: 9 additions & 0 deletions
9
...ansformer.Generator/Microsoft.Macios.Transformer.Generator/Properties/launchSettings.json
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,9 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"DebugRoslynSourceGenerator": { | ||
"commandName": "DebugRoslynComponent", | ||
"targetProject": "../Microsoft.Macios.Transformer.Generator.Sample/Microsoft.Macios.Transformer.Generator.Sample.csproj" | ||
} | ||
} | ||
} |
Oops, something went wrong.