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

[NEW-FEATURE][IXD] Add 'view-source' capability to plc API docs #229

Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/AXSharp.compiler/src/ixd/AXSharp.ixd.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LibGit2Sharp" Version="0.27.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="YamlDotNet" Version="13.0.0" />
<PackageReference Include="CommandLineParser" Version="2.9.1" />
Expand Down
80 changes: 69 additions & 11 deletions src/AXSharp.compiler/src/ixd/Helpers/Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,112 @@
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})";
}


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<IVariableDeclaration> 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;
}
}
}
}
20 changes: 17 additions & 3 deletions src/AXSharp.compiler/src/ixd/Mapper/CodeToYamlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Xml.Linq;
using AXSharp.ixc_doc.Visitors;
using NuGet.Packaging;

using Microsoft.CodeAnalysis;

namespace AXSharp.ixc_doc.Mapper
{
Expand All @@ -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),
Expand All @@ -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)
}

};
}

Expand Down
6 changes: 6 additions & 0 deletions src/AXSharp.compiler/src/ixd/Schemas/YamlSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down