feat.Publisher完整包上传

This commit is contained in:
lincube
2026-06-01 17:28:26 +08:00
parent 131043fe37
commit 0c8830133a
8 changed files with 245 additions and 16 deletions

View File

@@ -5,6 +5,7 @@ public sealed record PlondsPublishOptions(
string Repository,
string ManifestPath,
string ChangedZipPath,
string FilesZipPath,
string WorkDir,
string S3KeyPrefix,
PlondsS3ClientOptions S3);

View File

@@ -10,4 +10,9 @@ public sealed record PlondsPublishResult(
string ChangedZipUrl,
string ChangedFolderKey,
string ChangedFolderUrl,
int ChangedFileCount);
string FilesZipKey,
string FilesZipUrl,
string FilesFolderKey,
string FilesFolderUrl,
int ChangedFileCount,
int FilesFileCount);

View File

@@ -21,12 +21,15 @@ public sealed class PlondsPublisher
var repository = Require(options.Repository, nameof(options.Repository));
var manifestPath = Path.GetFullPath(Require(options.ManifestPath, nameof(options.ManifestPath)));
var changedZipPath = Path.GetFullPath(Require(options.ChangedZipPath, nameof(options.ChangedZipPath)));
var filesZipPath = Path.GetFullPath(Require(options.FilesZipPath, nameof(options.FilesZipPath)));
var workDir = Path.GetFullPath(Require(options.WorkDir, nameof(options.WorkDir)));
var version = releaseTag.TrimStart('v', 'V');
var prefix = NormalizePrefix(options.S3KeyPrefix);
var versionPrefix = $"{prefix}/{version}";
var changedFolderName = $"{version}-changed";
var filesFolderName = $"{version}-Files";
var changedExtractRoot = Path.Combine(workDir, changedFolderName);
var filesExtractRoot = Path.Combine(workDir, filesFolderName);
if (!File.Exists(manifestPath))
{
@@ -38,26 +41,30 @@ public sealed class PlondsPublisher
throw new FileNotFoundException("PLONDS changed.zip not found.", changedZipPath);
}
if (!File.Exists(filesZipPath))
{
throw new FileNotFoundException("PLONDS files zip not found.", filesZipPath);
}
var manifest = LoadManifest(manifestPath);
PayloadUtilities.EnsureCleanDirectory(changedExtractRoot);
ZipFile.ExtractToDirectory(changedZipPath, changedExtractRoot, overwriteFiles: true);
PayloadUtilities.EnsureCleanDirectory(filesExtractRoot);
ZipFile.ExtractToDirectory(filesZipPath, filesExtractRoot, overwriteFiles: true);
var manifestKey = $"{versionPrefix}/PLONDS.json";
var changedZipKey = $"{versionPrefix}/changed.zip";
var changedFolderKey = $"{versionPrefix}/{changedFolderName}";
var filesZipKey = $"{versionPrefix}/Files.zip";
var filesFolderKey = $"{versionPrefix}/{filesFolderName}";
using var s3 = new PlondsS3Client(options.S3);
var changedFileCount = 0;
foreach (var filePath in Directory.EnumerateFiles(changedExtractRoot, "*", SearchOption.AllDirectories).OrderBy(x => x, StringComparer.OrdinalIgnoreCase))
{
var relativePath = PayloadUtilities.NormalizeRelativePath(Path.GetRelativePath(changedExtractRoot, filePath));
var objectKey = $"{changedFolderKey}/{relativePath}";
await s3.UploadFileAsync(new PlondsS3ObjectUpload(filePath, objectKey, ResolveContentType(filePath)), cancellationToken).ConfigureAwait(false);
changedFileCount++;
}
var changedFileCount = await UploadDirectoryAsync(s3, changedExtractRoot, changedFolderKey, cancellationToken).ConfigureAwait(false);
var filesFileCount = await UploadDirectoryAsync(s3, filesExtractRoot, filesFolderKey, cancellationToken).ConfigureAwait(false);
await s3.UploadFileAsync(new PlondsS3ObjectUpload(changedZipPath, changedZipKey, "application/zip"), cancellationToken).ConfigureAwait(false);
await s3.UploadFileAsync(new PlondsS3ObjectUpload(filesZipPath, filesZipKey, "application/zip"), cancellationToken).ConfigureAwait(false);
var updatedManifest = manifest with
{
@@ -66,7 +73,8 @@ public sealed class PlondsPublisher
GitHub: new PlondsGitHubDownloadInfo(
ReleaseUrl: $"https://github.com/{repository}/releases/tag/{releaseTag}",
ManifestUrl: $"https://github.com/{repository}/releases/download/{releaseTag}/PLONDS.json",
ChangedZipUrl: $"https://github.com/{repository}/releases/download/{releaseTag}/changed.zip"),
ChangedZipUrl: $"https://github.com/{repository}/releases/download/{releaseTag}/changed.zip",
FilesZipUrl: $"https://github.com/{repository}/releases/download/{releaseTag}/{Path.GetFileName(filesZipPath)}"),
S3: new PlondsS3DownloadInfo(
Bucket: options.S3.Bucket,
Prefix: versionPrefix,
@@ -75,7 +83,11 @@ public sealed class PlondsPublisher
ChangedZipKey: changedZipKey,
ChangedZipUrl: s3.BuildPublicUrl(changedZipKey),
ChangedFolderKey: changedFolderKey,
ChangedFolderUrl: s3.BuildPublicUrl(changedFolderKey)))
ChangedFolderUrl: s3.BuildPublicUrl(changedFolderKey),
FilesZipKey: filesZipKey,
FilesZipUrl: s3.BuildPublicUrl(filesZipKey),
FilesFolderKey: filesFolderKey,
FilesFolderUrl: s3.BuildPublicUrl(filesFolderKey)))
};
File.WriteAllText(manifestPath, JsonSerializer.Serialize(updatedManifest, JsonOptions), new UTF8Encoding(false));
@@ -83,6 +95,7 @@ public sealed class PlondsPublisher
await s3.EnsureObjectExistsAsync(manifestKey, cancellationToken).ConfigureAwait(false);
await s3.EnsureObjectExistsAsync(changedZipKey, cancellationToken).ConfigureAwait(false);
await s3.EnsureObjectExistsAsync(filesZipKey, cancellationToken).ConfigureAwait(false);
return new PlondsPublishResult(
ReleaseTag: releaseTag,
@@ -94,7 +107,30 @@ public sealed class PlondsPublisher
ChangedZipUrl: s3.BuildPublicUrl(changedZipKey),
ChangedFolderKey: changedFolderKey,
ChangedFolderUrl: s3.BuildPublicUrl(changedFolderKey),
ChangedFileCount: changedFileCount);
FilesZipKey: filesZipKey,
FilesZipUrl: s3.BuildPublicUrl(filesZipKey),
FilesFolderKey: filesFolderKey,
FilesFolderUrl: s3.BuildPublicUrl(filesFolderKey),
ChangedFileCount: changedFileCount,
FilesFileCount: filesFileCount);
}
private static async Task<int> UploadDirectoryAsync(
PlondsS3Client s3,
string sourceDirectory,
string destinationKeyPrefix,
CancellationToken cancellationToken)
{
var count = 0;
foreach (var filePath in Directory.EnumerateFiles(sourceDirectory, "*", SearchOption.AllDirectories).OrderBy(x => x, StringComparer.OrdinalIgnoreCase))
{
var relativePath = PayloadUtilities.NormalizeRelativePath(Path.GetRelativePath(sourceDirectory, filePath));
var objectKey = $"{destinationKeyPrefix}/{relativePath}";
await s3.UploadFileAsync(new PlondsS3ObjectUpload(filePath, objectKey, ResolveContentType(filePath)), cancellationToken).ConfigureAwait(false);
count++;
}
return count;
}
private static PlondsManifest LoadManifest(string manifestPath)

