-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathDrivelutionMiddleware.cs
More file actions
100 lines (86 loc) · 3.73 KB
/
Copy pathDrivelutionMiddleware.cs
File metadata and controls
100 lines (86 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#if NET8_0_OR_GREATER
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using GeneralUpdate.Common.Internal.Pipeline;
using GeneralUpdate.Common.Shared;
using GeneralUpdate.Drivelution;
using GeneralUpdate.Drivelution.Abstractions.Models;
namespace GeneralUpdate.Core.Pipeline;
/// <summary>
/// Middleware for handling driver updates using GeneralUpdate.Drivelution functionality.
/// </summary>
public class DrivelutionMiddleware : IMiddleware
{
[RequiresUnreferencedCode("Driver update process includes signature validation that may require runtime reflection on some platforms")]
[RequiresDynamicCode("Driver update process includes signature validation that may require runtime code generation on some platforms")]
public async Task InvokeAsync(PipelineContext context)
{
try
{
var driverDirectory = context.Get<string>("DriverDirectory");
if (string.IsNullOrWhiteSpace(driverDirectory))
{
GeneralTracer.Info("Driver directory not specified in context, skipping driver update.");
return;
}
if (!Directory.Exists(driverDirectory))
{
GeneralTracer.Info($"Driver directory does not exist: {driverDirectory}, skipping driver update.");
return;
}
GeneralTracer.Info($"Starting driver update from directory: {driverDirectory}");
// Get drivers from the specified directory
var drivers = await GeneralDrivelution.GetDriversFromDirectoryAsync(driverDirectory);
if (drivers == null || !drivers.Any())
{
GeneralTracer.Info($"No drivers found in directory: {driverDirectory}");
return;
}
GeneralTracer.Info($"Found {drivers.Count} driver(s) in directory.");
// Update each driver
var successCount = 0;
var failureCount = 0;
var results = new List<UpdateResult>();
foreach (var driver in drivers)
{
try
{
GeneralTracer.Info($"Updating driver: {driver.Name} (Version: {driver.Version})");
var result = await GeneralDrivelution.QuickUpdateAsync(driver);
results.Add(result);
if (result.Success)
{
successCount++;
GeneralTracer.Info($"Driver {driver.Name} updated successfully. Status: {result.Status}");
}
else
{
failureCount++;
var errorMessage = result.Error != null
? $"{result.Error.Code}: {result.Error.Message}"
: "Unknown error";
GeneralTracer.Warn($"Driver {driver.Name} update failed. Error: {errorMessage}");
}
}
catch (Exception ex)
{
failureCount++;
GeneralTracer.Error($"Exception while updating driver {driver.Name}", ex);
}
}
GeneralTracer.Info($"Driver update completed. Success: {successCount}, Failed: {failureCount}");
// Store results in context for potential later use
context.Add("DriverUpdateResults", results);
}
catch (Exception ex)
{
GeneralTracer.Error($"Error in DrivelutionMiddleware while processing directory '{context.Get<string>("DriverDirectory")}'", ex);
throw;
}
}
}
#endif