1+ using System ;
2+ using System . Windows . Forms ;
3+
4+ namespace SnIoGui
5+ {
6+ public partial class CleanForm : Form
7+ {
8+ private Button btnCancel ;
9+
10+ // Cancel button event handler
11+ private void BtnCancel_Click ( object sender , EventArgs e )
12+ {
13+ this . DialogResult = DialogResult . Cancel ;
14+ this . Close ( ) ;
15+ }
16+
17+ public string ProfileName
18+ {
19+ get => txtProfileName . Text ;
20+ set => txtProfileName . Text = value ;
21+ }
22+ public string ExportPath
23+ {
24+ get => txtExportPath . Text ;
25+ set => txtExportPath . Text = value ;
26+ }
27+ public string CleanedPath
28+ {
29+ get => txtCleanedPath . Text ;
30+ set => txtCleanedPath . Text = value ;
31+ }
32+ public bool Minimal
33+ {
34+ get => chkMinimal . Checked ;
35+ set => chkMinimal . Checked = value ;
36+ }
37+ public string Script
38+ {
39+ get => txtScript . Text ;
40+ set => txtScript . Text = value ;
41+ }
42+
43+ private readonly Target _target ;
44+ private readonly string _cleanerExe ;
45+ private string _generatedScript ;
46+
47+ public CleanForm ( Target target , string cleanerExe )
48+ {
49+ InitializeComponent ( ) ;
50+ btnViewScript . Click += BtnViewScript_Click ! ;
51+ btnExecuteScript . Click += BtnExecuteScript_Click ! ;
52+ _target = target ;
53+ _cleanerExe = cleanerExe ;
54+ ProfileName = target ? . Name ?? string . Empty ;
55+ ExportPath = target ? . ExportPath ?? string . Empty ;
56+
57+ // Calculate cleaned path from export path
58+ CleanedPath = CalculateCleanedPath ( target ? . ExportPath ?? string . Empty ) ;
59+
60+ // Default minimal to true
61+ Minimal = true ;
62+
63+ // Script generation in constructor
64+ _generatedScript = GenerateScript ( ) ;
65+ btnCancel . Click += BtnCancel_Click ;
66+
67+ // Set CancelButton property so ESC triggers Cancel
68+ this . CancelButton = btnCancel ;
69+ }
70+
71+ private string CalculateCleanedPath ( string exportPath )
72+ {
73+ if ( string . IsNullOrEmpty ( exportPath ) )
74+ return string . Empty ;
75+
76+ // If export path ends with "_clean", use as is
77+ if ( exportPath . EndsWith ( "_clean" , StringComparison . OrdinalIgnoreCase ) )
78+ return exportPath ;
79+
80+ // Otherwise append "_clean" to the export path
81+ return exportPath + "_clean" ;
82+ }
83+
84+ private void BtnViewScript_Click ( object sender , EventArgs e )
85+ {
86+ // Regenerate script with current form values
87+ _generatedScript = GenerateScript ( ) ;
88+
89+ // Set multiline text in txtScript, preserving line breaks
90+ txtScript . Lines = _generatedScript . Split ( new [ ] { "\r \n " , "\n " , "\r " } , StringSplitOptions . None ) ;
91+ }
92+
93+ private void BtnExecuteScript_Click ( object sender , EventArgs e )
94+ {
95+ try
96+ {
97+ // Regenerate script with current form values
98+ _generatedScript = GenerateScript ( ) ;
99+
100+ // Save script to a temporary file
101+ string tempScriptPath = System . IO . Path . GetTempFileName ( ) + ".ps1" ;
102+ System . IO . File . WriteAllText ( tempScriptPath , _generatedScript ) ;
103+
104+ // Start PowerShell so it closes after execution (no -NoExit)
105+ bool started = ProcessHelper . RunPowerShellScriptFileInWindow ( tempScriptPath , keepOpen : false ) ;
106+ if ( started )
107+ {
108+ this . DialogResult = DialogResult . OK ;
109+ this . Close ( ) ;
110+ }
111+ else
112+ {
113+ MessageBox . Show ( "Failed to start PowerShell process." , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
114+ }
115+ }
116+ catch ( Exception ex )
117+ {
118+ MessageBox . Show ( $ "An error occurred while starting PowerShell:\n { ex . Message } ", "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
119+ }
120+ }
121+
122+ private string GenerateScript ( )
123+ {
124+ string cleanedPath = txtCleanedPath . Text ;
125+ string exportPath = txtExportPath . Text ;
126+ bool minimal = chkMinimal . Checked ;
127+
128+ // Script template with placeholders for clean operation
129+ string template = "$Exe = \" {CleanerExe}\" \n " +
130+ "$SourcePath = \" {ExportPath}\\ Root\" \n " +
131+ "$TargetPath = \" {CleanedPath}\\ Root\" \n " +
132+ "$MinimalFlag = \" {MinimalFlag}\" \n \n " +
133+ "& $Exe -SOURCE $SourcePath -TARGET $TargetPath $MinimalFlag\n " +
134+ "Write-Host 'Press any key to close...'\n " +
135+ "$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')" ;
136+
137+ string script = template
138+ . Replace ( "{CleanerExe}" , _cleanerExe )
139+ . Replace ( "{ExportPath}" , exportPath )
140+ . Replace ( "{CleanedPath}" , cleanedPath )
141+ . Replace ( "{MinimalFlag}" , minimal ? "-MINIMAL" : "" ) ;
142+
143+ return script ;
144+ }
145+ }
146+ }
0 commit comments