From 18056ae9fe01b9db613a6518109cf3808903ed8c Mon Sep 17 00:00:00 2001 From: Peter <61538034+PTKu@users.noreply.github.com> Date: Tue, 23 May 2023 06:40:18 +0200 Subject: [PATCH] ixd now allows for multiple project to be compiled at once in the same compilation --- .../src/ixd/Helpers/YamlHelpers.cs | 12 ++++---- .../src/ixd/Mapper/CodeToYamlMapper.cs | 6 ++-- src/AXSharp.compiler/src/ixd/Options.cs | 3 +- src/AXSharp.compiler/src/ixd/Program.cs | 30 +++++++++++++++---- .../src/ixd/Properties/launchSettings.json | 4 +-- .../src/ixd/Visitors/MyNodeVisitor.cs | 4 +-- .../src/ixd/Visitors/YamlBuilder.cs | 4 +-- .../src/ixd/YamlSerializer.cs | 21 +++++++++++-- 8 files changed, 59 insertions(+), 25 deletions(-) diff --git a/src/AXSharp.compiler/src/ixd/Helpers/YamlHelpers.cs b/src/AXSharp.compiler/src/ixd/Helpers/YamlHelpers.cs index 1a9ef5d0..91a364c8 100644 --- a/src/AXSharp.compiler/src/ixd/Helpers/YamlHelpers.cs +++ b/src/AXSharp.compiler/src/ixd/Helpers/YamlHelpers.cs @@ -18,11 +18,11 @@ namespace AXSharp.ixc_doc.Helpers { public class YamlHelpers { - public YamlHelpers(string projectPath) + public YamlHelpers() { - PathToProjectFile = projectPath; + } - public string PathToProjectFile {get; set;} + // return all inherited members from class declaration public string[] GetInheritedMembers(IClassDeclaration classDeclaration) { @@ -30,10 +30,10 @@ public string[] GetInheritedMembers(IClassDeclaration classDeclaration) IEnumerable extendedFields = new List(); // TODO check, if IClassDeclaration in sufficient - var members = classDeclaration.GetAllExtendedTypes().ToList() - .Select(p => ((IClassDeclaration)p).Fields.Where(f => CanBeFieldInherited(f, classDeclaration, p))).ToList(); + var members = classDeclaration.GetAllExtendedTypes().OfType().ToList() + .Select(p => p.Fields.Where(f => CanBeFieldInherited(f, classDeclaration, p))).ToList(); - classDeclaration.GetAllExtendedTypes().ToList() + classDeclaration.GetAllExtendedTypes().OfType().ToList() .Select(p => ((IClassDeclaration)p).Fields.Where(f => CanBeFieldInherited(f, classDeclaration, p))).ToList() .ForEach(member => extendedFields = extendedFields.Concat(member)); diff --git a/src/AXSharp.compiler/src/ixd/Mapper/CodeToYamlMapper.cs b/src/AXSharp.compiler/src/ixd/Mapper/CodeToYamlMapper.cs index 9c9a7ada..f4f123ad 100644 --- a/src/AXSharp.compiler/src/ixd/Mapper/CodeToYamlMapper.cs +++ b/src/AXSharp.compiler/src/ixd/Mapper/CodeToYamlMapper.cs @@ -30,7 +30,7 @@ public Item PopulateItem(IDeclaration declaration) Namespace = declaration is INamespaceDeclaration ? Helpers.Helpers.GetBaseUid(declaration.FullyQualifiedName) : Helpers.Helpers.GetBaseUid(declaration.ContainingNamespace?.FullyQualifiedName), Summary = _yh.GetComments(declaration.Location).summary, Remarks = _yh.GetComments(declaration.Location).remarks, - Assemblies = new string[] { _yh.GetAssembly(_yh.PathToProjectFile) }, + //Assemblies = new string[] { _yh.GetAssembly(_yh.PathToProjectFile) }, }; @@ -55,8 +55,8 @@ public Item PopulateItem(IClassDeclaration classDeclaration) var implementedInterfaces = classDeclaration.GetAllImplementedInterfacesUniquely().Select(i => Helpers.Helpers.GetBaseUid(i)); List extendedFields = new List(); - classDeclaration.GetAllExtendedTypes().ToList() - .Select(p => ((IClassDeclaration)p).Fields.Where(f => _yh.CanBeFieldInherited(f, classDeclaration, p))).ToList() + classDeclaration.GetAllExtendedTypes().OfType().ToList() + .Select(p => p.Fields.Where(f => _yh.CanBeFieldInherited(f, classDeclaration, p))).ToList() .ForEach(list => extendedFields.Concat(list)); var item = PopulateItem((IDeclaration)classDeclaration); diff --git a/src/AXSharp.compiler/src/ixd/Options.cs b/src/AXSharp.compiler/src/ixd/Options.cs index 34e2de93..de7c45b0 100644 --- a/src/AXSharp.compiler/src/ixd/Options.cs +++ b/src/AXSharp.compiler/src/ixd/Options.cs @@ -13,11 +13,12 @@ namespace AXSharp.ixc_doc internal class Options : ICompilerOptions { [Option('x', "source-project-folder", Required = true, HelpText = "Simatic-ax project folder")] - public string? AxSourceProjectFolder { get; set; } + public IEnumerable AxSourceProjectFolder { get; set; } [Option('o', "output-project-folder", Required = true, HelpText = "Output project folder where compiler emits result.")] public string? OutputProjectFolder { get; set; } + } } diff --git a/src/AXSharp.compiler/src/ixd/Program.cs b/src/AXSharp.compiler/src/ixd/Program.cs index 26ae9808..785fa144 100644 --- a/src/AXSharp.compiler/src/ixd/Program.cs +++ b/src/AXSharp.compiler/src/ixd/Program.cs @@ -16,6 +16,8 @@ using CliWrap; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; +using System.Collections.Generic; +using NuGet.Packaging; const string Logo = @"| \ / @@ -50,12 +52,28 @@ void GenerateYamls(Options o) { - + //var multipleProjects = new List(); + //foreach (var sf in Directory.EnumerateDirectories(o.AxSourceProjectFolder, "ctrl", SearchOption.AllDirectories)) + //{ + // multipleProjects.Add(new AxProject(sf)); + //} + DeleteYamlFilesIfExists(o.OutputProjectFolder); - var axProject = new AxProject(o.AxSourceProjectFolder); - Console.WriteLine($"Compiling project {axProject.ProjectInfo.Name}..."); - var projectSources = axProject.Sources.Select(p => (parseTree: STParser.ParseTextAsync(p).Result, source: p)); + + IList<(ISyntaxTree parseTree, SourceFileText source, AxProject project)> projectSources = + new List<(ISyntaxTree parseTree, SourceFileText source, AxProject project)>(); + + foreach (var source in o.AxSourceProjectFolder) + { + var axProject = new AxProject(source); + Console.WriteLine($"Compiling project {axProject.ProjectInfo.Name}..."); + projectSources.AddRange(axProject.Sources.Select(p => (parseTree: STParser.ParseTextAsync(p).Result, source: p, axProject))); + } + + //var axProject = new AxProject(o.AxSourceProjectFolder); + //Console.WriteLine($"Compiling project {axProject.ProjectInfo.Name}..."); + //var projectSources = axProject.Sources.Select(p => (parseTree: STParser.ParseTextAsync(p).Result, source: p)); var toCompile = projectSources.Select(p => p.parseTree); @@ -64,9 +82,9 @@ void GenerateYamls(Options o) var semanticTree = compilation.GetSemanticTree(); //visit - var myNodeVisitor = new MyNodeVisitor(axProject); + var myNodeVisitor = new MyNodeVisitor(); var yamlSerializer = new YamlSerializer(o); - var treeWalker = new YamlBuilder(yamlSerializer, axProject.ProjectFile); + var treeWalker = new YamlBuilder(yamlSerializer); semanticTree.GetRoot().Accept(myNodeVisitor, treeWalker); diff --git a/src/AXSharp.compiler/src/ixd/Properties/launchSettings.json b/src/AXSharp.compiler/src/ixd/Properties/launchSettings.json index afec8232..c73f4f0a 100644 --- a/src/AXSharp.compiler/src/ixd/Properties/launchSettings.json +++ b/src/AXSharp.compiler/src/ixd/Properties/launchSettings.json @@ -10,8 +10,8 @@ }, "ixd-3": { "commandName": "Project", - "commandLineArgs": "-x .\\src\\core\\ctrl\\ -o .\\docfx\\apictrl\\", - "workingDirectory": "C:\\MTS\\ix-ax\\AXSharp.framework\\" + "commandLineArgs": "-x C:\\W\\Develop\\gh\\ix-ax\\axopen\\src\\messaging\\ctrl C:\\W\\Develop\\gh\\ix-ax\\axopen\\src\\abstractions\\ctrl C:\\W\\Develop\\gh\\ix-ax\\axopen\\src\\data\\ctrl C:\\W\\Develop\\gh\\ix-ax\\axopen\\src\\core\\ctrl -o C:\\W\\Develop\\gh\\ix-ax\\axopen\\docfx\\apictrl\\", + "workingDirectory": "C:\\W\\Develop\\gh\\ix-ax\\axopen\\src\\data\\ctrl" } } } \ No newline at end of file diff --git a/src/AXSharp.compiler/src/ixd/Visitors/MyNodeVisitor.cs b/src/AXSharp.compiler/src/ixd/Visitors/MyNodeVisitor.cs index 5b02bf4d..057068d1 100644 --- a/src/AXSharp.compiler/src/ixd/Visitors/MyNodeVisitor.cs +++ b/src/AXSharp.compiler/src/ixd/Visitors/MyNodeVisitor.cs @@ -24,11 +24,11 @@ namespace AXSharp.ixc_doc.Visitors public partial class MyNodeVisitor : ISemanticNodeVisitor { public YamlSerializerHelper YamlHelper { get; set; } - public Compiler.AxProject axProject { get; set; } + //public Compiler.AxProject axProject { get; set; } public MyNodeVisitor(Compiler.AxProject? axProject = null) { - this.axProject = axProject; + //this.axProject = axProject; YamlHelper = new YamlSerializerHelper(); } diff --git a/src/AXSharp.compiler/src/ixd/Visitors/YamlBuilder.cs b/src/AXSharp.compiler/src/ixd/Visitors/YamlBuilder.cs index edfd373a..65f07473 100644 --- a/src/AXSharp.compiler/src/ixd/Visitors/YamlBuilder.cs +++ b/src/AXSharp.compiler/src/ixd/Visitors/YamlBuilder.cs @@ -28,9 +28,9 @@ public class YamlBuilder : IYamlBuiderVisitor private YamlHelpers _yh { get; set; } private CodeToYamlMapper _mp { get; set; } private List NamespaceWrappers {get; set; } = new List(); - internal YamlBuilder(YamlSerializer serializer, string projectPath) + internal YamlBuilder(YamlSerializer serializer) { - _yh = new YamlHelpers(projectPath); + _yh = new YamlHelpers(); _mp = new CodeToYamlMapper(_yh); _s = serializer; } diff --git a/src/AXSharp.compiler/src/ixd/YamlSerializer.cs b/src/AXSharp.compiler/src/ixd/YamlSerializer.cs index 8e4685e5..16e77219 100644 --- a/src/AXSharp.compiler/src/ixd/YamlSerializer.cs +++ b/src/AXSharp.compiler/src/ixd/YamlSerializer.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using YamlDotNet.Serialization.NamingConventions; using YamlDotNet.Serialization; +using Polly; namespace AXSharp.ixc_doc { @@ -44,8 +45,8 @@ public string SchemaToYaml(YamlSchema model, string fileName) stringBuilder.AppendLine(serializer.Serialize(model)); - - using (System.IO.StreamWriter file = new System.IO.StreamWriter(@$"{_options.OutputProjectFolder}\{fileName}.yml")) + + using (System.IO.StreamWriter file = new System.IO.StreamWriter(@$"{EnsureDirectory(_options.OutputProjectFolder)}\{fileName}.yml")) { file.WriteLine("## YamlMime:ManagedReference"); @@ -58,6 +59,20 @@ public string SchemaToYaml(YamlSchema model, string fileName) return stringBuilder.ToString(); } - + private string EnsureDirectory(string directory) + { + Policy + .Handle() + .WaitAndRetry(5, a => TimeSpan.FromMilliseconds(500)) + .Execute(() => + { + if (!Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + } + }); + + return directory; + } } }