Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed CSharp AaC generation to remove ALIAS field #115

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions C4InterFlow.Automation/C4InterFlow.Automation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<Deterministic>true</Deterministic>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Version>1.7.2</Version>
<Version>1.7.3</Version>
</PropertyGroup>

<PropertyGroup>
<PackageId>C4InterFlow.Automation</PackageId>
<PackageVersion>1.7.2</PackageVersion>
<PackageVersion>1.7.3</PackageVersion>
<Authors>Slava Vedernikov</Authors>
<Description>Revolutionise your Application Architecture Documentation with C4InterFlow. Designed for Architects and Engineers, this tool leverages the widely-recognised C4 Model (Architecture Visualisation framework), enhanced with unique features like Interface and Flow, to describe your Application Architecture as Code. Experience an intuitive, efficient way to document complex systems, ensuring clarity and consistency across your teams and products.</Description>
<Copyright>Copyright 2024 Slava Vedernikov</Copyright>
Expand Down
32 changes: 20 additions & 12 deletions C4InterFlow.Automation/Readers/CSharpAaCReaderStrategy.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.MSBuild;
using Newtonsoft.Json.Linq;
using YamlDotNet.Serialization.NamingConventions;
using YamlDotNet.Serialization;
using System.Text.Json.Nodes;
using C4InterFlow.Structures.Interfaces;
using System.Reflection;
using System.Runtime.Loader;
using C4InterFlow.Automation.Writers;
using System.Text;

