-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee78423
commit 3dd1360
Showing
21 changed files
with
748 additions
and
110 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
25 changes: 25 additions & 0 deletions
25
src/DNX.Extensions.Generators/DNX.Extensions.Generators.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,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<LangVersion>Latest</LangVersion> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
<IsRoslynComponent>true</IsRoslynComponent> | ||
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> | ||
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Include="Templates\ConversionGenerator.Template.txt" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
28 changes: 28 additions & 0 deletions
28
src/DNX.Extensions.Generators/Extensions/ResourceExtensions.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,28 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace DNX.Extensions.Generators.Extensions; | ||
|
||
[ExcludeFromCodeCoverage] | ||
internal static class ResourceExtensions | ||
{ | ||
internal static string GetEmbeddedResourceText(this object obj, string resourceName) | ||
{ | ||
var type = obj.GetType(); | ||
|
||
var assembly = type.Assembly; | ||
|
||
var manifestFileInfo = assembly.GetManifestResourceNames() | ||
.SingleOrDefault(x => x.EndsWith(resourceName)); | ||
|
||
if (string.IsNullOrWhiteSpace(manifestFileInfo)) | ||
throw new Exception($"Unable to locate resource : {resourceName}"); | ||
|
||
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(manifestFileInfo); | ||
using var reader = new StreamReader(stream); | ||
return reader.ReadToEnd(); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/DNX.Extensions.Generators/Generators/ConversionGenerator.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,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using DNX.Extensions.Generators.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace DNX.Extensions.Generators.Generators; | ||
|
||
[ExcludeFromCodeCoverage] | ||
[Generator] | ||
public class ConversionGenerator : ISourceGenerator | ||
{ | ||
private static readonly Dictionary<string, string> TypesForGeneration = new() | ||
{ | ||
{ "short", nameof(Int16) }, | ||
{ "int", nameof(Int32) }, | ||
{ "long", nameof(Int64) }, | ||
{ "bool", nameof(Boolean) }, | ||
}; | ||
|
||
public void Initialize(GeneratorInitializationContext context) | ||
{ | ||
} | ||
|
||
public void Execute(GeneratorExecutionContext context) | ||
{ | ||
context.ReportDiagnostic( | ||
Diagnostic.Create( | ||
new DiagnosticDescriptor( | ||
"DNX0001", | ||
$"{nameof(ConversionGenerator)} Executing", | ||
nameof(ConversionGenerator) + " Executing for {0} types", | ||
"Build", | ||
DiagnosticSeverity.Info, | ||
true | ||
), | ||
Location.None, | ||
TypesForGeneration.Count.ToString() | ||
) | ||
); | ||
|
||
var targetNamespace = GetType().Name.Replace("Generator", ""); | ||
|
||
var nameSpace = GetType().Namespace.Replace(".Generators", "") | ||
.Trim('.') | ||
+ $".{targetNamespace}"; | ||
|
||
var templateFileName = $"{GetType().Name}.Template.txt"; | ||
|
||
var templateText = this.GetEmbeddedResourceText(templateFileName); | ||
|
||
if (string.IsNullOrWhiteSpace(templateText)) | ||
throw new Exception($"{nameof(templateText)} is empty"); | ||
|
||
foreach (var kvp in TypesForGeneration) | ||
{ | ||
var typeName = kvp.Key; | ||
var typeDescription = kvp.Value; | ||
|
||
context.ReportDiagnostic( | ||
Diagnostic.Create( | ||
new DiagnosticDescriptor( | ||
"DNX0002", | ||
$"{nameof(ConversionGenerator)} Generating: {typeName}", | ||
nameof(ConversionGenerator) + " Generating: {0} as {1}, under {2}", | ||
"", | ||
DiagnosticSeverity.Info, | ||
true | ||
), | ||
Location.None, | ||
typeName, | ||
typeDescription, | ||
nameSpace | ||
) | ||
); | ||
|
||
var sourceText = templateText | ||
.Replace("#namespace#", nameSpace) | ||
.Replace("#type#", typeName) | ||
.Replace("#name#", typeDescription) | ||
; | ||
|
||
context.AddSource($"Convert{kvp.Value}.generated.cs", sourceText); | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/DNX.Extensions.Generators/Templates/ConversionGenerator.Template.txt
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,91 @@ | ||
using System; | ||
using DNX.Extensions.Exceptions; | ||
|
||
namespace #namespace#; | ||
|
||
/// <summary> | ||
/// Class Convert#Name#Extensions. | ||
/// </summary> | ||
/// <remarks> | ||
/// Extensions for converting #type# values | ||
/// </remarks> | ||
public static class Convert#name#Extensions | ||
{ | ||
/// <summary> | ||
/// Converts the string to a #type# | ||
/// </summary> | ||
/// <param name="text">The text.</param> | ||
/// <returns>#type#</returns> | ||
/// <exception cref=""DNX.Helpers.Exceptions.ConversionException"">Unable to convert value to Type</exception> | ||
public static #type# To#name#(this string text) | ||
{ | ||
#type# result; | ||
|
||
if (!#type#.TryParse(text, out result)) | ||
{ | ||
throw new ConversionException(text, "Unable to convert value to Type", typeof(#type#)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/// <summary> | ||
/// Converts the string to a #type#, or returns the default value if the conversion fails | ||
/// </summary> | ||
/// <param name="text">The text.</param> | ||
/// <param name="defaultValue">The default value.</param> | ||
/// <returns>#type#</returns> | ||
public static #type# To#name#(this string text, #type# defaultValue) | ||
{ | ||
try | ||
{ | ||
var result = text.To#name#(); | ||
|
||
return result; | ||
} | ||
catch (ConversionException) | ||
{ | ||
return defaultValue; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Determines if the string can be converted to a #type# or not | ||
/// </summary> | ||
/// <param name="text">The text.</param> | ||
/// <returns><c>true</c> if the specified text is a #type; otherwise, <c>false</c>.</returns> | ||
public static bool Is#name#(this string text) | ||
{ | ||
try | ||
{ | ||
text.To#name#(); | ||
|
||
return true; | ||
} | ||
catch (ConversionException) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Determines if the string can be converted to a #type# or not | ||
/// </summary> | ||
/// <param name="text">The text.</param> | ||
/// <returns><c>true</c> if the specified text is a #type; otherwise, <c>false</c>.</returns> | ||
public static bool Is#name#(this string text, out #type# value) | ||
{ | ||
value = default; | ||
|
||
try | ||
{ | ||
value = text.To#name#(); | ||
|
||
return true; | ||
} | ||
catch (ConversionException) | ||
{ | ||
return false; | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.