Skip to content

Commit

Permalink
Generator: allow to use Aliases to set type name alias (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper authored Feb 11, 2023
1 parent b03c35f commit a198df9
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

namespace BlazorBindings.Maui.Elements.AlohaKit
{
public partial class Button : BlazorBindings.Maui.Elements.GraphicsView
public partial class AlhButton : BlazorBindings.Maui.Elements.GraphicsView
{
static Button()
static AlhButton()
{
RegisterAdditionalHandlers();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

namespace BlazorBindings.Maui.Elements.AlohaKit
{
public partial class CheckBox : BlazorBindings.Maui.Elements.GraphicsView
public partial class AlhCheckBox : BlazorBindings.Maui.Elements.GraphicsView
{
static CheckBox()
static AlhCheckBox()
{
RegisterAdditionalHandlers();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

namespace BlazorBindings.Maui.Elements.AlohaKit
{
public partial class ProgressBar : BlazorBindings.Maui.Elements.GraphicsView
public partial class AlhProgressBar : BlazorBindings.Maui.Elements.GraphicsView
{
static ProgressBar()
static AlhProgressBar()
{
RegisterAdditionalHandlers();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

namespace BlazorBindings.Maui.Elements.AlohaKit
{
public partial class Slider : BlazorBindings.Maui.Elements.GraphicsView
public partial class AlhSlider : BlazorBindings.Maui.Elements.GraphicsView
{
static Slider()
static AlhSlider()
{
RegisterAdditionalHandlers();
}
Expand Down
18 changes: 9 additions & 9 deletions samples/ThirdPartyControlsSample/Pages/AlohaKitPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<BoxView HeightRequest="1" Color="Colors.Gray" />

<BlazorBindings.Maui.Elements.Slider Minimum="0" Maximum="1" @bind-Value="sliderValue" />
<Slider Minimum="0" Maximum="1" @bind-Value="sliderValue" />

<Grid ColumnDefinitions="*,Auto">
@*<BlazorBindings.Maui.Elements.AlohaKit.ProgressBar IsVertical="true" Progress="sliderValue" ProgressColor="Colors.Green" Grid.Column="1" />*@
Expand All @@ -37,22 +37,22 @@
<BoxView HeightRequest="1" Color="Colors.Gray" />

<HorizontalStackLayout>
<BlazorBindings.Maui.Elements.AlohaKit.CheckBox @bind-IsChecked="checkboxChecked" />
<AlhCheckBox @bind-IsChecked="checkboxChecked" />
<Label>Are you busy?</Label>
</HorizontalStackLayout>
<BlazorBindings.Maui.Elements.AlohaKit.BusyIndicator IsVisible="@checkboxChecked" />
<BusyIndicator IsVisible="@checkboxChecked" />

<HorizontalStackLayout>
<Label>Toggle @(toggleOn ? "on" : "off")</Label>
<ToggleSwitch @bind-IsOn="toggleOn" />
</HorizontalStackLayout>
<ToggleSwitch @bind-IsOn="toggleOn" />
</HorizontalStackLayout>

</VerticalStackLayout>
</VerticalStackLayout>

</ScrollView>
</ContentPage>
</ScrollView>
</ContentPage>

@code {
@code {
int ratingValue = 3;
string RatingDescription => ratingValue switch
{
Expand Down
8 changes: 4 additions & 4 deletions samples/ThirdPartyControlsSample/Properties/Elements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
[assembly: GenerateComponent(typeof(Rating))]
[assembly: GenerateComponent(typeof(BusyIndicator))]
[assembly: GenerateComponent(typeof(Avatar))]
[assembly: GenerateComponent(typeof(AlohaKit.Controls.Button))]
[assembly: GenerateComponent(typeof(AlohaKit.Controls.CheckBox))]
[assembly: GenerateComponent(typeof(AlohaKit.Controls.ProgressBar))]
[assembly: GenerateComponent(typeof(AlohaKit.Controls.Button), Aliases = new[] { "Button:AlhButton" })]
[assembly: GenerateComponent(typeof(AlohaKit.Controls.CheckBox), Aliases = new[] { "CheckBox:AlhCheckBox" })]
[assembly: GenerateComponent(typeof(AlohaKit.Controls.ProgressBar), Aliases = new[] { "ProgressBar:AlhProgressBar" })]
[assembly: GenerateComponent(typeof(NumericUpDown))]
[assembly: GenerateComponent(typeof(ProgressRadial))]
[assembly: GenerateComponent(typeof(ToggleSwitch),
Exclude = new[] { nameof(ToggleSwitch.Toggled) },
PropertyChangedEvents = new[] { nameof(ToggleSwitch.IsOn) })]
[assembly: GenerateComponent(typeof(AlohaKit.Controls.Slider))]
[assembly: GenerateComponent(typeof(AlohaKit.Controls.Slider), Aliases = new[] { "Slider:AlhSlider" })]
[assembly: GenerateComponent(typeof(PulseIcon))]

// CommunityToolkit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,24 @@ public partial class ComponentWrapperGenerator
//}

var typeToGenerate = generatedInfo.TypeSymbol;
var componentName = typeToGenerate.Name;
var componentName = generatedInfo.TypeAlias ?? typeToGenerate.Name;
var componentNamespace = GetComponentNamespace(typeToGenerate);

var baseType = GetBaseTypeOfInterest(typeToGenerate);
var componentBaseName = componentNamespace == GetComponentNamespace(baseType)
? baseType.Name
: $"{GetComponentNamespace(baseType)}.{baseType.Name}";
var componentBaseName = generatedInfo.BaseTypeInfo?.TypeAlias ?? baseType.Name;

if (componentNamespace != GetComponentNamespace(baseType))
componentBaseName = $"{GetComponentNamespace(baseType)}.{componentBaseName}";

// header
var headerText = generatedInfo.FileHeader;

// usings
var usings = GetDefaultUsings(typeToGenerate, componentNamespace);
var componentNamespacePrefix = GetNamespacePrefix(typeToGenerate, usings);
var generatedType = new GeneratedTypeInfo(compilation, generatedInfo, componentName, componentBaseName, typeToGenerate, usings);

var mauiTypeName = generatedType.GetTypeNameAndAddNamespace(typeToGenerate);

// props
var valueProperties = GeneratedPropertyInfo.GetValueProperties(generatedType);
var contentProperties = GeneratedPropertyInfo.GetContentProperties(generatedType);
Expand Down Expand Up @@ -130,7 +132,7 @@ namespace {componentNamespace}
{staticConstructorBody.Trim()}
}}
{propertyDeclarations}
public new {componentNamespacePrefix}{componentName} NativeControl => ({componentNamespacePrefix}{componentName})((BindableObject)this).NativeControl;
public new {mauiTypeName} NativeControl => ({mauiTypeName})((BindableObject)this).NativeControl;
{createNativeElement}
{handleParameter}{renderAdditionalElementContent}
Expand Down Expand Up @@ -168,32 +170,6 @@ private static List<UsingStatement> GetDefaultUsings(INamedTypeSymbol typeToGene
return usings;
}

private static string GetNamespacePrefix(INamedTypeSymbol type, List<UsingStatement> usings)
{
// Check if there's a 'using' already. If so, check if it has an alias. If not, add a new 'using'.
var namespaceAlias = string.Empty;
var namespaceName = type.ContainingNamespace.GetFullName();

var existingUsing = usings.FirstOrDefault(u => u.Namespace == namespaceName);
if (existingUsing == null)
{
usings.Add(new UsingStatement { Namespace = namespaceName, IsUsed = true, });
return string.Empty;
}
else
{
existingUsing.IsUsed = true;
if (existingUsing.Alias != null)
{
return existingUsing.Alias + ".";
}
else
{
return string.Empty;
}
}
}

internal static string GetXmlDocContents(ISymbol symbol, string indent)
{
var xmlDocString = symbol.GetDocumentationCommentXml();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class GenerateComponentSettings
private bool _isForcedGeneric;

public string FileHeader { get; set; }
public string TypeAlias { get; set; }
public INamedTypeSymbol TypeSymbol { get; set; }
public HashSet<string> Exclude { get; set; }
public HashSet<string> Include { get; set; }
Expand Down
14 changes: 12 additions & 2 deletions src/BlazorBindings.Maui.ComponentGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,27 @@ private static GenerateComponentSettings[] GetTypesToGenerate(Compilation compil
.Where(a => a.AttributeClass?.ToDisplayString() == "BlazorBindings.Maui.ComponentGenerator.GenerateComponentAttribute")
.Select(a =>
{
var typeSymbol = a.ConstructorArguments.FirstOrDefault().Value as INamedTypeSymbol;

var propertiesAliases = GetNamedArgumentValues(a, "Aliases")
.Select(v => v.Split(':'))
.ToDictionary(v => v[0], v => v[1]);

// Type alias has type name as a key.
propertiesAliases.Remove(typeSymbol.Name, out var typeAlias);

return new GenerateComponentSettings
{
FileHeader = FileHeader,
TypeSymbol = a.ConstructorArguments.FirstOrDefault().Value as INamedTypeSymbol,
TypeAlias = typeAlias,
TypeSymbol = typeSymbol,
Exclude = GetNamedArgumentValues(a, "Exclude").ToHashSet(),
Include = GetNamedArgumentValues(a, "Include").ToHashSet(),
ContentProperties = GetNamedArgumentValues(a, "ContentProperties").ToHashSet(),
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),
Aliases = GetNamedArgumentValues(a, "Aliases").Select(v => v.Split(':')).ToDictionary(v => v[0], v => v[1]),
Aliases = propertiesAliases,
IsGeneric = (a.NamedArguments.FirstOrDefault(a => a.Key == "IsGeneric").Value.Value as bool?) ?? false
};
})
Expand Down

0 comments on commit a198df9

Please sign in to comment.