Beispiel #1
0
 private async Task <UniversalPackageMetadata> GetMetadataToMergeAsync()
 {
     if (string.IsNullOrWhiteSpace(this.Manifest))
     {
         return(new UniversalPackageMetadata
         {
             Group = this.Group,
             Name = this.Name,
             Version = UniversalPackageVersion.TryParse(this.NewVersion),
             Title = this.Title,
             Description = this.PackageDescription,
             Icon = this.IconUrl
         });
     }
     else
     {
         try
         {
             using (var metadataStream = File.OpenRead(this.Manifest))
             {
                 return(await ReadManifestAsync(metadataStream));
             }
         }
         catch (Exception ex)
         {
             throw new UpackException($"The manifest file '{this.Manifest}' does not exist or could not be opened.", ex);
         }
     }
 }
Beispiel #2
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));
        }
Beispiel #3
0
        public override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            var client = CreateClient(this.SourceUrl, this.Authentication);

            UniversalPackageId packageId;

            try
            {
                packageId = UniversalPackageId.Parse(this.PackageName);
            }
            catch (ArgumentException ex)
            {
                throw new UpackException("Invalid package ID: " + ex.Message, ex);
            }

            UniversalPackageVersion version = null;

            if (!string.IsNullOrEmpty(this.Version))
            {
                version = UniversalPackageVersion.TryParse(this.Version);
                if (version == null)
                {
                    throw new UpackException($"Invalid UPack version number: {this.Version}");
                }
            }

            JObject data;

            using (var stream = await client.GetPackageFileStreamAsync(packageId, version, string.IsNullOrEmpty(this.FilePath) ? "upack.json" : this.FilePath, cancellationToken))
                using (var reader = new StreamReader(stream, Encoding.UTF8, true, 4096, true))
                    using (var jsonReader = new JsonTextReader(reader)
                    {
                        CloseInput = false
                    })
                    {
                        data = await JObject.LoadAsync(jsonReader, cancellationToken);
                    }

            foreach (var p in data.Properties())
            {
                Console.WriteLine($"{p.Name} = {p.Value}");
            }

            return(0);
        }
Beispiel #4
0
        public override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            var client = CreateClient(this.SourceUrl, this.Authentication);

            UniversalPackageId packageId;

            try
            {
                packageId = UniversalPackageId.Parse(this.PackageName);
            }
            catch (ArgumentException ex)
            {
                throw new UpackException("Invalid package ID: " + ex.Message, ex);
            }

            UniversalPackageVersion version = null;

            if (!string.IsNullOrEmpty(this.Version))
            {
                version = UniversalPackageVersion.TryParse(this.Version);
                if (version == null)
                {
                    throw new UpackException($"Invalid UPack version number: {this.Version}");
                }
            }

            JObject data;

            try
            {
                using (var stream = await client.GetPackageFileStreamAsync(packageId, version, string.IsNullOrEmpty(this.FilePath) ? "upack.json" : this.FilePath, cancellationToken))
                    using (var reader = new StreamReader(stream, Encoding.UTF8, true, 4096, true))
                        using (var jsonReader = new JsonTextReader(reader)
                        {
                            CloseInput = false
                        })
                        {
                            data = await JObject.LoadAsync(jsonReader, cancellationToken);
                        }
            }
            catch (WebException ex) when(ex.Response is HttpWebResponse r)
            {
                var error = $"Server returned {(int)r.StatusCode}: ";

                if (string.Equals(r.ContentType, "text/plain", StringComparison.OrdinalIgnoreCase))
                {
                    using (var reader = new StreamReader(r.GetResponseStream(), Encoding.UTF8))
                    {
                        var buffer = new char[1000];
                        reader.Read(buffer, 0, buffer.Length);
                        error += new string(buffer);
                    }
                }
                else
                {
                    error += r.StatusDescription;
                }

                throw new UpackException(error, ex);
            }

            foreach (var p in data.Properties())
            {
                Console.WriteLine($"{p.Name} = {p.Value}");
            }

            return(0);
        }
Beispiel #5
0
        public override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            if (this.NoAudit && !string.IsNullOrEmpty(this.Note))
            {
                Console.Error.WriteLine("--no-audit cannot be used with --note.");
                return(2);
            }

            UniversalPackageMetadata info;

            if (string.IsNullOrWhiteSpace(this.Manifest))
            {
                info = new UniversalPackageMetadata
                {
                    Group       = this.Group,
                    Name        = this.Name,
                    Version     = UniversalPackageVersion.TryParse(this.Version),
                    Title       = this.Title,
                    Description = this.PackageDescription,
                    Icon        = this.IconUrl
                };
            }
            else
            {
                if (!File.Exists(this.Manifest))
                {
                    Console.Error.WriteLine($"The manifest file '{this.Manifest}' does not exist.");
                    return(2);
                }

                using (var metadataStream = File.OpenRead(this.Manifest))
                {
                    info = await ReadManifestAsync(metadataStream);
                }
            }

            var error = ValidateManifest(info);

            if (error != null)
            {
                Console.Error.WriteLine("Invalid {0}: {1}", string.IsNullOrWhiteSpace(this.Manifest) ? "parameters" : "upack.json", error);
                return(2);
            }

            PrintManifest(info);

            if (!this.NoAudit)
            {
                info["createdDate"] = DateTime.UtcNow.ToString("u");
                if (!string.IsNullOrEmpty(this.Note))
                {
                    info["createdReason"] = this.Note;
                }
                info["createdUsing"] = "upack/" + typeof(Pack).Assembly.GetName().Version.ToString(3);
                info["createdBy"]    = Environment.UserName;
            }

            if (!Directory.Exists(this.SourcePath) && !File.Exists(this.SourcePath))
            {
                Console.Error.WriteLine($"The source directory '{this.SourcePath}' does not exist.");
                return(2);
            }

            string relativePackageFileName = $"{info.Name}-{info.Version.Major}.{info.Version.Minor}.{info.Version.Patch}.upack";
            string targetFileName          = Path.Combine(this.TargetDirectory ?? Environment.CurrentDirectory, relativePackageFileName);

            if (File.Exists(Path.Combine(this.SourcePath, relativePackageFileName)))
            {
                Console.Error.WriteLine("Warning: output file already exists in source directory and may be included inadvertently in the package contents.");
            }

            string tmpPath = Path.GetTempFileName();

            using (var builder = new UniversalPackageBuilder(tmpPath, info))
            {
                if (Directory.Exists(this.SourcePath))
                {
                    await builder.AddContentsAsync(
                        this.SourcePath,
                        "/",
                        true,
                        s => string.IsNullOrWhiteSpace(this.Manifest) || !string.Equals(s, "upack.json", StringComparison.OrdinalIgnoreCase),
                        cancellationToken
                        );
                }
                else
                {
                    using (var file = File.Open(this.SourcePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        await builder.AddFileAsync(file, Path.GetFileName(this.SourcePath), File.GetLastWriteTimeUtc(this.SourcePath), cancellationToken);
                    }
                }
            }

            Directory.CreateDirectory(Path.GetDirectoryName(targetFileName));
            File.Delete(targetFileName);
            File.Move(tmpPath, targetFileName);

            return(0);
        }