diff --git a/src/code/FindHelper.cs b/src/code/FindHelper.cs index 966429525..dd423c6b6 100644 --- a/src/code/FindHelper.cs +++ b/src/code/FindHelper.cs @@ -29,6 +29,8 @@ internal class FindHelper private CancellationToken _cancellationToken; private readonly PSCmdlet _cmdletPassedIn; private List _pkgsLeftToFind; + private List _tagsLeftToFind; + private ResourceType _type; private string _version; private SwitchParameter _prerelease = false; @@ -246,7 +248,7 @@ public List FindTag( _credential = credential; List foundPackages = new List(); - List tagsLeftToFind = new List(tag); + _tagsLeftToFind = new List(tag); if (tag.Length == 0) { @@ -292,18 +294,30 @@ public List FindTag( return foundPackages; } - for (int i = 0; i < repositoriesToSearch.Count && tagsLeftToFind.Any(); i++) + for (int i = 0; i < repositoriesToSearch.Count && _tagsLeftToFind.Any(); i++) { + if (_type != ResourceType.None && repositoriesToSearch[i].Name != "PSGallery") + { + _cmdletPassedIn.ThrowTerminatingError(new ErrorRecord( + new PSInvalidOperationException("-Type parameter is only supported with the PowerShellGallery."), + "ErrorUsingTypeParameter", + ErrorCategory.InvalidOperation, + this)); + } + _cmdletPassedIn.WriteVerbose(string.Format("Searching in repository {0}", repositoriesToSearch[i].Name)); if (repositoriesToSearch[i].ApiVersion == PSRepositoryInfo.APIVersion.v2) - { - // tag1, tag2, tag3 - + { // TODO: didn't really finsh come back here foreach (PSResourceInfo cmdInfo in HttpSearchFromRepository(repositoriesToSearch[i])) { foundPackages.Add(cmdInfo); - } + } + } + if (repositoriesToSearch[i].ApiVersion == PSRepositoryInfo.APIVersion.v3) + { + // TODO: implement this when we work on v3 requests + break; } } @@ -321,11 +335,17 @@ private IEnumerable HttpSearchFromRepository(PSRepositoryInfo re // -Name parameter and -Tag parameter are exclusive if (_tag != null) { + HashSet tagsFound; // TODO: this is currently very buggy and the url queries need to be fixed - foreach (PSResourceInfo pkgs in HttpFindTags(repositoryInfo, _type)) + foreach (PSResourceInfo pkgs in HttpFindTags(repositoryInfo, _type, out tagsFound)) { yield return pkgs; } + + foreach(string tag in tagsFound) + { + _tagsLeftToFind.Remove(tag); + } } else if (_pkgsLeftToFind.Count > 0) { @@ -337,7 +357,6 @@ private IEnumerable HttpSearchFromRepository(PSRepositoryInfo re yield break; } - // TODO: ADD command search, dscresource search, etc. } @@ -850,9 +869,9 @@ private bool IsTagMatch(PSResourceInfo pkg) return _tag.Intersect(pkg.Tags, StringComparer.InvariantCultureIgnoreCase).ToList().Count > 0; } - private PSResourceInfo[] HttpFindTags(PSRepositoryInfo repository, ResourceType type) + private PSResourceInfo[] HttpFindTags(PSRepositoryInfo repository, ResourceType type, out HashSet tagsFound) { - return _httpFindPSResource.FindTags(_tag, repository, _prerelease, type, out string errRecord); + return _httpFindPSResource.FindTags(_tag, repository, _prerelease, type, out tagsFound, out string errRecord); // TODO: write out error } diff --git a/src/code/FindPSResource.cs b/src/code/FindPSResource.cs index 1bf918563..5f8393571 100644 --- a/src/code/FindPSResource.cs +++ b/src/code/FindPSResource.cs @@ -285,15 +285,12 @@ private void ProcessTagParameterSet() return; } - List foundPackages = _findHelper.FindByResourceName( - name: Utils.EmptyStrArray, + List foundPackages = _findHelper.FindTag( type: Type, - version: Version, prerelease: Prerelease, tag: tagsToSearch, repository: Repository, - credential: Credential, - includeDependencies: IncludeDependencies); + credential: Credential); foreach (var package in foundPackages) { diff --git a/src/code/HttpFindPSResource.cs b/src/code/HttpFindPSResource.cs index b0bfb1698..80c6deedb 100644 --- a/src/code/HttpFindPSResource.cs +++ b/src/code/HttpFindPSResource.cs @@ -58,12 +58,13 @@ public PSResourceInfo FindAll(PSRepositoryInfo repository, bool includePrereleas /// - No prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsLatestVersion&searchTerm='tag:JSON' /// - Include prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsAbsoluteLatestVersion&searchTerm='tag:JSON'&includePrerelease=true /// - public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, ResourceType type, out string errRecord) + public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, ResourceType type, out HashSet tagsFound, out string errRecord) { errRecord = String.Empty; List pkgsFound = new List(); HashSet tagPkgs = new HashSet(); - + tagsFound = new HashSet(); + // TAG example: // chocolatey, crescendo // > chocolatey === ModuleA @@ -74,8 +75,6 @@ public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, boo { string[] responses = v2ServerAPICall.FindTag(tag, repository, includePrerelease, type, out errRecord); - // TODO: map the tag with the package which the tag came from - foreach (string response in responses) { var elemList = ConvertResponseToXML(response); @@ -93,6 +92,7 @@ public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, boo { tagPkgs.Add(psGetInfo.Name); pkgsFound.Add(psGetInfo); + tagsFound.Add(tag); } else { @@ -102,7 +102,7 @@ public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, boo errRecord = errorMsg; } } - } + } } return pkgsFound.ToArray(); diff --git a/src/code/IFindPSResource.cs b/src/code/IFindPSResource.cs index 4594d2956..6c5dbcddb 100644 --- a/src/code/IFindPSResource.cs +++ b/src/code/IFindPSResource.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using Microsoft.PowerShell.PowerShellGet.UtilClasses; using NuGet.Versioning; @@ -21,7 +22,7 @@ public interface IFindPSResource /// - No prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsLatestVersion&searchTerm='tag:JSON' /// - Include prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsAbsoluteLatestVersion&searchTerm='tag:JSON'&includePrerelease=true /// - PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, ResourceType type, out string errRecord); + PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, ResourceType type, out HashSet tagsFound, out string errRecord); /// /// Find method which allows for searching for packages with resource type specified from a repository and returns latest version for each.