From d8c45c7cc6d4a2bc44dffd044b484aee33aae57e Mon Sep 17 00:00:00 2001 From: Thomas Nieto <38873752+ThomasNieto@users.noreply.github.com> Date: Tue, 28 Mar 2023 10:01:28 -0500 Subject: [PATCH] Add Find-Package (#7) --- BuildSettings.psd1 | 4 +++- src/code/MsuProvider.cs | 42 ++++++++++++++++++++++++++++++++++++- src/code/MsuProvider.csproj | 5 +++-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/BuildSettings.psd1 b/BuildSettings.psd1 index fd0a90d..df8c191 100644 --- a/BuildSettings.psd1 +++ b/BuildSettings.psd1 @@ -1,6 +1,8 @@ @{ Path = @( - './src/code/bin/Release/netstandard2.0/MsuProvider.dll', + './src/code/bin/Release/net461/MsuProvider.dll', + './src/code/bin/Release/net461/Microsoft.Deployment.Compression.dll', + './src/code/bin/Release/net461/Microsoft.Deployment.Compression.Cab.dll', './src/AnyPackage.Msu.psd1' ) Destination = './module' diff --git a/src/code/MsuProvider.cs b/src/code/MsuProvider.cs index 1412526..26e64a4 100644 --- a/src/code/MsuProvider.cs +++ b/src/code/MsuProvider.cs @@ -2,18 +2,58 @@ // You may use, distribute and modify this code under the // terms of the MIT license. +using Microsoft.Deployment.Compression.Cab; using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; using System.Management; namespace AnyPackage.Provider.Msu { [PackageProvider("Msu")] - public sealed class MsuProvider : PackageProvider, IGetPackage + public sealed class MsuProvider : PackageProvider, IFindPackage, IGetPackage { private readonly static Guid s_id = new Guid("314633fe-c7e9-4eeb-824b-382a8a4e92b8"); public MsuProvider() : base(s_id) { } + public void FindPackage(PackageRequest request) + { + var file = new CabInfo(request.Name).GetFiles() + .Where(x => Path.GetExtension(x.Name) == ".txt") + .FirstOrDefault(); + + if (file is null) + { + return; + } + + string line; + Dictionary metadata = new Dictionary(); + using var reader = file.OpenText(); + + while ((line = reader.ReadLine()) is not null) + { + var values = line.Split('='); + var key = values[0].Replace(" ", ""); + + // Remove quotes around value + var value = values[1].Substring(1, values[1].Length - 2); + + metadata.Add(key, value); + } + + if (metadata.ContainsKey("KBArticleNumber")) + { + var kb = string.Format("KB{0}", metadata["KBArticleNumber"]); + request.WritePackage(kb, + new PackageVersion("0"), + metadata["PackageType"], + request.NewSourceInfo(request.Name, request.Name)); + } + } + public void GetPackage(PackageRequest request) { var quickFix = new ManagementObjectSearcher(@"root\cimv2", "select * from Win32_QuickFixEngineering"); diff --git a/src/code/MsuProvider.csproj b/src/code/MsuProvider.csproj index 28d172d..f1b1009 100644 --- a/src/code/MsuProvider.csproj +++ b/src/code/MsuProvider.csproj @@ -1,7 +1,7 @@ - netstandard2.0 + net461 latest enable © 2023 Thomas Nieto. All rights reserved. @@ -9,8 +9,9 @@ + - +