Skip to content

Commit af21cac

Browse files
Fix build for changes to .NET 8
1 parent 0d2a3c0 commit af21cac

File tree

9 files changed

+97
-25
lines changed

9 files changed

+97
-25
lines changed

doBuild.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,12 @@ function DoBuild
8989
'Azure.Core'
9090
'Azure.Identity'
9191
'Microsoft.Bcl.AsyncInterfaces'
92+
'Microsoft.Extensions.Caching.Abstractions'
93+
'Microsoft.Extensions.Caching.Memory'
9294
'Microsoft.Extensions.FileProviders.Abstractions'
9395
'Microsoft.Extensions.FileSystemGlobbing'
96+
'Microsoft.Extensions.Logging.Abstractions'
97+
'Microsoft.Extensions.Options'
9498
'Microsoft.Extensions.Primitives'
9599
'Microsoft.Identity.Client'
96100
'Microsoft.Identity.Client.Extensions.Msal'
@@ -107,6 +111,7 @@ function DoBuild
107111
'NuGet.ProjectModel'
108112
'NuGet.Protocol'
109113
'NuGet.Versioning'
114+
'OrasProject.Oras'
110115
'System.Buffers'
111116
'System.Diagnostics.DiagnosticSource'
112117
'System.IO.FileSystem.AccessControl'

src/code/FindHelper.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Management.Automation;
1010
using System.Net;
1111
using System.Runtime.ExceptionServices;
12+
using System.Runtime.InteropServices;
1213
using System.Text.RegularExpressions;
1314
using System.Threading;
1415

@@ -188,7 +189,11 @@ public IEnumerable<PSResourceInfo> FindByResourceName(
188189
{
189190
PSRepositoryInfo currentRepository = repositoriesToSearch[i];
190191

191-
bool isAllowed = GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(currentRepository.Uri);
192+
bool isAllowed = true;
193+
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
194+
{
195+
isAllowed = GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(currentRepository.Uri);
196+
}
192197

193198
if (!isAllowed)
194199
{
@@ -376,7 +381,12 @@ public IEnumerable<PSCommandResourceInfo> FindByCommandOrDscResource(
376381
{
377382
PSRepositoryInfo currentRepository = repositoriesToSearch[i];
378383

379-
bool isAllowed = GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(currentRepository.Uri);
384+
bool isAllowed = true;
385+
386+
if (OperatingSystem.IsWindows())
387+
{
388+
isAllowed = GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(currentRepository.Uri);
389+
}
380390

381391
if (!isAllowed)
382392
{
@@ -583,7 +593,12 @@ public IEnumerable<PSResourceInfo> FindByTag(
583593
{
584594
PSRepositoryInfo currentRepository = repositoriesToSearch[i];
585595

586-
bool isAllowed = GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(currentRepository.Uri);
596+
597+
bool isAllowed = true;
598+
if (OperatingSystem.IsWindows())
599+
{
600+
isAllowed = GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(currentRepository.Uri);
601+
}
587602

588603
if (!isAllowed)
589604
{
@@ -701,7 +716,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
701716
_cmdletPassedIn.WriteDebug("No version specified, package name is '*'");
702717
// Example: Find-PSResource -Name "*"
703718

704-
// Note: Just for resources from V2 servers, specifically PSGallery, if the resource is unlisted and was requested non-explicitly
719+
// Note: Just for resources from V2 servers, specifically PSGallery, if the resource is unlisted and was requested non-explicitly
705720
// (i.e requested name has wildcard) the resource should not be returned and ResponseUtil.ConvertToPSResourceResult() call needs to be informed of this.
706721
// In all other cases, return the resource regardless of whether it was requested explicitly or not.
707722
bool isResourceRequestedWithWildcard = isV2Resource;
@@ -752,7 +767,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
752767
// Example: Find-PSResource -Name "Az*" -Tag "Storage"
753768
_cmdletPassedIn.WriteDebug("No version specified, package name contains a wildcard.");
754769

755-
// Note: Just for resources from V2 servers, specifically PSGallery, if the resource is unlisted and was requested non-explicitly
770+
// Note: Just for resources from V2 servers, specifically PSGallery, if the resource is unlisted and was requested non-explicitly
756771
// (i.e requested name has wildcard) the resource should not be returned and ResponseUtil.ConvertToPSResourceResult() call needs to be informed of this.
757772
// In all other cases, return the resource regardless of whether it was requested explicitly or not.
758773
bool isResourceRequestedWithWildcard = isV2Resource;
@@ -1173,7 +1188,7 @@ internal IEnumerable<PSResourceInfo> FindDependencyPackages(
11731188
}
11741189
else if(dep.VersionRange.MaxVersion != null && dep.VersionRange.MinVersion != null && dep.VersionRange.MaxVersion.OriginalVersion.Equals(dep.VersionRange.MinVersion.OriginalVersion))
11751190
{
1176-
string depPkgVersion = dep.VersionRange.MaxVersion.OriginalVersion;
1191+
string depPkgVersion = dep.VersionRange.MaxVersion.OriginalVersion;
11771192
FindResults responses = currentServer.FindVersion(dep.Name, version: dep.VersionRange.MaxVersion.ToNormalizedString(), _type, out ErrorRecord errRecord);
11781193
if (errRecord != null)
11791194
{

src/code/GroupPolicyRepositoryEnforcement.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Linq;
9+
using System.Runtime.Versioning;
910
using Microsoft.PowerShell.PSResourceGet.UtilClasses;
1011
using Microsoft.Win32;
1112

@@ -14,6 +15,7 @@ namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
1415
/// <summary>
1516
/// This class is used to enforce group policy for repositories.
1617
/// </summary>
18+
[SupportedOSPlatform("windows")]
1719
public class GroupPolicyRepositoryEnforcement
1820
{
1921
const string userRoot = "HKEY_CURRENT_USER";
@@ -29,6 +31,7 @@ private GroupPolicyRepositoryEnforcement()
2931
/// </summary>
3032
///
3133
/// <returns>True if the group policy is enabled, false otherwise.</returns>
34+
[SupportedOSPlatform("windows")]
3235
public static bool IsGroupPolicyEnabled()
3336
{
3437
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
@@ -57,6 +60,7 @@ public static bool IsGroupPolicyEnabled()
5760
/// </summary>
5861
/// <returns>Array of allowed URIs.</returns>
5962
/// <exception cref="InvalidOperationException">Thrown when the group policy is not enabled.</exception>
63+
[SupportedOSPlatform("windows")]
6064
public static Uri[]? GetAllowedRepositoryURIs()
6165
{
6266
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
@@ -92,6 +96,7 @@ public static bool IsGroupPolicyEnabled()
9296
}
9397
}
9498

99+
[SupportedOSPlatform("windows")]
95100
internal static bool IsRepositoryAllowed(Uri repositoryUri)
96101
{
97102
bool isAllowed = false;
@@ -113,6 +118,7 @@ internal static bool IsRepositoryAllowed(Uri repositoryUri)
113118
return isAllowed;
114119
}
115120

121+
[SupportedOSPlatform("windows")]
116122
private static List<KeyValuePair<string, Uri>>? ReadGPFromRegistry()
117123
{
118124
List<KeyValuePair<string, Uri>> allowedRepositories = new List<KeyValuePair<string, Uri>>();
@@ -169,7 +175,13 @@ internal static bool IsRepositoryAllowed(Uri repositoryUri)
169175
throw new InvalidOperationException("Invalid registry value.");
170176
}
171177

172-
string valueString = value.ToString();
178+
string? valueString = value.ToString();
179+
180+
if (string.IsNullOrEmpty(valueString))
181+
{
182+
throw new InvalidOperationException("Invalid registry value.");
183+
}
184+
173185
var kvRegValue = ConvertRegValue(valueString);
174186
allowedRepositories.Add(kvRegValue);
175187
}

src/code/InstallHelper.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,12 @@ private List<PSResourceInfo> ProcessRepositories(
270270
{
271271
PSRepositoryInfo currentRepository = listOfRepositories[i];
272272

273-
bool isAllowed = GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(currentRepository.Uri);
273+
bool isAllowed = true;
274+
275+
if (OperatingSystem.IsWindows())
276+
{
277+
isAllowed = GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(currentRepository.Uri);
278+
}
274279

275280
if (!isAllowed)
276281
{
@@ -659,7 +664,7 @@ private List<PSResourceInfo> InstallPackages(
659664
ErrorCategory.InvalidOperation,
660665
_cmdletPassedIn));
661666

662-
throw e;
667+
throw;
663668
}
664669
finally
665670
{

src/code/PSScriptMetadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public PSScriptMetadata(
115115
}
116116

117117
Version = !String.IsNullOrEmpty(version) ? new NuGetVersion (version) : new NuGetVersion("1.0.0.0");
118-
Guid = (guid == null || guid == Guid.Empty) ? Guid.NewGuid() : guid;
118+
Guid = guid;
119119
Author = !String.IsNullOrEmpty(author) ? author : Environment.UserName;
120120
CompanyName = companyName;
121121
Copyright = copyright;

src/code/PublishHelper.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,12 @@ internal void PushResource(string Repository, string modulePrefix, bool SkipDepe
355355
return;
356356
}
357357

358-
bool isAllowed = GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(repository.Uri);
358+
bool isAllowed = true;
359+
360+
if (OperatingSystem.IsWindows())
361+
{
362+
isAllowed = GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(repository.Uri);
363+
}
359364

360365
if (!isAllowed)
361366
{

src/code/RepositorySettings.cs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Xml;
1111
using System.Xml.Linq;
1212
using Microsoft.PowerShell.PSResourceGet.Cmdlets;
13+
using NuGet.Protocol.Core.Types;
1314
using static Microsoft.PowerShell.PSResourceGet.UtilClasses.PSRepositoryInfo;
1415

1516
namespace Microsoft.PowerShell.PSResourceGet.UtilClasses
@@ -288,7 +289,12 @@ public static PSRepositoryInfo Add(string repoName, Uri repoUri, int repoPriorit
288289
throw new PSInvalidOperationException(String.Format("Adding to repository store failed: {0}", e.Message));
289290
}
290291

291-
bool isAllowed = GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled() ? GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(repoUri) : true;
292+
bool isAllowed = true;
293+
294+
if (OperatingSystem.IsWindows())
295+
{
296+
isAllowed = GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled() ? GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(repoUri) : true;
297+
}
292298

293299
return new PSRepositoryInfo(repoName, repoUri, repoPriority, repoTrusted, repoCredentialInfo, repoCredentialProvider, apiVersion, isAllowed);
294300
}
@@ -447,10 +453,13 @@ public static PSRepositoryInfo Update(string repoName, Uri repoUri, int repoPrio
447453
node.Attribute(PSCredentialInfo.SecretNameAttribute).Value);
448454
}
449455

450-
if (GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled())
456+
if (OperatingSystem.IsWindows())
451457
{
452-
var allowedList = GroupPolicyRepositoryEnforcement.GetAllowedRepositoryURIs();
458+
if (GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled())
459+
{
460+
var allowedList = GroupPolicyRepositoryEnforcement.GetAllowedRepositoryURIs();
453461

462+
}
454463
}
455464

456465
// Update CredentialProvider if necessary
@@ -468,7 +477,12 @@ public static PSRepositoryInfo Update(string repoName, Uri repoUri, int repoPrio
468477
}
469478
}
470479

471-
bool isAllowed = GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled() ? GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(thisUrl) : true;
480+
bool isAllowed = true;
481+
482+
if (OperatingSystem.IsWindows())
483+
{
484+
isAllowed = GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled() ? GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(thisUrl) : true;
485+
}
472486

473487
updatedRepo = new PSRepositoryInfo(repoName,
474488
thisUrl,
@@ -564,7 +578,12 @@ public static List<PSRepositoryInfo> Remove(string[] repoNames, out string[] err
564578
string attributeUrlUriName = urlAttributeExists ? "Url" : "Uri";
565579
Uri repoUri = new Uri(node.Attribute(attributeUrlUriName).Value);
566580

567-
bool isAllowed = GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled() ? GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(repoUri) : true;
581+
bool isAllowed = true;
582+
583+
if (OperatingSystem.IsWindows())
584+
{
585+
isAllowed = GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled() ? GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(repoUri) : true;
586+
}
568587

569588
removedRepos.Add(
570589
new PSRepositoryInfo(repo,
@@ -704,7 +723,11 @@ public static List<PSRepositoryInfo> Read(string[] repoNames, out string[] error
704723
continue;
705724
}
706725

707-
bool isAllowed = GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled() ? GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(thisUrl) : true;
726+
bool isAllowed = true;
727+
if (OperatingSystem.IsWindows())
728+
{
729+
isAllowed = GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled() ? GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(thisUrl) : true;
730+
}
708731

709732
PSRepositoryInfo currentRepoItem = new PSRepositoryInfo(repo.Attribute("Name").Value,
710733
thisUrl,
@@ -817,7 +840,12 @@ public static List<PSRepositoryInfo> Read(string[] repoNames, out string[] error
817840
continue;
818841
}
819842

820-
bool isAllowed = GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled() ? GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(thisUrl) : true;
843+
bool isAllowed = true;
844+
845+
if (OperatingSystem.IsWindows())
846+
{
847+
isAllowed = GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled() ? GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(thisUrl) : true;
848+
}
821849

822850
PSRepositoryInfo currentRepoItem = new PSRepositoryInfo(node.Attribute("Name").Value,
823851
thisUrl,

src/code/UpdateModuleManifest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ private void CreateModuleManifestHelper(Hashtable parsedMetadata, string resolve
604604
parsedMetadata["Prerelease"] = Prerelease;
605605
}
606606

607-
if (RequireLicenseAcceptance != null && RequireLicenseAcceptance.IsPresent)
607+
if (RequireLicenseAcceptance.IsPresent)
608608
{
609609
parsedMetadata["RequireLicenseAcceptance"] = RequireLicenseAcceptance;
610610
}
@@ -953,7 +953,7 @@ private void CreateModuleManifestForWinPSHelper(Hashtable parsedMetadata, string
953953
prerelease = Prerelease;
954954
}
955955

956-
if (RequireLicenseAcceptance != null && RequireLicenseAcceptance.IsPresent)
956+
if (RequireLicenseAcceptance.IsPresent)
957957
{
958958
requireLicenseAcceptance = RequireLicenseAcceptance;
959959
}

src/code/Utils.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,7 @@ public static bool ValidateModuleManifest(string moduleManifestPath, out string
14261426
return false;
14271427
}
14281428
}
1429-
1429+
14301430
// Check for any errors from Test-ModuleManifest
14311431
if (pwsh.HadErrors)
14321432
{
@@ -1653,7 +1653,9 @@ public static void DeleteDirectoryWithRestore(string dirPath)
16531653
}
16541654
catch (Exception e)
16551655
{
1656-
throw e;
1656+
throw new PSInvalidOperationException(
1657+
$"An error occurred while attempting to delete the directory at path {dirPath} with restore. Error: {e.Message}",
1658+
e);
16571659
}
16581660
finally
16591661
{
@@ -1684,7 +1686,7 @@ public static void DeleteDirectory(string dirPath)
16841686
{
16851687
if (!Directory.Exists(dirPath))
16861688
{
1687-
throw new Exception($"Path '{dirPath}' that was attempting to be deleted does not exist.");
1689+
throw new PSInvalidOperationException($"Path '{dirPath}' that was attempting to be deleted does not exist.");
16881690
}
16891691

16901692
// Remove read only file attributes first
@@ -1721,10 +1723,10 @@ public static void DeleteDirectory(string dirPath)
17211723
if (ex.Message.Contains("The directory is not empty") && psVersion.StartsWith("5"))
17221724
{
17231725
// there is a known bug with WindowsPowerShell and OneDrive based module paths, where .NET Directory.Delete() will throw a 'The directory is not empty.' error.
1724-
throw new Exception(string.Format("Cannot uninstall module with OneDrive based path on Windows PowerShell due to .NET issue. Try installing and uninstalling using PowerShell 7+ if using OneDrive."), ex);
1726+
throw new PSInvalidOperationException("Cannot uninstall module with OneDrive based path on Windows PowerShell due to .NET issue. Try installing and uninstalling using PowerShell 7+ if using OneDrive.", ex);
17251727
}
17261728

1727-
throw new Exception(string.Format("Access denied to path while deleting path {0}", dirPath), ex);
1729+
throw new PSInvalidOperationException(string.Format("Access denied to path while deleting path {0}", dirPath), ex);
17281730
}
17291731
else
17301732
{

0 commit comments

Comments
 (0)