Skip to content

Commit b05c24c

Browse files
committed
refactor 2
1 parent 8b80035 commit b05c24c

File tree

1 file changed

+90
-67
lines changed

1 file changed

+90
-67
lines changed

Src/Generate/Program.cs

Lines changed: 90 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ public static class Resources
316316
{
317317
""");
318318

319-
var metadataContent = new StringBuilder();
320-
metadataContent.AppendLine($$"""
319+
var refContent = new StringBuilder();
320+
refContent.AppendLine($$"""
321321
public static partial class {{name}}
322322
{
323323
public static class References
@@ -332,62 +332,15 @@ public static class ReferenceInfos
332332
{
333333
""");
334334

335-
var lowerName = name.ToLower();
336-
var allPropNames = new List<string>();
337-
foreach (var dllInfo in FindDlls(realPackagePaths))
338-
{
339-
var dllName = Path.GetFileName(dllInfo.FilePath)!;
340-
var dll = Path.GetFileNameWithoutExtension(dllName);
341-
var logicalName = $"{lowerName}.{dll}";
342-
var dllResourcePath = Path.Join(targetsPrefix, dllInfo.RelativeFilePath);
343-
344-
targetsContent.AppendLine($$"""
345-
<EmbeddedResource Include="{{dllResourcePath}}" WithCulture="false">
346-
<LogicalName>{{logicalName}}</LogicalName>
347-
<Link>Resources\{{lowerName}}\{{dllName}}</Link>
348-
</EmbeddedResource>
349-
""");
350-
351-
var propName = dll.Replace(".", "");
352-
allPropNames.Add(propName);
353-
var fieldName = $"_{propName}";
354-
resourcesContent.AppendLine($$"""
355-
/// <summary>
356-
/// The image bytes for {{dllName}}
357-
/// </summary>
358-
public static byte[] {{propName}} => ResourceLoader.GetOrCreateResource(ref {{fieldName}}, "{{logicalName}}");
359-
private static byte[]? {{fieldName}};
360-
361-
""");
362-
363-
refInfoContent.AppendLine($$"""
364-
365-
/// <summary>
366-
/// The <see cref="ReferenceInfo"/> for {{dllName}}
367-
/// </summary>
368-
public static ReferenceInfo {{propName}} => new ReferenceInfo("{{dllName}}", Resources.{{propName}}, {{name}}.References.{{propName}}, global::System.Guid.Parse("{{dllInfo.Mvid}}"));
369-
""");
370-
371-
metadataContent.AppendLine($$"""
372-
private static PortableExecutableReference? {{fieldName}};
373-
374-
/// <summary>
375-
/// The <see cref="PortableExecutableReference"/> for {{dllName}}
376-
/// </summary>
377-
public static PortableExecutableReference {{propName}}
378-
{
379-
get
380-
{
381-
if ({{fieldName}} is null)
382-
{
383-
{{fieldName}} = AssemblyMetadata.CreateFromImage(Resources.{{propName}}).GetReference(filePath: "{{dllName}}", display: "{{dll}} ({{lowerName}})");
384-
}
385-
return {{fieldName}};
386-
}
387-
}
388-
389-
""");
390-
}
335+
var allPropNames = ProcessDlls(
336+
name,
337+
realPackagePaths,
338+
realPackagePrefix,
339+
targetsPrefix,
340+
targetsContent,
341+
resourcesContent,
342+
refContent,
343+
refInfoContent);
391344

392345
refInfoContent.AppendLine("""
393346
private static ImmutableArray<ReferenceInfo> _all;
@@ -401,7 +354,7 @@ public static ImmutableArray<ReferenceInfo> All
401354
[
402355
""");
403356

404-
metadataContent.AppendLine("""
357+
refContent.AppendLine("""
405358
private static ImmutableArray<PortableExecutableReference> _all;
406359
public static ImmutableArray<PortableExecutableReference> All
407360
{
@@ -416,10 +369,10 @@ public static ImmutableArray<PortableExecutableReference> All
416369
foreach (var propName in allPropNames)
417370
{
418371
refInfoContent.AppendLine($" {propName},");
419-
metadataContent.AppendLine($" {propName},");
372+
refContent.AppendLine($" {propName},");
420373
}
421374

422-
metadataContent.AppendLine("""
375+
refContent.AppendLine("""
423376
];
424377
}
425378
return _all;
@@ -462,7 +415,7 @@ namespace Basic.Reference.Assemblies;
462415

463416
codeContent.AppendLine(resourcesContent.ToString());
464417
codeContent.AppendLine(refInfoContent.ToString());
465-
codeContent.AppendLine(metadataContent.ToString());
418+
codeContent.AppendLine(refContent.ToString());
466419
codeContent.AppendLine(GetReferenceInfo(name));
467420

468421
targetsContent.AppendLine("""
@@ -472,13 +425,83 @@ namespace Basic.Reference.Assemblies;
472425

473426
return (codeContent.ToString(), targetsContent.ToString());
474427

475-
IEnumerable<(string FilePath, string RelativeFilePath, Guid Mvid)> FindDlls(string[] packagePaths)
428+
static List<string> ProcessDlls(
429+
string name,
430+
string[] packagePaths,
431+
string packagePrefix,
432+
string targetsPrefix,
433+
StringBuilder targetsContent,
434+
StringBuilder resourcesContent,
435+
StringBuilder refContent,
436+
StringBuilder refInfoContent)
437+
{
438+
var lowerName = name.ToLower();
439+
var allPropNames = new List<string>();
440+
foreach (var dllInfo in FindDlls(packagePaths, packagePrefix))
441+
{
442+
var dllName = Path.GetFileName(dllInfo.FilePath)!;
443+
var dll = Path.GetFileNameWithoutExtension(dllName);
444+
var logicalName = $"{lowerName}.{dll}";
445+
var dllResourcePath = Path.Join(targetsPrefix, dllInfo.RelativeFilePath);
446+
447+
targetsContent.AppendLine($$"""
448+
<EmbeddedResource Include="{{dllResourcePath}}" WithCulture="false">
449+
<LogicalName>{{logicalName}}</LogicalName>
450+
<Link>Resources\{{lowerName}}\{{dllName}}</Link>
451+
</EmbeddedResource>
452+
""");
453+
454+
var propName = dll.Replace(".", "");
455+
allPropNames.Add(propName);
456+
var fieldName = $"_{propName}";
457+
resourcesContent.AppendLine($$"""
458+
/// <summary>
459+
/// The image bytes for {{dllName}}
460+
/// </summary>
461+
public static byte[] {{propName}} => ResourceLoader.GetOrCreateResource(ref {{fieldName}}, "{{logicalName}}");
462+
private static byte[]? {{fieldName}};
463+
464+
""");
465+
466+
refInfoContent.AppendLine($$"""
467+
468+
/// <summary>
469+
/// The <see cref="ReferenceInfo"/> for {{dllName}}
470+
/// </summary>
471+
public static ReferenceInfo {{propName}} => new ReferenceInfo("{{dllName}}", Resources.{{propName}}, {{name}}.References.{{propName}}, global::System.Guid.Parse("{{dllInfo.Mvid}}"));
472+
""");
473+
474+
refContent.AppendLine($$"""
475+
private static PortableExecutableReference? {{fieldName}};
476+
477+
/// <summary>
478+
/// The <see cref="PortableExecutableReference"/> for {{dllName}}
479+
/// </summary>
480+
public static PortableExecutableReference {{propName}}
481+
{
482+
get
483+
{
484+
if ({{fieldName}} is null)
485+
{
486+
{{fieldName}} = AssemblyMetadata.CreateFromImage(Resources.{{propName}}).GetReference(filePath: "{{dllName}}", display: "{{dll}} ({{lowerName}})");
487+
}
488+
return {{fieldName}};
489+
}
490+
}
491+
492+
""");
493+
}
494+
495+
return allPropNames;
496+
}
497+
498+
static IEnumerable<(string FilePath, string RelativeFilePath, Guid Mvid)> FindDlls(string[] packagePaths, string packagePrefix)
476499
{
477500
var dllPaths = new List<string>();
478-
foreach (var realPackagePath in realPackagePaths)
501+
foreach (var packagePath in packagePaths)
479502
{
480-
dllPaths.AddRange(Directory.GetFiles(realPackagePath, "*.dll"));
481-
var facadesPath = Path.Combine(realPackagePath, "Facades");
503+
dllPaths.AddRange(Directory.GetFiles(packagePath, "*.dll"));
504+
var facadesPath = Path.Combine(packagePath, "Facades");
482505
if (Directory.Exists(facadesPath))
483506
{
484507
dllPaths.AddRange(Directory.GetFiles(facadesPath, "*.dll"));
@@ -493,7 +516,7 @@ namespace Basic.Reference.Assemblies;
493516
continue;
494517
}
495518

496-
var relativeFilePath = dllPath.Substring(realPackagePrefix.Length);
519+
var relativeFilePath = dllPath.Substring(packagePrefix.Length);
497520
yield return (dllPath, relativeFilePath, mvid);
498521
}
499522
}

0 commit comments

Comments
 (0)