Skip to content

Commit

Permalink
unified exporter & cli changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrof committed Aug 11, 2024
1 parent 9ad3a25 commit c28a0ea
Show file tree
Hide file tree
Showing 12 changed files with 388 additions and 33 deletions.
119 changes: 95 additions & 24 deletions EDSSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,53 +43,73 @@ static void Main(string[] args)
}


if (argskvp.ContainsKey("--type") && argskvp.ContainsKey("--infile") && argskvp.ContainsKey("--outfile"))
if (argskvp.ContainsKey("--infile") && argskvp.ContainsKey("--outfile"))
{
string infile = argskvp["--infile"];
string outfile = argskvp["--outfile"];
string outtype = "";
if (argskvp.ContainsKey("--type"))
{
outtype = argskvp["--type"];
}

ExporterFactory.Exporter type = ExporterFactory.Exporter.CANOPENNODE_LEGACY; //sensible default

if (argskvp["--type"].IndexOf("4") > 0)
type = ExporterFactory.Exporter.CANOPENNODE_V4;

switch (Path.GetExtension(infile).ToLower())
{
case ".xdd":
openXDDfile(infile, outfile,type);
openXDDfile(infile);
break;

case ".eds":
openEDSfile(infile, outfile,InfoSection.Filetype.File_EDS,type);
openEDSfile(infile);
break;


default:
return;

}
if(eds != null)
{
Export(outfile, outtype);
}
}
else
{
Console.WriteLine("Usage EDSEditor --type [CanOpenNode|CanOpenNodeV4] --infile file.[xdd|eds] --outfile [CO_OD.c|OD]");
PrintHelpText();
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
PrintHelpText();
}
}

private static void openEDSfile(string infile, string outfile, InfoSection.Filetype ft, ExporterFactory.Exporter exporttype)
private static void openEDSfile(string infile)
{

eds.Loadfile(infile);
}

exportCOOD(outfile,exporttype);
private static void openXDDfile(string path)
{
CanOpenXDD_1_1 coxml_1_1 = new CanOpenXDD_1_1();
eds = coxml_1_1.ReadXML(path);

if (eds == null)
{
CanOpenXDD coxml = new CanOpenXDD();
eds = coxml.readXML(path);

if (eds == null)
return;
}

eds.projectFilename = path;
}

