コード例 #1
0
ファイル: Repack.cs プロジェクト: AmilaDevops/upack
#pragma warning disable CS0612 // Type or member is obsolete
        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);
            }

            var info        = GetPackageMetadata(this.SourcePath);
            var infoToMerge = await GetMetadataToMergeAsync();

            var hash = GetSHA1(this.SourcePath);

            var id = (string.IsNullOrEmpty(info.Group) ? "" : info.Group + "/") + info.Name + ":" + info.Version + ":" + hash;

            foreach (var modifiedProperty in infoToMerge)
            {
                info[modifiedProperty.Key] = modifiedProperty.Value;
            }

            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)
            {
                JArray history;
                if (info.ContainsKey("repackageHistory"))
                {
                    history = (JArray)info["repackageHistory"];
                }
                else
                {
                    history = new JArray();
                    info["repackageHistory"] = history;
                }

                var entry = new Dictionary <string, object>
                {
                    { "id", id },
                    { "date", DateTime.UtcNow.ToString("u") },
                    { "using", "upack/" + typeof(Repack).Assembly.GetName().Version.ToString(3) },
                    { "by", Environment.UserName }
                };

                if (!string.IsNullOrEmpty(this.Note))
                {
                    entry["reason"] = this.Note;
                }

                history.Add(JObject.FromObject(entry));
            }

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

            if (!this.Overwrite && File.Exists(targetFileName))
            {
                throw new UpackException($"Target file '{targetFileName}' exists and overwrite was set to false.");
            }

            string tmpPath = Path.GetTempFileName();

            using (var existingPackage = new UniversalPackage(this.SourcePath))
                using (var builder = new UniversalPackageBuilder(tmpPath, info))
                {
                    var entries = from e in existingPackage.Entries
                                  where !string.Equals(e.RawPath, "upack.json", StringComparison.OrdinalIgnoreCase)
                                  select e;

                    foreach (var entry in entries)
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        if (entry.IsDirectory)
                        {
                            builder.AddEmptyDirectoryRaw(entry.RawPath);
                        }
                        else
                        {
                            using (var stream = entry.Open())
                            {
                                await builder.AddFileRawAsync(stream, entry.RawPath, entry.Timestamp, cancellationToken);
                            }
                        }
                    }
                }

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

            return(0);
        }
コード例 #2
0
ファイル: Pack.cs プロジェクト: AmilaDevops/upack
        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);
        }
コード例 #3
0
        protected override async Task <object> RemoteExecuteAsync(IRemoteOperationExecutionContext context)
        {
            var fullPath = context.ResolvePath(this.FileName);

            this.LogInformation($"Changing \"{fullPath}\" package version to {AH.CoalesceString(this.NewVersion, "remove pre-release label")}...");

            var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("n"));

            try
            {
                DirectoryEx.Create(tempPath);
                UniversalPackageMetadata currentMetadata;
                using (var upack = new UniversalPackage(fullPath))
                {
                    currentMetadata = upack.GetFullMetadata();
                    await upack.ExtractAllItemsAsync(tempPath, context.CancellationToken);

                    FileEx.Delete(PathEx.Combine(tempPath, "upack.json"));
                }

                var newMetadata = currentMetadata.Clone();
                if (string.IsNullOrEmpty(this.NewVersion))
                {
                    newMetadata.Version = new UniversalPackageVersion(currentMetadata.Version.Major, currentMetadata.Version.Minor, currentMetadata.Version.Patch);
                }
                else
                {
                    newMetadata.Version = UniversalPackageVersion.Parse(this.NewVersion);
                }

                if (currentMetadata.Version == newMetadata.Version)
                {
                    this.LogWarning($"Current package version {currentMetadata.Version} and the new version {newMetadata.Version} are the same; nothing to do.");
                    return(null);
                }

                this.LogInformation("New version: " + newMetadata.Version);

                this.LogDebug("Adding repacking entry...");
                newMetadata.RepackageHistory.Add(
                    new RepackageHistoryEntry
                {
                    Id     = new UniversalPackageId(currentMetadata.Group, currentMetadata.Name) + ":" + currentMetadata.Version,
                    Date   = DateTimeOffset.Now,
                    Using  = SDK.ProductName + "/" + SDK.ProductVersion,
                    Reason = this.Reason
                }
                    );

                using (var builder = new UniversalPackageBuilder(fullPath, newMetadata))
                {
                    await builder.AddRawContentsAsync(tempPath, string.Empty, true, c => true, context.CancellationToken);
                }

                this.LogInformation("Package version changed.");

                return(null);
            }
            finally
            {
                try
                {
                    this.LogDebug($"Deleting temporary files from {tempPath}...");
                    DirectoryEx.Clear(tempPath);
                    DirectoryEx.Delete(tempPath);
                }
                catch (Exception ex)
                {
                    this.LogWarning("Unable to delete temporary files: " + ex.Message);
                }
            }
        }