ReleaseExtensions.cs (2423B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="ReleaseExtensions.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.WinGet.Client.Engine.Extensions 8 { 9 using System.Linq; 10 using Microsoft.WinGet.Client.Engine.Exceptions; 11 using Microsoft.WinGet.Resources; 12 using Octokit; 13 14 /// <summary> 15 /// Extension methods for Octokit.Release. 16 /// </summary> 17 internal static class ReleaseExtensions 18 { 19 /// <summary> 20 /// Gets the Asset. 21 /// </summary> 22 /// <param name="release">GitHub release.</param> 23 /// <param name="name">Name of asset.</param> 24 /// <returns>The asset.</returns> 25 public static ReleaseAsset GetAsset(this Release release, string name) 26 { 27 var asset = TryGetAsset(release, name); 28 29 if (asset != null) 30 { 31 return asset; 32 } 33 34 throw new WinGetRepairException(string.Format(Resources.ReleaseAssetNotFound, name)); 35 } 36 37 /// <summary> 38 /// Gets the Asset if present. 39 /// </summary> 40 /// <param name="release">GitHub release.</param> 41 /// <param name="name">Name of asset.</param> 42 /// <returns>The asset, or null if not found.</returns> 43 public static ReleaseAsset? TryGetAsset(this Release release, string name) 44 { 45 var assets = release.Assets.Where(a => a.Name == name); 46 return assets.Any() ? assets.First() : null; 47 } 48 49 /// <summary> 50 /// Gets the asset that ends with the string. 51 /// </summary> 52 /// <param name="release">GitHub release.</param> 53 /// <param name="name">Asset last part name.</param> 54 /// <returns>The asset.</returns> 55 public static ReleaseAsset GetAssetEndsWith(this Release release, string name) 56 { 57 var assets = release.Assets.Where(a => a.Name.EndsWith(name)); 58 59 if (assets.Any()) 60 { 61 return assets.First(); 62 } 63 64 throw new WinGetRepairException(string.Format(Resources.ReleaseAssetNotFound, name)); 65 } 66 } 67 }