private static void exportCOOD(string outpath,ExporterFactory.Exporter type)
private static void Export(string outpath, string outType)
{
outpath = Path.GetFullPath(outpath);

Expand All @@ -99,10 +119,15 @@ private static void exportCOOD(string outpath,ExporterFactory.Exporter type)

Warnings.warning_list.Clear();

IExporter exporter = ExporterFactory.getExporter(type);
var filepath = Path.Combine(savePath, Path.GetFileNameWithoutExtension(outpath));
var exporterDef = FindMatchingExporter(outpath, outType);

exporter.export(filepath, eds);
if(exporterDef == null)
{
throw new Exception("Unable to find matching exporter)");
}

var edss = new List<EDSsharp> { eds };
exporterDef.Func(outpath, edss);

foreach(string warning in Warnings.warning_list)
{
Expand All @@ -111,22 +136,68 @@ private static void exportCOOD(string outpath,ExporterFactory.Exporter type)

}

private static void openXDDfile(string path, string outpath,ExporterFactory.Exporter exportertype)
static ExporterDiscriptor FindMatchingExporter(string outpath, string outType)
{
CanOpenXDD_1_1 coxml_1_1 = new CanOpenXDD_1_1();
eds = coxml_1_1.ReadXML(path);
//Find exporter(s) matching the file extension
var exporters = Filetypes.GetExporters();

if (eds == null)
var outFiletype = Path.GetExtension(outpath);
var exporterMatchingFiletype = new List<ExporterDiscriptor>();
foreach (var exporter in exporters)
{
CanOpenXDD coxml = new CanOpenXDD();
eds = coxml.readXML(path);
foreach (var type in exporter.Filetypes)
{
if (type == outFiletype)
{
exporterMatchingFiletype.Add(exporter);
break;
}
}
}

if (eds == null)
return;
if (exporterMatchingFiletype.Count == 1)
{
//If only one match we use that one.
return exporterMatchingFiletype[0];
}

eds.projectFilename = path;
exportCOOD(outpath,exportertype);
//If multiple or zero matches use type
foreach (var exporter in exporters)
{
if (exporter.Description.Replace(" ", null) == outType)
{
return exporter;
}
}
return null;
}

static void PrintHelpText()
{
Console.WriteLine("Usage: EDSEditor --infile file.[xdd|eds] --outfile [valid output file] [OPTIONAL] --type [valid type]");
Console.WriteLine("The output file format depends on --outfile extension and --type");
Console.WriteLine("If --outfile extension matcher one exporter then --type IS NOT needed");
Console.WriteLine("If --outfile extension matcher multiple exporter then --type IS needed");
Console.WriteLine("If --outfile has no extension --type IS needed");
Console.WriteLine("Exporters:");

var exporters = Filetypes.GetExporters();
foreach (var exporter in exporters)
{
string filetypes = "";
for (int i = 0; i < exporter.Filetypes.Length; i++)
{
filetypes += exporter.Filetypes[i];
//add seperator char if multiple filetypes
if(i +1 != exporter.Filetypes.Length)
{
filetypes += ',';
}
}

string description = $" {exporter.Description.Replace(" ",null)} [{filetypes}]";
Console.WriteLine(description);
}
}
}
}
29 changes: 29 additions & 0 deletions Tests/CLITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ public class CliTest : libEDSsharp.EDSsharp
{
string RunEDSSharp(string arguments)
{
File.Delete("Legacy.c");
File.Delete("Legacy.h");
File.Delete("V4.c");
File.Delete("V4.h");
File.Delete("file.eds");
File.Delete("file.xpd");

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
Expand All @@ -33,5 +40,27 @@ public void XddToCanOpenNodeV4()
string[] files = Directory.GetFiles(".", "V4.*");
Assert.Equal(2, files.Length);
}
[Fact]
public void OnlySingleExporterByExtensionPossible()
{
RunEDSSharp("--infile minimal_project.xdd --outfile file.eds");
string[] files = Directory.GetFiles(".", "file.eds");
Assert.Single(files);
}
[Fact]
public void MultipleExporterByExtensionPossibleWithoutType()
{
//this should fail
RunEDSSharp("--infile minimal_project.xdd --outfile file.xpd");
string[] files = Directory.GetFiles(".", "file.xpd");
Assert.Empty(files);
}
[Fact]
public void MultipleExporterByExtensionPossibleWithType()
{
RunEDSSharp("--type CanOpenXPDv1.0 --infile minimal_project.xdd --outfile file.xpd");
string[] files = Directory.GetFiles(".", "file.xpd");
Assert.Single(files);
}
}
}
18 changes: 17 additions & 1 deletion libEDSsharp/CanOpenNodeExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace libEDSsharp
/// <summary>
/// Export .c and .h files for CanOpenNode v1-3
/// </summary>
public class CanOpenNodeExporter : IExporter
public class CanOpenNodeExporter : IExporter, IFileExporter
{

private string folderpath;
Expand All @@ -59,6 +59,22 @@ public class CanOpenNodeExporter : IExporter
ODentry maxRXmappingsOD=null;
ODentry maxTXmappingsOD=null;

/// <summary>
/// Fetches all the different fileexporter types the class supports
/// </summary>
/// <returns>List of the different exporters the class supports</returns>
public ExporterDiscriptor[] GetExporters()
{
return new ExporterDiscriptor[]
{
new ExporterDiscriptor("CanOpenNode", new string[] { ".h", ".c" }, ExporterDiscriptor.ExporterFlags.CanOpenNode, delegate (string filepath, List<EDSsharp> edss)
{
var e = new CanOpenNodeExporter();
e.export(filepath, edss[0]);
})
};
}

/// <summary>
/// Register names of index and subindex that need to have standard names to be able to work with CanOpenNode
/// </summary>
Expand Down
18 changes: 17 additions & 1 deletion libEDSsharp/CanOpenNodeExporter_V4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace libEDSsharp
/// <summary>
/// Exporter for CanOpenNode_V4
/// </summary>
public class CanOpenNodeExporter_V4 : IExporter
public class CanOpenNodeExporter_V4 : IExporter, IFileExporter
{
private string odname;

Expand All @@ -47,6 +47,22 @@ public class CanOpenNodeExporter_V4 : IExporter
private Dictionary<string, UInt16> ODCnt;
private Dictionary<string, int> ODArrSize;

/// <summary>
/// Fetches all the different fileexporter types the class supports
/// </summary>
/// <returns>List of the different exporters the class supports</returns>
public ExporterDiscriptor[] GetExporters()
{
return new ExporterDiscriptor[]
{
new ExporterDiscriptor("CanOpenNodeV4", new string[] { ".h", ".c" }, ExporterDiscriptor.ExporterFlags.CanOpenNode, delegate (string filepath, List<EDSsharp> edss)
{
var e = new CanOpenNodeExporter_V4();
e.export(filepath, edss[0]);
})
};
}

/// <summary>
/// export the current data set in the CanOpen Node format V4
/// </summary>
Expand Down
23 changes: 22 additions & 1 deletion libEDSsharp/CanOpenXDD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,30 @@ based heavily on the files CO_OD.h and CO_OD.c from CANopenNode which are

namespace libEDSsharp
{
public class CanOpenXDD
public class CanOpenXDD : IFileExporter
{
public ISO15745ProfileContainer dev;

/// <summary>
/// Fetches all the different fileexporter types the class supports
/// </summary>
/// <returns>List of the different exporters the class supports</returns>
public ExporterDiscriptor[] GetExporters()
{
return new ExporterDiscriptor[] {
new ExporterDiscriptor("CanOpen XDD v1.0", new string[] { ".xdd" }, 0, delegate (string filepath, List<EDSsharp> edss)
{
var e = new CanOpenXDD();
e.writeXML(filepath,edss[0]);
}),
new ExporterDiscriptor("CanOpen XPD v1.0", new string[] { ".xpd" }, ExporterDiscriptor.ExporterFlags.MultipleNodeSupport, delegate (string filepath, List<EDSsharp> edss)
{
var e = new CanOpenXDD();
e.writeMultiXML(filepath,edss);
})
};
}

public EDSsharp readXML(string file)
{

Expand Down
34 changes: 32 additions & 2 deletions libEDSsharp/CanOpenXDD_1_1.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
This file is part of libEDSsharp.
libEDSsharp is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -38,8 +38,38 @@ namespace libEDSsharp
/// Convert to/from EDSsharp and CanOpenXDD v1.1, it uses the generated source file CanOpenXSD_1_1
/// </summary>
/// <seealso cref="CanOpenXSD_1_1"/>
public class CanOpenXDD_1_1
public class CanOpenXDD_1_1 : IFileExporter
{
/// <summary>
/// Fetches all the different fileexporter types the class supports
/// </summary>
/// <returns>List of the different exporters the class supports</returns>
public ExporterDiscriptor[] GetExporters()
{
return new ExporterDiscriptor[] {
new ExporterDiscriptor("CanOpen XDD v1.1", new string[] { ".xdd" }, 0, delegate (string filepath, List<EDSsharp> edss)
{
var e = new CanOpenXDD_1_1();
e.WriteXML(filepath,edss[0],false,false);
}),
new ExporterDiscriptor("CanOpen XDD v1.1 stripped", new string[] { ".xdd" }, 0, delegate (string filepath, List<EDSsharp> edss)
{
var e = new CanOpenXDD_1_1();
e.WriteXML(filepath,edss[0],false,true);
}),
new ExporterDiscriptor("CanOpen XPD v1.1", new string[] { ".xpd" }, ExporterDiscriptor.ExporterFlags.MultipleNodeSupport, delegate (string filepath, List<EDSsharp> edss)
{
var e = new CanOpenXDD_1_1();
//What is commissioned .xpd extension??
e.WriteMultiXML(filepath,edss,false);
}),
new ExporterDiscriptor("CanOpen XDC v1.1", new string[] { ".xdc" }, 0, delegate (string filepath, List<EDSsharp> edss)
{
var e = new CanOpenXDD_1_1();
e.WriteXML(filepath,edss[0],true,true);
})
};
}
/// <summary>
/// Read XDD file into EDSsharp object
/// </summary>
Expand Down
17 changes: 16 additions & 1 deletion libEDSsharp/DocumentationGenHtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,25 @@ namespace libEDSsharp
/// <summary>
/// Documentation generator
/// </summary>
public class DocumentationGenHtml
public class DocumentationGenHtml : IFileExporter
{
StreamWriter file = null;

/// <summary>
/// Fetches all the different fileexporter types the class supports
/// </summary>
/// <returns>List of the different exporters the class supports</returns>
public ExporterDiscriptor[] GetExporters()
{
return new ExporterDiscriptor[]
{
new ExporterDiscriptor("Documentation HTML", new string[] { ".html" }, ExporterDiscriptor.ExporterFlags.Documentation, delegate (string filepath, List<EDSsharp> edss)
{
var e = new DocumentationGenMarkup();
e.genmddoc(filepath, edss[0]);
})
};
}
/// <summary>
/// Generate html documentation
/// </summary>
Expand Down
Loading

0 comments on commit c28a0ea

Please sign in to comment.