Exemple #1
0
        public Task <IEnumerable <Package> > QueryPackages(string query, PkgFilter pkgFilter, CollectionFilter collectionFilter, string location)
        {
            string localPath = null;

            // there are three possibilities:
            // 1. The parameter is a file on disk, or a remote file.
            try {
                // some kind of local file?
                if (File.Exists(query))
                {
                    return(PackagesFromLocalFile(query.EnsureFileIsLocal(), Path.GetDirectoryName(query.GetFullPath()), pkgFilter, collectionFilter));
                }

                var uri = new Uri(query);
                if (uri.IsWebUri())
                {
                    Task.Factory.StartNew(() => {
                        var lp         = uri.AbsolutePath.MakeSafeFileName().GenerateTemporaryFilename();
                        var remoteFile = new RemoteFile(uri, lp, itemUri => {
                            Event <DownloadCompleted> .Raise(uri.AbsolutePath, lp); // got the file!
                            localPath = lp;
                        },
                                                        itemUri => {
                            localPath = null;     // failed
                        },
                                                        (itemUri, percent) => Event <DownloadProgress> .Raise(uri.AbsolutePath, localPath, percent));
                        remoteFile.Get();

                        if (localPath != null)
                        {
                            return(PackagesFromLocalFile(localPath, null, pkgFilter, collectionFilter).Result);
                        }
                        return(Enumerable.Empty <Package>());
                    });
                }
            }
            catch {
                // ignore what can't be fixed
            }

            // 2. A directory path or filemask (ie, creating a one-time 'feed' )
            if (Directory.Exists(query) || query.IndexOf('\\') > -1 || query.IndexOf('/') > -1 || (query.IndexOf('*') > -1 && query.ToLower().EndsWith(".msi")))
            {
                // specified a folder, or some kind of path that looks like a feed.
                // add it as a feed, and then get the contents of that feed.
                return((Remote.AddFeed(query, true) as Task <PackageManagerResponseImpl>).Continue(response => {
                    // a feed!
                    if (response.Feeds.Any())
                    {
                        return (Remote.FindPackages(null, pkgFilter, collectionFilter, response.Feeds.First().Location) as Task <PackageManagerResponseImpl>).Continue(response1 => response1.Packages).Result;
                    }

                    query = query.GetFullPath();
                    return (Remote.AddFeed(query, true) as Task <PackageManagerResponseImpl>).Continue(response2 => {
                        if (response.Feeds.Any())
                        {
                            return (Remote.FindPackages(null, pkgFilter, collectionFilter, response2.Feeds.First().Location) as Task <PackageManagerResponseImpl>).Continue(response1 => response1.Packages).Result;
                        }
                        return Enumerable.Empty <Package>();
                    }).Result;
                }));
            }

            // 3. A partial/canonical name of a package.
            return((Remote.FindPackages(query, pkgFilter, collectionFilter, location) as Task <PackageManagerResponseImpl>).Continue(response => response.Packages));
        }