namespace C4InterFlow.Automation.Readers
{
Expand Down Expand Up @@ -42,19 +36,33 @@ public override string GetComponentInterfaceAlias()

public override string GetComponentInterfaceAlias(string filePath)
{
var result = string.Empty;
var result = new StringBuilder();

var architectureClassSyntaxTree = CurrentClassDeclaration?.SyntaxTree;
var architectureProject = ArchitectureWorkspace?.CurrentSolution.Projects.FirstOrDefault(p => p.Documents.Any(d => d.FilePath == architectureClassSyntaxTree?.FilePath));
var architectureCompilation = architectureProject?.GetCompilationAsync().Result;

var interfaceClassSyntaxTree = architectureCompilation?.SyntaxTrees.FirstOrDefault(x => x.FilePath == filePath);
var interfaceAliasField = interfaceClassSyntaxTree?.GetRoot().DescendantNodes()
.OfType<FieldDeclarationSyntax>().First(f => f.Declaration.Variables.Any(v => v.Identifier.Text == "ALIAS"));
var rootNode = interfaceClassSyntaxTree?.GetRoot();

result = interfaceAliasField?.Declaration?.Variables[0]?.Initializer?.Value.ToString().Replace("\"", string.Empty);
if (rootNode == null) return string.Empty;

return result ?? string.Empty;
// Get the namespace declaration
var namespaceDeclaration = rootNode.DescendantNodes().OfType<NamespaceDeclarationSyntax>().FirstOrDefault();
if (namespaceDeclaration != null)
{
result.Append(namespaceDeclaration.Name.ToString());
}

// Get the class declarations and build the hierarchy
var classDeclarations = rootNode.DescendantNodes().OfType<ClassDeclarationSyntax>();
foreach (var classDecl in classDeclarations)
{
result.Append($".{classDecl.Identifier.Text}");
}

return result.ToString();
}

}
}
76 changes: 29 additions & 47 deletions C4InterFlow.Automation/Writers/CSharpCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@ public class CSharpCodeWriter : ICodeWriter
}
public string GetContainerCode(string architectureNamespace, string softwareSystemName, string name, string label, string? type = null, string? description = null, string? technology = null, string? boundary = null)
{
var softwareSystemAlias = AnyCodeWriter.GetSoftwareSystemAlias(architectureNamespace, softwareSystemName);
var result = new StringBuilder(GetSoftwareSystemsCodeHeader(architectureNamespace));
var containerName = AnyCodeWriter.GetName(name);

result.Append($@"
public partial class {AnyCodeWriter.GetName(softwareSystemName)}
{{
public partial class Containers
{{
public partial class {AnyCodeWriter.GetName(name)} : IContainerInstance
public partial class {containerName} : IContainerInstance
{{
private static readonly string ALIAS = {$"\"{AnyCodeWriter.GetContainerAlias(architectureNamespace, softwareSystemName, name)}\""};

public static Container Instance => new Container(
{softwareSystemAlias}.ALIAS, ALIAS, {AnyCodeWriter.EnsureDoubleQuotes(label)})
Utils.GetStructureAlias<{AnyCodeWriter.GetName(containerName)}>(), {AnyCodeWriter.EnsureDoubleQuotes(label)})
{{
ContainerType = ContainerType.{(!string.IsNullOrEmpty(type) ? type : "None")},
Description = {(!string.IsNullOrEmpty(description) ? AnyCodeWriter.EnsureDoubleQuotes(description) : "\"\"")},
Expand Down Expand Up @@ -55,6 +53,7 @@ private string GetSoftwareSystemsCodeHeader(string architectureNamespace)
var result = new StringBuilder();

result.AppendLine(@"// <auto-generated/>");
result.AppendLine($"using {ROOT_ARCHITECTURE_NAMESPACE};");
result.AppendLine($"using {ROOT_ARCHITECTURE_NAMESPACE}.Structures;");
result.AppendLine($"using {ROOT_ARCHITECTURE_NAMESPACE}.Structures.Interfaces;");
result.AppendLine();
Expand All @@ -69,6 +68,7 @@ private string GetActorsCodeHeader(string architectureNamespace)
var result = new StringBuilder();

result.AppendLine(@"// <auto-generated/>");
result.AppendLine($"using {ROOT_ARCHITECTURE_NAMESPACE};");
result.AppendLine($"using {ROOT_ARCHITECTURE_NAMESPACE}.Structures;");
result.AppendLine($"using {ROOT_ARCHITECTURE_NAMESPACE}.Structures.Interfaces;");
result.AppendLine();
Expand Down Expand Up @@ -96,14 +96,13 @@ private string GetBusinessProcessesCodeHeader(string architectureNamespace)
public string GetSoftwareSystemCode(string architectureNamespace, string name, string label, string? description = null, string? boundary = null)
{
var result = new StringBuilder(GetSoftwareSystemsCodeHeader(architectureNamespace));
var softwareSystemName = AnyCodeWriter.GetName(name);

result.Append($@"
public partial class {AnyCodeWriter.GetName(name)} : ISoftwareSystemInstance
public partial class {softwareSystemName} : ISoftwareSystemInstance
{{
private static readonly string ALIAS = {$"\"{AnyCodeWriter.GetSoftwareSystemAlias(architectureNamespace, name)}\""};

public static SoftwareSystem Instance => new SoftwareSystem(
ALIAS, {AnyCodeWriter.EnsureDoubleQuotes(label)})
Utils.GetStructureAlias<{AnyCodeWriter.GetName(softwareSystemName)}>(), {AnyCodeWriter.EnsureDoubleQuotes(label)})
{{
Description = {(!string.IsNullOrEmpty(description) ? AnyCodeWriter.EnsureDoubleQuotes(description) : "\"\"")},
Boundary = Boundary.{(!string.IsNullOrEmpty(boundary) ? boundary : "Internal")}
Expand All @@ -125,14 +124,13 @@ public partial class Interfaces
public string GetActorCode(string architectureNamespace, string type, string name, string label, string? description = null)
{
var result = new StringBuilder(GetActorsCodeHeader(architectureNamespace));
var actorName = AnyCodeWriter.GetName(name);

result.Append($@"
public class {AnyCodeWriter.GetName(name)} : I{type}Instance
public class {actorName} : I{type}Instance
{{
private static readonly string ALIAS = {$"\"{AnyCodeWriter.GetActorAlias(architectureNamespace, name)}\""};

public static {type} Instance => new {type}(
ALIAS, {AnyCodeWriter.EnsureDoubleQuotes(label)})
Utils.GetStructureAlias<{AnyCodeWriter.GetName(actorName)}>(), {AnyCodeWriter.EnsureDoubleQuotes(label)})
{{
Description = {(description != null ? description : "\"\"")},
}};
Expand All @@ -146,8 +144,8 @@ public class {AnyCodeWriter.GetName(name)} : I{type}Instance

public string GetComponentCode(string architectureNamespace, string softwareSystemName, string containerName, string name, string label, string componentType = "None", string? description = null, string? technology = null)
{
var containerAlias = AnyCodeWriter.GetContainerAlias(architectureNamespace, softwareSystemName, containerName);
var result = new StringBuilder(GetSoftwareSystemsCodeHeader(architectureNamespace));
var componentName = AnyCodeWriter.GetName(name);

result.Append($@"
public partial class {AnyCodeWriter.GetName(softwareSystemName)}
Expand All @@ -158,12 +156,10 @@ public partial class {AnyCodeWriter.GetName(containerName)}
{{
public partial class Components
{{
public partial class {AnyCodeWriter.GetName(name)} : IComponentInstance
public partial class {componentName} : IComponentInstance
{{
private static readonly string ALIAS = {$"\"{AnyCodeWriter.GetComponentAlias(architectureNamespace, softwareSystemName, containerName, name)}\""};

public static Component Instance => new Component(
{containerAlias}.ALIAS, ALIAS, {AnyCodeWriter.EnsureDoubleQuotes(label)})
Utils.GetStructureAlias<{componentName}>(), {AnyCodeWriter.EnsureDoubleQuotes(label)})
{{
ComponentType = ComponentType.{componentType},
Description = {(!string.IsNullOrEmpty(description) ? AnyCodeWriter.EnsureDoubleQuotes(description) : "\"\"")},
Expand All @@ -186,8 +182,8 @@ public partial class Interfaces

public string GetEntityCode(string architectureNamespace, string softwareSystemName, string containerName, string name, string label, string? type = null, string? description = null, string[]? composedOfMany = null, string[]? composedOfOne = null, string? extends = null)
{
var containerAlias = AnyCodeWriter.GetContainerAlias(architectureNamespace, softwareSystemName, containerName);
var result = new StringBuilder(GetSoftwareSystemsCodeHeader(architectureNamespace));
var entityName = AnyCodeWriter.GetName(name);

result.Append($@"
public partial class {AnyCodeWriter.GetName(softwareSystemName)}
Expand All @@ -198,12 +194,10 @@ public partial class {AnyCodeWriter.GetName(containerName)}
{{
public partial class Entities
{{
public partial class {AnyCodeWriter.GetName(name)} : IEntityInstance
public partial class {entityName} : IEntityInstance
{{
private static readonly string ALIAS = {$"\"{AnyCodeWriter.GetEntityAlias(architectureNamespace, softwareSystemName, containerName, name)}\""};

public static Entity Instance => new Entity(
{containerAlias}.ALIAS, ALIAS, {AnyCodeWriter.EnsureDoubleQuotes(label)}, {(type != null ? type : "EntityType.None")})
Utils.GetStructureAlias<{entityName}>(), {AnyCodeWriter.EnsureDoubleQuotes(label)}, {(type != null ? type : "EntityType.None")})
{{
Description = {(!string.IsNullOrEmpty(description) ? AnyCodeWriter.EnsureDoubleQuotes(description) : "\"\"")},
ComposedOfMany = new string[] {{{(composedOfMany != null ? string.Join(", ", composedOfMany.Select(x => AnyCodeWriter.EnsureDoubleQuotes(x))) : " ")}}},
Expand All @@ -223,8 +217,8 @@ public partial class {AnyCodeWriter.GetName(name)} : IEntityInstance

public string GetComponentInterfaceCode(string architectureNamespace, string softwareSystemName, string containerName, string componentName, string name, string label, string? description = null, string? protocol = null, string? path = null, bool? isPrivate = null, string? uses = null, string? input = null, string? inputTemplate = null, string? output = null, string? outputTemplate = null)
{
var componentAlias = AnyCodeWriter.GetComponentAlias(architectureNamespace, softwareSystemName, containerName, componentName);
var result = new StringBuilder(GetSoftwareSystemsCodeHeader(architectureNamespace));
var componentInterfaceName = AnyCodeWriter.GetName(name);

result.Append($@"
public partial class {AnyCodeWriter.GetName(softwareSystemName)}
Expand All @@ -239,18 +233,16 @@ public partial class {componentName}
{{
public partial class Interfaces
{{
public partial class {AnyCodeWriter.GetName(name)} : IInterfaceInstance
public partial class {componentInterfaceName} : IInterfaceInstance
{{
private static readonly string ALIAS = {$"\"{AnyCodeWriter.GetComponentInterfaceAlias(componentAlias, name)}\""};

public static Interface Instance => new Interface(
{componentAlias}.ALIAS, ALIAS, {AnyCodeWriter.EnsureDoubleQuotes(label)})
Utils.GetStructureAlias<{componentInterfaceName}>(), {AnyCodeWriter.EnsureDoubleQuotes(label)})
{{
Description = {(description != null ? AnyCodeWriter.EnsureDoubleQuotes(description) : "\"\"")},
Path = {(path != null ? AnyCodeWriter.EnsureDoubleQuotes(path) : "\"\"")},
IsPrivate = {(isPrivate != null ? isPrivate.ToString().ToLower() : "false")},
Protocol = {(protocol != null ? AnyCodeWriter.EnsureDoubleQuotes(protocol) : "\"\"")},
Flow = new Flow(ALIAS),
Flow = new Flow(Utils.GetStructureAlias<{componentInterfaceName}>()),
Input = {(input != null ? input : "\"\"")},
InputTemplate = {(inputTemplate != null ? inputTemplate : "\"\"")},
Output = {(output != null ? output : "\"\"")},
Expand All @@ -271,8 +263,8 @@ public partial class {AnyCodeWriter.GetName(name)} : IInterfaceInstance

public string GetContainerInterfaceCode(string architectureNamespace, string softwareSystemName, string containerName, string name, string label, string? description = null, string? protocol = null, string? uses = null, string? input = null, string? inputTemplate = null, string? output = null, string? outputTemplate = null)
{
var containerAlias = AnyCodeWriter.GetContainerAlias(architectureNamespace, softwareSystemName, containerName);
var result = new StringBuilder(GetSoftwareSystemsCodeHeader(architectureNamespace));
var containerInterfaceName = AnyCodeWriter.GetName(name);

result.Append($@"
public partial class {AnyCodeWriter.GetName(softwareSystemName)}
Expand All @@ -283,15 +275,13 @@ public partial class {containerName}
{{
public partial class Interfaces
{{
public partial class {AnyCodeWriter.GetName(name)} : IInterfaceInstance
public partial class {containerInterfaceName} : IInterfaceInstance
{{
private static readonly string ALIAS = {$"\"{AnyCodeWriter.GetContainerInterfaceAlias(architectureNamespace, softwareSystemName, containerName, name)}\""};

public static Interface Instance => new Interface(
{containerAlias}.ALIAS, ALIAS, {AnyCodeWriter.EnsureDoubleQuotes(label)})
Utils.GetStructureAlias<{containerInterfaceName}>(), {AnyCodeWriter.EnsureDoubleQuotes(label)})
{{
Description = {(description != null ? AnyCodeWriter.EnsureDoubleQuotes(description) : "\"\"")},
Flow = new Flow(ALIAS),
Flow = new Flow(Utils.GetStructureAlias<{containerInterfaceName}>()),
Protocol = {(protocol != null ? AnyCodeWriter.EnsureDoubleQuotes(protocol) : "\"\"")},
Input = {(input != null ? input : "\"\"")},
InputTemplate = {(inputTemplate != null ? inputTemplate : "\"\"")},
Expand All @@ -311,24 +301,21 @@ public partial class {AnyCodeWriter.GetName(name)} : IInterfaceInstance

public string GetSoftwareSystemInterfaceCode(string architectureNamespace, string softwareSystemName, string name, string label, string? description = null, string? protocol = null, string? uses = null, string? input = null, string? inputTemplate = null, string? output = null, string? outputTemplate = null)
{
var softwareSystemAlias = AnyCodeWriter.GetSoftwareSystemAlias(architectureNamespace, softwareSystemName);

var result = new StringBuilder(GetSoftwareSystemsCodeHeader(architectureNamespace));
var softwareSystemInterfaceName = AnyCodeWriter.GetName(name);

result.Append($@"
public partial class {softwareSystemName}
{{
public partial class Interfaces
{{
public partial class {AnyCodeWriter.GetName(name)} : IInterfaceInstance
public partial class {softwareSystemInterfaceName} : IInterfaceInstance
{{
private static readonly string ALIAS = {$"\"{AnyCodeWriter.GetSoftwareSystemInterfaceAlias(architectureNamespace, softwareSystemName, name)}\""};

public static Interface Instance => new Interface(
{softwareSystemAlias}.ALIAS, ALIAS, {AnyCodeWriter.EnsureDoubleQuotes(label)})
Utils.GetStructureAlias<{softwareSystemInterfaceName}>(), {AnyCodeWriter.EnsureDoubleQuotes(label)})
{{
Description = {(description != null ? AnyCodeWriter.EnsureDoubleQuotes(description) : "\"\"")},
Flow = new Flow(ALIAS),
Flow = new Flow(Utils.GetStructureAlias<{softwareSystemInterfaceName}>()),
Protocol = {(protocol != null ? AnyCodeWriter.EnsureDoubleQuotes(protocol) : "\"\"")},
Input = {(input != null ? input : "\"\"")},
InputTemplate = {(inputTemplate != null ? inputTemplate : "\"\"")},
Expand All @@ -344,11 +331,6 @@ public partial class {AnyCodeWriter.GetName(name)} : IInterfaceInstance
return result.ToString();
}

public string GetFlowCode()
{
return $"new Flow(ALIAS)";
}

public string GetFlowCode(Flow flow)
{
var result = new StringBuilder();
Expand Down
2 changes: 1 addition & 1 deletion C4InterFlow.Automation/Writers/CSharpToAnyCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ public static string GetFlowCode(
CSharpToAnyAaCWriter writer,
IEnumerable<NetToAnyAlternativeInvocationMapperConfig>? alternativeInvocationMappers = null)
{
var result = new StringBuilder().AppendLine(CodeWriter.GetFlowCode());
var result = new StringBuilder();


if (methodDeclaration.Body == null)
Expand Down
Loading