Skip to content

Commit 859412a

Browse files
committed
add AssemblyMetadataAttribute output to assembly info
1 parent b700b4f commit 859412a

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/ApiListWriter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ private void WriteAssemblyInfo()
8888
BaseWriter.WriteLine($"// InformationalVersion: {assembly.GetAssemblyMetadataAttributeValue<AssemblyInformationalVersionAttribute, string>()}");
8989
BaseWriter.WriteLine($"// TargetFramework: {assembly.GetAssemblyMetadataAttributeValue<TargetFrameworkAttribute, string>()}");
9090
BaseWriter.WriteLine($"// Configuration: {assembly.GetAssemblyMetadataAttributeValue<AssemblyConfigurationAttribute, string>()}");
91+
92+
foreach (var (key, value) in assembly.GetAssemblyMetadataAttributes<AssemblyMetadataAttribute, (object?, object?)>(
93+
static constructorArguments => (
94+
constructorArguments.FirstOrDefault().Value,
95+
constructorArguments.Skip(1).FirstOrDefault().Value
96+
)
97+
)) {
98+
BaseWriter.WriteLine($"// Metadata: {key}={value}");
99+
}
91100
}
92101

93102
private unsafe void WriteReferencedAssemblies()

src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/AssemblyExtensions.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,34 @@ public static class AssemblyExtensions {
1919
// TODO: change assm -> assembly
2020
public static TValue GetAssemblyMetadataAttributeValue<TAssemblyMetadataAttribute, TValue>(this Assembly assm)
2121
where TAssemblyMetadataAttribute : Attribute
22-
=> (TValue)GetAssemblyMetadataAttributeValue<TAssemblyMetadataAttribute>(
22+
=> (TValue?)GetAssemblyMetadataAttributesCore<TAssemblyMetadataAttribute>(
2323
assm ?? throw new ArgumentNullException(nameof(assm))
24-
)!;
24+
)
25+
.FirstOrDefault(static d => ROCType.FullNameEquals(typeof(TAssemblyMetadataAttribute), d.AttributeType))
26+
?.ConstructorArguments
27+
?.FirstOrDefault()
28+
.Value;
29+
30+
internal static IEnumerable<TValue?>
31+
GetAssemblyMetadataAttributes<TAssemblyMetadataAttribute, TValue>(
32+
this Assembly assembly,
33+
Func<IList<CustomAttributeTypedArgument>, TValue?> constructorArgumentsConverter
34+
) where TAssemblyMetadataAttribute : Attribute
35+
=> GetAssemblyMetadataAttributesCore<TAssemblyMetadataAttribute>(
36+
assembly ?? throw new ArgumentNullException(nameof(assembly))
37+
)
38+
.Where(static d => ROCType.FullNameEquals(typeof(TAssemblyMetadataAttribute), d.AttributeType))
39+
.Select(attr => constructorArgumentsConverter(attr.ConstructorArguments));
2540

26-
private static object? GetAssemblyMetadataAttributeValue<TAssemblyMetadataAttribute>(Assembly assm)
41+
private static IList<CustomAttributeData> GetAssemblyMetadataAttributesCore<TAssemblyMetadataAttribute>(this Assembly assembly)
2742
where TAssemblyMetadataAttribute : Attribute
2843
{
29-
IList<System.Reflection.CustomAttributeData> attributesData;
30-
3144
try {
32-
attributesData = assm.GetCustomAttributesData();
45+
return assembly.GetCustomAttributesData();
3346
}
3447
catch (FileNotFoundException ex) { // when (!string.IsNullOrEmpty(ex.FusionLog))
3548
// in the case of reference assembly cannot be loaded
36-
throw AssemblyFileNotFoundException.Create(assm.GetName(), ex);
49+
throw AssemblyFileNotFoundException.Create(assembly.GetName(), ex);
3750
}
38-
39-
return attributesData
40-
.FirstOrDefault(static d => ROCType.FullNameEquals(typeof(TAssemblyMetadataAttribute), d.AttributeType))
41-
?.ConstructorArguments
42-
?.FirstOrDefault()
43-
.Value;
4451
}
4552
}

tests/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/ApiListWriter.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ private static System.Collections.IEnumerable YieldTestCases_WriteHeader_WriteAs
379379
[assembly: System.Reflection.AssemblyInformationalVersionAttribute(""1.2.3-InformationalVersion"")]
380380
[assembly: System.Reflection.AssemblyConfigurationAttribute(""Configuration"")]
381381
[assembly: System.Runtime.Versioning.TargetFrameworkAttribute(""TargetFramework"")]
382+
[assembly: System.Reflection.AssemblyMetadataAttribute(""RepositoryUrl"", ""https://example.com/nonexistent.git"")]
383+
[assembly: System.Reflection.AssemblyMetadataAttribute(""RepositoryBranch"", ""main"")]
382384
",
383385
"TestCase1Assembly",
384386
new[] {
@@ -391,7 +393,9 @@ private static System.Collections.IEnumerable YieldTestCases_WriteHeader_WriteAs
391393
// AssemblyVersion: 1.2.3.4
392394
// InformationalVersion: 1.2.3-InformationalVersion
393395
// TargetFramework: TargetFramework
394-
// Configuration: Configuration"
396+
// Configuration: Configuration
397+
// Metadata: RepositoryUrl=https://example.com/nonexistent.git
398+
// Metadata: RepositoryBranch=main"
395399
};
396400

397401
yield return new object[] {

0 commit comments

Comments
 (0)