Skip to content

Commit 8dec9ec

Browse files
committed
feature: Add the function of executing the script after the update is completed on the Linux platform.
1 parent 584bd61 commit 8dec9ec

File tree

8 files changed

+214
-8
lines changed

8 files changed

+214
-8
lines changed

src/c#/GeneralUpdate.Client/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ static async Task Main(string[] args)
3535
//产品id
3636
ProductId = "2d974e2a-31e6-4887-9bb1-b4689e98c77a",
3737
//应用密钥
38-
AppSecretKey = "dfeb5833-975e-4afb-88f1-6278ee9aeff6"
38+
AppSecretKey = "dfeb5833-975e-4afb-88f1-6278ee9aeff6",
39+
Script = "linux/script.shell"
3940
};
4041
_ = await new GeneralClientBootstrap() //单个或多个更新包下载通知事件
4142
//单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件
162 Bytes
Binary file not shown.

src/c#/GeneralUpdate.Common/Shared/Object/Configinfo.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public class Configinfo
7878
public string Scheme { get; set; }
7979

8080
public string Token { get; set; }
81+
82+
/// <summary>
83+
/// Script to grant permissions to a specified file on Linux
84+
/// </summary>
85+
public string Script { get; set; }
8186

8287
public void Validate()
8388
{

src/c#/GeneralUpdate.Common/Shared/Object/GlobalConfigInfo.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,9 @@ public class GlobalConfigInfo
128128
public string Scheme { get; set; }
129129

130130
public string Token { get; set; }
131+
132+
/// <summary>
133+
/// Script to grant permissions to a specified file on Linux
134+
/// </summary>
135+
public string Script { get; set; }
131136
}

src/c#/GeneralUpdate.Common/Shared/Object/ProcessInfo.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public ProcessInfo(string appName
2525
, string bowl
2626
, string scheme
2727
, string token
28+
, string script
2829
, List<string> blackFileFormats
2930
, List<string> blackFiles
3031
, List<string> skipDirectories)
@@ -47,6 +48,7 @@ public ProcessInfo(string appName
4748
Bowl = bowl;
4849
Scheme = scheme;
4950
Token = token;
51+
Script = script;
5052
BlackFileFormats = blackFileFormats;
5153
BlackFiles = blackFiles;
5254
SkipDirectorys = skipDirectories;
@@ -141,5 +143,11 @@ public ProcessInfo(string appName
141143

142144
[JsonPropertyName("SkipDirectorys")]
143145
public List<string> SkipDirectorys { get; set; }
146+
147+
/// <summary>
148+
/// Script to grant permissions to a specified file on Linux
149+
/// </summary>
150+
[JsonPropertyName("Script")]
151+
public string Script { get; set; }
144152
}
145153
}

src/c#/GeneralUpdate.Core/GeneralUpdateBootstrap.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public GeneralUpdateBootstrap()
5555
Scheme = processInfo.Scheme,
5656
Token = processInfo.Token,
5757
DriveEnabled = GetOption(UpdateOption.Drive) ?? false,
58-
PatchEnabled = GetOption(UpdateOption.Patch) ?? true
58+
PatchEnabled = GetOption(UpdateOption.Patch) ?? true,
59+
Script = processInfo.Script
5960
};
6061
}
6162

Lines changed: 189 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,189 @@
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+
}

src/c#/GeneralUpdate.Core/Strategys/WindowsStrategy.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,10 @@ public override void StartApp()
110110

111111
Process.Start(mainAppPath);
112112

113-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
113+
var bowlAppPath = CheckPath(_configinfo.InstallPath, _configinfo.Bowl);
114+
if (!string.IsNullOrEmpty(bowlAppPath))
114115
{
115-
var bowlAppPath = CheckPath(_configinfo.InstallPath, _configinfo.Bowl);
116-
if (!string.IsNullOrEmpty(bowlAppPath))
117-
Process.Start(bowlAppPath);
116+
Process.Start(bowlAppPath);
118117
}
119118
}
120119
catch (Exception e)

0 commit comments

Comments
 (0)