diff --git a/src/AXSharp.compiler/src/ixd/AXSharp.ixd.csproj b/src/AXSharp.compiler/src/ixd/AXSharp.ixd.csproj index 966bc462..aadfaaa9 100644 --- a/src/AXSharp.compiler/src/ixd/AXSharp.ixd.csproj +++ b/src/AXSharp.compiler/src/ixd/AXSharp.ixd.csproj @@ -47,6 +47,7 @@ + diff --git a/src/AXSharp.compiler/src/ixd/Helpers/Helpers.cs b/src/AXSharp.compiler/src/ixd/Helpers/Helpers.cs index 78f761c4..7ac5dd3a 100644 --- a/src/AXSharp.compiler/src/ixd/Helpers/Helpers.cs +++ b/src/AXSharp.compiler/src/ixd/Helpers/Helpers.cs @@ -1,29 +1,31 @@ using AX.ST.Semantic.Model.Declarations; +using AX.Text; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace AXSharp.ixc_doc.Helpers { internal static class Helpers { public static string GetBaseUid(IDeclaration declaration) => $"plc.{declaration.FullyQualifiedName}"; - + public static string GetBaseUid(string @namespace) => $"plc.{@namespace}"; public static string GetBaseUid(IFunctionDeclaration functionDeclaration) { var inputParamsDeclaration = functionDeclaration.Variables.Where(v => v.Section == Section.Input).ToList(); - var declaration=GetMethodTypeDeclaration(inputParamsDeclaration); + var declaration = GetMethodTypeDeclaration(inputParamsDeclaration); return $"plc.{functionDeclaration.FullyQualifiedName}({declaration})"; } public static string GetBaseUid(IMethodDeclaration methodDeclaration) { var inputParamsDeclaration = methodDeclaration.Variables.Where(v => v.Section == Section.Input).ToList(); - var declaration=GetMethodTypeDeclaration(inputParamsDeclaration); + var declaration = GetMethodTypeDeclaration(inputParamsDeclaration); return $"plc.{methodDeclaration.FullyQualifiedName}({declaration})"; } @@ -31,24 +33,80 @@ public static string GetBaseUid(IMethodDeclaration methodDeclaration) public static string GetBaseUid(IMethodPrototypeDeclaration methodDeclaration) { var inputParamsDeclaration = methodDeclaration.Variables.Where(v => v.Section == Section.Input).ToList(); - var declaration=GetMethodTypeDeclaration(inputParamsDeclaration); + var declaration = GetMethodTypeDeclaration(inputParamsDeclaration); return $"plc.{methodDeclaration.FullyQualifiedName}({declaration.ToString()})"; } private static string GetMethodTypeDeclaration(IList inputParams) - { - StringBuilder typeDeclaration = new StringBuilder(); - + { + StringBuilder typeDeclaration = new StringBuilder(); + foreach (var p in inputParams) { typeDeclaration.Append(p.Type); - if(inputParams.Last() != p) - { + if (inputParams.Last() != p) + { typeDeclaration.Append(","); } - } + } return typeDeclaration.ToString(); } + + + public static string? FindGitRepositoryRoot(string directoryPath) + { + DirectoryInfo? directory = new DirectoryInfo(directoryPath); + + while (directory != null) + { + if (Directory.Exists(Path.Combine(directory.FullName, ".git"))) + { + return directory.FullName; + } + + directory = directory.Parent; + } + + return null; + } + + public static (string?, string?) GetGitBranchAndRepo(string gitPath) + { + LibGit2Sharp.Branch? currentBranch = null; + string? remoteUrl = null; + + using (var repo = new LibGit2Sharp.Repository(gitPath)) + { + currentBranch = repo.Head; + + if (repo.Network.Remotes.Count() > 0) + { + remoteUrl = repo.Network.Remotes["origin"].Url; + } + } + + return (currentBranch.FriendlyName, remoteUrl); + } + + public static int GetLineNumber(int characterCount, TextLineCollection lines) + { + int totalCharacters = 0; + int lineNumber = 0; + + foreach (TextLine line in lines) + { + totalCharacters += line.Span.Length; + + if (totalCharacters >= characterCount) + { + return lineNumber; + } + + lineNumber++; + } + + return lineNumber; + } } -} +} \ No newline at end of file diff --git a/src/AXSharp.compiler/src/ixd/Mapper/CodeToYamlMapper.cs b/src/AXSharp.compiler/src/ixd/Mapper/CodeToYamlMapper.cs index 20a8c9ba..e5e6cf97 100644 --- a/src/AXSharp.compiler/src/ixd/Mapper/CodeToYamlMapper.cs +++ b/src/AXSharp.compiler/src/ixd/Mapper/CodeToYamlMapper.cs @@ -7,7 +7,7 @@ using System.Xml.Linq; using AXSharp.ixc_doc.Visitors; using NuGet.Packaging; - +using Microsoft.CodeAnalysis; namespace AXSharp.ixc_doc.Mapper { @@ -21,6 +21,9 @@ public CodeToYamlMapper(YamlHelpers yh) public Item PopulateItem(IDeclaration declaration) { + var gitPath = Helpers.Helpers.FindGitRepositoryRoot(((SourceLocation)declaration.Location).SourceText.Filename); + (string? branch, string? repo) = Helpers.Helpers.GetGitBranchAndRepo(gitPath); + return new Item { Uid = Helpers.Helpers.GetBaseUid(declaration), @@ -31,8 +34,19 @@ public Item PopulateItem(IDeclaration declaration) Summary = _yh.GetComments(declaration.Location).summary, Remarks = _yh.GetComments(declaration.Location).remarks, //Assemblies = new string[] { _yh.GetAssembly(_yh.PathToProjectFile) }, - - + Source = new Source + { + Remote = new Remote + { + Path = ((SourceLocation)declaration.Location).SourceText.Filename.Replace(gitPath + "\\", ""), + Branch = branch, + Repo = repo + }, + Id = Helpers.Helpers.GetBaseUid(declaration.FullyQualifiedName), + Path = ((SourceLocation)declaration.Location).SourceText.Filename.Replace(gitPath + "\\", ""), + StartLine = Helpers.Helpers.GetLineNumber(declaration.Location.FullSpan.Start, ((SourceLocation)declaration.Location).SourceText.Lines) + } + }; } diff --git a/src/AXSharp.compiler/src/ixd/Schemas/YamlSchema.cs b/src/AXSharp.compiler/src/ixd/Schemas/YamlSchema.cs index 6462a59d..7c2723f4 100644 --- a/src/AXSharp.compiler/src/ixd/Schemas/YamlSchema.cs +++ b/src/AXSharp.compiler/src/ixd/Schemas/YamlSchema.cs @@ -117,6 +117,12 @@ public partial class Remote { [JsonProperty("path")] public string Path { get; set; } + + [JsonProperty("branch")] + public string Branch { get; set; } + + [JsonProperty("repo")] + public string Repo { get; set; } } public partial class Syntax