protected override void RunCore() { if (LoadConfiguration()) { List <Exception> exs = new List <Exception>(); try { foreach (ContractServiceDefinition item in _Configuration.ContractServiceInterfaces) { item.IsPartial = true; InterfaceTemplate template = new InterfaceTemplate(); template.Interface = (ContractServiceInterfaceDefinition)item; template.Output.File = String.Format(@"{0}.cs", item.Name); template.Render(); } } catch (Exception ex) { exs.Add(ex); } if (exs.Count > 0) { ErrorTemplate errortemplate = new ErrorTemplate(); ErrorTemplate.Exceptions = exs; errortemplate.Output.File = "Error.log"; errortemplate.Render(); throw new Exception("See generated output for errors."); } } }
public virtual string GenerateCode() { InterfaceTemplate t = new InterfaceTemplate(); t.Interface = this; return(t.TransformText()); }
public static string GenerateInterfaceCode(InterfaceData interfaceData) { InterfaceTemplate t = new InterfaceTemplate(); t.Session = new Microsoft.VisualStudio.TextTemplating.TextTemplatingSession(); t.Session["interfaceParameters"] = interfaceData; t.Session["methods"] = interfaceData.Methods; // Add other parameter values to t.Session here. t.Initialize(); // Must call this to transfer values. string resultText = t.TransformText(); return(resultText); }
/// <summary> /// 生成接口代码 /// </summary> /// <param name="table">表名</param> /// <param name="namespacePfx">命名空间前辍</param> /// <param name="type">类型</param> /// <param name="fileName">文件名</param> /// <returns>接口代码</returns> private string BuilderInterfaceCode(TableInfo table, string namespacePfx, string type, out string fileName) { string basicName = table.Name.FristUpper(); string name = $"I{basicName}Persistence"; fileName = $"{name}.cs"; var desc = string.IsNullOrWhiteSpace(table.Description) ? basicName : table.Description; return(InterfaceTemplate .Replace("|NamespacePfx|", namespacePfx) .Replace("|Description|", desc) .Replace("|Name|", name) .Replace("|Model|", basicName)); }
/// <summary> /// 生成接口代码 /// </summary> /// <param name="table">表名</param> /// <param name="codeParam">代码参数</param> /// <param name="fileName">文件名</param> /// <returns>接口代码</returns> private string BuilderInterfaceCode(TableInfo table, CodeParamInfo codeParam, out string fileName) { string basicName = table.Name.FristUpper(); string name = $"I{basicName}Service"; fileName = $"{name}.cs"; var desc = string.IsNullOrWhiteSpace(table.Description) ? basicName : table.Description; return(InterfaceTemplate .Replace("|NamespacePfx|", codeParam.NamespacePfx) .Replace("|Description|", desc) .Replace("|Name|", name) .Replace("|Model|", basicName) .Replace("|PkType|", codeParam.PkType.ToCodeString())); }
static void Generate(string apiPath) { try { // Enumerate through all *.json files in the provided path and generate classes if (Directory.Exists(apiPath)) { Log("Scanning {0}...", apiPath); var jsonFiles = Directory.EnumerateFiles(apiPath, "*.json").ToList(); Log("Found {0} objects.", jsonFiles.Count); if (jsonFiles.Count > 0) { // Parse JSON into metadata classes var generatedPath = Path.Combine(apiPath, "generated"); if (!Directory.Exists(generatedPath)) { Directory.CreateDirectory(generatedPath); } Log("Parsing JSON files..."); var metaClasses = new List <MetaClass>(); foreach (var jsonFile in jsonFiles) { Log("Parsing {0}...", Path.GetFileName(jsonFile)); var node = JsonConvert.DeserializeObject <Node>(File.ReadAllText(jsonFile)); if (node.Attributes != null) { var metaClass = new MetaClass(node); if (!String.IsNullOrWhiteSpace(metaClass.Name)) { metaClasses.Add(metaClass); } } } // Generate classes from the metadata Log("Generating classes..."); foreach (var metaClass in metaClasses) { Log("Generating {0}...", metaClass.FullName); // Add properties from mixins to the classes metaClass.AddMixins(metaClasses); // Add class interfaces metaClass.AddInterfaces(metaClasses); // Generate class/interface from T4 template string content = null; if (!metaClass.IsInterface) { var template = new ClassTemplate { Session = new Dictionary <string, object> { { "Model", metaClass } } }; content = template.TransformText(); } else if (metaClass.IsInterface) { var template = new InterfaceTemplate { Session = new Dictionary <string, object> { { "Model", metaClass } } }; content = template.TransformText(); } // Write output class to .cs file if (content != null) { var relOutputFile = metaClass.OriginalFullName.Replace(".", @"\") + ".cs"; if (!relOutputFile.StartsWith(@"qx\")) { relOutputFile = @"qx\" + relOutputFile; } var outputFile = Path.Combine(generatedPath, relOutputFile); var outputPath = Path.GetDirectoryName(outputFile); if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } File.WriteAllText(outputFile, content); } } } Log("Generation completed."); } else { Log("Path not found: " + apiPath); } } catch (Exception e) { Log(e); } }