Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 123 additions & 114 deletions src/code/FindHelper.cs

Large diffs are not rendered by default.

68 changes: 54 additions & 14 deletions src/code/FindPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using Microsoft.PowerShell.PowerShellGet.UtilClasses;
using NuGet.Versioning;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -30,7 +31,6 @@ public sealed class FindPSResource : PSCmdlet
private const string NameParameterSet = "NameParameterSet";
private const string CommandNameParameterSet = "CommandNameParameterSet";
private const string DscResourceNameParameterSet = "DscResourceNameParameterSet";
private const string TagParameterSet = "TagParameterSet";
private CancellationTokenSource _cancellationTokenSource;
private FindHelper _findHelper;

Expand All @@ -50,10 +50,9 @@ public sealed class FindPSResource : PSCmdlet

/// <summary>
/// Specifies one or more resource types to find.
/// Resource types supported are: Module, Script, Command, DscResource
/// Resource types supported are: Module, Script
/// </summary>
[Parameter(ParameterSetName = NameParameterSet)]
[Parameter(ParameterSetName = TagParameterSet)]
public ResourceType Type { get; set; }

/// <summary>
Expand Down Expand Up @@ -86,7 +85,7 @@ public sealed class FindPSResource : PSCmdlet
/// <summary>
/// Filters search results for resources that include one or more of the specified tags.
/// </summary>
[Parameter(ParameterSetName = TagParameterSet)]
[Parameter(ParameterSetName = NameParameterSet)]
[ValidateNotNull]
public string[] Tag { get; set; }

Expand Down Expand Up @@ -157,10 +156,6 @@ protected override void ProcessRecord()
ProcessCommandOrDscParameterSet(isSearchingForCommands: false);
break;

case TagParameterSet:
ProcessTagParameterSet();
break;

default:
Dbg.Assert(false, "Invalid parameter set");
break;
Expand All @@ -173,20 +168,28 @@ protected override void ProcessRecord()

private void ProcessResourceNameParameterSet()
{
// only cases where Name is allowed to not be specified is if Type or Tag parameters are
if (!MyInvocation.BoundParameters.ContainsKey(nameof(Name)))
{
// only cases where Name is allowed to not be specified is if Type or Tag parameters are
if (!MyInvocation.BoundParameters.ContainsKey(nameof(Type)) && !MyInvocation.BoundParameters.ContainsKey(nameof(Tag)))
if (MyInvocation.BoundParameters.ContainsKey(nameof(Tag)))
{
// case where Name specified: false, Tag specified: true (i.e just search by Tags)
ProcessTagParameterSet(); // TODO: rename
return;
}
else if (MyInvocation.BoundParameters.ContainsKey(nameof(Type)))
{
Name = new string[] {"*"};
}
else
{
ThrowTerminatingError(
new ErrorRecord(
new PSInvalidOperationException("Name parameter must be provided."),
new PSInvalidOperationException("Name parameter must be provided, unless Tag or Type parameters are used."),
"NameParameterNotProvided",
ErrorCategory.InvalidOperation,
this));
}

Name = new string[] {"*"};
}

Name = Utils.ProcessNameWildcards(Name, removeWildcardEntries:false, out string[] errorMsgs, out bool nameContainsWildcard);
Expand All @@ -205,11 +208,48 @@ private void ProcessResourceNameParameterSet()
if (Name.Length == 0)
{
return;
}
}

// determine/parse out Version param
VersionType versionType = VersionType.VersionRange;
NuGetVersion nugetVersion = null;
VersionRange versionRange = null;

if (Version != null)
{
if (!NuGetVersion.TryParse(Version, out nugetVersion))
{
if (Version.Trim().Equals("*"))
{
versionRange = VersionRange.All;
versionType = VersionType.VersionRange;
}
else if (!VersionRange.TryParse(Version, out versionRange))
{
WriteError(new ErrorRecord(
new ArgumentException("Argument for -Version parameter is not in the proper format"),
"IncorrectVersionFormat",
ErrorCategory.InvalidArgument,
this));
return;
}
}
else
{
versionType = VersionType.SpecificVersion;
}
}
else
{
versionType = VersionType.NoVersion;
}

