private void RevertBackProjectFileToNuGet(ProjectInfo project, OperationContext context) { string newText = ""; string textWithoutHashKey = ""; string origHash = null; // Read the hash code using (StreamReader reader = new StreamReader(project.ProjectFile)) { // Remove all the project references and add the original NuGet references while (!reader.EndOfStream) { string line = reader.ReadLine(); if (line.IndexOf("<!--DebugMode") != -1) { // read the hash code int hashStartPos = line.IndexOf(" ") + 1; int hashEndPos = line.IndexOf("-->"); origHash = line.Substring(hashStartPos, hashEndPos - hashStartPos); } else { textWithoutHashKey += line + Environment.NewLine; } } } using (StreamReader reader = new StreamReader(project.ProjectFile)) { // Remove all the project references and add the original NuGet references while (!reader.EndOfStream) { string line = reader.ReadLine(); // Skip the debug mode comment line if (line.IndexOf("<!--DebugMode") != -1) { continue; } if (line.IndexOf("<!--NuGetTool") != -1) { int idx = line.IndexOf("<!--NuGetTool"); string uncommentedLine = line.Substring(idx + 13); newText += uncommentedLine + Environment.NewLine; // Read until the end of the comment line = reader.ReadLine(); while (line.IndexOf("</Reference>") == -1) { newText += line + Environment.NewLine; line = reader.ReadLine(); } // Remove the end of the comment int endOfCommentPos = line.IndexOf("-->"); uncommentedLine = line.Substring(0, endOfCommentPos); newText += uncommentedLine + Environment.NewLine; // Remove the project reference line = reader.ReadLine(); while (line.IndexOf("</ProjectReference>") == -1) { line = reader.ReadLine(); } } else { newText += line + Environment.NewLine; } } } // If the file was checked out first time by the tool, and has not changed // outside the tool, then undo the check out when switching back to NuGet mode if (!String.IsNullOrEmpty(origHash)) { File.WriteAllText(project.ProjectFile, textWithoutHashKey); string newHash = GeneralUtils.ComputeHash(project.ProjectFile); File.WriteAllText(project.ProjectFile, newText); if (origHash == newHash) { TFSUtilities.UndoCheckOut(project.ProjectFile, context.TfsServerUri); } } else { File.WriteAllText(project.ProjectFile, newText); } }
private static void RevertBackPackageConfigToNuGet(ProjectInfo project, OperationContext context) { string packagesConfigFile = Path.Combine(project.Directory, "packages.config"); if (File.Exists(packagesConfigFile)) { string textWithoutHashKey = ""; string newText = ""; string origHash = null; using (StreamReader reader = new StreamReader(packagesConfigFile)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); if (line.IndexOf("<!--DebugMode") != -1) { // read the hash code int hashStartPos = line.IndexOf(" ") + 1; int hashEndPos = line.IndexOf("-->"); origHash = line.Substring(hashStartPos, hashEndPos - hashStartPos); continue; } else { textWithoutHashKey += line + Environment.NewLine; } if (line.IndexOf("<!--NuGetTool") != -1) { int packageStartPos = line.IndexOf("<package"); if (packageStartPos != -1) { int packageEndPos = line.IndexOf("/>", packageStartPos + 1); string uncommentedLine = line.Substring(packageStartPos, packageEndPos - packageStartPos + 2).Trim(); newText += " " + uncommentedLine + Environment.NewLine; } } else { newText += line + Environment.NewLine; } } } // If the file was checked out first time by the tool, and has not changed // outside the tool, then undo the check out when switching back to NuGet mode if (!String.IsNullOrEmpty(origHash)) { File.WriteAllText(packagesConfigFile, textWithoutHashKey); string newHash = GeneralUtils.ComputeHash(packagesConfigFile); File.WriteAllText(packagesConfigFile, newText); // If the files are identical, then undo the checkout if (origHash == newHash) { TFSUtilities.UndoCheckOut(packagesConfigFile, context.TfsServerUri); } } else { File.WriteAllText(packagesConfigFile, newText); } } else { System.Diagnostics.Debug.Write("No packages.config found in " + project.Directory); } }