|
using System;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;
namespace RemoveSCCInfo
{
/// <summary>
/// Command to remove SCC info from a project file
/// </summary>
internal class RemoveSccFrom2003ProjectCommand : RemoveSccBaseCommand
{
public RemoveSccFrom2003ProjectCommand(string projectFileName, bool createBackup)
: base (projectFileName,createBackup)
{
}
public override void PatchFile()
{
switch(Path.GetExtension(_fileName.ToLower()))
{
case ".vcproj":
PatchXmlProjectFile("/VisualStudioProject");
break;
case ".csproj":
PatchXmlProjectFile("/VisualStudioProject/CSHARP");
break;
case ".vbproj":
PatchXmlProjectFile("/VisualStudioProject/VisualBasic");
break;
case ".vdproj":
PatchDeployProject();
break;
}
}
private void PatchDeployProject()
{
string tempFileName = Path.GetDirectoryName(_fileName) + @"\old-scc-remove-"+ Path.GetFileName(_fileName);
try
{
if(_createBackup)
File.Copy(_fileName,_backupDir+ Path.GetFileName(_fileName));
// move the .vdproj file to a temp file
File.Move(_fileName,tempFileName);
RemoveReadOnlyOnFile(tempFileName);
StreamReader rdr = new StreamReader(tempFileName, System.Text.Encoding.Default);
String allFile = rdr.ReadToEnd();
rdr.Close();
Regex r = new Regex("\\\"Scc\\w*\\\"\\s*=\\s*\\\".*\\\"");
StreamWriter wri = new StreamWriter(_fileName,false,System.Text.Encoding.Default);
wri.Write(r.Replace(allFile,""));
wri.Close();
}
finally
{
if(File.Exists(tempFileName))
File.Delete(tempFileName);
}
}
private void PatchXmlProjectFile(string elementPath)
{
// First attempt with a XMLDoc was a failure due to
// the possibility to add non valid xml-chars in the project file
// XmlDocument doc = new XmlDocument();
// XmlElement proj = null;
//
//
// if(_createBackup)
// File.Copy(_fileName,_backupDir+ Path.GetFileName(_fileName));
// doc.PreserveWhitespace=true;
//
// doc.Load(_fileName);
//
// if(doc.DocumentElement==null)
// throw new FileNotFoundException("The project file is unknown or not a valid VS2003 project",_fileName);
//
//
// proj = doc.SelectSingleNode(elementPath) as XmlElement;
//
// if(proj==null)
// return;
//
// proj.RemoveAttribute("SccProjectName");
// proj.RemoveAttribute("SccLocalPath");
// proj.RemoveAttribute("SccProvider");
//
//
// RemoveReadOnlyOnFile(_fileName);
// doc.Save(_fileName);
// So I translated everything in a RegExp, for similarity with the PatchDeployment
string tempFileName = Path.GetDirectoryName(_fileName) + @"\old-scc-remove-"+ Path.GetFileName(_fileName);
try
{
if(_createBackup)
File.Copy(_fileName,_backupDir+ Path.GetFileName(_fileName));
// move the .??proj file to a temp file
File.Move(_fileName,tempFileName);
RemoveReadOnlyOnFile(tempFileName);
StreamReader rdr = new StreamReader(tempFileName, System.Text.Encoding.Default);
String allFile = rdr.ReadToEnd();
rdr.Close();
Regex r = new Regex("Scc\\w*\\s*=\\s*\\\".*\\\"");
StreamWriter wri = new StreamWriter(_fileName,false,System.Text.Encoding.Default);
wri.Write(r.Replace(allFile,""));
wri.Close();
}
finally
{
if(File.Exists(tempFileName))
File.Delete(tempFileName);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.