Skip to content

Commit

Permalink
#27: Added first version of Dependencies finder
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Dec 26, 2023
1 parent 6d56374 commit cb7b745
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Synergy.Documentation.Api;
using Synergy.Documentation.Markup;

namespace Synergy.Documentation.Tests.Architecture.Dependencies;

[UsesVerify]
public class Relations
{
[Theory]
[InlineData(typeof(Markdown))]
[InlineData(typeof(Markdown.Document))]
public async Task Generate(Type type)
{
// ARRANGE
var dependencies = Synergy.Documentation.Api.Dependencies.Of(type);


// TODO: Marcin Celej [from: Marcin Celej on: 26-12-2023]: Doe not work for Markdown.Document
// ACT
var publicApi = ApiDescription.GenerateFor(dependencies);

// ASSERT
await Verifier.Verify(publicApi, "md")
.UseMethodName("of." + type.Name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Synergy.Documentation.Markup.Markdown (class)
- ctor()

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
- GetEnumerator() : IEnumerator<Markdown+IElement>
- ToString() : string

## Api.Dependencies (class)
- ctor()
- Dependencies.Of(
root: Type
) : List<Type>
- DependsOn(
type: Type
) : IEnumerable<Type> [IteratorStateMachine]

## Code.ClassReader (class)
- ctor()
- ClassReader.ReadMethodBody(
Expand Down
64 changes: 64 additions & 0 deletions Documentation/Synergy.Documentation/Api/Dependencies.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Collections;
using System.Reflection;
using Synergy.Catalogue;

namespace Synergy.Documentation.Api;

public class Dependencies
{
public static List<Type> Of(Type root)
{
return new Dependencies().DependsOn(root).ToList();
}

private readonly List<Type> visited = new(100);
public Dependencies()
{
}

public IEnumerable<Type> DependsOn(Type type)
{
var underlyingType = Nullable.GetUnderlyingType(type);
if (underlyingType != null)
type = underlyingType;

if (type.IsPrimitive)
yield break;

if (type.In(typeof(string), typeof(Guid), typeof(DateTime), typeof(decimal)))
yield break;

if (type.IsArray)
type = type.GetElementType();
else if (type.IsAssignableTo(typeof(IEnumerable)))
{
if ( type.GetGenericArguments().Length == 0)
yield break;

type = type.GetGenericArguments().First();
}

if (type.Namespace.StartsWith("System."))
yield break;

if (this.visited.Contains(type))
yield break;

this.visited.Add(type);

yield return type;

foreach (var property in this.GetPropertiesOf(type))
{
foreach (var dependency in this.DependsOn(property.PropertyType))
{
yield return dependency;
}
}
}

private PropertyInfo[] GetPropertiesOf(Type type)
{
return type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
}
}

0 comments on commit cb7b745

Please sign in to comment.