foreach (PSResourceInfo pkg in _findHelper.FindByResourceName(
name: Name,
type: Type,
versionRange: versionRange,
nugetVersion: nugetVersion,
versionType: versionType,
version: Version,
prerelease: Prerelease,
tag: Tag,
Expand Down
7 changes: 6 additions & 1 deletion src/code/IServerAPICalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public interface IServerAPICalls
/// - Include prerelease: http://www.powershellgallery.com/api/v2/FindPackagesById()?id='PowerShellGet'
/// Implementation Note: Need to filter further for latest version (prerelease or non-prerelease dependening on user preference)
/// </summary>
/// // TODO: change repository from string to PSRepositoryInfo
string FindName(string packageName, bool includePrerelease, ResourceType type, out ExceptionDispatchInfo edi);

string FindNameWithTag(string packageName, string[] tags, bool includePrerelease, ResourceType type, out ExceptionDispatchInfo edi);

/// <summary>
/// Find method which allows for searching for single name with version range.
/// Name: no wildcard support
Expand All @@ -48,6 +49,8 @@ public interface IServerAPICalls
/// </summary>
string[] FindNameGlobbing(string packageName, bool includePrerelease, ResourceType type, out ExceptionDispatchInfo edi);

string[] FindNameGlobbingWithTag(string packageName, string[] tags, bool includePrerelease, ResourceType type, out ExceptionDispatchInfo edi);

/// <summary>
/// Find method which allows for searching for single name with specific version.
/// Name: no wildcard support
Expand All @@ -65,6 +68,8 @@ public interface IServerAPICalls
/// API call: http://www.powershellgallery.com/api/v2/Packages(Id='PowerShellGet', Version='2.2.5')
/// </summary>
string FindVersion(string packageName, string version, ResourceType type, out ExceptionDispatchInfo edi);

string FindVersionWithTag(string packageName, string version, string[] tags, ResourceType type, out ExceptionDispatchInfo edi);


/// <summary>
Expand Down
20 changes: 20 additions & 0 deletions src/code/InstallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ internal class InstallHelper
private readonly PSCmdlet _cmdletPassedIn;
private List<string> _pathsToInstallPkg;
private VersionRange _versionRange;
private NuGetVersion _nugetVersion;
private VersionType _versionType;
private string _versionString;
private bool _prerelease;
private bool _acceptLicense;
Expand Down Expand Up @@ -126,6 +128,21 @@ public IEnumerable<PSResourceInfo> InstallPackages(
_pathsToInstallPkg = pathsToInstallPkg;
_tmpPath = tmpPath ?? Path.GetTempPath();

bool parsedAsNuGetVersion = NuGetVersion.TryParse(_versionString, out _nugetVersion);
if (parsedAsNuGetVersion)
{
_versionType = VersionType.SpecificVersion;
}
else if (!parsedAsNuGetVersion && _versionRange == null)
{
_versionType = VersionType.NoVersion;
}
else
{
// versionRange != null
_versionType = VersionType.VersionRange;
}

// Create list of installation paths to search.
_pathsToSearch = new List<string>();
_pkgNamesToInstall = names.ToList();
Expand Down Expand Up @@ -242,6 +259,9 @@ private List<PSResourceInfo> ProcessRepositories(
List<PSResourceInfo> pkgsFromRepoToInstall = findHelper.FindByResourceName(
name: _pkgNamesToInstall.ToArray(),
type: ResourceType.None,
versionRange: _versionRange,
nugetVersion: _nugetVersion,
versionType: _versionType,
version: _versionRange?.OriginalString,
prerelease: _prerelease,
tag: null,
Expand Down
10 changes: 9 additions & 1 deletion src/code/PSGetException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ public JsonParsingException(string message)
{
}
}
}

public class SpecifiedTagsNotFoundException : Exception
{
public SpecifiedTagsNotFoundException(string message)
: base (message)
{
}
}
}
38 changes: 29 additions & 9 deletions src/code/PublishPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -765,23 +765,43 @@ private bool CheckDependenciesExist(Hashtable dependencies, string repositoryNam
depVersion = string.IsNullOrWhiteSpace(depVersion) ? "*" : depVersion;

VersionRange versionRange = null;
if (!Utils.TryParseVersionOrVersionRange(depVersion, out versionRange))
VersionType versionType = VersionType.VersionRange;
NuGetVersion nugetVersion = null;

if (depVersion != null)
{
if (!NuGetVersion.TryParse(depVersion, out nugetVersion))
{
if (depVersion.Trim().Equals("*"))
{
versionRange = VersionRange.All;
versionType = VersionType.VersionRange;
}
else if (!VersionRange.TryParse(depVersion, out versionRange))
{
ThrowTerminatingError(new ErrorRecord(
new ArgumentException("Argument for -Version parameter is not in the proper format"),
"IncorrectVersionFormat",
ErrorCategory.InvalidArgument,
this));
}
}
else
{
versionType = VersionType.SpecificVersion;
}
}
else
{
// This should never be true because Test-ModuleManifest will throw an error if dependency versions are incorrectly formatted
// This is being left as a safeguard for parsing a version from a string to a version range.
ThrowTerminatingError(new ErrorRecord(
new ArgumentException(string.Format("Error parsing dependency version {0}, from the module {1}", depVersion, depName)),
"IncorrectVersionFormat",
ErrorCategory.InvalidArgument,
this));
versionType = VersionType.NoVersion;
}

// Search for and return the dependency if it's in the repository.
FindHelper findHelper = new FindHelper(_cancellationToken, this, _networkCredential);
bool depPrerelease = depVersion.Contains("-");

var repository = new[] { repositoryName };
var dependencyFound = findHelper.FindByResourceName(depName, ResourceType.Module, depVersion, depPrerelease, null, repository, false);
var dependencyFound = findHelper.FindByResourceName(depName, ResourceType.Module, versionRange, nugetVersion, versionType, depVersion, depPrerelease, null, repository, false);
if (dependencyFound == null || !dependencyFound.Any())
{
var message = String.Format("Dependency '{0}' was not found in repository '{1}'. Make sure the dependency is published to the repository before publishing this module.", dependency, repositoryName);
Expand Down
6 changes: 6 additions & 0 deletions src/code/ServerApiCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public ServerApiCall(PSRepositoryInfo repository, NetworkCredential networkCrede
/// </summary>
public abstract string FindName(string packageName, bool includePrerelease, ResourceType type, out ExceptionDispatchInfo edi);

public abstract string FindNameWithTag(string packageName, string[] tags, bool includePrerelease, ResourceType type, out ExceptionDispatchInfo edi);

/// <summary>
/// Find method which allows for searching for single name with wildcards and returns latest version.
/// Name: supports wildcards
Expand All @@ -73,6 +75,8 @@ public ServerApiCall(PSRepositoryInfo repository, NetworkCredential networkCrede
/// Implementation Note: filter additionally and verify ONLY package name was a match.
/// </summary>
public abstract string[] FindNameGlobbing(string packageName, bool includePrerelease, ResourceType type, out ExceptionDispatchInfo edi);

public abstract string[] FindNameGlobbingWithTag(string packageName, string[] tags, bool includePrerelease, ResourceType type, out ExceptionDispatchInfo edi);
/// <summary>
/// Find method which allows for searching for single name with version range.
/// Name: no wildcard support
Expand All @@ -93,6 +97,8 @@ public ServerApiCall(PSRepositoryInfo repository, NetworkCredential networkCrede
/// </summary>
public abstract string FindVersion(string packageName, string version, ResourceType type, out ExceptionDispatchInfo edi);

public abstract string FindVersionWithTag(string packageName, string version, string[] tags, ResourceType type, out ExceptionDispatchInfo edi);

/** INSTALL APIS **/

/// <summary>
Expand Down
50 changes: 36 additions & 14 deletions src/code/UpdatePSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,41 @@ protected override void BeginProcessing()

protected override void ProcessRecord()
{
VersionRange versionRange;
// determine/parse out Version param
VersionType versionType = VersionType.VersionRange;
NuGetVersion nugetVersion = null;
VersionRange versionRange = null;

// handle case where Version == null
if (Version == null) {
versionRange = VersionRange.All;
if (Version != null)
{
if (!NuGetVersion.TryParse(Version, out nugetVersion))
{
if (Version.Trim().Equals("*"))
{
versionRange = VersionRange.All;
versionType = VersionType.VersionRange;
}
else if (!VersionRange.TryParse(Version, out versionRange))
{
WriteError(new ErrorRecord(
new ArgumentException("Argument for -Version parameter is not in the proper format"),
"IncorrectVersionFormat",
ErrorCategory.InvalidArgument,
this));
return;
}
}
else
{
versionType = VersionType.SpecificVersion;
}
}
else if (!Utils.TryParseVersionOrVersionRange(Version, out versionRange))
else
{
// Only returns false if the range was incorrectly formatted and couldn't be parsed.
WriteError(new ErrorRecord(
new PSInvalidOperationException("Cannot parse Version parameter provided into VersionRange"),
"ErrorParsingVersionParamIntoVersionRange",
ErrorCategory.InvalidArgument,
this));
return;
versionType = VersionType.NoVersion;
}

var namesToUpdate = ProcessPackageNames(Name, versionRange);
var namesToUpdate = ProcessPackageNames(Name, versionRange, nugetVersion, versionType);

if (namesToUpdate.Length == 0)
{
Expand Down Expand Up @@ -253,7 +270,9 @@ protected override void EndProcessing()
/// </Summary>
private string[] ProcessPackageNames(
string[] namesToProcess,
VersionRange versionRange)
VersionRange versionRange,
NuGetVersion nuGetVersion,
VersionType versionType)
{
namesToProcess = Utils.ProcessNameWildcards(
pkgNames: namesToProcess,
Expand Down Expand Up @@ -310,6 +329,9 @@ private string[] ProcessPackageNames(
foreach (var foundResource in _findHelper.FindByResourceName(
name: installedPackages.Keys.ToArray(),
type: ResourceType.None,
versionRange: versionRange,
nugetVersion: nuGetVersion,
versionType: versionType,
version: Version,
prerelease: Prerelease,
tag: null,
Expand Down
7 changes: 7 additions & 0 deletions src/code/V2ResponseUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(string[]
foreach (string response in responses)
{
var elemList = ConvertResponseToXML(response);
if (elemList.Length == 0)
{
// this indicates we got a non-empty, XML response (as noticed for V2 server) but it's not a response that's meaningful (contains 'properties')
string errorMsg = $"Response didn't contain properties element";
yield return new PSResourceResult(returnedObject: null, errorMsg: errorMsg, isTerminatingError: false);
}

foreach (var element in elemList)
{
if (!PSResourceInfo.TryConvertFromXml(element, out PSResourceInfo psGetInfo, repository.Name, out string errorMsg))
Expand Down
Loading