View File

@@ -11,7 +11,8 @@ public sealed record PlondsDownloadInfo(
public sealed record PlondsGitHubDownloadInfo(
string ReleaseUrl,
string ManifestUrl,
string ChangedZipUrl);
string ChangedZipUrl,
string FilesZipUrl);
public sealed record PlondsS3DownloadInfo(
string Bucket,
@@ -21,4 +22,8 @@ public sealed record PlondsS3DownloadInfo(
string ChangedZipKey,
string ChangedZipUrl,
string ChangedFolderKey,
string ChangedFolderUrl);
string ChangedFolderUrl,
string FilesZipKey,
string FilesZipUrl,
string FilesFolderKey,
string FilesFolderUrl);

View File

@@ -103,6 +103,7 @@ internal static class PlondsCli
Repository: Require(options, "repository"),
ManifestPath: Require(options, "manifest"),
ChangedZipPath: Require(options, "changed-zip"),
FilesZipPath: Require(options, "files-zip"),
WorkDir: Get(options, "work-dir", "plonds-publish-work") ?? "plonds-publish-work",
S3KeyPrefix: Get(options, "s3-prefix", "lanmountain/update/plonds") ?? "lanmountain/update/plonds",
S3: new PlondsS3ClientOptions(
@@ -119,7 +120,10 @@ internal static class PlondsCli
Console.WriteLine($" Manifest: {result.ManifestUrl}");
Console.WriteLine($" ChangedZip: {result.ChangedZipUrl}");
Console.WriteLine($" ChangedFolder: {result.ChangedFolderUrl}");
Console.WriteLine($" FilesZip: {result.FilesZipUrl}");
Console.WriteLine($" FilesFolder: {result.FilesFolderUrl}");
Console.WriteLine($" ChangedFileCount: {result.ChangedFileCount}");
Console.WriteLine($" FilesFileCount: {result.FilesFileCount}");
return 0;
}
@@ -199,6 +203,7 @@ internal static class PlondsCli
Console.WriteLine(" --repository <owner/repo> GitHub repository");
Console.WriteLine(" --manifest <file> PLONDS.json path");
Console.WriteLine(" --changed-zip <file> changed.zip path");
Console.WriteLine(" --files-zip <file> Full files zip path");
Console.WriteLine(" --s3-endpoint <url> S3-compatible endpoint");
Console.WriteLine(" --s3-region <region> S3 signing region");
Console.WriteLine(" --s3-bucket <bucket> S3 bucket");