1- namespace GeneralUpdate . Core . Strategys ;
2- public class LinuxStrategy : WindowsStrategy ;
1+ using System ;
2+ using System . Diagnostics ;
3+ using System . IO ;
4+ using System . Threading . Tasks ;
5+ using GeneralUpdate . Common . FileBasic ;
6+ using GeneralUpdate . Common . Internal ;
7+ using GeneralUpdate . Common . Internal . Event ;
8+ using GeneralUpdate . Common . Internal . Pipeline ;
9+ using GeneralUpdate . Common . Internal . Strategy ;
10+ using GeneralUpdate . Common . Shared ;
11+ using GeneralUpdate . Common . Shared . Object ;
12+ using GeneralUpdate . Common . Shared . Object . Enum ;
13+ using GeneralUpdate . Common . Shared . Service ;
14+ using GeneralUpdate . Core . Pipeline ;
15+
16+ namespace GeneralUpdate . Core . Strategys ;
17+
18+ public class LinuxStrategy : AbstractStrategy
19+ {
20+ private GlobalConfigInfo _configinfo = new ( ) ;
21+
22+ public override void Create ( GlobalConfigInfo parameter ) => _configinfo = parameter ;
23+
24+ public override void Execute ( )
25+ {
26+ Task . Run ( async ( ) =>
27+ {
28+ try
29+ {
30+ var status = ReportType . None ;
31+ var patchPath = StorageManager . GetTempDirectory ( Patchs ) ;
32+ foreach ( var version in _configinfo . UpdateVersions )
33+ {
34+ try
35+ {
36+ var context = new PipelineContext ( ) ;
37+ //Common
38+ context . Add ( "ZipFilePath" ,
39+ Path . Combine ( _configinfo . TempPath , $ "{ version . Name } { _configinfo . Format } ") ) ;
40+ //Hash middleware
41+ context . Add ( "Hash" , version . Hash ) ;
42+ //Zip middleware
43+ context . Add ( "Format" , _configinfo . Format ) ;
44+ context . Add ( "Name" , version . Name ) ;
45+ context . Add ( "Encoding" , _configinfo . Encoding ) ;
46+ //Patch middleware
47+ context . Add ( "SourcePath" , _configinfo . InstallPath ) ;
48+ context . Add ( "PatchPath" , patchPath ) ;
49+ context . Add ( "PatchEnabled" , _configinfo . PatchEnabled ) ;
50+ //Driver middleware
51+ if ( _configinfo . DriveEnabled == true )
52+ {
53+ context . Add ( "DriverOutPut" , StorageManager . GetTempDirectory ( "DriverOutPut" ) ) ;
54+ context . Add ( "FieldMappings" , _configinfo . FieldMappings ) ;
55+ }
56+
57+ var pipelineBuilder = new PipelineBuilder ( context )
58+ . UseMiddlewareIf < PatchMiddleware > ( _configinfo . PatchEnabled )
59+ . UseMiddleware < CompressMiddleware > ( )
60+ . UseMiddleware < HashMiddleware > ( )
61+ . UseMiddlewareIf < DriverMiddleware > ( _configinfo . DriveEnabled ) ;
62+ await pipelineBuilder . Build ( ) ;
63+ status = ReportType . Success ;
64+ }
65+ catch ( Exception e )
66+ {
67+ status = ReportType . Failure ;
68+ GeneralTracer . Error (
69+ "The Execute method in the GeneralUpdate.Core.WindowsStrategy class throws an exception." ,
70+ e ) ;
71+ EventManager . Instance . Dispatch ( this , new ExceptionEventArgs ( e , e . Message ) ) ;
72+ }
73+ finally
74+ {
75+ await VersionService . Report ( _configinfo . ReportUrl
76+ , version . RecordId
77+ , status
78+ , version . AppType
79+ , _configinfo . Scheme
80+ , _configinfo . Token ) ;
81+ }
82+ }
83+
84+ if ( ! string . IsNullOrEmpty ( _configinfo . UpdateLogUrl ) )
85+ {
86+ OpenBrowser ( _configinfo . UpdateLogUrl ) ;
87+ }
88+
89+ Clear ( patchPath ) ;
90+ Clear ( _configinfo . TempPath ) ;
91+ StartApp ( ) ;
92+ }
93+ catch ( Exception e )
94+ {
95+ GeneralTracer . Error (
96+ "The Execute method in the GeneralUpdate.Core.WindowsStrategy class throws an exception." , e ) ;
97+ EventManager . Instance . Dispatch ( this , new ExceptionEventArgs ( e , e . Message ) ) ;
98+ }
99+ } ) ;
100+ }
101+
102+ public override void StartApp ( )
103+ {
104+ try
105+ {
106+ var mainAppPath = CheckPath ( _configinfo . InstallPath , _configinfo . MainAppName ) ;
107+ if ( string . IsNullOrEmpty ( mainAppPath ) )
108+ throw new Exception ( $ "Can't find the app { mainAppPath } !") ;
109+
110+ ExecuteScript ( ) ;
111+ Process . Start ( mainAppPath ) ;
112+ }
113+ catch ( Exception e )
114+ {
115+ GeneralTracer . Error (
116+ "The StartApp method in the GeneralUpdate.Core.WindowsStrategy class throws an exception." , e ) ;
117+ EventManager . Instance . Dispatch ( this , new ExceptionEventArgs ( e , e . Message ) ) ;
118+ }
119+ finally
120+ {
121+ GeneralTracer . Dispose ( ) ;
122+ Process . GetCurrentProcess ( ) . Kill ( ) ;
123+ }
124+ }
125+
126+ private string CheckPath ( string path , string name )
127+ {
128+ if ( string . IsNullOrWhiteSpace ( path ) || string . IsNullOrWhiteSpace ( name ) ) return string . Empty ;
129+ var tempPath = Path . Combine ( path , name ) ;
130+ return File . Exists ( tempPath ) ? tempPath : string . Empty ;
131+ }
132+
133+ /// <summary>
134+ /// Executes the user-specified script.
135+ /// </summary>
136+ private void ExecuteScript ( )
137+ {
138+ try
139+ {
140+ // Check if the script path is valid (_configinfo should come from the base class configuration)
141+ if ( string . IsNullOrEmpty ( _configinfo . Script ) || ! File . Exists ( _configinfo . Script ) )
142+ {
143+ GeneralTracer . Info ( "No valid script path specified, skipping script execution" ) ;
144+ return ;
145+ }
146+
147+ GeneralTracer . Info ( $ "Starting to execute script: { _configinfo . Script } ") ;
148+
149+ // Start process to execute Linux script (using bash)
150+ var processStartInfo = new ProcessStartInfo
151+ {
152+ FileName = "/bin/bash" ,
153+ Arguments = $ "-c \" { _configinfo . Script } \" ", // Execute the script
154+ RedirectStandardOutput = true ,
155+ RedirectStandardError = true ,
156+ UseShellExecute = false ,
157+ CreateNoWindow = true
158+ } ;
159+
160+ using var process = Process . Start ( processStartInfo ) ;
161+ if ( process == null )
162+ {
163+ GeneralTracer . Error ( "Failed to start script process" ) ;
164+ return ;
165+ }
166+
167+ // Read script output logs
168+ var output = process . StandardOutput . ReadToEnd ( ) ;
169+ var error = process . StandardError . ReadToEnd ( ) ;
170+ process . WaitForExit ( ) ; // Wait for the script to finish execution
171+
172+ if ( ! string . IsNullOrEmpty ( output ) )
173+ GeneralTracer . Info ( $ "Script output: { output } ") ;
174+
175+ if ( ! string . IsNullOrEmpty ( error ) )
176+ GeneralTracer . Warn ( $ "Script warning: { error } ") ;
177+
178+ if ( process . ExitCode != 0 )
179+ throw new Exception ( $ "Script execution failed, exit code: { process . ExitCode } ") ;
180+
181+ GeneralTracer . Info ( "Script executed successfully" ) ;
182+ }
183+ catch ( Exception ex )
184+ {
185+ GeneralTracer . Error ( "An exception occurred while executing the script" , ex ) ;
186+ EventManager . Instance . Dispatch ( this , new ExceptionEventArgs ( ex , "Script execution failed" ) ) ;
187+ }
188+ }
189+ }
0 commit comments