Skip to content

Commit

Permalink
elements, url factories, file name factory and sections are now case …
Browse files Browse the repository at this point in the history
…insensitive in configuration (closes #157)
  • Loading branch information
Doraku committed Oct 11, 2024
1 parent cfe6a00 commit 3ec6ac2
Show file tree
Hide file tree
Showing 59 changed files with 166 additions and 153 deletions.
1 change: 1 addition & 0 deletions documentation/NEXT_RELEASENOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

- added DefaultDocumentation.GeneratedAccessModifiers.Api which regroup Public, Protected and InternalProtected access modifiers (closes #116)
- added support for record
- elements, url factories, file name factory and sections are now case insensitive in configuration (closes #157)

## Bug fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static bool TryGetTypeParameterDocItem(this DocItem item, string name, [N
{
if (currentItem is ITypeParameterizedDocItem typeParameters)
{
typeParameterDocItem = typeParameters.TypeParameters.FirstOrDefault(i => i.TypeParameter.Name == name);
typeParameterDocItem = typeParameters.TypeParameters.FirstOrDefault(typeParameter => typeParameter.TypeParameter.Name == name);
}

currentItem = currentItem.Parent;
Expand Down Expand Up @@ -97,7 +97,7 @@ public static bool TryGetParameterDocItem(this DocItem item, string name, [NotNu
{
if (currentItem is IParameterizedDocItem typeParameters)
{
parameterDocItem = typeParameters.Parameters.FirstOrDefault(i => i.Parameter.Name == name);
parameterDocItem = typeParameters.Parameters.FirstOrDefault(parameter => parameter.Parameter.Name == name);
}

currentItem = currentItem.Parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public static class IPageContextExtensions
context.ThrowIfNull();
id.ThrowIfNull();

return context.UrlFactories.Select(f => f.GetUrl(context, id)).FirstOrDefault(url => url is not null);
return context.UrlFactories.Select(urlFactory => urlFactory.GetUrl(context, id)).FirstOrDefault(url => url is not null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public ConstructorDocItem(TypeDocItem parent, IMethod method, XElement? document
documentation)
{
Method = method;
Parameters = method.Parameters.Select(p => new ParameterDocItem(this, p)).ToArray();
Parameters = method.Parameters.Select(parameter => new ParameterDocItem(this, parameter)).ToArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ExplicitInterfaceImplementationDocItem(TypeDocItem parent, IProperty prop
{
Member = property;
TypeParameters = [];
Parameters = property.Parameters.Select(p => new ParameterDocItem(this, p)).ToArray();
Parameters = property.Parameters.Select(parameter => new ParameterDocItem(this, parameter)).ToArray();
}

/// <summary>
Expand All @@ -72,7 +72,7 @@ public ExplicitInterfaceImplementationDocItem(TypeDocItem parent, IMethod method
documentation)
{
Member = method;
TypeParameters = method.TypeParameters.Select(p => new TypeParameterDocItem(this, p)).ToArray();
Parameters = method.Parameters.Select(p => new ParameterDocItem(this, p)).ToArray();
TypeParameters = method.TypeParameters.Select(typeParameter => new TypeParameterDocItem(this, typeParameter)).ToArray();
Parameters = method.Parameters.Select(parameter => new ParameterDocItem(this, parameter)).ToArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public MethodDocItem(TypeDocItem parent, IMethod method, XElement? documentation
documentation)
{
Method = method;
TypeParameters = method.TypeParameters.Select(p => new TypeParameterDocItem(this, p)).ToArray();
Parameters = method.Parameters.Select(p => new ParameterDocItem(this, p)).ToArray();
TypeParameters = method.TypeParameters.Select(typeParameter => new TypeParameterDocItem(this, typeParameter)).ToArray();
Parameters = method.Parameters.Select(parameter => new ParameterDocItem(this, parameter)).ToArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public OperatorDocItem(TypeDocItem parent, IMethod method, XElement? documentati
documentation)
{
Method = method;
Parameters = method.Parameters.Select(p => new ParameterDocItem(this, p)).ToArray();
Parameters = method.Parameters.Select(parameter => new ParameterDocItem(this, parameter)).ToArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public PropertyDocItem(TypeDocItem parent, IProperty property, XElement? documen
documentation)
{
Property = property;
Parameters = Property.Parameters.Select(p => new ParameterDocItem(this, p)).ToArray();
Parameters = Property.Parameters.Select(parameter => new ParameterDocItem(this, parameter)).ToArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal ParameterDocItem(DocItem parent, IParameter parameter)
$"{parent.Id}.{parameter.Name}",
$"{parent.FullName}.{parameter.Name}",
parameter.Name,
parent.Documentation.GetParameters()?.FirstOrDefault(d => d.GetNameAttribute() == parameter.Name))
parent.Documentation.GetParameters()?.FirstOrDefault(node => node.GetNameAttribute() == parameter.Name))
{
Parameter = parameter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal TypeParameterDocItem(DocItem parent, ITypeParameter typeParameter)
$"{parent.Id}.{typeParameter.Name}",
$"{parent.FullName}.{typeParameter.Name}",
typeParameter.Name,
parent.Documentation.GetTypeParameters()?.FirstOrDefault(d => d.GetNameAttribute() == typeParameter.Name))
parent.Documentation.GetTypeParameters()?.FirstOrDefault(node => node.GetNameAttribute() == typeParameter.Name))
{
TypeParameter = typeParameter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public DelegateDocItem(DocItem parent, ITypeDefinition type, XElement? documenta
: base(parent, type, documentation)
{
InvokeMethod = type.GetDelegateInvokeMethod();
Parameters = InvokeMethod.Parameters.Select(p => new ParameterDocItem(this, p)).ToArray();
Parameters = InvokeMethod.Parameters.Select(parameter => new ParameterDocItem(this, parameter)).ToArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ private protected TypeDocItem(DocItem parent, ITypeDefinition type, XElement? do
documentation)
{
Type = type;
TypeParameters = Type.TypeParameters.Select(p => new TypeParameterDocItem(this, p)).ToArray();
TypeParameters = Type.TypeParameters.Select(typeParameter => new TypeParameterDocItem(this, typeParameter)).ToArray();
}
}
44 changes: 22 additions & 22 deletions source/DefaultDocumentation.Common/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,24 @@ private Generator(Target loggerTarget, IRawSettings settings)
Environment.CurrentDirectory = Path.GetDirectoryName(settings.ConfigurationFilePath);
}

AddSetting(s => s.LogLevel, string.IsNullOrEmpty);
AddSetting(s => s.AssemblyFilePath, string.IsNullOrEmpty);
AddSetting(s => s.DocumentationFilePath, string.IsNullOrEmpty);
AddSetting(s => s.ProjectDirectoryPath, string.IsNullOrEmpty);
AddSetting(s => s.OutputDirectoryPath, string.IsNullOrEmpty);
AddSetting(s => s.AssemblyPageName, string.IsNullOrEmpty);
AddSetting(s => s.GeneratedAccessModifiers, v => v == GeneratedAccessModifiers.Default);
AddSetting(s => s.IncludeUndocumentedItems, v => !v);
AddSetting(s => s.GeneratedPages, v => v == GeneratedPages.Default);
AddSetting(s => s.LinksOutputFilePath, string.IsNullOrEmpty);
AddSetting(s => s.LinksBaseUrl, string.IsNullOrEmpty);
AddSetting(s => s.ExternLinksFilePaths, v => !(v ?? []).Any(), v => v.ToArray());
AddSetting(settings => settings.LogLevel, string.IsNullOrEmpty);
AddSetting(settings => settings.AssemblyFilePath, string.IsNullOrEmpty);
AddSetting(settings => settings.DocumentationFilePath, string.IsNullOrEmpty);
AddSetting(settings => settings.ProjectDirectoryPath, string.IsNullOrEmpty);
AddSetting(settings => settings.OutputDirectoryPath, string.IsNullOrEmpty);
AddSetting(settings => settings.AssemblyPageName, string.IsNullOrEmpty);
AddSetting(settings => settings.GeneratedAccessModifiers, value => value == GeneratedAccessModifiers.Default);
AddSetting(settings => settings.IncludeUndocumentedItems, value => !value);
AddSetting(settings => settings.GeneratedPages, value => value == GeneratedPages.Default);
AddSetting(settings => settings.LinksOutputFilePath, string.IsNullOrEmpty);
AddSetting(settings => settings.LinksBaseUrl, string.IsNullOrEmpty);
AddSetting(settings => settings.ExternLinksFilePaths, value => !(value ?? []).Any(), value => value.ToArray());
// context settings
AddSetting(s => s.Plugins, v => !(v ?? []).Any(), v => v.ToArray());
AddSetting(s => s.FileNameFactory, string.IsNullOrEmpty, "FullName");
AddSetting(s => s.UrlFactories, v => !(v ?? []).Any(), v => v.ToArray(), ["DocItem", "DotnetApi"]);
AddSetting(s => s.Sections, v => !(v ?? []).Any(), v => v.ToArray(), ["Header", "Default"]);
AddSetting(s => s.Elements, v => !(v ?? []).Any(), v => v.ToArray());
AddSetting(settings => settings.Plugins, value => !(value ?? []).Any(), value => value.ToArray());
AddSetting(settings => settings.FileNameFactory, string.IsNullOrEmpty, "FullName");
AddSetting(settings => settings.UrlFactories, value => !(value ?? []).Any(), value => value.ToArray(), ["DocItem", "DotnetApi"]);
AddSetting(settings => settings.Sections, value => !(value ?? []).Any(), value => value.ToArray(), ["Header", "Default"]);
AddSetting(settings => settings.Elements, value => !(value ?? []).Any(), value => value.ToArray());

string? logLevel = GetSetting<string>(nameof(logLevel));
LoggingConfiguration logConfiguration = new();
Expand Down Expand Up @@ -92,7 +92,7 @@ private Generator(Target loggerTarget, IRawSettings settings)
_configuration,
new[] { typeof(Markdown.Writers.MarkdownWriter).Assembly }
.Concat((GetSetting<string[]>(nameof(settings.Plugins)) ?? Enumerable.Empty<string>()).Select(Assembly.LoadFrom))
.SelectMany(a => a.GetTypes())
.SelectMany(assembly => assembly.GetTypes())
.ToArray(),
resolvedSettings,
DocItemReader.GetItems(resolvedSettings));
Expand Down Expand Up @@ -125,7 +125,7 @@ private void AddSetting<TSetting>(
Expression<Func<IRawSettings, TSetting>> property,
Predicate<TSetting> noValuePredicate,
TSetting? defaultValue = default)
=> AddSetting(property, noValuePredicate, v => v, defaultValue);
=> AddSetting(property, noValuePredicate, value => value, defaultValue);

private void WritePage(DocItem item, StringBuilder builder)
{
Expand All @@ -134,7 +134,7 @@ private void WritePage(DocItem item, StringBuilder builder)

PageWriter writer = new(builder, new PageContext(_context, item));

foreach (ISection sectionWriter in writer.Context.GetSetting(item, c => c.Sections) ?? [])
foreach (ISection sectionWriter in writer.Context.GetSetting(item, context => context.Sections) ?? [])
{
sectionWriter.Write(writer);
}
Expand All @@ -159,7 +159,7 @@ private void WriteLinks()
PageContext context = new(_context, new ExternDocItem("T:", "", ""));

writer.WriteLine(_context.Settings.LinksBaseUrl);
foreach (DocItem item in _context.Items.Values.Where(i => i is not ExternDocItem and not AssemblyDocItem and not TypeParameterDocItem and not ParameterDocItem))
foreach (DocItem item in _context.Items.Values.Where(item => item is not ExternDocItem and not AssemblyDocItem and not TypeParameterDocItem and not ParameterDocItem))
{
writer.Write(item.Id);
writer.Write('|');
Expand All @@ -179,7 +179,7 @@ private void Execute()

StringBuilder builder = new();

foreach (DocItem item in _context.Items.Values.Where(i => i.HasOwnPage(_context)))
foreach (DocItem item in _context.Items.Values.Where(item => item.HasOwnPage(_context)))
{
try
{
Expand Down
27 changes: 16 additions & 11 deletions source/DefaultDocumentation.Common/Internal/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,34 @@ public Context(
_configuration = configuration;

string? fileNameFactory = GetSetting<string>(nameof(IRawSettings.FileNameFactory));
FileNameFactory = string.IsNullOrEmpty(fileNameFactory) ? null : availableTypes
.Where(t => typeof(IFileNameFactory).IsAssignableFrom(t) && !t.IsAbstract)
.Select(t => (IFileNameFactory)Activator.CreateInstance(t))
.LastOrDefault(f => f.Name == fileNameFactory || $"{f.GetType().FullName} {f.GetType().Assembly.GetName().Name}" == fileNameFactory)
?? throw new Exception($"FileNameFactory '{fileNameFactory}' not found");
FileNameFactory =
string.IsNullOrEmpty(fileNameFactory)
? null
: availableTypes
.Where(type => typeof(IFileNameFactory).IsAssignableFrom(type) && !type.IsAbstract)
.Select(type => (IFileNameFactory)Activator.CreateInstance(type))
.LastOrDefault(fineNameFactory =>
fileNameFactory!.Equals(fineNameFactory.Name, StringComparison.OrdinalIgnoreCase)
|| $"{fineNameFactory.GetType().FullName} {fineNameFactory.GetType().Assembly.GetName().Name}" == fileNameFactory)
?? throw new Exception($"FileNameFactory '{fileNameFactory}' not found");

string[]? sections = GetSetting<string[]>(nameof(IRawSettings.Sections));

if (sections != null)
{
Dictionary<string, ISection> availableSections = availableTypes
.Where(t => typeof(ISection).IsAssignableFrom(t) && !t.IsAbstract)
.Select(t => (ISection)Activator.CreateInstance(t))
.GroupBy(w => w.Name)
.ToDictionary(w => w.Key, w => w.Last());
.Where(type => typeof(ISection).IsAssignableFrom(type) && !type.IsAbstract)
.Select(type => (ISection)Activator.CreateInstance(type))
.GroupBy(section => section.Name, StringComparer.OrdinalIgnoreCase)
.ToDictionary(group => group.Key, group => group.Last(), StringComparer.OrdinalIgnoreCase);

Sections = sections
.Select(id =>
availableSections.TryGetValue(id, out ISection section)
? section
: availableTypes
.Where(t => typeof(ISection).IsAssignableFrom(t) && !t.IsAbstract && $"{t.FullName} {t.Assembly.GetName().Name}" == id)
.Select(t => (ISection)Activator.CreateInstance(t))
.Where(type => typeof(ISection).IsAssignableFrom(type) && !type.IsAbstract && $"{type.FullName} {type.Assembly.GetName().Name}" == id)
.Select(type => (ISection)Activator.CreateInstance(type))
.FirstOrDefault()
?? throw new Exception($"Section '{id}' not found"))
.ToArray();
Expand Down
8 changes: 4 additions & 4 deletions source/DefaultDocumentation.Common/Internal/DocItemReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private DocItemReader(Settings settings)
GetDocumentation($"T:{_decompiler.TypeSystem.MainModule.AssemblyName}.AssemblyDoc"));
Add(assemblyDocItem);

foreach (ITypeDefinition type in _decompiler.TypeSystem.MainModule.TypeDefinitions.Where(t => t.Name is not "NamespaceDoc" and not "AssemblyDoc"))
foreach (ITypeDefinition type in _decompiler.TypeSystem.MainModule.TypeDefinitions.Where(type => type.Name is not "NamespaceDoc" and not "AssemblyDoc"))
{
_logger.Debug($"handling type \"{type.FullName}\"");

Expand Down Expand Up @@ -273,7 +273,7 @@ private bool TryGetDocumentation(IEntity? entity, [NotNullWhen(true)] out XEleme
.GetAllBaseTypeDefinitions()
.Reverse()
.Skip(1)
.FirstOrDefault(t => TryGetDocumentation(t, out baseDocumentation, referencedIds));
.FirstOrDefault(baseType => TryGetDocumentation(baseType, out baseDocumentation, referencedIds));
}
else if (entity is IMember member && member.IsExplicitInterfaceImplementation)
{
Expand All @@ -287,8 +287,8 @@ private bool TryGetDocumentation(IEntity? entity, [NotNullWhen(true)] out XEleme
.GetAllBaseTypeDefinitions()
.Reverse()
.Skip(1)
.SelectMany(t => t.Members)
.FirstOrDefault(e => e.GetIdString()[e.DeclaringTypeDefinition.GetIdString().Length..] == id && TryGetDocumentation(e, out baseDocumentation, referencedIds));
.SelectMany(baseType => baseType.Members)
.FirstOrDefault(member => member.GetIdString()[member.DeclaringTypeDefinition.GetIdString().Length..] == id && TryGetDocumentation(member, out baseDocumentation, referencedIds));
}

documentation = baseDocumentation;
Expand Down
Loading

0 comments on commit 3ec6ac2

Please sign in to comment.