JD2022-TU1/main/tools/legacy/ProjectScanner/ProjectScannerP4V/FrmChangePath.cs

205 lines
7.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Reflection;
using System.IO;
using System.Diagnostics;
namespace ProjectScannerP4V
{
public partial class FrmChangePath : Form
{
public string[] Args { get; set; }
public string Port { get; set; }
public string User { get; set; }
public string Client { get; set; }
public List<string> Files { get; set; }
public string DefaultDataPath { get; set; }
P4Tools p4t = new P4Tools();
bool bSubmitAfterProcess = false;
public FrmChangePath()
{
InitializeComponent();
this.MinimumSize = new Size(0, this.Height);
}
private void FrmChangePath_Load(object sender, EventArgs e)
{
Client = Args[1];
Port = Args[2];
//Root = Args[2];
User = Args[3];
cmbChangelist.SelectedIndex = 0;
Files = new List<string>();
for (int i = 4; i < Args.Length; i++)
Files.Add(Args[i]);
p4t.User = User;
p4t.Client = Client;
p4t.Port = Port;
//p4t.Root = Root;
string[] cl = p4t.GetPendingChangelists();
if (cl != null)
{
cmbChangelist.Items.AddRange(cl);
}
txtOldPath.Text = Files[0].Replace(DefaultDataPath, "");
}
private void chkPartialPath_CheckedChanged(object sender, EventArgs e)
{
}
bool ChangePathsInTechno(string sourcePath, string destPath, bool Submit, string CL,bool FolderMode)
{
if (CL.Equals("New"))
{
CL = p4t.CreateCl(string.Format("Changed references of \"{0}\" to \"{1}\"", sourcePath, destPath));
}
else if (CL.Equals("Default"))
{
CL = null;
}
else
{
CL = CL.Substring(0, CL.IndexOf(' '));
}
string ExeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string ErrorFile = Path.Combine(ExeDir, "Errors.txt");
string TempFile = Path.Combine(Environment.GetEnvironmentVariable("Temp"), Path.GetRandomFileName());
string LocalPath = p4t.GetLocalPath(DefaultDataPath + "...");
LocalPath = LocalPath.Substring(0, LocalPath.Length - 4);
XmlDocument xml = new XmlDocument();
XmlDeclaration xmldec = xml.CreateXmlDeclaration("1.0", "UTF-8", "no");
xml.AppendChild(xmldec);
XmlNode RootNode = xml.AppendChild(xml.CreateElement("root"));
XmlNode ErrorNode = xml.CreateElement("Errors");
ErrorNode.Attributes.Append(xml.CreateAttribute("os"));
ErrorNode.Attributes["os"].Value = "DOS";
ErrorNode.Attributes.Append(xml.CreateAttribute("prompt"));
ErrorNode.Attributes["prompt"].Value = "Yes";
ErrorNode.Attributes.Append(xml.CreateAttribute("Wait_For_Key"));
ErrorNode.Attributes["Wait_For_Key"].Value = "no";
ErrorNode.Attributes.Append(xml.CreateAttribute("Dump_File"));
ErrorNode.Attributes["Dump_File"].Value = "Errors.txt";
RootNode.AppendChild(ErrorNode);
XmlNode FilesNode = xml.CreateElement("Files");
FilesNode.Attributes.Append(xml.CreateAttribute("Data_Path"));
FilesNode.Attributes["Data_Path"].Value = LocalPath;
if (FolderMode)
{
FilesNode.Attributes.Append(xml.CreateAttribute("Folder_Mode"));
FilesNode.Attributes["Folder_Mode"].Value = "1";
}
RootNode.AppendChild(FilesNode);
XmlNode ActionsNode = xml.CreateElement("Action");
ActionsNode.Attributes.Append(xml.CreateAttribute("oldPath"));
ActionsNode.Attributes["oldPath"].Value = sourcePath.Replace(DefaultDataPath, "");
ActionsNode.Attributes.Append(xml.CreateAttribute("newPath"));
ActionsNode.Attributes["newPath"].Value = destPath.Replace(DefaultDataPath, "");
RootNode.AppendChild(ActionsNode);
XmlNode P4Node = xml.CreateElement("Perforce");
P4Node.Attributes.Append(xml.CreateAttribute("User"));
P4Node.Attributes["User"].Value = User;
P4Node.Attributes.Append(xml.CreateAttribute("Port"));
P4Node.Attributes["Port"].Value = Port;
P4Node.Attributes.Append(xml.CreateAttribute("Client"));
P4Node.Attributes["Client"].Value = Client;
P4Node.Attributes.Append(xml.CreateAttribute("Cl"));
P4Node.Attributes["Cl"].Value = CL;
RootNode.AppendChild(P4Node);
xml.Save(TempFile);
if (File.Exists(ErrorFile))
File.Delete(ErrorFile);
Process p = new Process();
p.StartInfo.FileName = Path.Combine(
ExeDir,
"ProjectScanner.exe");
p.StartInfo.Arguments = TempFile;
p.Start();
p.WaitForExit();
File.Delete(TempFile);
bool bSucess = true;
if (File.Exists(ErrorFile))
{
bSucess = false;
DialogResult dr = MessageBox.Show(this,
"Errors were detected while running ProjectScanner\nDo you want to open the error log ?",
"Errors detected",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (dr == DialogResult.Yes)
{
Process.Start(ErrorFile);
}
}
bSubmitAfterProcess = false;
return bSucess;
}
private void btnSaveCL_Click(object sender, EventArgs e)
{
if (!ChangePathsInTechno( txtOldPath.Text,
txtNewPath.Text,
bSubmitAfterProcess,
cmbChangelist.Items[cmbChangelist.SelectedIndex].ToString(),
chkPartialPath.Checked))
{
MessageBox.Show(this,
"Errors were detected while running project scanner, you may need to revert some files in Perforce",
"Project scanner errors",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
this.Close();
}
void EnableSubmitSaveButtons()
{
if (string.IsNullOrEmpty(txtNewPath.Text) && string.IsNullOrEmpty(txtOldPath.Text))
{
btnSaveCL.Enabled = false;
btnSubmitCL.Enabled = false;
}
else
{
btnSaveCL.Enabled = true;
btnSubmitCL.Enabled = true;
}
}
private void txtNewPath_TextChanged(object sender, EventArgs e)
{
EnableSubmitSaveButtons();
}
private void txtOldPath_TextChanged(object sender, EventArgs e)
{
EnableSubmitSaveButtons();
}
private void btnSubmitCL_Click(object sender, EventArgs e)
{
bSubmitAfterProcess = true;
btnSaveCL_Click(sender, e);
}
private void button3_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}
}