JD2022-TU1/main/tools/apps/TeaBox/Script/Tools/CodeQuality/CodingUtilitiesMacros.cs

102 lines
No EOL
4 KiB
C#

using System;
using System.Xml;
using System.Xml.Schema;
using TeaBox.Macros;
using System.IO;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
namespace TeaBox.Code
{
[Module(Icon = "code")]
[DisplayName("Code")]
[AvailableOn(TeaBoxFlavor.Code)]
public static class CodingUtilitiesMacros
{
[DisplayName("Check all CRC in source codebase")]
[Category(@"Robot\Code\CodeQuality")]
[Macro(ThreadAffinity.ExecuteAsync, Icon = "tool.png")]
public static void CheckAllCrc(IExecutionContext context, RelativePath path)
{
StringID.CheckAllCrc(context, path);
}
[DisplayName("Make string ID (CRC)")]
[Macro(ThreadAffinity.ExecuteInGUIThread, Icon = "tool.png", ExecutionPolicy = ConcurrentExecutionPolicy.Immediate)]
public static void MakeStringID(IExecutionContext context, string strings = null)
{
if (!string.IsNullOrEmpty(strings))
{
var entries = strings.Split(new char[] { ',', ';', '\n' }).ToList();
entries.ForEach(entry => entry.Trim());
entries.RemoveAll(entry => String.IsNullOrEmpty(entry));
entries.ForEach(entry => {
context.LogMessage($"For String -> {entry} \n ==> {ComputeCRC(entry)}");
});
}
else
{
var className = Clipboard.GetText();
Clipboard.SetText(ComputeCRC(className));
}
}
private static string ComputeCRC(string className)
{
return StringID.ComputeCRC(className).ToString();
}
[Macro(Icon = "tool.png"), Description("Analyze the xml and extract c# classes from it")]
public static void XmlToCSharpClass(IExecutionContext context, string xml)
{
// Creating xsd from xml
XmlReader reader = XmlReader.Create(new StringReader(xml));
XmlSchemaInference inference = new XmlSchemaInference();
var schemaschemaSet = inference.InferSchema(reader);
var tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
System.IO.Directory.CreateDirectory(tempDirectory);
string schemaFileName = Path.Combine(tempDirectory, "schema.xsd");
string csharpFileName = Path.Combine(tempDirectory, "schema.cs");
var schema = schemaschemaSet.Schemas().Cast<XmlSchema>().First();
using (var stream = File.Create(schemaFileName))
{
schema.Write(stream);
}
// Creating c# from xsd.
string windowsSdk = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), @"Microsoft SDKs\Windows");
string xsdExe = (from directory in System.IO.Directory.GetDirectories(windowsSdk)
let file = Path.Combine(directory, @"bin\xsd.exe")
where File.Exists(file)
select file).FirstOrDefault();
System.Diagnostics.Process.Start(xsdExe, string.Format("{0} /classes /outputdir:{1}", schemaFileName, tempDirectory)).WaitForExit();
Process.Start(csharpFileName);
}
[Category("(Advanced)")]
[Macro(ThreadAffinity.ExecuteAsync, Icon = "tool.png")]
public static void LogTestMessage(IExecutionContext context, [DefaultValue("Default Message")] string message, LogCategory logCategory)
{
if (logCategory == LogCategory.Message)
{
context.LogMessage(message);
}
else if (logCategory == LogCategory.Warning)
{
context.LogWarning(message);
}
else
{
context.Log(new Error(LogCategory.Error, message) { Code = "CODE1234" });
}
}
}
}