Skip to content

Commit

Permalink
stash from hp
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsmith1968 committed Jan 12, 2025
1 parent ee78423 commit 3dd1360
Show file tree
Hide file tree
Showing 21 changed files with 748 additions and 110 deletions.
29 changes: 19 additions & 10 deletions DNX.Extensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32825.248
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E832563B-3DD6-4CCD-B5DA-91F89AE3B98A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{27BBD260-1C33-4F57-925A-12AB922D6AB5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DNX.Extensions", "src\DNX.Extensions\DNX.Extensions.csproj", "{E2B8F640-BCFE-4BD7-B158-E7FE957A38CB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".build", ".build", "{9E70C755-0A39-46EA-8063-0A3B594B7271}"
ProjectSection(SolutionItems) = preProject
.github\workflows\ci-build.yml = .github\workflows\ci-build.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DNX.Extensions.Tests", "tests\DNX.Extensions.Tests\DNX.Extensions.Tests.csproj", "{B14EFA70-CA77-4439-9C08-357061B97E4D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".code", ".code", "{5936EA03-F95A-4407-9149-A23F4A378E4E}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
Expand All @@ -28,7 +20,19 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".docs", ".docs", "{8700E3D9
To Do.md = To Do.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DNX.Extensions.Benchmarks", "tests\DNX.Extensions.Benchmarks\DNX.Extensions.Benchmarks.csproj", "{D9664527-B415-498F-B8A9-9AF016D7D91F}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E832563B-3DD6-4CCD-B5DA-91F89AE3B98A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{27BBD260-1C33-4F57-925A-12AB922D6AB5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "verification", "verification", "{A4EAD2B7-21AC-48E8-ADE3-6AE0B5D80A6B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DNX.Extensions", "src\DNX.Extensions\DNX.Extensions.csproj", "{E2B8F640-BCFE-4BD7-B158-E7FE957A38CB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DNX.Extensions.Tests", "tests\DNX.Extensions.Tests\DNX.Extensions.Tests.csproj", "{B14EFA70-CA77-4439-9C08-357061B97E4D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DNX.Extensions.Benchmarks", "verification\DNX.Extensions.Benchmarks\DNX.Extensions.Benchmarks.csproj", "{D9664527-B415-498F-B8A9-9AF016D7D91F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DNX.Extensions.Generators", "src\DNX.Extensions.Generators\DNX.Extensions.Generators.csproj", "{2D817A1A-E754-45F0-9187-A9F1FE7B7630}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -48,14 +52,19 @@ Global
{D9664527-B415-498F-B8A9-9AF016D7D91F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9664527-B415-498F-B8A9-9AF016D7D91F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D9664527-B415-498F-B8A9-9AF016D7D91F}.Release|Any CPU.Build.0 = Release|Any CPU
{2D817A1A-E754-45F0-9187-A9F1FE7B7630}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2D817A1A-E754-45F0-9187-A9F1FE7B7630}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D817A1A-E754-45F0-9187-A9F1FE7B7630}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D817A1A-E754-45F0-9187-A9F1FE7B7630}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E2B8F640-BCFE-4BD7-B158-E7FE957A38CB} = {E832563B-3DD6-4CCD-B5DA-91F89AE3B98A}
{B14EFA70-CA77-4439-9C08-357061B97E4D} = {27BBD260-1C33-4F57-925A-12AB922D6AB5}
{D9664527-B415-498F-B8A9-9AF016D7D91F} = {27BBD260-1C33-4F57-925A-12AB922D6AB5}
{D9664527-B415-498F-B8A9-9AF016D7D91F} = {A4EAD2B7-21AC-48E8-ADE3-6AE0B5D80A6B}
{2D817A1A-E754-45F0-9187-A9F1FE7B7630} = {E832563B-3DD6-4CCD-B5DA-91F89AE3B98A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A971C3D4-3729-43C6-88E7-16371F03161F}
Expand Down
25 changes: 25 additions & 0 deletions src/DNX.Extensions.Generators/DNX.Extensions.Generators.csproj
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 src/DNX.Extensions.Generators/Extensions/ResourceExtensions.cs
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 src/DNX.Extensions.Generators/Generators/ConversionGenerator.cs
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);
}
}
}
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;
}
}
}
12 changes: 3 additions & 9 deletions src/DNX.Extensions/Assemblies/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,9 @@ public static string GetEmbeddedResourceText(this Assembly assembly, string rela

var resourceName = $"{nameSpace}.{relativeResourceName}";

using (var stream = assembly.GetManifestResourceStream(resourceName))
{
using (var reader = new StreamReader(stream))
{
var result = reader.ReadToEnd();

return result;
}
}
using var stream = assembly.GetManifestResourceStream(resourceName);
using var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
catch (Exception e)
{
Expand Down
26 changes: 0 additions & 26 deletions src/DNX.Extensions/Conversion/ConvertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,6 @@ public static string ToStringOrDefault(this object obj, string defaultValue = ""
return obj?.ToString() ?? defaultValue;
}

/// <summary>
/// Converts to boolean.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns><c>true/false</c> if can be converted, <c>defaultValue</c> otherwise.</returns>
public static bool ToBoolean(this string text, bool defaultValue = default)
{
return bool.TryParse(text, out var value)
? value
: defaultValue;
}

/// <summary>
/// Converts to int32.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>System.Int32.</returns>
public static int ToInt32(this string text, int defaultValue = default)
{
return int.TryParse(text, out var value)
? value
: defaultValue;
}

/// <summary>
/// Converts to enum.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/DNX.Extensions/DNX.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DNX.Extensions.Generators\DNX.Extensions.Generators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"/>
</ItemGroup>

</Project>
Loading

0 comments on commit 3dd1360

Please sign in to comment.