// Returns one (1) on program failure or no elements found to move, zero (0) on success. static int Main(string[] args) { bool ret = false; // assume failure if (args.Length != 1) { Console.WriteLine(@"Example usage: C:\Workspaces\MARS_DEV2\Foo>moveto ..\Bar"); return 1; } if (!init()) return 1; // program startup initialization string destfolder = args[0]; string tempfile; if (store(destfolder, out tempfile)) // get elements to be moved { if (ready(destfolder)) // ensure folder exists and is in AccuRev { try { AcResult r = AcCommand.run($@"move -l ""{tempfile}"" ""{destfolder}"""); ret = (r.RetVal == 0); } catch (AcUtilsException ecx) { Console.WriteLine($"AcUtilsException caught in Program.Main{Environment.NewLine}{ecx.Message}"); } } } if (tempfile != null) File.Delete(tempfile); return ret ? 0 : 1; }
// Store the list of elements for the move operation in a temp file. // Returns true if the operation succeeded, otherwise false on error or if no elements found. private static bool store(string destfolder, out string tempfile) { tempfile = null; bool ret = false; // assume failure try { AcResult r = AcCommand.run("stat -fax *"); // the current directory if (r.RetVal == 0) // if command succeeded { string fullpath = Path.GetFullPath(destfolder); // in case the relative path was given XElement xml = XElement.Parse(r.CmdResult); IEnumerable<XElement> filter = from e in xml.Elements("element") where !_skipOver.Any(s => e.Attribute("status").Value.Contains(s)) && // avoid: "You cannot move an element into itself." !fullpath.Equals((string)e.Attribute("location"), StringComparison.OrdinalIgnoreCase) select e; tempfile = Path.GetTempFileName(); using (StreamWriter sw = new StreamWriter(tempfile)) { foreach (XElement e in filter) sw.WriteLine((string)e.Attribute("location")); } FileInfo fi = new FileInfo(tempfile); ret = fi.Length > 0; } } catch (AcUtilsException ecx) { Console.WriteLine($"AcUtilsException caught in Program.store{Environment.NewLine}{ecx.Message}"); } catch (Exception ecx) { Console.WriteLine($"Exception caught in Program.store{Environment.NewLine}{ecx.Message}"); } return ret; }
// Determines whether the user's default directory is located somewhere in the current workspace tree. // Returns true if the operation succeeded, false otherwise. private static bool isCurrDirInWSpace() { bool found = false; // assume no workspace found try { AcResult r = AcCommand.run("info"); if (r.RetVal == 0) { using (StringReader sr = new StringReader(r.CmdResult)) { string line; char[] sep = new char[] { ':' }; while ((line = sr.ReadLine()) != null) { string[] arr = line.Split(sep); // "Workspace/ref: MARS_DEV2_barnyrd" if (arr.Length == 2) { if (String.Equals(arr[0], "Workspace/ref")) { found = true; break; } } } } } } catch (AcUtilsException ecx) { Console.WriteLine($"AcUtilsException caught in Program.isCurrDirInWSpace{Environment.NewLine}{ecx.Message}"); } catch (Exception ecx) { Console.WriteLine($"Exception caught in Program.isCurrDirInWSpace{Environment.NewLine}{ecx.Message}"); } return found; }
// Ensure that the destination folder exists and is in AccuRev. // Returns true if the operation succeeded, false on error. private static bool ready(string dest) { bool ret = false; // assume failure try { if (!Directory.Exists(dest)) { Directory.CreateDirectory(dest); AcCommand.run($@"add ""{dest}"""); } else { AcResult r = AcCommand.run($@"stat -fx ""{dest}"""); if (r.RetVal == 0) { XElement xml = XElement.Parse(r.CmdResult); string status = (string)xml.Element("element").Attribute("status"); if (status == "(external)") AcCommand.run($@"add ""{dest}"""); } } ret = true; } catch (AcUtilsException ecx) { Console.WriteLine($"AcUtilsException caught in Program.ready{Environment.NewLine}{ecx.Message}"); } catch (Exception ecx) { Console.WriteLine($"Exception caught in Program.ready{Environment.NewLine}{ecx.Message}"); } return ret; }