Skip to content

Commit 78d58b1

Browse files
committed
More refactoring
1 parent 12c9b94 commit 78d58b1

File tree

9 files changed

+187
-113
lines changed

9 files changed

+187
-113
lines changed

.github/actions/spelling/allow.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ MAKEINTRESOURCE
167167
makemsix
168168
MANIFESTSCHEMA
169169
MANIFESTVERSION
170+
mcp
170171
Memberwise
171172
meme
172173
metadatas

.github/actions/spelling/excludes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,5 @@
104104
^src/SfsClient/
105105
^src/Xlang/
106106
^src/VcpkgPortOverlay/
107+
^src/WinGetMCPServer/WinGetMCPServer.csproj$
107108
ignore$
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// -----------------------------------------------------------------------------
2+
// <copyright file="PackageListExtensions.cs" company="Microsoft Corporation">
3+
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
4+
// </copyright>
5+
// -----------------------------------------------------------------------------
6+
7+
namespace WinGetMCPServer.Extensions
8+
{
9+
using Microsoft.Management.Deployment;
10+
using WinGetMCPServer.Response;
11+
12+
/// <summary>
13+
/// Extensions for List<FindPackageResult>.
14+
/// </summary>
15+
internal static class PackageListExtensions
16+
{
17+
static public List<FindPackageResult> AddPackages(this List<FindPackageResult> list, FindPackagesResult findResult)
18+
{
19+
for (int i = 0; i < findResult.Matches!.Count; ++i)
20+
{
21+
list.Add(FindPackageResultFromCatalogPackage(findResult.Matches[i].CatalogPackage));
22+
}
23+
24+
return list;
25+
}
26+
static public FindPackageResult FindPackageResultFromCatalogPackage(CatalogPackage package)
27+
{
28+
FindPackageResult findPackageResult = new FindPackageResult()
29+
{
30+
Identifier = package.Id,
31+
Name = package.Name,
32+
};
33+
34+
var installedVersion = package.InstalledVersion;
35+
findPackageResult.IsInstalled = installedVersion != null;
36+
37+
if (installedVersion != null)
38+
{
39+
findPackageResult.Catalog = installedVersion.PackageCatalog?.Info?.Name;
40+
if (string.IsNullOrEmpty(findPackageResult.Catalog))
41+
{
42+
findPackageResult.Catalog = package.DefaultInstallVersion?.PackageCatalog?.Info?.Name;
43+
}
44+
45+
findPackageResult.InstalledVersion = installedVersion.Version;
46+
findPackageResult.IsUpdateAvailable = package.IsUpdateAvailable;
47+
48+
string installLocation = installedVersion.GetMetadata(PackageVersionMetadataField.InstalledLocation);
49+
if (!string.IsNullOrEmpty(installLocation))
50+
{
51+
findPackageResult.InstalledLocation = installLocation;
52+
}
53+
}
54+
else
55+
{
56+
findPackageResult.Catalog = package.DefaultInstallVersion.PackageCatalog.Info.Name;
57+
}
58+
59+
return findPackageResult;
60+
}
61+
}
62+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// -----------------------------------------------------------------------------
2+
// <copyright file="FindPackageResult.cs" company="Microsoft Corporation">
3+
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
4+
// </copyright>
5+
// -----------------------------------------------------------------------------
6+
7+
namespace WinGetMCPServer.Response
8+
{
9+
/// <summary>
10+
/// Contains information about found packages.
11+
/// </summary>
12+
internal class FindPackageResult
13+
{
14+
public required string Identifier { get; init; }
15+
16+
public string? Name { get; set; }
17+
18+
public string? Catalog { get; set; }
19+
20+
public bool? IsInstalled { get; set; }
21+
22+
public string? InstalledVersion { get; set; }
23+
24+
public string? InstalledLocation { get; set; }
25+
26+
public bool? IsUpdateAvailable { get; set; }
27+
}
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// -----------------------------------------------------------------------------
2+
// <copyright file="InstallOperationResult.cs" company="Microsoft Corporation">
3+
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
4+
// </copyright>
5+
// -----------------------------------------------------------------------------
6+
7+
namespace WinGetMCPServer.Response
8+
{
9+
/// <summary>
10+
/// Contains information about an install operation.
11+
/// </summary>
12+
internal class InstallOperationResult
13+
{
14+
public string? Message { get; set; }
15+
16+
public bool? RebootRequired { get; set; }
17+
18+
public int? ErrorCode { get; set; }
19+
20+
public uint? InstallerErrorCode { get; set; }
21+
22+
public FindPackageResult? InstalledPackageInformation { get; set; }
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// -----------------------------------------------------------------------------
2+
// <copyright file="PackageResponse.cs" company="Microsoft Corporation">
3+
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
4+
// </copyright>
5+
// -----------------------------------------------------------------------------
6+
7+
namespace WinGetMCPServer.Response
8+
{
9+
using Microsoft.Management.Deployment;
10+
11+
/// <summary>
12+
/// Contains information relevant to a failure to identify a package.
13+
/// </summary>
14+
internal class PackageIdentityErrorResult
15+
{
16+
public required string Message { get; init; }
17+
18+
public required string Identifier { get; init; }
19+
20+
public string? Catalog { get; set; }
21+
22+
public List<FindPackageResult> Packages { get; set; } = new List<FindPackageResult>();
23+
}
24+
}

src/WinGetMCPServer/Response/PackageResponse.cs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,33 @@ namespace WinGetMCPServer.Response
88
{
99
using Microsoft.Management.Deployment;
1010
using ModelContextProtocol.Protocol;
11-
using System.Text.Json;
11+
using WinGetMCPServer.Extensions;
1212

1313
/// <summary>
1414
/// Contains reusable responses for package tools.
1515
/// </summary>
1616
internal static class PackageResponse
1717
{
18-
public static CallToolResponse ResponseForConnectError(ConnectResult connectResult)
18+
/// <summary>
19+
/// Creates a response for a ConnectResult error.
20+
/// </summary>
21+
/// <param name="connectResult">The connect result.</param>
22+
/// <returns>The response.</returns>
23+
public static CallToolResponse ForConnectError(ConnectResult connectResult)
1924
{
2025
return new CallToolResponse()
2126
{
2227
IsError = true,
23-
Content = [new Content() { Text = $"Failed when connecting to the WinGet package catalog with error: {connectResult.ExtendedErrorCode.Message} [0x{connectResult.ExtendedErrorCode.HResult:X8}]" }],
28+
Content = [new Content() { Text = $"Failed when connecting to the package catalog with error: {connectResult.ExtendedErrorCode.Message} [0x{connectResult.ExtendedErrorCode.HResult:X8}]" }],
2429
};
2530
}
2631

27-
public static CallToolResponse ResponseForFindError(FindPackagesResult findResult)
32+
/// <summary>
33+
/// Creates a response for a FindPackagesResult error.
34+
/// </summary>
35+
/// <param name="findResult">The find packages result.</param>
36+
/// <returns>The response.</returns>
37+
public static CallToolResponse ForFindError(FindPackagesResult findResult)
2838
{
2939
return new CallToolResponse()
3040
{
@@ -33,7 +43,11 @@ public static CallToolResponse ResponseForFindError(FindPackagesResult findResul
3343
};
3444
}
3545

36-
public static CallToolResponse ResponseForCancelBeforeSystemChange()
46+
/// <summary>
47+
/// Creates a response that indicates the operation was cancelled before any changes were made.
48+
/// </summary>
49+
/// <returns>The response.</returns>
50+
public static CallToolResponse ForCancelBeforeSystemChange()
3751
{
3852
return new CallToolResponse()
3953
{
@@ -42,7 +56,13 @@ public static CallToolResponse ResponseForCancelBeforeSystemChange()
4256
};
4357
}
4458

45-
public static CallToolResponse ResponseForEmptyFind(string identifer, string? catalog)
59+
/// <summary>
60+
/// Creates a response for not finding any packages.
61+
/// </summary>
62+
/// <param name="identifer">The identifier used when searching.</param>
63+
/// <param name="catalog">The catalog that was searched.</param>
64+
/// <returns>The response.</returns>
65+
public static CallToolResponse ForEmptyFind(string identifer, string? catalog)
4666
{
4767
PackageIdentityErrorResult result = new()
4868
{
@@ -51,10 +71,10 @@ public static CallToolResponse ResponseForEmptyFind(string identifer, string? ca
5171
Catalog = catalog,
5272
};
5373

54-
return ResponseFromObject(result);
74+
return ToolResponse.FromObject(result);
5575
}
5676

57-
public static CallToolResponse ResponseForMultiFind(string identifer, string? catalog, FindPackagesResult findResult)
77+
public static CallToolResponse ForMultiFind(string identifer, string? catalog, FindPackagesResult findResult)
5878
{
5979
PackageIdentityErrorResult result = new()
6080
{
@@ -63,12 +83,12 @@ public static CallToolResponse ResponseForMultiFind(string identifer, string? ca
6383
Catalog = catalog,
6484
};
6585

66-
AddFoundPackagesToList(result.Packages, findResult);
86+
result.Packages.AddPackages(findResult);
6787

68-
return ResponseFromObject(result);
88+
return ToolResponse.FromObject(result);
6989
}
7090

71-
public static CallToolResponse ResponseForInstallOperation(InstallResult installResult, FindPackagesResult? findResult)
91+
public static CallToolResponse ForInstallOperation(InstallResult installResult, FindPackagesResult? findResult)
7292
{
7393
InstallOperationResult result = new InstallOperationResult();
7494

@@ -126,10 +146,10 @@ public static CallToolResponse ResponseForInstallOperation(InstallResult install
126146

127147
if (findResult != null && findResult.Status == FindPackagesResultStatus.Ok && findResult.Matches?.Count == 1)
128148
{
129-
result.InstalledPackageInformation = FindPackageResultFromCatalogPackage(findResult.Matches[0].CatalogPackage);
149+
result.InstalledPackageInformation = PackageListExtensions.FindPackageResultFromCatalogPackage(findResult.Matches[0].CatalogPackage);
130150
}
131151

132-
return ResponseFromObject(result, installResult.Status != InstallResultStatus.Ok);
152+
return ToolResponse.FromObject(result, installResult.Status != InstallResultStatus.Ok);
133153
}
134154
}
135155
}

src/WinGetMCPServer/Response/ToolResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static CallToolResponse FromObject(object value, bool isError = false)
3333
/// <param name="isError">Whether or not the response is an error.</param>
3434
/// <param name="jsonSerializerOptions">The JSON serializer options for serializing the object.</param>
3535
/// <returns>The response.</returns>
36-
public static CallToolResponse FromObject(object value, bool isError = false, JsonSerializerOptions jsonSerializerOptions)
36+
public static CallToolResponse FromObject(object value, bool isError, JsonSerializerOptions jsonSerializerOptions)
3737
{
3838
return new CallToolResponse()
3939
{

0 commit comments

Comments
 (0)