Tuesday, June 23, 2009

VSS => TFS converter application update

After a bit of thought, I decided what would be the easiest and the most convinient way to run my small application that helps to convert projects from VSS to TFS.

I will start the command line tool passing them together with parameters to cmd.exe application, and run cmd.exe with the -k parameter to prevent the command window from closing after the tool exits. I will keep the ID of the process that runs cmd.exe. Next time I run the cmd.exe, I will check if there is ID present, and if yes, I will kill the process, and then start a new one. This way the user's computer will not be littered with command windows.

So, the small class that would take care of process management looks like this

public class ProcessFactory
{
private static int _currentProcessID = -1;

private static Process _cmdProcess;

private static ProcessStartInfo _startInfo;

public static ProcessStartInfo StartInfo
{
get
{
if (_startInfo == null)
{
_startInfo = new ProcessStartInfo();
}
return _startInfo;
}
}

public static void RunProcess(string filename, string args, string workingdir)
{
try
{
if (_currentProcessID > 0)
{
Process processToClose = Process.GetProcessById(_currentProcessID);
if (processToClose != null)
{
processToClose.Kill();
}
_currentProcessID = -1;
}

StartInfo.FileName = filename;
StartInfo.Arguments = args;
StartInfo.WorkingDirectory = workingdir;

_cmdProcess = Process.Start(StartInfo);

if (_cmdProcess != null)
{
_currentProcessID = _cmdProcess.Id;
}
}
catch (Exception ex)
{
Logger.LogInfo(ex);
}
}
}

And then I just call the RunProcess as many times as I want, but the user will not be bothered with "leftover" command windows

string args = "/k ssarc.exe -d- -i -y" + SettingsManager.GetSetting(Constants.VSSLOGIN)
+ "," +
SettingsManager.GetSetting(Constants.VSSPASSWORD) + " -s" +
SettingsManager.GetSetting(Constants.VSSDBFOLDER) + " " +
SettingsManager.GetSetting(Constants.VSSARCHIVEFILENAME)
+ ".ssa" + " \"" + SettingsManager.GetSetting(Constants.VSSPROJECTNAME) + "\"";

ProcessFactory.RunProcess("cmd.exe", args, SettingsManager.GetSetting
(Constants.VSSINSTFOLDER));

.....

args = "/k ssrestor.exe \"-p" + SettingsManager.GetSetting(Constants.VSSPROJECTNAME)
+ "\"" + " -s" + SettingsManager.GetSetting(Constants.VSSARCHIVEFOLDER) +
" -y" + SettingsManager.GetSetting(Constants.VSSLOGIN) + "," +
SettingsManager.GetSetting(Constants.VSSPASSWORD) + " " +
SettingsManager.GetSetting(Constants.VSSARCHIVEFILENAME) + ".ssa" +
" \"" + SettingsManager.GetSetting(Constants.VSSPROJECTNAME) + "\"";

ProcessFactory.RunProcess("cmd.exe", args, SettingsManager.GetSetting
(Constants.VSSINSTFOLDER));

etc., until finished.

by . Also posted on my website

No comments: