Exemplo n.º 1
0
        internal static async Task <UniversalPackageVersion> GetVersionAsync(UniversalFeedClient client, UniversalPackageId id, string version, bool prerelease, CancellationToken cancellationToken)
        {
            if (!string.IsNullOrEmpty(version) && !string.Equals(version, "latest", StringComparison.OrdinalIgnoreCase) && !prerelease)
            {
                var parsed = UniversalPackageVersion.TryParse(version);
                if (parsed != null)
                {
                    return(parsed);
                }

                throw new UpackException($"Invalid UPack version number: {version}");
            }

            IReadOnlyList <RemoteUniversalPackageVersion> versions;

            try
            {
                versions = await client.ListPackageVersionsAsync(id, false, null, cancellationToken);
            }
            catch (WebException ex)
            {
                throw ConvertWebException(ex);
            }

            if (!versions.Any())
            {
                throw new UpackException($"No versions of package {id} found.");
            }

            return(versions.Max(v => v.Version));
        }
        public override async Task <IEnumerable <string> > EnumerateValuesAsync(ValueEnumerationContext context)
        {
            var client = new UniversalFeedClient(this.GetEndpoint());

            return((await client.ListPackagesAsync(this.Group, 100).ConfigureAwait(false))
                   // additional level of filtering in case of bugs in server
                   .Where(p => string.IsNullOrEmpty(this.Group) || string.Equals(this.Group, p.Group, StringComparison.OrdinalIgnoreCase))
                   .OrderBy(p => p.Name)
                   .Select(p => p.Name));
        }
        public override async Task <IEnumerable <string> > EnumerateValuesAsync(ValueEnumerationContext context)
        {
            var id = UniversalPackageId.Parse(this.PackageId);

            var client = new UniversalFeedClient(this.GetEndpoint());

            return((await client.ListPackageVersionsAsync(id, false).ConfigureAwait(false))
                   .Where(p => this.IncludePrerelease || string.IsNullOrEmpty(p.Version.Prerelease))
                   .OrderByDescending(p => p.Version)
                   .Select(p => p.Version.ToString()));
        }
Exemplo n.º 4
0
        public override async Task ExecuteAsync(IOperationExecutionContext context)
        {
            if (string.IsNullOrWhiteSpace(this.PackageFile))
            {
                if (string.IsNullOrWhiteSpace(this.FeedUrl))
                {
                    this.LogError("FeedUrl is required if PackageFile is not specified.");
                    return;
                }

                if (string.IsNullOrWhiteSpace(this.PackageName))
                {
                    this.LogError("Name is required if PackageFile is not specified.");
                    return;
                }

                var endpoint = new UniversalFeedEndpoint(new Uri(this.FeedUrl), this.UserName, this.Password);

                this.LogInformation($"Getting package information for {this.PackageName} from {endpoint}...");
                var client   = new UniversalFeedClient(endpoint);
                var versions = await client.ListPackageVersionsAsync(UniversalPackageId.Parse(this.PackageName));

                this.LogDebug($"Server return info for {versions.Count} packages.");

                RemoteUniversalPackageVersion package;
                if (!string.IsNullOrWhiteSpace(this.PackageVersion))
                {
                    this.LogDebug($"Checking for {this.PackageVersion} in result set...");
                    var parsedVersion = UniversalPackageVersion.Parse(this.PackageVersion);
                    package = versions.FirstOrDefault(p => p.Version == parsedVersion);
                    if (package != null)
                    {
                        this.LogInformation($"Package {this.PackageName} {this.PackageVersion} found.");
                    }
                    else
                    {
                        this.LogInformation($"Package {this.PackageName} {this.PackageVersion} not found.");
                    }
                }
                else
                {
                    if (versions.Count > 0)
                    {
                        this.LogDebug($"Determining latest version of {this.PackageName}...");
                        package = versions.Aggregate((p1, p2) => p1.Version >= p2.Version ? p1 : p2);
                        this.LogInformation($"Latest version of {this.PackageName} is {package.Version}.");
                    }
                    else
                    {
                        this.LogInformation($"Package {this.PackageName} not found.");
                        package = null;
                    }
                }

                if (package != null)
                {
                    this.Exists   = true;
                    this.Metadata = this.Convert(package.AllProperties).AsDictionary();
                }
                else
                {
                    this.Exists = false;
                }
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(this.FeedUrl))
                {
                    this.LogWarning("FeedUrl is ignored when PackageFile is specified.");
                }

                if (!string.IsNullOrWhiteSpace(this.PackageName))
                {
                    this.LogError("Name is ignored when PackageFile is specified.");
                }

                if (!string.IsNullOrWhiteSpace(this.PackageVersion))
                {
                    this.LogError("Version is ignored when PackageFile is specified.");
                }

                var fileOps = await context.Agent.GetServiceAsync <IFileOperationsExecuter>();

                var fullPath = context.ResolvePath(this.PackageFile);

                this.LogInformation($"Reading {fullPath}...");

                if (await fileOps.FileExistsAsync(fullPath))
                {
                    this.LogDebug("Package file exists; reading metadata...");
                    UniversalPackageMetadata metadata;
                    try
                    {
                        using (var stream = await fileOps.OpenFileAsync(fullPath, FileMode.Open, FileAccess.Read))
                            using (var package = new UniversalPackage(stream))
                            {
                                metadata = package.GetFullMetadata();
                            }
                    }
                    catch (Exception ex)
                    {
                        this.LogError("Error reading package: " + ex);
                        return;
                    }

                    this.Exists   = true;
                    this.Metadata = this.Convert(metadata).AsDictionary();
                }
                else
                {
                    this.LogInformation("Package file not found.");
                    this.Exists = false;
                }
            }
        }
