Skip to content

Commit

Permalink
Updated transpiler
Browse files Browse the repository at this point in the history
  • Loading branch information
tbm0115 committed Aug 8, 2024
1 parent 64fa41a commit f56b0cb
Show file tree
Hide file tree
Showing 1,346 changed files with 27,338 additions and 5,351 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using CaseExtensions;
using MtconnectTranspiler.CodeGenerators.ScribanTemplates;
using MtconnectTranspiler.Sinks.CSharp.Example;
using MtconnectTranspiler.Sinks.ScribanTemplates;
using MtconnectTranspiler.Xmi;
using MtconnectTranspiler.Xmi.UML;
using System.Collections.Generic;
using System.Linq;

namespace MtconnectTranspiler.Sinks.CSharp.Models
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using CaseExtensions;
using MtconnectTranspiler.CodeGenerators.ScribanTemplates;
using MtconnectTranspiler.Sinks.CSharp.Example;
using MtconnectTranspiler.Sinks.ScribanTemplates;
using MtconnectTranspiler.Xmi;
using MtconnectTranspiler.Xmi.UML;
using System.Collections.Generic;
using System.Linq;

namespace MtconnectTranspiler.Sinks.CSharp.Models
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System;
using MtconnectTranspiler.Xmi.UML;
using MtconnectTranspiler.Contracts;
using MtconnectTranspiler.CodeGenerators.ScribanTemplates;

