private static void createPartialSettingsFile(Document document) { string partialFileName = document.FullName + ".partial"; string partialShortFileName = document.Name + ".partial"; // Check if partial already exists if (File.Exists(partialFileName)) { outputPartial(string.Format("File {0} already exists.", partialShortFileName)); return; } List<string> lines = File.ReadAllLines(document.FullName).Skip(1).ToList(); outputPartial(string.Format("Creating {0}.", partialShortFileName)); output("\t\tWrite the partial names, each in its own line. Then save the file."); string partialFileContents = ""; if (lines.Count > 0) { partialFileContents = "original"; string originalFileContents = string.Join(Environment.NewLine, lines); FileName originalFileName = new FileName(document); string partialOriginalPath = originalFileName.Combine("original"); createAndInsertIntoProjectItem(partialOriginalPath, originalFileContents, document); } createAndInsertIntoProjectItem(partialFileName, partialFileContents, document, true); // Close the main file. document.Close(vsSaveChanges.vsSaveChangesNo); // Rewrite the file to remove the partialize! line File.WriteAllLines(document.FullName, lines); }
private static void parsePartialSettingsFile(Document document) { try { List<string> parts = File.ReadAllLines(document.FullName) .Select(p => p.Trim()) .Where(p => p.Length > 0).ToList(); FileName originalFileName = new FileName(document.FullName.Replace(".partial", "")); ProjectItem originalProjectItem = VS.Solution.FindProjectItem(originalFileName.FullPath); foreach (string part in parts) { string partPath = originalFileName.Combine(part); if (File.Exists(partPath)) { ProjectItem existingItem = VS.Solution.FindProjectItem(partPath); if (existingItem == null) { originalProjectItem.ProjectItems.AddFromFile(partPath); } } else { File.WriteAllText(partPath, ""); originalProjectItem.ProjectItems.AddFromFile(partPath); } } originalProjectItem.ExpandView(); originalProjectItem.ContainingProject.Save(); } catch { outputPartial("ERROR: couldn't find the parent item."); } }
private static void updateOriginalFile(FileName sourceFileName, string partialFileName) { List<string> parts = File.ReadAllLines(partialFileName) .Select(p => p.Trim()) .Where(p => p.Length > 0).ToList(); string originalFileNamePath = sourceFileName.OriginalFileName; FileName originalFileName = new FileName(originalFileNamePath); outputPartial(string.Format("Creating {0} from:", originalFileName.FullPath)); using (StreamWriter dest = new StreamWriter(originalFileName.FullPath, false, Encoding.UTF8)) { foreach (string partName in parts) { string partFileName = originalFileName.Combine(partName); output(string.Format("\t\t - {0}", partFileName)); using (StreamReader source = new StreamReader(partFileName, Encoding.Default)) dest.WriteLine(source.ReadToEnd()); } } }
private static void onDocumentSaved(Document document) { try { FileName sourceFileName = new FileName(document); if (isDestination(document)) { MessageBox.Show(savingDestinationMessage, "Ooops!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (sourceFileName.Extension == "partial") { parsePartialSettingsFile(document); } else if (shouldCreateSettingsFile(document)) { createPartialSettingsFile(document); } else { string partialFileName = getPartialParent(document); if (partialFileName != null) { updateOriginalFile(sourceFileName, partialFileName); } } } catch (Exception ex) { outputPartial("Error: " + ex); } }