Skip to content

Commit 2baa32f

Browse files
committed
Move get valid plugin info to shared space
1 parent 5a92b4b commit 2baa32f

File tree

2 files changed

+91
-83
lines changed

2 files changed

+91
-83
lines changed

Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs

Lines changed: 3 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ private void Execute(SourceProductionContext spc,
100100
var assemblyName = compilation.AssemblyName ?? Constants.DefaultNamespace;
101101
var useDI = configOptions.GetFLLUseDependencyInjection();
102102

103-
var pluginInfo = GetValidPluginInfo(pluginClasses, spc, useDI);
103+
var pluginInfo = PluginInfoHelper.GetValidPluginInfoAndReportDiagnostic(pluginClasses, spc, useDI);
104+
105+
if (pluginInfo == null) return;
104106

105107
GenerateSource(
106108
spc,
@@ -421,88 +423,6 @@ private static string GetLocalizationKeyFromInvocation(GeneratorSyntaxContext co
421423

422424
#endregion
423425

424-
#region Get Plugin Class Info
425-
426-
private static PluginClassInfo GetValidPluginInfo(
427-
ImmutableArray<PluginClassInfo> pluginClasses,
428-
SourceProductionContext context,
429-
bool useDI)
430-
{
431-
// If p is null, this class does not implement IPluginI18n
432-
var iPluginI18nClasses = pluginClasses.Where(p => p != null).ToArray();
433-
if (iPluginI18nClasses.Length == 0)
434-
{
435-
context.ReportDiagnostic(Diagnostic.Create(
436-
SourceGeneratorDiagnostics.CouldNotFindPluginEntryClass,
437-
Location.None
438-
));
439-
return null;
440-
}
441-
442-
// If we use dependency injection, we do not need to check if there is a valid plugin context
443-
// Also we do not need to return the plugin info
444-
if (useDI)
445-
{
446-
return null;
447-
}
448-
449-
// If p.PropertyName is null, this class does not have PluginInitContext property
450-
var iPluginI18nClassesWithContext = iPluginI18nClasses.Where(p => p.PropertyName != null).ToArray();
451-
if (iPluginI18nClassesWithContext.Length == 0)
452-
{
453-
foreach (var pluginClass in iPluginI18nClasses)
454-
{
455-
context.ReportDiagnostic(Diagnostic.Create(
456-
SourceGeneratorDiagnostics.CouldNotFindContextProperty,
457-
pluginClass.Location,
458-
pluginClass.ClassName
459-
));
460-
}
461-
return null;
462-
}
463-
464-
// Rest classes have implemented IPluginI18n and have PluginInitContext property
465-
// Check if the property is valid
466-
foreach (var pluginClass in iPluginI18nClassesWithContext)
467-
{
468-
if (pluginClass.IsValid == true)
469-
{
470-
return pluginClass;
471-
}
472-
473-
if (!pluginClass.IsStatic)
474-
{
475-
context.ReportDiagnostic(Diagnostic.Create(
476-
SourceGeneratorDiagnostics.ContextPropertyNotStatic,
477-
pluginClass.Location,
478-
pluginClass.PropertyName
479-
));
480-
}
481-
482-
if (pluginClass.IsPrivate)
483-
{
484-
context.ReportDiagnostic(Diagnostic.Create(
485-
SourceGeneratorDiagnostics.ContextPropertyIsPrivate,
486-
pluginClass.Location,
487-
pluginClass.PropertyName
488-
));
489-
}
490-
491-
if (pluginClass.IsProtected)
492-
{
493-
context.ReportDiagnostic(Diagnostic.Create(
494-
SourceGeneratorDiagnostics.ContextPropertyIsProtected,
495-
pluginClass.Location,
496-
pluginClass.PropertyName
497-
));
498-
}
499-
}
500-
501-
return null;
502-
}
503-
504-
#endregion
505-
506426
#region Generate Source
507427

508428
private static void GenerateSource(
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System.Collections.Immutable;
2+
using System.Linq;
3+
using Flow.Launcher.Localization.Shared;
4+
using Microsoft.CodeAnalysis;
5+
6+
namespace Flow.Launcher.Localization.SourceGenerators
7+
{
8+
internal class PluginInfoHelper
9+
{
10+
public static PluginClassInfo GetValidPluginInfoAndReportDiagnostic(
11+
ImmutableArray<PluginClassInfo> pluginClasses,
12+
SourceProductionContext context,
13+
bool useDI)
14+
{
15+
// If p is null, this class does not implement IPluginI18n
16+
var iPluginI18nClasses = pluginClasses.Where(p => p != null).ToArray();
17+
if (iPluginI18nClasses.Length == 0)
18+
{
19+
context.ReportDiagnostic(Diagnostic.Create(
20+
SourceGeneratorDiagnostics.CouldNotFindPluginEntryClass,
21+
Location.None
22+
));
23+
return null;
24+
}
25+
26+
// If we use dependency injection, we do not need to check if there is a valid plugin context
27+
// Also we do not need to return the plugin info
28+
if (useDI)
29+
{
30+
return null;
31+
}
32+
33+
// If p.PropertyName is null, this class does not have PluginInitContext property
34+
var iPluginI18nClassesWithContext = iPluginI18nClasses.Where(p => p.PropertyName != null).ToArray();
35+
if (iPluginI18nClassesWithContext.Length == 0)
36+
{
37+
foreach (var pluginClass in iPluginI18nClasses)
38+
{
39+
context.ReportDiagnostic(Diagnostic.Create(
40+
SourceGeneratorDiagnostics.CouldNotFindContextProperty,
41+
pluginClass.Location,
42+
pluginClass.ClassName
43+
));
44+
}
45+
return null;
46+
}
47+
48+
// Rest classes have implemented IPluginI18n and have PluginInitContext property
49+
// Check if the property is valid
50+
foreach (var pluginClass in iPluginI18nClassesWithContext)
51+
{
52+
if (pluginClass.IsValid == true)
53+
{
54+
return pluginClass;
55+
}
56+
57+
if (!pluginClass.IsStatic)
58+
{
59+
context.ReportDiagnostic(Diagnostic.Create(
60+
SourceGeneratorDiagnostics.ContextPropertyNotStatic,
61+
pluginClass.Location,
62+
pluginClass.PropertyName
63+
));
64+
}
65+
66+
if (pluginClass.IsPrivate)
67+
{
68+
context.ReportDiagnostic(Diagnostic.Create(
69+
SourceGeneratorDiagnostics.ContextPropertyIsPrivate,
70+
pluginClass.Location,
71+
pluginClass.PropertyName
72+
));
73+
}
74+
75+
if (pluginClass.IsProtected)
76+
{
77+
context.ReportDiagnostic(Diagnostic.Create(
78+
SourceGeneratorDiagnostics.ContextPropertyIsProtected,
79+
pluginClass.Location,
80+
pluginClass.PropertyName
81+
));
82+
}
83+
}
84+
85+
return null;
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)