namespace MtconnectTranspiler.Sinks.CSharp.Models
{
/// <summary>
/// Helper methods to process content for scriban templates
/// </summary>
public class CSharpHelperMethods : ScribanTemplates.ScribanHelperMethods
public class CSharpHelperMethods : ScribanHelperMethods
{
private static Dictionary<string, Type> umlDataTypeToCSharp = new Dictionary<string, Type>()
{
Expand Down
36 changes: 9 additions & 27 deletions MtconnectTranspiler.Sinks.CSharp.Example/Models/CSharpPackage.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using CaseExtensions;
using MtconnectTranspiler.CodeGenerators.ScribanTemplates;
using MtconnectTranspiler.Sinks.CSharp.Models;
using MtconnectTranspiler.Sinks.Models;
using MtconnectTranspiler.Sinks.ScribanTemplates;
using MtconnectTranspiler.Xmi;
using MtconnectTranspiler.Xmi.UML;
using System.Data;
Expand Down Expand Up @@ -59,12 +58,20 @@ public string Filename
/// </summary>
public IEnumerable<CSharpEnum> Enums => _enums;

/// <summary>
/// Reference to any Comments written in the SysML model to be converted into a C# format <c>&lt;summary /&gt;</c>
/// </summary>
public Summary Summary { get; protected set; }

public CSharpPackage(XmiDocument model, UmlPackage source) : base(model, source)
{
_name = CSharpHelperMethods.ToPascalCase(source.Name);

ReferenceId = source!.Id;

if (source.Comments?.Length > 0)
Summary = new Summary(source.Comments);

_packages = source!.Packages
?.Select(o => new CSharpPackage(model, o))
?.ToList()
Expand All @@ -74,31 +81,6 @@ public CSharpPackage(XmiDocument model, UmlPackage source) : base(model, source)
?.Select(o => new CSharpClass(model, o))
?.ToList()
?? new List<CSharpClass>();
//var classGroupings = _classes.GroupBy(o => o.Name);
//foreach (var classGrouping in classGroupings)
//{
// if (classGrouping.Count() <= 1)
// continue;
// var classes = _classes.Where(o => o.Name == classGrouping.Key).ToList();
// foreach (var @class in classes)
// {
// if (!string.IsNullOrEmpty(@class?.Generalization))
// {
// string generalization = CSharpHelperMethods.TypeDeepSearch(model, @class?.Generalization, out XmiElement? remoteType);
// if (generalization.EndsWith("Class"))
// {
// string remoteClassName = generalization.Replace("Class", string.Empty);
// if (@class.Name.EndsWith(remoteClassName))
// {
// @class.Name += "Class";
// }else
// {
// @class.Name += generalization;
// }
// }
// }
// }
//}

_enums = source!.Enumerations
?.Select(o => new CSharpEnum(model, o))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
using MtconnectTranspiler.Sinks.CSharp.Models;
using MtconnectTranspiler.Sinks.ScribanTemplates;
using MtconnectTranspiler.Xmi.UML;
using MtconnectTranspiler.Xmi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using MtconnectTranspiler.CodeGenerators.ScribanTemplates;

namespace MtconnectTranspiler.Sinks.CSharp.Example.Models
{
Expand Down
3 changes: 1 addition & 2 deletions MtconnectTranspiler.Sinks.CSharp.Example/Models/Summary.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using MtconnectTranspiler.Sinks.ScribanTemplates;
using MtconnectTranspiler.CodeGenerators.ScribanTemplates;
using MtconnectTranspiler.Xmi;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using MtconnectTranspiler.Xmi;
using MtconnectTranspiler.Sinks.Models;

namespace MtconnectTranspiler.Sinks.CSharp.Models
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Consoul" Version="1.6.5" />
<PackageReference Include="Consoul" Version="1.6.6" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="MtconnectTranspiler" Version="1.0.8.3-prerelease.3" />
<PackageReference Include="MtconnectTranspiler.Sinks.ScribanTemplates" Version="1.0.15.3-prerelease.2" />
<PackageReference Include="MtconnectTranspiler.CodeGenerators.ScribanTemplates" Version="2.3.0-prerelease.2" />
</ItemGroup>

<ItemGroup>
Expand All @@ -38,4 +44,10 @@
</Content>
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using MtconnectTranspiler.Sinks.CSharp.Models;
using MtconnectTranspiler.Sinks.Models;
using MtconnectTranspiler.Sinks.ScribanTemplates;
using MtconnectTranspiler.Sinks.Models;
using MtconnectTranspiler.Xmi;

namespace MtconnectTranspiler.Sinks.CSharp
Expand Down
46 changes: 41 additions & 5 deletions MtconnectTranspiler.Sinks.CSharp.Example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using ConsoulLibrary;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MtconnectTranspiler;
using MtconnectTranspiler.CodeGenerators.ScribanTemplates;
using MtconnectTranspiler.Sinks;
using MtconnectTranspiler.Sinks.CSharp.Example;

Expand All @@ -16,10 +19,41 @@ private static void Main(string[] args)
Consoul.Write("Creating project path: " + projectPath);
Directory.CreateDirectory(projectPath);
}
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();

var logFactory = LoggerFactory.Create((o) => o.AddConsoulLogger());
var dispatchLogger = logFactory.CreateLogger<TranspilerDispatcher>();
var transpilerLogger = logFactory.CreateLogger<ITranspilerSink>();
//setup our DI
var services = new ServiceCollection()
.AddLogging((builder) =>
{
builder.AddConsoulLogger();
});
var serviceProvider = services
.AddSingleton<ScribanTemplateGenerator>((builder) =>
{
return new ScribanTemplateGenerator(configuration["OutputPath"], builder.GetService<ILoggerFactory>().CreateLogger<ScribanTemplateGenerator>());
})
.AddSingleton(configuration)
.AddSingleton<Transpiler>()
.BuildServiceProvider();

//configure console logging
//serviceProvider
// .GetService<ILoggerFactory>()
// .AddConsole(LogLevel.Debug);

var logger = serviceProvider.GetService<ILoggerFactory>()
.CreateLogger<Program>();
logger.LogDebug("Starting application");


//var logFactory = LoggerFactory.Create((o) => o.AddConsoulLogger());
//var dispatchLogger = logFactory.CreateLogger<TranspilerDispatcher>();
//var transpilerLogger = logFactory.CreateLogger<ITranspilerSink>();


// NOTE: The GitHubRelease can be a reference to a specific tag referring to the version in which to download.
Expand All @@ -38,9 +72,11 @@ private static void Main(string[] args)
}

using (var tokenSource = new CancellationTokenSource())
using (var dispatcher = new TranspilerDispatcher(dispatchOptions, dispatchLogger))
using (var dispatcher = new TranspilerDispatcher(dispatchOptions, serviceProvider.GetService<ILoggerFactory>()
.CreateLogger<TranspilerDispatcher>()))
{
dispatcher.AddSink(new Transpiler(projectPath, transpilerLogger));
var defaultTranspiler = serviceProvider.GetService<Transpiler>();
dispatcher.AddSink(defaultTranspiler);

Consoul.Write("Beginning deserialization and dispatching");
var task = Task.Run(() => dispatcher.TranspileAsync(tokenSource.Token));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ using MtconnectTranspiler.Sinks.CSharp.Contracts.Interfaces;

namespace {{ to_code_safe source.namespace }}
{
{{~ if source?.summary != null && source?.summary != "" ~}}
/// <summary>
{{ source?.summary }}
/// </summary>
{{~ end ~}}
[GeneratedCode("MtconnectTranspiler.Sinks.CSharp", "{{ version }}")]
public sealed class {{ code_name }} : IClass
{
/// <inheritdoc />
public string ReferenceId => "{{ source.reference_id }}";

/// <inheritdoc />
public string Summary => @"{{ source.summary | string.replace "\"" "\"\"" | string.replace "\\" "\\\\" }}";

/// <inheritdoc />
public string Name => "{{ source.name }}";

Expand Down Expand Up @@ -70,8 +78,16 @@ namespace {{ to_code_safe source.namespace }}
item_suffix = "Property"
end
~}}
/// <summary>
/// <inheritdoc cref="{{ item_code_name}}{{ item_suffix }}" path="/summary" /><br/>
/// <remarks>Original Name: {{ item.name }}</remarks>
/// </summary>
public {{ item_code_name }}{{ item_suffix }} {{ item_code_name }}{{ if item.association }}Part{{ end }} { get; } = new {{ item_code_name }}{{ item_suffix }}();
{{~ if item?.summary != null && item?.summary != "" ~}}
/// <summary>
{{ item?.summary }}
/// </summary>
{{~ end ~}}
public sealed class {{ item_code_name }}{{ item_suffix }} : IProperty
{
/// <summary>
Expand All @@ -89,6 +105,9 @@ namespace {{ to_code_safe source.namespace }}
/// <inheritdoc />
public string Name => "{{ item.name }}";

/// <inheritdoc />
public string Summary => @"{{ source.summary | string.replace "\"" "\"\"" | string.replace "\\" "\\\\" }}";

/// <inheritdoc />
public string AccessModifier => "{{ item.access_modifier }}";

Expand All @@ -114,8 +133,6 @@ namespace {{ to_code_safe source.namespace }}
// Note: DefaultValue.Name
/// <inheritdoc />
public string DefaultValue => "{{ item.default_value }}";

// TODO: Add Summary
}
{{~ end ~}}
};
Expand All @@ -130,6 +147,7 @@ namespace {{ to_code_safe source.namespace }}
/// {{ constraint.name }}
/// </summary>
/// <remarks>Specification Language: <c>{{ constraint?.specification?.language ?? "Unspecified" }}</c></remarks>
public string {{ constraint.name }} => @"{{ constraint?.raw_script | string.replace "\"" "\"\"" }}";
/*
{{ constraint?.raw_script ?? "No Content" }}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,57 @@ using MtconnectTranspiler.Sinks.CSharp.Contracts.Interfaces;

namespace {{ to_code_safe source.namespace }}
{
/// <summary>
/// Static root for the MTConnect SysML model.
/// </summary>
[GeneratedCode("MtconnectTranspiler.Sinks.CSharp", "{{ version }}")]
public static class MtconnectModel
{
/// <summary>
/// Collection of root <see cref="IPackage" />s.
/// </summary>
public static IPackage[] Packages => new IPackage[] {
{{~ for item in source.packages ~}}
{{~ item_code_name = item.name | to_code_safe | string.remove "." ~}}
{{ item_code_name }}Package,
{{~ end ~}}
};

/// <summary>
/// Collection of root <see cref="IClass" />'.
/// </summary>
public static IClass[] Classes => new IClass[] {
{{~ for item in source.classes ~}}
{{~ item_class_code_name = item.name | to_code_safe | string.remove "." ~}}
{{ item_class_code_name }},
{{~ end ~}}
};

#region Packages
{{~ for item in source.packages ~}}
{{~ item_code_name = item.name | to_code_safe | string.remove "." ~}}
private static {{ item_code_name }}Package _{{ item_code_name }}Package;
/// <summary>
/// <ineritdoc cref="Mtconnect.{{ item_code_name }}Package" path="/summary" />
/// </summary>
public static {{ item_code_name }}Package {{ item_code_name }}Package => _{{ item_code_name}}Package ?? (_{{ item_code_name }}Package = new {{ item_code_name }}Package());
{{~ end ~}}

{{ end ~}}
#endregion

#region Classes
{{~ for item in source.classes ~}}
{{~ item_class_code_name = item.name | to_code_safe | string.remove "." ~}}
private static {{ item_class_code_name }} _{{ item_class_code_name }};
{{~ if item.ReferenceId ~}}
/// <summary>
/// Id: {{ item.ReferenceId }}
/// Id: {{ item.ReferenceId }}<br/>
/// <inheritdoc cref="Package.{{ item_class_code_name}}" path="/summary" />
/// </summary>
{{~ end ~}}
public static Package.{{ item_class_code_name }} {{ item_class_code_name }} => _{{ item_class_code_name }} ?? (_{{ item_class_code_name }} = new {{ item_class_code_name }}());
{{~ end ~}}

{{ end ~}}
#endregion
}
}
Loading

0 comments on commit f56b0cb

Please sign in to comment.