using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.SourceSafe.Interop;
using System.Configuration;
using System.IO;
using System.Collections;
using Infor.SyteLine.FormRename.Configuration;
using System.Data;
using System.Diagnostics;
namespace Infor.SyteLine.FormRename.BusinessService
{
/// <summary>
/// VssLatestVersionLoader to load the latest version from VSS.
/// </summary>
internal class VisualSourceSafeService : IBusinessService
{
private static VssConfigurationSection section = ConfigurationUtil.GetCustomerSection(VssConfigurationSection.Name) as VssConfigurationSection;
public static string AppRootDir { get { return section.LocalFolder + "\\SLSI9.00App"; } }
public static string ObjRootDir { get { return section.LocalFolder + "\\SLSI9.00Obj"; } }
public static string RootDir { get { return section.LocalFolder; } }
public string ExecutablePath { get; set; }
private IDictionary vssPathSettings;
private IVSSDatabase vssAppDatabase;
private IVSSDatabase vssObjDatabase;
private string originalHold;
private string newHold;
public VisualSourceSafeService()
{
vssPathSettings = ConfigurationUtil.GetSection("vssPathSettings");
}
public void Open()
{
vssAppDatabase = new VSSDatabase();
vssAppDatabase.Open(section.AppPath, section.User, section.Password);
vssObjDatabase = new VSSDatabase();
vssObjDatabase.Open(section.ObjPath, section.User, section.Password);
}
private VSSItem GetVssItem(string spec)
{
VSSItem item = null;
string[] parts = spec.Split('|');
try
{
if (parts.Length < 2)
{
item = vssAppDatabase.get_VSSItem(parts[0], false);
}
else if (parts[0].Equals("App"))
{
item = vssAppDatabase.get_VSSItem(parts[1], false); // item = vssAppDatabase.get_VSSItem("App|$/ApplicationDB/Stored Procedures/2CurrCnvtSp.sp", false);
}
else
{
item = vssObjDatabase.get_VSSItem(parts[1], false);
}
}
catch (Exception)
{
if (parts.Length < 2)
{
item = vssObjDatabase.get_VSSItem(parts[0], false);
}
else if (parts[0].Equals("App"))
{
item = vssObjDatabase.get_VSSItem(parts[1], false);
}
else
{
item = vssAppDatabase.get_VSSItem(parts[1], false);
}
}
return item;
}
public void RenameItem(string newText, string vssPath)
{
VSSItem item = GetVssItem(vssPath);
//item.Type=0:项目文件夹 item.Type=1:项目文件
if (item.Type == 1)
{
if (item.IsCheckedOut == 0)
{
originalHold = item.Name;
newHold = newText;
item.Name = newText;
}
else
{
throw new Exception("The file has been locked. So rename failed.");
}
}
else
{
throw new Exception("Do you really want to rename the file directory?");
}
}
private string GetItemSpec(DataRow row)
{
string type = row["Type"] as string;
string path = vssPathSettings[type] as string;
string spec = null;
if ("SQL_STORED_PROCEDURE".Equals(type))
{
string name = row["Name"] + ".sp";
if (!string.IsNullOrEmpty(originalHold) && name.Equals(originalHold))
{
spec = path + newHold;
}
else {
spec = path + row["Name"] + ".sp";
}
}
return spec;
}
public int Commit(DataRow row, string comment)
{
string spec = GetItemSpec(row);
VSSItem item = GetVssItem(spec);
// item.IsCheckedOut=0:未签出 item.IsCheckedOut=1:被别人签出 item.IsCheckedOut=2:被自己签出
int status = item.IsCheckedOut;
if (item.IsCheckedOut == 2)
{
string local = item.Spec;
string localSpec = local.Replace("$", RootDir);
item.Checkin(comment, localSpec, 0);
}
return status;
}
public int Unlock(DataRow row)
{
string spec = GetItemSpec(row);
VSSItem item = GetVssItem(spec);
// item.IsCheckedOut=0:未签出 item.IsCheckedOut=1:被别人签出 item.IsCheckedOut=2:被自己签出
int status = item.IsCheckedOut;
if (item.IsCheckedOut == 2)
{
string local = item.Spec;
string localSpec = local.Replace("$", RootDir);
item.UndoCheckout(localSpec, 0);
}
return status;
}
public int Process(DataRow row, string originalText, string newText, string comment)
{
string spec = GetItemSpec(row);
VSSItem item = GetVssItem(spec);
// item.IsCheckedOut=0:未签出 item.IsCheckedOut=1:被别人签出 item.IsCheckedOut=2:被自己签出
int status = item.IsCheckedOut;
if (item.IsCheckedOut == 0)
{
string local = item.Spec;
string localSpec = local.Replace("$", RootDir);
int index = localSpec.LastIndexOf("/");
local = localSpec.Remove(index, localSpec.Length - index);
Directory.CreateDirectory(local);
item.Checkout(comment, localSpec, 0);
Replace(localSpec, originalText, newText);
}
return status;
}
private void Replace(string localSpec, string originalText, string newText)
{
string path = ExecutablePath + "\\Resource\\Replace.bat";
if (File.Exists(path))
{
string batch = string.Format("\"{0}\" \"{1}\" \"{2}\"", originalText, newText, localSpec.Replace(@"/", @"\"));
var process = new Process();
process.StartInfo.FileName = path;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = batch;
process.Start();
process.WaitForExit();
if (!process.HasExited)
{
process.Kill();
}
}
else {
throw new Exception("Did you remove my local batch file Replace.bat?");
}
}
public void Close()
{
if (vssAppDatabase != null)
{
vssAppDatabase.Close();
}
if (vssObjDatabase != null)
{
vssObjDatabase.Close();
}
}
}
private static void Load(string vssIni, string rootDir)
{
string rootProject = section.VssProject;
string user = section.User;
string pwd = section.Password;
try
{
vssDatabase.Open(@vssIni, user, pwd);
GetFiles(rootProject, rootDir);
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("BackupApplication", "Error occured while opening database or connecting to the project"
+ ex.Message.ToString(), System.Diagnostics.EventLogEntryType.Error);
Console.WriteLine("Failed!");
}
finally
{
vssDatabase.Close();
}
}
private static void GetFiles(string projectPath, string copyDirectory)
{
// This is the variable for holding the reference to the VSS Item (or folders).
VSSItem item = vssDatabase.get_VSSItem(projectPath, false);
LoadItem(copyDirectory, item);
foreach (VSSItem vssItem in item.get_Items(false))
{
Console.WriteLine(vssItem.Spec.ToString());
if (vssItem.Type == (int)VSSItemType.VSSITEM_PROJECT)
{
string copyDirLocal = copyDirectory + "\\" + vssItem.Name.ToString();
//vssItem.Get(ref copyDirLocal, 0);
LoadItem(copyDirLocal, vssItem);
string childProjectPath = vssItem.Spec + "/";
GetFiles(childProjectPath, copyDirLocal);
}
}
}
private static void LoadItem(string copyDirectory, VSSItem item)
{
item.Get(ref copyDirectory, 0);
}
}
No comments:
Post a Comment