CtrlClone.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Quadarax.Foundation.Core.Value.Extensions;
  2. using System.Text;
  3. namespace qdr.app.tools.qfu.gui.Subforms
  4. {
  5. public partial class CtrlClone : UserControl, ICommanControl
  6. {
  7. public string Description => "Clone Command\r\nClone a source file to one or more destination files.\r\n\r\nSyntax:\r\nqfu clone <source_file> <target_files> [options]\r\n\r\nArguments:\r\n- `source_file`: Path to the source file to clone\r\n- `target_files`: Pipe-separated list of target file paths (`file1.txt|file2.txt|file3.txt`)\r\n\r\nOptions:\r\n- `-pe` or `-path_ensure`: Create destination directories if they don't exist\r\n- `-force`: Overwrite existing destination files\r\n- `-list`: Treat the first target file as a text file containing a list of destination paths (one per line)\r\n- `-n <count>`: Generate N numbered copies instead of using target files list";
  8. public string ViewName => "Clone";
  9. public CtrlClone()
  10. {
  11. InitializeComponent();
  12. chbFileList_CheckedChanged(this, new EventArgs());
  13. chbRepeat_CheckedChanged(this, new EventArgs());
  14. }
  15. private void chbRepeat_CheckedChanged(object sender, EventArgs e)
  16. {
  17. tbNRepeat.Enabled = chbRepeat.Checked;
  18. tbOutputs.Enabled = !chbRepeat.Checked;
  19. chbFileList.Enabled = !chbRepeat.Checked;
  20. }
  21. public void Execute()
  22. {
  23. var arguments = new StringBuilder();
  24. arguments.Append("clone");
  25. arguments.Append(" ");
  26. arguments.Append("\"").Append(ctrlSource.SelectedFile).Append("\"");
  27. if (chbRepeat.Enabled && chbRepeat.Checked)
  28. {
  29. if (int.TryParse(tbNRepeat.Text, out int n) && n > 0)
  30. {
  31. arguments.Append(" -n:").Append(n);
  32. }
  33. else
  34. throw new ArgumentException($"Invalid number of repeats specified '{tbNRepeat.Text}'. Please enter a valid positive integer.");
  35. }
  36. else if (chbFileList.Checked)
  37. {
  38. arguments.Append(" \"").Append(ctrlFileList.SelectedFile).Append("\"");
  39. arguments.Append(" -list");
  40. }
  41. else
  42. {
  43. arguments.Append(" ");
  44. var outputs = tbOutputs.Text.Split("\n", StringSplitOptions.RemoveEmptyEntries);
  45. foreach (var output in outputs)
  46. arguments.Append(output.Trim()).Append("|");
  47. }
  48. if (chbPe.Checked)
  49. {
  50. arguments.Append(" -pe");
  51. }
  52. if (chbForce.Checked)
  53. {
  54. arguments.Append(" -force");
  55. }
  56. ConsoleRunner.Run(arguments.ToString().SplitQuoted(" ", "\"", removeEmptyItems: true, trimValues: false, keepBracket: true));
  57. }
  58. private void chbFileList_CheckedChanged(object sender, EventArgs e)
  59. {
  60. ctrlFileList.Enabled = chbFileList.Checked;
  61. tbOutputs.Enabled = !chbFileList.Checked;
  62. chbRepeat.Enabled = !chbFileList.Checked;
  63. }
  64. private void ctrlFileList_SelectedFileChanged_1(object sender, EventArgs e)
  65. {
  66. tbOutputs.Text = File.ReadAllText(ctrlFileList.SelectedFile);
  67. }
  68. }
  69. }