public bool ModifyProjectFile(FileEntry entry) { bool success = true; try { if (entry == null) { throw new ArgumentException("Internal Error: ModifyProjectFile called with a file that is not a project"); } string filename = entry.Info.FullName; // Load the Project file XDocument doc = null; Encoding encoding = new UTF8Encoding(false); using (StreamReader reader = new StreamReader(filename, encoding)) { doc = XDocument.Load(reader); encoding = reader.CurrentEncoding; } // Modify the Source Control Elements RemoveSCCElementsAttributes(doc.Root); // Remove the read-only flag var original_attr = File.GetAttributes(filename); File.SetAttributes(filename, FileAttributes.Normal); //if the original document doesn't include the encoding attribute //in the declaration then do not write it to the outpu file. if (doc.Declaration == null || String.IsNullOrEmpty(doc.Declaration.Encoding)) { encoding = null; } //else if its not utf (i.e. utf-8, utf-16, utf32) format which use a BOM //then use the encoding identified in the XML file. else if (!doc.Declaration.Encoding.StartsWith("utf", StringComparison.OrdinalIgnoreCase)) { encoding = Encoding.GetEncoding(doc.Declaration.Encoding); } // Write out the XML using (var writer = new System.Xml.XmlTextWriter(filename, encoding)) { writer.Formatting = System.Xml.Formatting.Indented; doc.Save(writer); writer.Close(); } // Restore the original file attributes File.SetAttributes(filename, original_attr); } catch { success = false; } if (UnbindProgress != null) { UnbindProgressEvents fec = new UnbindProgressEvents(success, entry); UnbindProgress(this, fec); } return(success); }
private void UnbindProgress(object sender, UnbindProgressEvents e) { FileEntry entry = e.File; string eventType = ParseType.GetStringType(entry.Type); if (e.Success) { ParseMessageType type = ParseType.GetType(entry.Type); _logs.Add(string.Format("Unbinding file: {0} was successfully completed.", entry.Name), "Events", eventType + " " + entry.Name, type); } else { _logs.Add(string.Format("Unsuccessfull unbinding file: {0} ", entry.Name), "Events", eventType + " " + entry.Name, ParseMessageType.Error); } }
/// <summary> /// Delete source control file /// </summary> /// <param name="entry"></param> public bool DeleteSourceFile(FileEntry entry) { bool success = true; try { if (entry == null) { throw new ArgumentException("Internal Error: DeleteSourceFile called with a file that is not a source control"); } string filename = entry.Info.FullName; File.SetAttributes(filename, FileAttributes.Normal); File.Delete(filename); } catch { success = false; } if (UnbindProgress != null) { UnbindProgressEvents fec = new UnbindProgressEvents(success, entry); UnbindProgress(this, fec); } return(success); }
public bool ModifySolutionFile(FileEntry entry) { bool success = true; try { if (entry == null) { throw new ArgumentException("Internal Error: ModifyProjectFile called with a file that is not a solution"); } string filename = entry.Info.FullName; // Remove the read-only flag var original_attr = File.GetAttributes(filename); File.SetAttributes(filename, FileAttributes.Normal); var output_lines = new List <string>(); bool in_sourcecontrol_section = false; Encoding encoding; var lines = ReadAllLines(filename, out encoding); foreach (string line in lines) { var line_trimmed = line.Trim(); // lines can contain separators which interferes with the regex // escape them to prevent regex from having problems line_trimmed = Uri.EscapeDataString(line_trimmed); if (line_trimmed.StartsWith("GlobalSection(SourceCodeControl)") || line_trimmed.StartsWith("GlobalSection(TeamFoundationVersionControl)") || System.Text.RegularExpressions.Regex.IsMatch(line_trimmed, @"GlobalSection\(.*Version.*Control", System.Text.RegularExpressions.RegexOptions.IgnoreCase)) { // this means we are starting a Source Control Section // do not copy the line to output in_sourcecontrol_section = true; } else if (in_sourcecontrol_section && line_trimmed.StartsWith("EndGlobalSection")) { // This means we were Source Control section and now see the ending marker // do not copy the line containing the ending marker in_sourcecontrol_section = false; } else if (line_trimmed.StartsWith("Scc")) { // These lines should be ignored completely no matter where they are seen } else { // No handle every other line // Basically as long as we are not in a source control section // then that line can be copied to output if (!in_sourcecontrol_section) { output_lines.Add(line); } } } // Write the file back out File.WriteAllLines(filename, output_lines, encoding); // Restore the original file attributes File.SetAttributes(filename, original_attr); } catch { success = false; } if (UnbindProgress != null) { UnbindProgressEvents fec = new UnbindProgressEvents(success, entry); UnbindProgress(this, fec); } return(success); }