public void DoCopyWorks(string templateDataRoot, string targetProjectRoot, FilterProcessor filter) { foreach (Tuple <PathData, PathData> currentEntry in copyAllEntries) { _CopyAllIn(currentEntry.Item1.CombineWith(templateDataRoot), currentEntry.Item2.CombineWith(targetProjectRoot)); } foreach (Tuple <PathData, PathData> currentEntry in copyEntries) { string sourceFilePath = currentEntry.Item1.CombineWith(templateDataRoot); if (!File.Exists(sourceFilePath)) { Console.Error.WriteLine(" [W] Template project data file not exists: {0}", sourceFilePath); continue; } string targetFilePath = currentEntry.Item2.CombineWith(targetProjectRoot); try { string parentDir = Directory.GetParent(targetFilePath).FullName; Directory.CreateDirectory(parentDir); File.Copy(sourceFilePath, targetFilePath); } catch { Console.Error.WriteLine(" [W] An error occured during copy {0} to {1}", sourceFilePath, targetFilePath); } } var utf8WithoutBOM = new UTF8Encoding(false); foreach (Tuple <PathData, PathData> currentEntry in filterEntries) { string sourceFilePath = currentEntry.Item1.CombineWith(templateDataRoot); if (!File.Exists(sourceFilePath)) { Console.Error.WriteLine(" [W] Template project data file not exists: {0}", sourceFilePath); continue; } string fileContents = null; try { fileContents = File.ReadAllText(sourceFilePath, Encoding.UTF8); } catch { Console.Error.WriteLine(" [W] An error occured during reading file data in {0}", sourceFilePath); continue; } string filteredContents = filter.Filter(fileContents); string targetFilePath = currentEntry.Item2.CombineWith(targetProjectRoot); string fileExtension = Path.GetExtension(targetFilePath); bool isNeedBOM = false; switch (fileExtension.ToLower()) { case ".cpp": case ".h": isNeedBOM = true; break; } try { string parentDir = Directory.GetParent(targetFilePath).FullName; Directory.CreateDirectory(parentDir); if (isNeedBOM) { File.WriteAllText(targetFilePath, filteredContents, Encoding.UTF8); } else { File.WriteAllText(targetFilePath, filteredContents, utf8WithoutBOM); } } catch { Console.Error.WriteLine(" [W] An error occured during writing to project file in {0}", targetFilePath); } } }
public static TemplateList ReadFile(string templateListFilePath, FilterProcessor contentsFilter) { if (string.IsNullOrWhiteSpace(templateListFilePath)) { return(null); } TemplateList listData = new TemplateList(); try { using (FileStream fs = new FileStream(templateListFilePath, FileMode.Open, FileAccess.Read)) using (TextReader rd = new StreamReader(fs)) { WorkSet currentTargetSet = listData.mainWorkSet; string lineBuffer = rd.ReadLine(); while (lineBuffer != null) { if (string.IsNullOrWhiteSpace(lineBuffer) || lineBuffer.StartsWith('#')) { lineBuffer = rd.ReadLine(); continue; } lineBuffer = contentsFilter.Filter(lineBuffer).Trim(); string[] lineTokens = lineBuffer.Split(':', StringSplitOptions.TrimEntries); if (lineTokens.Length > 0) { if (lineTokens[0].StartsWith('[')) { // Section Descriptor string sectionName = _PopSectionName(lineTokens[0]); if (sectionName != null) { sectionName = sectionName.Trim(); if (sectionName == string.Empty) { currentTargetSet = listData.mainWorkSet; } else { if (!listData.conditionalWorkSets.ContainsKey(sectionName)) { listData.conditionalWorkSets.Add(sectionName, new WorkSet()); } currentTargetSet = listData.conditionalWorkSets[sectionName]; } } } else { switch (lineTokens[0]) { case "C": // Copy { if (lineTokens.Length > 1) { PathData srcPathData = PathData.ParsePathString(lineTokens[1]); PathData targetPathData = null; if (lineTokens.Length > 2) { targetPathData = PathData.ParsePathString(lineTokens[2]); } else { targetPathData = srcPathData; } if (lineTokens[1].EndsWith("/*")) { currentTargetSet.copyAllEntries.Add(new Tuple <PathData, PathData>(srcPathData, targetPathData)); } else { currentTargetSet.copyEntries.Add(new Tuple <PathData, PathData>(srcPathData, targetPathData)); } } } break; case "F": // Filter { if (lineTokens.Length > 1) { PathData srcPathData = PathData.ParsePathString(lineTokens[1]); PathData targetPathData = null; if (lineTokens.Length > 2) { targetPathData = PathData.ParsePathString(lineTokens[2]); } else { targetPathData = srcPathData; } currentTargetSet.filterEntries.Add(new Tuple <PathData, PathData>(srcPathData, targetPathData)); } } break; } } } lineBuffer = rd.ReadLine(); } } } catch { return(null); } return(listData); }