Skip to content

Commit bf06452

Browse files
authored
Merge pull request #2541 from marticliment/improvements-to-package-operations
Migration to OperationProvider
2 parents 2645501 + e38e7fe commit bf06452

File tree

43 files changed

+863
-968
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+863
-968
lines changed

src/UniGetUI.PAckageEngine.Interfaces/IPackageManager.cs

+3-65
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace UniGetUI.PackageEngine.Interfaces
99
{
10-
public interface IPackageManager : ISourceProvider, IPackageDetailsProvider
10+
public interface IPackageManager : ISourceProvider, IPackageDetailsProvider, IOperationProvider
1111
{
1212
public ManagerProperties Properties { get; }
1313
public ManagerCapabilities Capabilities { get; }
@@ -20,6 +20,7 @@ public interface IPackageManager : ISourceProvider, IPackageDetailsProvider
2020

2121
public ISourceProvider? SourceProvider { get; }
2222
public IPackageDetailsProvider? PackageDetailsProvider { get; }
23+
public IOperationProvider? OperationProvider { get; }
2324

2425
/// <summary>
2526
/// Initializes the Package Manager (asynchronously). Must be run before using any other method of the manager.
@@ -54,76 +55,13 @@ public interface IPackageManager : ISourceProvider, IPackageDetailsProvider
5455
/// </summary>
5556
public Task<IPackage[]> GetInstalledPackages();
5657

57-
/// <summary>
58-
/// Returns the command-line parameters to install the given package.
59-
/// Each manager MUST implement this method.
60-
/// </summary>
61-
/// <param name="package">The Package going to be installed</param>
62-
/// <param name="options">The options in which it is going to be installed</param>
63-
/// <returns>An array of strings containing the parameters without the manager executable file</returns>
64-
public abstract string[] GetInstallParameters(IPackage package, IInstallationOptions options);
65-
66-
67-
/// <summary>
68-
/// Returns the command-line parameters to update the given package.
69-
/// Each manager MUST implement this method.
70-
/// </summary>
71-
/// <param name="package">The Package going to be updated</param>
72-
/// <param name="options">The options in which it is going to be updated</param>
73-
/// <returns>An array of strings containing the parameters without the manager executable file</returns>
74-
public abstract string[] GetUpdateParameters(IPackage package, IInstallationOptions options);
75-
76-
/// <summary>
77-
/// Returns the command-line parameters to uninstall the given package.
78-
/// Each manager MUST implement this method.
79-
/// </summary>
80-
/// <param name="package">The Package going to be uninstalled</param>
81-
/// <param name="options">The options in which it is going to be uninstalled</param>
82-
/// <returns>An array of strings containing the parameters without the manager executable file</returns>
83-
public abstract string[] GetUninstallParameters(IPackage package, IInstallationOptions options);
84-
85-
/// <summary>
86-
/// Decides and returns the verdict of the install operation.
87-
/// Each manager MUST implement this method.
88-
/// </summary>
89-
/// <param name="package">The package that was installed</param>
90-
/// <param name="options">The options with which the package was installed. They may be modified if the returned value is OperationVeredict.AutoRetry</param>
91-
/// <param name="ReturnCode">The exit code of the process</param>
92-
/// <param name="Output">the output of the process</param>
93-
/// <returns>An OperationVeredict value representing the result of the installation</returns>
94-
public abstract OperationVeredict GetInstallOperationVeredict(IPackage package, IInstallationOptions options, int ReturnCode, string[] Output);
95-
96-
97-
/// <summary>
98-
/// Decides and returns the verdict of the update operation.
99-
/// Each manager MUST implement this method.
100-
/// </summary>
101-
/// <param name="package">The package that was updated</param>
102-
/// <param name="options">The options with which the package was updated. They may be modified if the returned value is OperationVeredict.AutoRetry</param>
103-
/// <param name="ReturnCode">The exit code of the process</param>
104-
/// <param name="Output">the output of the process</param>
105-
/// <returns>An OperationVeredict value representing the result of the update</returns>
106-
public abstract OperationVeredict GetUpdateOperationVeredict(IPackage package, IInstallationOptions options, int ReturnCode, string[] Output);
107-
108-
/// <summary>
109-
/// Decides and returns the verdict of the uninstall operation.
110-
/// Each manager MUST implement this method.
111-
/// </summary>
112-
/// <param name="package">The package that was uninstalled</param>
113-
/// <param name="options">The options with which the package was uninstalled. They may be modified if the returned value is OperationVeredict.AutoRetry</param>
114-
/// <param name="ReturnCode">The exit code of the process</param>
115-
/// <param name="Output">the output of the process</param>
116-
/// <returns>An OperationVeredict value representing the result of the uninstall</returns>
117-
public abstract OperationVeredict GetUninstallOperationVeredict(IPackage package, IInstallationOptions options, int ReturnCode, string[] Output);
118-
11958
/// <summary>
12059
/// Refreshes the Package Manager sources/indexes
12160
/// Each manager MUST implement this method.
12261
/// </summary>
12362
public Task RefreshPackageIndexes();
63+
12464
public IManagerSource GetSourceOrDefault(string SourceName);
12565
public IManagerSource? GetSourceIfExists(string SourceName);
126-
127-
public void LogOperation(Process process, string output);
12866
}
12967
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using UniGetUI.PackageEngine.Enums;
2+
3+
namespace UniGetUI.PackageEngine.Interfaces.ManagerProviders;
4+
public interface IOperationProvider
5+
{
6+
/// <summary>
7+
/// Returns the list of arguments that need to be passed to the Package Manager executable so
8+
/// that the requested operation is performed over the given package, with its corresponding
9+
/// installation options.
10+
/// </summary>
11+
public abstract IEnumerable<string> GetOperationParameters(
12+
IPackage package,
13+
IInstallationOptions options,
14+
OperationType operation
15+
);
16+
17+
/// <summary>
18+
/// Returns the veredict of the given package operation, given the package, the operation type,
19+
/// the corresponding output and the return code.
20+
/// </summary>
21+
public abstract OperationVeredict GetOperationResult(
22+
IPackage package,
23+
IInstallationOptions options,
24+
OperationType operation,
25+
IEnumerable<string> processOutput,
26+
int returnCode
27+
);
28+
}

src/UniGetUI.PackageEngine.Enums/Enums.cs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public enum OperationVeredict
3131
{
3232
Succeeded,
3333
Failed,
34+
RestartRequired,
3435
AutoRetry,
3536
}
3637
public enum OperationStatus

src/UniGetUI.PackageEngine.Managers.Chocolatey/Chocolatey.cs

+1-101
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public Chocolatey()
6161

6262
SourceProvider = new ChocolateySourceProvider(this);
6363
PackageDetailsProvider = new ChocolateyDetailsProvider(this);
64+
OperationProvider = new ChocolateyOperationProvider(this);
6465
}
6566

