Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
namespace Aspid.MVVM.Unity.Generators.ContextMenu;

[Generator(LanguageNames.CSharp)]
public sealed class AddComponentContextMenuGenerator : IIncrementalGenerator
public sealed class AddBinderContextMenuGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var provider = context.SyntaxProvider.ForAttributeWithMetadataName(AddComponentContextMenuAttribute.FullName, SyntacticPredicate, Find)
var provider = context.SyntaxProvider.ForAttributeWithMetadataName(AddBinderContextMenuAttribute.FullName, SyntacticPredicate, Find)
.Where(foundForSourceGenerator => foundForSourceGenerator.IsNeed)
.Select((foundForSourceGenerator, _) => foundForSourceGenerator.Container);

Expand All @@ -32,25 +32,42 @@ private static FoundForGenerator<ContextMenuData> Find(GeneratorAttributeSyntaxC
var declaration = (ClassDeclarationSyntax)context.TargetNode;
if (context.TargetSymbol is not INamedTypeSymbol symbol) return default;

if (symbol.HasAnyAttribute(out var attribute, AddComponentContextMenuAttribute))
if (symbol.HasAnyAttribute(out var attribute, AddBinderContextMenuAttribute))
{
if (attribute!.ConstructorArguments[0].Value is not ITypeSymbol type) return default;

string? path = null;

if (attribute.ConstructorArguments.Length > 1)
path = attribute.ConstructorArguments[1].Value?.ToString();

var arguments = attribute.NamedArguments;
var priority = (int)(arguments.FirstOrDefault(pair => pair.Key == "Priority").Value.Value ?? 100001);
var path = arguments.FirstOrDefault(pair => pair.Key == "Path").Value.Value as string;
var suPath = arguments.FirstOrDefault(pair => pair.Key == "SubPath").Value.Value as string;
suPath = suPath is not null ? $"{suPath}/" : string.Empty;

if (path is null)
{
if (symbol.HasAnyAttribute(out var addComponentMenuAttribute, "UnityEngine.AddComponentMenu"))
{
path = addComponentMenuAttribute!.ConstructorArguments[0].Value as string;

if (path is not null)
path = $"Add {type.Name} Binder" + path.Substring(path.LastIndexOf("/", StringComparison.Ordinal));
}

return new FoundForGenerator<ContextMenuData>(new ContextMenuData(declaration, symbol, symbol.Name, type.Name, path, priority));
path ??= $"Add {type.Name} Binder/{symbol.Name}";
}

if (!string.IsNullOrWhiteSpace(path) && !string.IsNullOrWhiteSpace(suPath))
{
var index = path.IndexOf('/');
if (index is -1) index = path.Length - 2;

index++;
if (index >= 0 && index < path.Length)
path = path.Insert(path.IndexOf('/') + 1, suPath);
}

return new FoundForGenerator<ContextMenuData>(new ContextMenuData(declaration, symbol, symbol.Name, type.Name, path, 100001));
}

return default;

string? GetName(string? path) =>
path?.Substring(path.LastIndexOf("/", StringComparison.Ordinal) + 1);
}

private static void GenerateCode(SourceProductionContext context, ContextMenuData data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Aspid.MVVM.Generators.Descriptions;
// ReSharper disable InconsistentNaming
public static partial class Classes
{
public static readonly AttributeText AddComponentContextMenuAttribute =
new ("AddComponentContextMenu", Namespaces.Aspid_MVVM);
public static readonly AttributeText AddBinderContextMenuAttribute =
new ("AddBinderContextMenu", Namespaces.Aspid_MVVM);
}
11 changes: 11 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project>

<Target Name="CopyToUnity" AfterTargets="Build" Condition="'$(IsRoslynComponent)' == 'true'">
<PropertyGroup>
<_UnityDestination>$(MSBuildThisFileDirectory)../Aspid.MVVM/Assets/Aspid/MVVM/Unity/</_UnityDestination>
</PropertyGroup>
<MakeDir Directories="$(_UnityDestination)" />
<Copy SourceFiles="$(TargetPath)" DestinationFolder="$(_UnityDestination)" SkipUnchangedFiles="true" />
</Target>

</Project>