private static void WriteCSharp(List <IEngineObject> engineObjects, QlikApiConfig config, QlikApiGenerator qlikApiGenerator) { //pragmas for compiler warnings var pragmas = new List <string>() { "#pragma warning disable IDE1006", "#pragma warning disable CS1591" }; logger.Info("Write Enums..."); var objectResults = engineObjects.Where(o => o.EngType == EngineType.ENUM).ToList(); var savePath = Path.Combine(config.OutputFolder, "Enums.cs"); qlikApiGenerator.SaveTo(config, objectResults, savePath, pragmas); logger.Info("Write Interfaces..."); objectResults = engineObjects.Where(o => o.EngType == EngineType.INTERFACE).ToList(); savePath = Path.Combine(config.OutputFolder, "Interfaces.cs"); qlikApiGenerator.SaveTo(config, objectResults, savePath); logger.Info("Write Classes..."); objectResults = engineObjects.Where(o => o.EngType == EngineType.CLASS).ToList(); foreach (var classResult in objectResults) { savePath = Path.Combine(config.OutputFolder, $"{classResult.Name}.cs"); qlikApiGenerator.SaveTo(config, new List <IEngineObject>() { classResult }, savePath); } }
private static void WriteTypeScript(List <IEngineObject> engineObjects, QlikApiConfig config, QlikApiGenerator qlikApiGenerator) { logger.Info("Write Enums..."); var objectResults = engineObjects.Where(o => o.EngType == EngineType.ENUM).ToList(); var enumFile = Path.Combine(config.TypeScriptFolder, "Enums.d.ts"); qlikApiGenerator.SaveTo(config, objectResults, enumFile, null); logger.Info("Write Interfaces..."); objectResults = engineObjects.Where(o => o.EngType == EngineType.INTERFACE).ToList(); var interfaceFile = Path.Combine(config.TypeScriptFolder, "Interfaces.d.ts"); qlikApiGenerator.SaveTo(config, objectResults, interfaceFile, null); logger.Info("Write Classes..."); objectResults = engineObjects.Where(o => o.EngType == EngineType.CLASS).ToList(); var classFile = Path.Combine(config.TypeScriptFolder, $"Class.d.ts"); qlikApiGenerator.SaveTo(config, objectResults, classFile, null); logger.Info("Write Index file..."); var indexBuilder = new StringBuilder(); indexBuilder.AppendLine("// Type definitions for qlik-engineapi 12.67"); indexBuilder.AppendLine("// Project: https://help.qlik.com/en-US/sense-developer/February2019/Subsystems/EngineAPI/Content/Sense_EngineAPI/introducing-engine-API.htm"); indexBuilder.AppendLine("// Definitions by: Konrad Mattheis <https://github.com/konne>"); indexBuilder.AppendLine("// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped"); indexBuilder.AppendLine(); indexBuilder.Append($"declare namespace {config.NamespaceName.Replace("Qlik.","")} {{"); var content = File.ReadAllText(enumFile); indexBuilder.AppendLine($"\n {content}"); content = File.ReadAllText(interfaceFile); indexBuilder.AppendLine($"\n {content}"); content = File.ReadAllText(classFile); indexBuilder.AppendLine($"\n {content}"); indexBuilder.AppendLine("}"); indexBuilder.AppendLine(""); indexBuilder.AppendLine("declare namespace enigmaJS {"); indexBuilder.AppendLine(" interface IGeneratedAPI {"); indexBuilder.AppendLine(" }"); indexBuilder.AppendLine("}"); File.WriteAllText(Path.Combine(config.TypeScriptFolder, "index.d.ts"), indexBuilder.ToString()); File.Delete(enumFile); File.Delete(interfaceFile); File.Delete(classFile); }
public void SaveToCSharp(QlikApiConfig config, List <IEngineObject> engineObjects, string savePath) { try { var enumList = new List <string>(); var fileContent = new StringBuilder(); fileContent.Append($"namespace {config.NamespaceName}"); fileContent.AppendLine(); fileContent.AppendLine("{"); fileContent.AppendLine(QlikApiUtils.Indented("#region Usings", 1)); fileContent.AppendLine(QlikApiUtils.Indented("using System;", 1)); fileContent.AppendLine(QlikApiUtils.Indented("using System.ComponentModel;", 1)); fileContent.AppendLine(QlikApiUtils.Indented("using System.Collections.Generic;", 1)); fileContent.AppendLine(QlikApiUtils.Indented("using Newtonsoft.Json;", 1)); fileContent.AppendLine(QlikApiUtils.Indented("using Newtonsoft.Json.Linq;", 1)); fileContent.AppendLine(QlikApiUtils.Indented("using System.Threading.Tasks;", 1)); fileContent.AppendLine(QlikApiUtils.Indented("using System.Threading;", 1)); fileContent.AppendLine(QlikApiUtils.Indented("using enigma;", 1)); fileContent.AppendLine(QlikApiUtils.Indented("#endregion", 1)); fileContent.AppendLine(); var lineCounter = 0; var enumObjects = engineObjects.Where(d => d.EngType == EngineType.ENUM).ToList(); if (enumObjects.Count > 0) { fileContent.AppendLine(QlikApiUtils.Indented("#region Enums", 1)); logger.Debug($"Write Enums {enumObjects.Count}"); foreach (EngineEnum enumValue in enumObjects) { lineCounter++; var enumResult = GetFormatedEnumBlock(enumValue); fileContent.AppendLine(enumResult); if (lineCounter < enumObjects.Count) { fileContent.AppendLine(); } } fileContent.AppendLine(QlikApiUtils.Indented("#endregion", 1)); fileContent.AppendLine(); } var interfaceObjects = engineObjects.Where(d => d.EngType == EngineType.INTERFACE).ToList(); if (interfaceObjects.Count > 0) { logger.Debug($"Write Interfaces {interfaceObjects.Count}"); fileContent.AppendLine(QlikApiUtils.Indented("#region Interfaces", 1)); lineCounter = 0; foreach (EngineInterface interfaceObject in interfaceObjects) { lineCounter++; if (interfaceObject.Name == "IObjectInterface") { continue; } // TODO fix that simple workaround to remove the ObjectInterface //Special for ObjectInterface => Add IObjectInterface var implInterface = String.Empty; if (Config.BaseObjectInterfaceName != interfaceObject.Name) { implInterface = $" : {Config.BaseObjectInterfaceName}"; } fileContent.AppendLine(QlikApiUtils.Indented($"public interface {interfaceObject.Name}{implInterface}", 1)); fileContent.AppendLine(QlikApiUtils.Indented("{", 1)); foreach (var property in interfaceObject.Properties) { fileContent.AppendLine(QlikApiUtils.Indented($"{QlikApiUtils.GetDotNetType(property.Type)} {property.Name} {{ get; set; }}", 2)); } foreach (var methodObject in interfaceObject.Methods) { fileContent.AppendLine(GetFormatedMethod(methodObject)); } if (Config.BaseObjectInterfaceName == interfaceObject.Name) { fileContent.AppendLine(QlikApiUtils.Indented("event EventHandler Changed;", 2)); fileContent.AppendLine(QlikApiUtils.Indented("event EventHandler Closed;", 2)); fileContent.AppendLine(QlikApiUtils.Indented("void OnChanged();", 2)); } fileContent.AppendLine(QlikApiUtils.Indented("}", 1)); if (lineCounter < interfaceObjects.Count) { fileContent.AppendLine(); } } fileContent.AppendLine(QlikApiUtils.Indented("#endregion", 2)); fileContent.AppendLine(); } var classObjects = engineObjects.Where(d => d.EngType == EngineType.CLASS).ToList(); if (classObjects.Count > 0) { fileContent.AppendLine(QlikApiUtils.Indented("#region Classes", 1)); logger.Debug($"Write Classes {classObjects.Count}"); lineCounter = 0; var descBuilder = new DescritpionBuilder(Config.UseDescription); foreach (EngineClass classObject in classObjects) { lineCounter++; descBuilder.Summary = classObject.Description; descBuilder.SeeAlso = classObject.SeeAlso; var desc = descBuilder.Generate(1); if (!String.IsNullOrEmpty(desc)) { fileContent.AppendLine(desc); } fileContent.AppendLine(QlikApiUtils.Indented($"public class {classObject.Name}<###implements###>", 1)); fileContent.AppendLine(QlikApiUtils.Indented("{", 1)); if (classObject.Properties.Count > 0) { fileContent.AppendLine(QlikApiUtils.Indented("#region Properties", 2)); var propertyCount = 0; foreach (var property in classObject.Properties) { propertyCount++; if (!String.IsNullOrEmpty(property.Description)) { var builder = new DescritpionBuilder(Config.UseDescription) { Summary = property.Description, }; var description = builder.Generate(2); if (!String.IsNullOrEmpty(description)) { fileContent.AppendLine(description); } } var dValue = String.Empty; if (property.Default != null) { dValue = property.Default.ToLowerInvariant(); if (property.IsEnumType) { dValue = GetEnumDefault(property.Type, property.Default); } fileContent.AppendLine(QlikApiUtils.Indented($"[DefaultValue({dValue})]", 2)); } var implements = String.Empty; var refType = property.GetRefType(); if (classObject.Type == "array") { implements = GetImplemention(property); fileContent.Replace("<###implements###>", implements); } else if (property.Type == "array") { fileContent.AppendLine(QlikApiUtils.Indented($"public List<{QlikApiUtils.GetDotNetType(refType)}> {property.Name} {{ get; set; }}", 2)); } else { var resultType = QlikApiUtils.GetDotNetType(property.Type); if (!String.IsNullOrEmpty(refType)) { resultType = refType; } var propertyText = QlikApiUtils.Indented($"public {resultType} {property.Name} {{ get; set; }}", 2); if (property.Default != null) { propertyText += $" = {dValue};"; } fileContent.AppendLine(propertyText); } if (propertyCount < classObject.Properties.Count) { fileContent.AppendLine(); } } fileContent.AppendLine(QlikApiUtils.Indented("#endregion", 2)); } fileContent.Replace("<###implements###>", ""); fileContent.AppendLine(QlikApiUtils.Indented("}", 1)); if (lineCounter < classObjects.Count) { fileContent.AppendLine(); } } fileContent.AppendLine(QlikApiUtils.Indented("#endregion", 1)); } fileContent.AppendLine("}"); var content = fileContent.ToString().Trim(); File.WriteAllText(savePath, content); } catch (Exception ex) { logger.Error(ex, $"The .NET file could not create."); } }
public QlikApiGenerator(QlikApiConfig config) { Config = config; }