6667
protected override async Task<Package[]> GetAvailableUpdates_UnSafe()
@@ -170,107 +171,6 @@ protected override async Task<Package[]> GetInstalledPackages_UnSafe()
170171

171172
return Packages.ToArray();
172173
}
173-
public override OperationVeredict GetInstallOperationVeredict(IPackage package, IInstallationOptions options, int ReturnCode, string[] Output)
174-
{
175-
string output_string = string.Join("\n", Output);
176-
177-
if (ReturnCode is 1641 or 0)
178-
{
179-
return OperationVeredict.Succeeded;
180-
}
181-
182-
if (ReturnCode == 3010)
183-
{
184-
return OperationVeredict.Succeeded; // TODO: Restart required
185-
}
186-
187-
if ((output_string.Contains("Run as administrator") || output_string.Contains("The requested operation requires elevation") || output_string.Contains("ERROR: Exception calling \"CreateDirectory\" with \"1\" argument(s): \"Access to the path")) && !options.RunAsAdministrator)
188-
{
189-
options.RunAsAdministrator = true;
190-
return OperationVeredict.AutoRetry;
191-
}
192-
193-
return OperationVeredict.Failed;
194-
}
195-
196-
public override OperationVeredict GetUpdateOperationVeredict(IPackage package, IInstallationOptions options, int ReturnCode, string[] Output)
197-
{
198-
return GetInstallOperationVeredict(package, options, ReturnCode, Output);
199-
}
200-
201-
public override OperationVeredict GetUninstallOperationVeredict(IPackage package, IInstallationOptions options, int ReturnCode, string[] Output)
202-
{
203-
string output_string = string.Join("\n", Output);
204-
205-
if (ReturnCode is 1641 or 1614 or 1605 or 0)
206-
{
207-
return OperationVeredict.Succeeded;
208-
}
209-
210-
if (ReturnCode == 3010)
211-
{
212-
return OperationVeredict.Succeeded; // TODO: Restart required
213-
}
214-
215-
if ((output_string.Contains("Run as administrator") || output_string.Contains("The requested operation requires elevation")) && !options.RunAsAdministrator)
216-
{
217-
options.RunAsAdministrator = true;
218-
return OperationVeredict.AutoRetry;
219-
}
220-
221-
return OperationVeredict.Failed;
222-
}
223-
public override string[] GetInstallParameters(IPackage package, IInstallationOptions options)
224-
{
225-
List<string> parameters = GetUninstallParameters(package, options).ToList();
226-
parameters[0] = Properties.InstallVerb;
227-
parameters.Add("--no-progress");
228-
229-
if (options.Architecture == Architecture.X86)
230-
{
231-
parameters.Add("--forcex86");
232-
}
233-
234-
if (options.PreRelease)
235-
{
236-
parameters.Add("--prerelease");
237-
}
238-
239-
if (options.SkipHashCheck)
240-
{
241-
parameters.AddRange(["--ignore-checksums", "--force"]);
242-
}
243-
244-
if (options.Version != "")
245-
{
246-
parameters.AddRange([$"--version={options.Version}", "--allow-downgrade"]);
247-
}
248-
249-
return parameters.ToArray();
250-
}
251-
public override string[] GetUpdateParameters(IPackage package, IInstallationOptions options)
252-
{
253-
string[] parameters = GetInstallParameters(package, options);
254-
parameters[0] = Properties.UpdateVerb;
255-
return parameters;
256-
}
257-
258-
public override string[] GetUninstallParameters(IPackage package, IInstallationOptions options)
259-
{
260-
List<string> parameters = [Properties.UninstallVerb, package.Id, "-y"];
261-
262-
if (options.CustomParameters is not null)
263-
{
264-
parameters.AddRange(options.CustomParameters);
265-
}
266-
267-
if (options.InteractiveInstallation)
268-
{
269-
parameters.Add("--notsilent");
270-
}
271-
272-
return parameters.ToArray();
273-
}
274174

