-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathExtensionSystemExample.cs
More file actions
419 lines (348 loc) · 14.9 KB
/
Copy pathExtensionSystemExample.cs
File metadata and controls
419 lines (348 loc) · 14.9 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace GeneralUpdate.Extension.Examples
{
/// <summary>
/// Example usage of the GeneralUpdate.Extension system.
/// Demonstrates all key features of the extension update system with the refactored architecture.
/// </summary>
public class ExtensionSystemExample
{
private IExtensionHost? _host;
/// <summary>
/// Initialize the extension host with typical settings.
/// </summary>
public void Initialize()
{
// Set up paths for your application
var hostVersion = new Version(1, 5, 0);
var installPath = @"C:\MyApp\Extensions";
var downloadPath = @"C:\MyApp\Temp\Downloads";
var serverUrl = "https://your-server.com/api/extensions";
// Detect current platform
var currentPlatform = DetectCurrentPlatform();
// Create the extension host using the new config-based approach
var config = new ExtensionHostConfig
{
HostVersion = hostVersion,
InstallBasePath = installPath,
DownloadPath = downloadPath,
ServerUrl = serverUrl,
TargetPlatform = currentPlatform,
DownloadTimeout = 300 // 5 minutes
};
_host = new GeneralExtensionHost(config);
// Subscribe to events for monitoring
SubscribeToEvents();
// Load existing installed extensions
_host.LoadInstalledExtensions();
Console.WriteLine($"Extension Host initialized for version {hostVersion}");
Console.WriteLine($"Platform: {currentPlatform}");
}
/// <summary>
/// Subscribe to all extension events for monitoring and logging.
/// </summary>
private void SubscribeToEvents()
{
if (_host == null) return;
_host.UpdateStateChanged += (sender, args) =>
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Extension '{args.ExtensionName}' state changed: {args.PreviousState} -> {args.CurrentState}");
if (args.CurrentState == Download.UpdateState.UpdateFailed)
{
Console.WriteLine($" Error: {args.Operation.ErrorMessage}");
}
};
_host.DownloadProgress += (sender, args) =>
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Downloading '{args.ExtensionName}': {args.ProgressPercentage:F1}% ({args.Speed})");
};
_host.DownloadCompleted += (sender, args) =>
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Download completed for '{args.ExtensionName}'");
};
_host.DownloadFailed += (sender, args) =>
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Download failed for '{args.ExtensionName}'");
};
_host.InstallationCompleted += (sender, args) =>
{
if (args.Success)
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Installation successful: '{args.ExtensionName}' at {args.InstallPath}");
}
else
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Installation failed: '{args.ExtensionName}' - {args.ErrorMessage}");
}
};
_host.RollbackCompleted += (sender, args) =>
{
if (args.Success)
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Rollback successful for '{args.ExtensionName}'");
}
else
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Rollback failed for '{args.ExtensionName}': {args.ErrorMessage}");
}
};
}
/// <summary>
/// Example: List all installed extensions.
/// </summary>
public void ListInstalledExtensions()
{
if (_host == null)
{
Console.WriteLine("Host not initialized");
return;
}
var extensions = _host.GetInstalledExtensions();
Console.WriteLine($"\nInstalled Extensions ({extensions.Count}):");
Console.WriteLine("".PadRight(80, '='));
foreach (var ext in extensions)
{
Console.WriteLine($"Name: {ext.Descriptor.DisplayName}");
Console.WriteLine($" ID: {ext.Descriptor.Name}");
Console.WriteLine($" Version: {ext.Descriptor.Version}");
Console.WriteLine($" Installed: {ext.InstallDate:yyyy-MM-dd}");
Console.WriteLine($" Auto-Update: {ext.AutoUpdateEnabled}");
Console.WriteLine($" Enabled: {ext.IsEnabled}");
Console.WriteLine($" Platform: {ext.Descriptor.SupportedPlatforms}");
Console.WriteLine();
}
}
/// <summary>
/// Example: Fetch and display compatible remote extensions.
/// </summary>
public async Task<List<Metadata.AvailableExtension>> FetchCompatibleExtensions()
{
if (_host == null)
{
Console.WriteLine("Host not initialized");
return new List<Metadata.AvailableExtension>();
}
// In a real application, fetch this from your server
string remoteJson = await FetchExtensionsFromServer();
// Parse available extensions
var allExtensions = _host.ParseAvailableExtensions(remoteJson);
// Filter to only compatible extensions
var compatibleExtensions = _host.GetCompatibleExtensions(allExtensions);
Console.WriteLine($"\nCompatible Extensions ({compatibleExtensions.Count}):");
Console.WriteLine("".PadRight(80, '='));
foreach (var ext in compatibleExtensions)
{
Console.WriteLine($"Name: {ext.Descriptor.DisplayName}");
Console.WriteLine($" Version: {ext.Descriptor.Version}");
Console.WriteLine($" Description: {ext.Descriptor.Description}");
Console.WriteLine($" Author: {ext.Descriptor.Publisher}");
Console.WriteLine();
}
return compatibleExtensions;
}
/// <summary>
/// Example: Queue a specific extension for update.
/// </summary>
public void QueueExtensionUpdate(string extensionName, List<Metadata.AvailableExtension> availableExtensions)
{
if (_host == null)
{
Console.WriteLine("Host not initialized");
return;
}
// Find the best version for this extension
var bestVersion = _host.FindBestUpgrade(extensionName, availableExtensions);
if (bestVersion == null)
{
Console.WriteLine($"No compatible version found for extension '{extensionName}'");
return;
}
Console.WriteLine($"Queueing update for '{bestVersion.Descriptor.DisplayName}' to version {bestVersion.Descriptor.Version}");
try
{
var operation = _host.QueueUpdate(bestVersion, enableRollback: true);
Console.WriteLine($"Successfully queued. Operation ID: {operation.OperationId}");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to queue extension: {ex.Message}");
}
}
/// <summary>
/// Example: Check for updates and queue them automatically.
/// </summary>
public async Task<int> CheckAndQueueAutoUpdates()
{
if (_host == null)
{
Console.WriteLine("Host not initialized");
return 0;
}
Console.WriteLine("Checking for updates...");
// Fetch available extensions
var availableExtensions = await FetchCompatibleExtensions();
// Queue all auto-updates
var queuedOperations = _host.QueueAutoUpdates(availableExtensions);
Console.WriteLine($"Queued {queuedOperations.Count} extension(s) for update");
return queuedOperations.Count;
}
/// <summary>
/// Example: Process all queued updates.
/// </summary>
public async Task ProcessAllQueuedUpdates()
{
if (_host == null)
{
Console.WriteLine("Host not initialized");
return;
}
var operations = _host.GetUpdateQueue();
if (operations.Count == 0)
{
Console.WriteLine("No updates in queue");
return;
}
Console.WriteLine($"Processing {operations.Count} queued update(s)...");
await _host.ProcessAllUpdatesAsync();
Console.WriteLine("All updates processed");
// Check results
var successful = _host.GetUpdatesByState(Download.UpdateState.UpdateSuccessful);
var failed = _host.GetUpdatesByState(Download.UpdateState.UpdateFailed);
Console.WriteLine($"Successful: {successful.Count}, Failed: {failed.Count}");
// Clean up completed items
_host.ClearCompletedUpdates();
}
/// <summary>
/// Example: Configure auto-update settings.
/// </summary>
public void ConfigureAutoUpdate()
{
if (_host == null)
{
Console.WriteLine("Host not initialized");
return;
}
// Enable global auto-update
_host.GlobalAutoUpdateEnabled = true;
Console.WriteLine("Global auto-update enabled");
// Enable auto-update for specific extension
_host.SetAutoUpdate("my-extension-id", true);
Console.WriteLine("Auto-update enabled for 'my-extension-id'");
// Disable auto-update for another extension
_host.SetAutoUpdate("another-extension-id", false);
Console.WriteLine("Auto-update disabled for 'another-extension-id'");
}
/// <summary>
/// Example: Check version compatibility.
/// </summary>
public void CheckVersionCompatibility(Metadata.ExtensionDescriptor descriptor)
{
if (_host == null)
{
Console.WriteLine("Host not initialized");
return;
}
bool compatible = _host.IsCompatible(descriptor);
Console.WriteLine($"Extension '{descriptor.DisplayName}' version {descriptor.Version}:");
Console.WriteLine($" Host version: {_host.HostVersion}");
Console.WriteLine($" Required range: {descriptor.Compatibility.MinHostVersion} - {descriptor.Compatibility.MaxHostVersion}");
Console.WriteLine($" Compatible: {(compatible ? "Yes" : "No")}");
}
/// <summary>
/// Complete workflow example: Check for updates and install them.
/// </summary>
public async Task RunCompleteUpdateWorkflow()
{
Console.WriteLine("=== Extension Update Workflow ===\n");
// Step 1: Initialize
Initialize();
// Step 2: List installed extensions
ListInstalledExtensions();
// Step 3: Check for updates
int updateCount = await CheckAndQueueAutoUpdates();
// Step 4: Process updates if any
if (updateCount > 0)
{
await ProcessAllQueuedUpdates();
}
else
{
Console.WriteLine("All extensions are up to date");
}
Console.WriteLine("\n=== Update Workflow Complete ===");
}
#region Helper Methods
/// <summary>
/// Detect the current platform at runtime.
/// </summary>
private Metadata.TargetPlatform DetectCurrentPlatform()
{
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
return Metadata.TargetPlatform.Windows;
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
return Metadata.TargetPlatform.Linux;
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
return Metadata.TargetPlatform.MacOS;
return Metadata.TargetPlatform.None;
}
/// <summary>
/// Simulates fetching remote extensions from a server.
/// In a real application, this would make an HTTP request to your extension server.
/// </summary>
private async Task<string> FetchExtensionsFromServer()
{
// Simulate network delay
await Task.Delay(100);
// In a real application, you would fetch this from your server:
// using (var client = new HttpClient())
// {
// return await client.GetStringAsync("https://your-server.com/api/extensions");
// }
// Sample JSON response
return @"[
{
""descriptor"": {
""id"": ""sample-extension"",
""name"": ""Sample Extension"",
""version"": ""1.0.0"",
""description"": ""A sample extension for demonstration"",
""author"": ""Extension Developer"",
""license"": ""MIT"",
""supportedPlatforms"": 7,
""compatibility"": {
""minHostVersion"": ""1.0.0"",
""maxHostVersion"": ""2.0.0""
},
""downloadUrl"": ""https://example.com/extensions/sample-1.0.0.zip"",
""hash"": ""sha256-example-hash"",
""size"": 1048576,
""releaseDate"": ""2024-01-01T00:00:00Z""
},
""isPreRelease"": false
}
]";
}
#endregion
}
/// <summary>
/// Entry point for running the example.
/// </summary>
public class Program
{
public static async Task Main(string[] args)
{
var example = new ExtensionSystemExample();
try
{
await example.RunCompleteUpdateWorkflow();
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
Console.WriteLine(ex.StackTrace);
}
}
}
}