Skip to content

Commit

Permalink
mend
Browse files Browse the repository at this point in the history
  • Loading branch information
PTKu committed May 7, 2024
1 parent 9844c74 commit 4353225
Show file tree
Hide file tree
Showing 13 changed files with 702 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ public void CreateFile(IFileSyntax fileSyntax, IxNodeVisitor visitor)
AddToSource("using AXSharp.Connector.ValueTypes;");
AddToSource("using System.Collections.Generic;");
AddToSource("using AXSharp.Connector.Localizations;");


foreach (var fileSyntaxUsingDirective in fileSyntax.UsingDirectives
.Where(p => this.Compilation.GetSemanticTree().Namespaces.Select(p => p.FullyQualifiedName).Contains(p.QualifiedIdentifierList.GetText())))
{
AddToSource($"using {fileSyntaxUsingDirective.QualifiedIdentifierList.GetText()};");
}

fileSyntax.Declarations.ToList().ForEach(p => p.Visit(visitor, this));
}

Expand All @@ -73,7 +71,7 @@ private string ReplaceGenericSignature(IClassDeclaration? classDeclaration)

var generics = new List<string>();
var genericSignature = classDeclaration?.ExtendedType?.GetGenericAttributes()?.Product;
if(string.IsNullOrEmpty(genericSignature))
if (string.IsNullOrEmpty(genericSignature))
{
return string.Empty;
}
Expand All @@ -89,11 +87,11 @@ private string ReplaceGenericSignature(IClassDeclaration? classDeclaration)
{
if (attribute.GenericTypeAssignment.isPoco)
{
genericSignature = genericSignature.Replace(attribute.GenericTypeAssignment.type, $"Pocos.{fieldDeclaresGenericType?.Type.FullyQualifiedName}");
genericSignature = genericSignature.Replace(attribute.GenericTypeAssignment.type, $"Pocos.{fieldDeclaresGenericType?.Type.FullyQualifiedName}");
}
else
{
genericSignature = genericSignature.Replace(attribute.GenericTypeAssignment.type, fieldDeclaresGenericType?.Type.FullyQualifiedName);
genericSignature = genericSignature.Replace(attribute.GenericTypeAssignment.type, fieldDeclaresGenericType?.Type.FullyQualifiedName);
}
}
}
Expand All @@ -110,10 +108,22 @@ public void CreateClassDeclaration(IClassDeclarationSyntax classDeclarationSynta
IxNodeVisitor visitor)
{
TypeCommAccessibility = classDeclaration.GetCommAccessibility(this);


// This is a workaround for abstract classes where semantic model does not contain pragmas even when declared in the source.
if (classDeclarationSyntax.ClassKeyword.FullText.Trim().ToLower().StartsWith("{S7.extern=ReadWrite}".ToLower()))
{
TypeCommAccessibility = eCommAccessibility.ReadWrite;
}

if (classDeclarationSyntax.ClassKeyword.FullText.Trim().ToLower().StartsWith("{S7.extern=Read}".ToLower()))
{
TypeCommAccessibility = eCommAccessibility.ReadOnly;
}


classDeclarationSyntax.UsingDirectives.ToList().ForEach(p => p.Visit(visitor, this));
var generic = classDeclaration.GetGenericAttributes();

AddToSource(classDeclaration.Pragmas.AddAttributes());
AddToSource($"{classDeclaration.AccessModifier.Transform()}partial class {classDeclaration.Name}{generic?.Product}");
AddToSource(":");
Expand Down Expand Up @@ -149,7 +159,7 @@ public void CreateClassDeclaration(IClassDeclarationSyntax classDeclarationSynta
AddToSource(CsOnlinerPlainerShadowToPlainBuilder.Create(visitor, classDeclaration, this, isExtended).Output);
AddToSource(CsOnlinerPlainerShadowToPlainProtectedBuilder.Create(visitor, classDeclaration, this, isExtended).Output);
AddToSource(CsOnlinerPlainerPlainToShadowBuilder.Create(visitor, classDeclaration, this, isExtended).Output);

AddToSource(CsOnlinerHasChangedBuilder.Create(visitor, classDeclaration, this, isExtended).Output);

AddPollingMethod(isExtended);
Expand Down Expand Up @@ -178,7 +188,7 @@ public void CreateConfigDeclaration(IConfigDeclarationSyntax configDeclarationSy
IxNodeVisitor visitor)
{
TypeCommAccessibility = eCommAccessibility.None;

AddToSource(
$"public partial class {Project.TargetProject.ProjectRootNamespace}TwinController : ITwinController {{");
AddToSource($"public {typeof(Connector.Connector).n()} Connector {{ get; }}");
Expand All @@ -205,9 +215,9 @@ public void CreateEnumTypeDeclaration(IEnumTypeDeclarationSyntax enumTypeDeclara
IxNodeVisitor visitor)
{
TypeCommAccessibility = eCommAccessibility.None;

AddToSource($"public enum {enumTypeDeclarationSyntax.Name.Text} {{");
AddToSource(string.Join("\n,", enumTypeDeclarationSyntax.EnumValues.Select(p => p.Name.Text)));
AddToSource(string.Join("\n,", enumTypeDeclarationSyntax.EnumValueList.EnumValues.Select(p => p.Name.Text)));
AddToSource("}");
}

Expand All @@ -216,9 +226,9 @@ public void CreateNamedValueTypeDeclaration(INamedValueTypeDeclarationSyntax nam
INamedValueTypeDeclaration namedValueTypeDeclaration, IxNodeVisitor visitor)
{
TypeCommAccessibility = eCommAccessibility.None;

AddToSource(
$"public enum {namedValueTypeDeclarationSyntax.Name.Text} : {namedValueTypeDeclarationSyntax.Type.TransformType()} {{");
$"public enum {namedValueTypeDeclarationSyntax.Name.Text} : {namedValueTypeDeclarationSyntax.BaseType.TransformType()} {{");

// TODO: Value re-interpretation should be done according to the type.

Expand Down Expand Up @@ -267,7 +277,7 @@ public void CreateInterfaceDeclaration(IInterfaceDeclarationSyntax interfaceDecl
IxNodeVisitor visitor)
{
TypeCommAccessibility = eCommAccessibility.None;

AddToSource($"{interfaceDeclaration.AccessModifier.Transform()} partial interface {interfaceDeclaration.Name} {{}}");
}

Expand Down Expand Up @@ -359,7 +369,7 @@ private void CreateITwinObjectImplementation()
"public string HumanReadable { get => string.IsNullOrEmpty(_humanReadable) ? SymbolTail : _humanReadable.Interpolate(this).CleanUpLocalizationTokens(); set => _humanReadable = value; }" +
"public System.String GetHumanReadable(System.Globalization.CultureInfo culture) { return this.Translate(_humanReadable, culture); }" +
"protected System.String @SymbolTail { get; set;}" +
$"protected {typeof(ITwinObject).n()} @Parent {{ get; set; }}"+
$"protected {typeof(ITwinObject).n()} @Parent {{ get; set; }}" +
$"public AXSharp.Connector.Localizations.Translator Interpreter => global::{Project.TargetProject.ProjectRootNamespace}.PlcTranslator.Instance;"
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,18 @@ public void CreateClassDeclaration(IClassDeclarationSyntax classDeclarationSynta
IxNodeVisitor visitor)
{
TypeCommAccessibility = classDeclaration.GetCommAccessibility(this);


// This is a workaround for abstract classes where semantic model does not contain pragmas even when declared in the source.
if (classDeclarationSyntax.ClassKeyword.FullText.Trim().ToLower().StartsWith("{S7.extern=ReadWrite}".ToLower()))
{
TypeCommAccessibility = eCommAccessibility.ReadWrite;
}

if (classDeclarationSyntax.ClassKeyword.FullText.Trim().ToLower().StartsWith("{S7.extern=Read}".ToLower()))
{
TypeCommAccessibility = eCommAccessibility.ReadOnly;
}

classDeclarationSyntax.UsingDirectives.ToList().ForEach(p => p.Visit(visitor, this));
AddToSource($"{classDeclaration.AccessModifier.Transform()}partial class {classDeclaration.Name}");

Expand All @@ -64,17 +75,17 @@ public void CreateClassDeclaration(IClassDeclarationSyntax classDeclarationSynta
if (isExtended)
AddToSource($" : {classDeclaration.ExtendedTypeAccesses.FirstOrDefault()?.Type.FullyQualifiedName}");



AddToSource(isExtended ? ", AXSharp.Connector.IPlain" : ": AXSharp.Connector.IPlain");

AddToSource(classDeclarationSyntax.ImplementsList != null
? ", "
: "");

classDeclarationSyntax.ImplementsList?.Visit(visitor, this);



AddToSource("{");
classDeclarationSyntax.UsingDirectives.ToList().ForEach(p => p.Visit(visitor, this));
Expand Down Expand Up @@ -107,7 +118,7 @@ public void CreateFieldDeclaration(IFieldDeclaration fieldDeclaration, IxNodeVis
AddToSource("[]");
AddToSource($" {fieldDeclaration.Name}");
AddToSource("{get; set;}");

AddToSource($"= new");
arrayType.ElementTypeAccess.Type.Accept(visitor, this);
AddToSource($"[");
Expand Down Expand Up @@ -168,14 +179,13 @@ public virtual void CreateNamedValueTypeDeclaration(INamedValueTypeDeclaration n
public void CreateFile(IFileSyntax fileSyntax, IxNodeVisitor visitor)
{
AddToSource("using System;");
foreach (var fileSyntaxUsingDirective in

foreach (var fileSyntaxUsingDirective in
fileSyntax.UsingDirectives
.Where(p => this.Compilation.GetSemanticTree().Namespaces.Select(p => p.FullyQualifiedName).Contains(p.QualifiedIdentifierList.GetText())))
{
AddToSource($"using Pocos.{fileSyntaxUsingDirective.QualifiedIdentifierList.GetText()};");
}

AddToSource("namespace Pocos {");
fileSyntax.Declarations.ToList().ForEach(p => p.Visit(visitor, this));
AddToSource("}");
Expand All @@ -187,7 +197,7 @@ public void CreateConfigDeclaration(IConfigDeclarationSyntax configDeclarationSy
IxNodeVisitor visitor)
{
TypeCommAccessibility = eCommAccessibility.None;

AddToSource($"public partial class {Project.TargetProject.ProjectRootNamespace}TwinController{{");
configurationDeclaration.Variables.ToList().ForEach(p => p.Accept(visitor, this));
AddToSource("}");
Expand Down Expand Up @@ -288,7 +298,7 @@ public void CreateVariableDeclaration(IVariableDeclaration fieldDeclaration, IxN
break;
}
}

}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@
<None Remove="samples\units\test\test.st" />
</ItemGroup>

<ItemGroup>
<None Include="samples\units\expected\.g\POCO\generics.g.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<None Include="samples\units\expected\.g\Onliners\generics.g.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<None Include="samples\units\expected\.g\POCO\abstract_members.g.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,7 @@ public void abstract_members()
[Fact]
public void generics()
{
var memberName = GetMethodName();
CompareOutputs(memberName);
CompareOutputs(GetMethodName());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using AXSharp.Compiler;
using AXSharp.Compiler.Cs.Onliner;
using AXSharp.Compiler.Cs.Plain;
using Castle.Core.Resource;
using Polly;
using Xunit.Abstractions;

Expand Down Expand Up @@ -100,6 +101,8 @@ public void should_match_expected_and_generated_whole_project()

if (Directory.Exists(project.OutputFolder)) Directory.Delete(project.OutputFolder, true);



project.Generate();

var rootSourceFolder = Path.Combine(testFolder, @"samples\units\expected\.g\");
Expand All @@ -121,14 +124,15 @@ public void should_match_expected_and_generated_whole_project()
var currentIndex = index++;
var expectedFileContent = File.ReadAllText(exp);
var actualFileContent = File.ReadAllText(actualList[currentIndex]);

try
{
var actualFileContentLines = actualFileContent.Split("\n").Select(a => a.Trim()).ToArray();
var expectedFileContentLines = expectedFileContent.Split("\n").Select(a => a.Trim()).ToArray();

for (int i = 0; i < expectedFileContentLines.Length; i++)
{
Assert.Equal(expectedFileContentLines[i], actualFileContentLines[i]);
Assert.Equal(expectedFileContentLines[i], actualFileContentLines[i]);
}
}
catch (Exception)
Expand Down
Loading

0 comments on commit 4353225

Please sign in to comment.