275175
protected override async Task<ManagerStatus> LoadManager()
276176
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.InteropServices;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using UniGetUI.PackageEngine.Classes.Manager.BaseProviders;
8+
using UniGetUI.PackageEngine.Enums;
9+
using UniGetUI.PackageEngine.Interfaces;
10+
using UniGetUI.PackageEngine.Managers.ChocolateyManager;
11+
using UniGetUI.PackageEngine.PackageClasses;
12+
13+
namespace UniGetUI.PackageEngine.Managers.ChocolateyManager;
14+
internal sealed class ChocolateyOperationProvider : BaseOperationProvider<Chocolatey>
15+
{
16+
public ChocolateyOperationProvider(Chocolatey manager) : base(manager) { }
17+
18+
public override IEnumerable<string> GetOperationParameters(
19+
IPackage package,
20+
IInstallationOptions options,
21+
OperationType operation)
22+
{
23+
List<string> parameters = [operation switch {
24+
OperationType.Install => Manager.Properties.InstallVerb,
25+
OperationType.Update => Manager.Properties.UpdateVerb,
26+
OperationType.Uninstall => Manager.Properties.UninstallVerb,
27+
_ => throw new InvalidDataException("Invalid package operation")
28+
}];
29+
parameters.AddRange([package.Id, "-y"]);
30+
31+
if (options.CustomParameters is not null)
32+
parameters.AddRange(options.CustomParameters);
33+
34+
if (options.InteractiveInstallation)
35+
parameters.Add("--notsilent");
36+
37+
if(operation is OperationType.Install or OperationType.Update)
38+
{
39+
parameters.Add("--no-progress");
40+
41+
if (options.Architecture == Architecture.X86)
42+
parameters.Add("--forcex86");
43+
44+
if (options.PreRelease)
45+
parameters.Add("--prerelease");
46+
47+
if (options.SkipHashCheck)
48+
parameters.AddRange(["--ignore-checksums", "--force"]);
49+
50+
if (options.Version != "")
51+
parameters.AddRange([$"--version={options.Version}", "--allow-downgrade"]);
52+
}
53+
54+
return parameters;
55+
}
56+
57+
public override OperationVeredict GetOperationResult(
58+
IPackage package,
59+
IInstallationOptions options,
60+
OperationType operation,
61+
IEnumerable<string> processOutput,
62+
int returnCode)
63+
{
64+
if(returnCode is 3010)
65+
{
66+
return OperationVeredict.RestartRequired;
67+
}
68+
69+
if (returnCode is 1641 or 1614 or 1605 or 0)
70+
{
71+
return OperationVeredict.Succeeded;
72+
}
73+
74+
75+
string output_string = string.Join("\n", processOutput);
76+
if (!options.RunAsAdministrator &&
77+
(output_string.Contains("Run as administrator")
78+
|| output_string.Contains("The requested operation requires elevation")
79+
|| output_string.Contains("ERROR: Exception calling \"CreateDirectory\" with \"1\" argument(s): \"Access to the path")) )
80+
{
81+
options.RunAsAdministrator = true;
82+
return OperationVeredict.AutoRetry;
83+
}
84+
85+
return OperationVeredict.Failed;
86+
}
87+
}

0 commit comments

Comments
 (0)