Skip to content

Commit

Permalink
#27: Added documentation for generating documentation for a specific …
Browse files Browse the repository at this point in the history
…class with its dependencies.
  • Loading branch information
MarcinCelej committed Jan 6, 2024
1 parent 8e8261a commit f39e788
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 21 deletions.
36 changes: 36 additions & 0 deletions Documentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,41 @@ When all tech debt for the project is materialized in single file - we can start
It also helps to keep track of all tech debt in the project.
Moreover, it is much easier to spot new technical debt during the code review.

## Enlisting dependencies of a class

To document dependencies of a specific class, we use the following tool:

```csharp
[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);

// ACT
var publicApi = ApiDescription.GenerateFor(dependencies);

// ASSERT
await Verifier.Verify(publicApi, "md")
.UseMethodName("of." + type.Name);
}
}
```

For sample code, please check: [Relations.cs](Synergy.Documentation.Tests/Architecture/Dependencies/Relations.cs)

To see the results, please check: [Relations.of.Document.verified.md](Synergy.Documentation.Tests/Architecture/Dependencies/Relations.of.Document.verified.md)

[//]: # (TODO Write the documentation of Dependencies genaretor)

## Generating markdown files from code - Docs as Code

TBC

[//]: # (TODO Write the documentation of Markdown class usage)

Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ 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);

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@

## Synergy.Documentation.Markup.Markdown+Document (class) : IEnumerable<Markdown+IElement>, IEnumerable
- ctor()
- Append(
element: Markdown+IElement
) : Markdown+Document
- Append(
newElements: IEnumerable<Markdown+IElement>
) : Markdown+Document
- GetEnumerator() : IEnumerator<Markdown+IElement>
- ToString() : string

Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
- Dependencies.Of(
root: Type
) : List<Type>
- DependsOn(
type: Type
) : IEnumerable<Type> [IteratorStateMachine]

## Code.ClassReader (class)
- ctor()
Expand Down
44 changes: 29 additions & 15 deletions Documentation/Synergy.Documentation/Api/Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,63 @@ public class Dependencies
{
public static List<Type> Of(Type root)
{
return new Dependencies().DependsOn(root).ToList();
return new Dependencies().DependenciesOf(root)
.ToList();
}

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

private IEnumerable<Type> DependenciesOf(Type type)
{
return this.DependenciesOf(type, 0);
}

public IEnumerable<Type> DependsOn(Type type)
private IEnumerable<Type> DependenciesOf(Type type, int level)
{
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 (level == 0)
{
this.visited.Add(type);
yield return type;
}

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

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

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

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

if (this.visited.Contains(type) && level > 0)
yield break;

this.visited.Add(type);

yield return type;

if (this.visited.Contains(type) == false)
{
this.visited.Add(type);
yield return type;
}

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

0 comments on commit f39e788

Please sign in to comment.