Skip to content

Commit

Permalink
chore: update to C# 13
Browse files Browse the repository at this point in the history
  • Loading branch information
Denny09310 authored and Dreamescaper committed Oct 30, 2024
1 parent 1932978 commit 5f7a2c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>

<Description>Dotnet tool which generate BlazorBindings.Maui components for MAUI elements.</Description>
<PackAsTool>true</PackAsTool>
Expand Down
49 changes: 23 additions & 26 deletions src/BlazorBindings.Maui.ComponentGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System;

namespace BlazorBindings.Maui.ComponentGenerator;

public class Program
{
const string FileHeader = @"// <auto-generated>
// This code was generated by a BlazorBindings.Maui component generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
";
const string FileHeader = """
// <auto-generated>
// This code was generated by a BlazorBindings.Maui component generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>

""";

public static async Task Main(string[] args)
{
Expand All @@ -31,16 +33,11 @@ await Parser.Default
.ParseArguments<Options>(args)
.WithParsedAsync(async o =>
{
if (o.ProjectPath is null)
{
o.ProjectPath = Directory.GetFiles(Directory.GetCurrentDirectory()).FirstOrDefault(f
o.ProjectPath ??= Array.Find(Directory.GetFiles(Directory.GetCurrentDirectory()), f
=> f.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase))
?? throw new Exception("Cannot find any csproj files.");
}
if (o.OutPath is null)
{
o.OutPath = Path.Combine(o.ProjectPath, "..", "Elements");
}
?? throw new ArgumentException("Cannot find any csproj files.");
o.OutPath ??= Path.Combine(o.ProjectPath, "..", "Elements");
var compilation = await CreateComplitation(o);
Expand Down Expand Up @@ -91,10 +88,10 @@ private static GenerateComponentSettings[] GetTypesToGenerate(Compilation compil
FileHeader = FileHeader,
TypeAlias = typeAlias,
TypeSymbol = typeSymbol,
Exclude = GetNamedArgumentValues(a, "Exclude").ToHashSet(),
Include = GetNamedArgumentValues(a, "Include").ToHashSet(),
ContentProperties = GetNamedArgumentValues(a, "ContentProperties").ToHashSet(),
NonContentProperties = GetNamedArgumentValues(a, "NonContentProperties").ToHashSet(),
Exclude = [.. GetNamedArgumentValues(a, "Exclude")],
Include = [.. GetNamedArgumentValues(a, "Include")],
ContentProperties = [.. GetNamedArgumentValues(a, "ContentProperties")],
NonContentProperties = [.. GetNamedArgumentValues(a, "NonContentProperties")],
PropertyChangedEvents = GetNamedArgumentValues(a, "PropertyChangedEvents"),
GenericProperties = GetNamedArgumentValues(a, "GenericProperties").Select(v => v.Split(':')).ToDictionary(v => v[0],
v => v.ElementAtOrDefault(1) is string genericArgName ? compilation.GetTypeByMetadataName(genericArgName) : null),
Expand Down Expand Up @@ -132,24 +129,24 @@ private static GenerateComponentSettings[] GetTypesToGenerate(Compilation compil

foreach (var info in typesToGenerate)
{
var baseTypeInfo = typesToGenerate.FirstOrDefault(t => SymbolEqualityComparer.Default.Equals(t.TypeSymbol, info.TypeSymbol?.BaseType));
var baseTypeInfo = typesToGenerate.Find(t => SymbolEqualityComparer.Default.Equals(t.TypeSymbol, info.TypeSymbol?.BaseType));
info.BaseTypeInfo = baseTypeInfo;
}

return typesToGenerate.ToArray();
return [.. typesToGenerate];
}

private static string[] GetNamedArgumentValues(AttributeData attribute, string name)
{
var argumentConstant = attribute.NamedArguments.FirstOrDefault(a => a.Key == name).Value;

if (argumentConstant.Kind != TypedConstantKind.Array)
return Array.Empty<string>();
return [];

var values = argumentConstant.Values;

if (values.IsDefaultOrEmpty)
return Array.Empty<string>();
return [];

return values.Select(a => a.Value as string).Where(v => v is not null).ToArray();
}
Expand All @@ -165,7 +162,7 @@ private static async Task<Compilation> CreateComplitation(Options o)
return compilation;
}

private class Options
private sealed class Options
{
[Value(0, HelpText = "Project file path to run generator.")]
public string ProjectPath { get; set; }
Expand Down

0 comments on commit 5f7a2c9

Please sign in to comment.