Exemplo n.º 5
0
        private async Task <Stream> FetchPackageStreamAsync(ArgList args, CancellationToken cancellationToken)
        {
            if (this.PackageId != null && this.PackageVersion != null)
            {
                var cachedStream = await tryGetCached();

                if (cachedStream != null)
                {
                    return(cachedStream);
                }
            }

            if (this.FileName != null)
            {
                if (File.Exists(this.FileName))
                {
                    FileStream fileStream;
                    try
                    {
                        fileStream = new FileStream(this.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                    }
                    catch (IOException)
                    {
                        goto FileNotFound;
                    }

                    this.VerifyAndPopulate(fileStream);
                    fileStream.Position = 0;

                    await addToCache(fileStream);

                    fileStream.Position = 0;
                    return(fileStream);
                }

FileNotFound:
                throw new RompException("File not found: " + this.FileName);
            }

            if (this.Source != null)
            {
                var client = new UniversalFeedClient(this.Source);
                RemoteUniversalPackageVersion package;

                if (this.PackageVersion == null)
                {
                    package = (await client.ListPackageVersionsAsync(this.PackageId, false, null, cancellationToken))
                              .OrderByDescending(v => v.Version)
                              .FirstOrDefault();
                    if (package == null)
                    {
                        throw new RompException($"Package {this.PackageId} not found at the specified source.");
                    }
                }
                else
                {
                    package = await client.GetPackageVersionAsync(this.PackageId, this.PackageVersion, false, cancellationToken);

                    if (package == null)
                    {
                        throw new RompException($"Package {this.PackageId} version {this.PackageVersion} not found at the specified source.");
                    }
                }

                using (var remoteStream = await client.GetPackageStreamAsync(package.FullName, package.Version, cancellationToken))
                {
                    if (remoteStream == null)
                    {
                        throw new RompException($"Package {package.FullName} version {package.Version} not found at the specified source.");
                    }

                    var tempStream = TemporaryStream.Create(package.Size);
                    try
                    {
                        await remoteStream.CopyToAsync(tempStream, 81920, cancellationToken);

                        tempStream.Position = 0;

                        this.VerifyAndPopulate(tempStream);
                        tempStream.Position = 0;

                        await addToCache(tempStream);

                        tempStream.Position = 0;

                        return(tempStream);
                    }
                    catch
                    {
                        tempStream.Dispose();
                        throw;
                    }
                }
            }

            throw new InvalidOperationException("Invalid package specification.");

            async Task <Stream> tryGetCached()
            {
                return(await PackageRegistry.GetRegistry(RompConfig.UserMode).TryOpenFromCacheAsync(
                           this.PackageId,
                           this.PackageVersion
                           ));
            }

            async Task addToCache(Stream stream)
            {
                try
                {
                    using (var registry = PackageRegistry.GetRegistry(RompConfig.UserMode))
                    {
                        await registry.WriteToCacheAsync(this.PackageId, this.PackageVersion, stream);
                    }
                }
                catch (Exception ex)
                {
                    throw new RompException($"Could not add package to cache: " + ex.Message, ex);
                }
            }
        }