public TemplateNode Clone() { //var t = (TemplateNode)this.MemberwiseClone(); var t = new TemplateNode(); t.Extension = this.Extension; t.FileName = this.FileName; t.IsFolder = this.IsFolder; t.IsSelected = this.IsSelected; t.ObjDefName = this.ObjDefName; t.Name = this.Name; t.Path = this.Path; var list = new List <TemplateParameter>(); foreach (var p in this.Parameters) { var newParam = p.Clone(); list.Add(newParam); } t.Parameters = list.ToArray(); foreach (TemplateNode child in this.Nodes) { var newNode = child.Clone(); t.Nodes.Add(newNode); } return(t); }
public TemplateNode LoadTemplates(string folderFullPath, bool recursive = true, bool checkParams = true) { TemplateNode root = new TemplateNode(); root.Name = Path.GetFileNameWithoutExtension(folderFullPath); // "Templates"; root.Path = string.Empty; root.IsFolder = true; LoadTemplates(folderFullPath, root, folderFullPath, recursive, checkParams); return(root); }
private void FindTemplate(TemplateNode node, string templateKey) { if (foundNode != null) { return; } if (node.Key.Equals(templateKey)) { foundNode = node; } else { foreach (TemplateNode childNode in node.Nodes) { FindTemplate(childNode, templateKey); } } }
private TemplateOutput GenCode(ICodeGenerator <CodeGenArgs, string> codeGen, TemplateNode node, string outputFileName, string downloadPath) { string inputFileName = Path.Combine(templatePath, node.Path); TemplateOutput output = new TemplateOutput(); FileInfo fiOut = new FileInfo(outputFileName); output.DownloadName = fiOut.Name; output.Extension = fiOut.Extension; CodeGenArgs args = new CodeGenArgs(); args.InputFileName = inputFileName; args.OutputFileName = outputFileName; foreach (TemplateParameter param in node.Parameters) { args.Parameters.Add(param); } if (codeGen.Process(args)) { output.FullName = string.Empty; // outputFileName; output.Succeeded = true; FileInfo fi = new FileInfo(outputFileName); output.DownloadName = fi.Name; output.DownloadPath = downloadPath; output.Content = codeGen.Output; } else { output.Succeeded = false; output.FullName = outputFileName; string errorMessages = string.Empty; foreach (string error in codeGen.Errors) { errorMessages += error + "\r\n"; } output.ErrorMessages = errorMessages; } return(output); }
private void LoadTemplates(string folderFullPath, TemplateNode folder, string rootPath, bool recursive = true, bool checkParams = true) { DirectoryInfo parentDir = new DirectoryInfo(folderFullPath); if ((from f in ExcludeFolders where f.ToLower() == parentDir.Name.ToLower() select f).Any()) { return; } foreach (DirectoryInfo di in parentDir.GetDirectories()) { if ((from f in ExcludeFolders where f.ToLower() == di.Name.ToLower() select f).Any()) { continue; } TemplateNode fn = new TemplateNode(); fn.Name = di.Name; fn.Path = di.FullName.Replace(rootPath, string.Empty); if (fn.Path.StartsWith("\\")) { fn.Path = fn.Path.Substring(1); } fn.IsFolder = true; folder.Nodes.Add(fn); if (recursive) { LoadTemplates(di.FullName, fn, rootPath, recursive, checkParams); } } foreach (FileInfo fi in parentDir.GetFiles(searchPattern)) { TemplateNode tn = new TemplateNode(); tn.Name = fi.Name; tn.Path = fi.FullName.Replace(rootPath, string.Empty); if (tn.Path.StartsWith("\\")) { tn.Path = tn.Path.Substring(1); } tn.IsFolder = false; if (checkParams) { string content = File.ReadAllText(fi.FullName); Match m; string paramPattern = @"\<\#\s*//Parameters\s*((?<paramType>(string|bool))\s+(?<paramName>\w+);\s*)*\#\>"; m = Regex.Match(content, paramPattern, RegexOptions.Multiline | RegexOptions.IgnoreCase); List <TemplateParameter> dictItemList = new List <TemplateParameter>(); int i = 0; foreach (Capture c in m.Groups["paramName"].Captures) { string typeName = m.Groups["paramType"].Captures[i].Value; TemplateParameter item = new TemplateParameter(c.Value, typeName, ""); dictItemList.Add(item); i++; } tn.Parameters = dictItemList.ToArray(); string pattern = "\\<\\#\\@\\s*output\\s+extension=\\\"(?<ext>[^\\\"]+)\\\"\\s+fileName=\\\"(?<fn>[^\\\"]+)\\\"\\s*\\#\\>"; m = Regex.Match(content, pattern); if (m.Groups["ext"].Success) { tn.Extension = m.Groups["ext"].Value; } if (m.Groups["fn"].Success) { tn.FileName = m.Groups["fn"].Value; } } folder.Nodes.Add(tn); } }
public TemplateNode Find(TemplateNode node, string templateKey) { foundNode = null; FindTemplate(node, templateKey); return(foundNode); }