Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index.json in Consolidated zip stores full path, not just hash #1416

Open
wants to merge 2 commits into
base: release/27.3.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Calamari.ConsolidateCalamariPackages.Tests
[TestFixture]
public class IntegrationTests
{
readonly Assent.Configuration assentConfiguration = new Assent.Configuration().UsingSanitiser(s => Sanitise4PartVersions(SanitiseHashes(s)));
readonly Assent.Configuration assentConfiguration = new Assent.Configuration().UsingSanitiser(s => Sanitise4PartVersions(SanitiseFilenamesInIndex(s)));
static readonly string TestPackagesDirectory = "../../../testPackages";

private string temp;
Expand Down Expand Up @@ -85,7 +85,7 @@ public void AndThenThePackageIsCreated()
public void AndThenThePackageContentsShouldBe()
{
using (var zip = ZipFile.Open(expectedZip, ZipArchiveMode.Read))
this.Assent(string.Join("\r\n", zip.Entries.Select(e => SanitiseHashes(e.FullName)).OrderBy(k => k)), assentConfiguration);
this.Assent(string.Join("\r\n", zip.Entries.Select(e => SanitiseHashesInPackageList(e.FullName)).OrderBy(k => k)), assentConfiguration);
}

public void AndThenTheIndexShouldBe()
Expand All @@ -96,8 +96,12 @@ public void AndThenTheIndexShouldBe()
this.Assent(sr.ReadToEnd(), assentConfiguration);
}

private static string SanitiseHashes(string s)
private static string SanitiseFilenamesInIndex(string s)
=> Regex.Replace(s, "[a-z0-9]{32}", "<hash>");

private static string SanitiseHashesInPackageList(string s)
=> Regex.Replace(s, "[a-z0-9]{32}", "<hash>");


private static string Sanitise4PartVersions(string s)
=> Regex.Replace(s, @"[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+", "<version>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public IReadOnlyList<SourceFile> GetSourceFiles(ILogger log)
IsNupkg = false,
FullNameInDestinationArchive = string.Join("/", parts.Skip(1)),
FullNameInSourceArchive = entry.FullName,
Hash = hasher.Hash(entry)
Hash = hasher.Hash(entry),
FileName = parts.Last()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW entry.Name should give you the file name

};
})
.ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public IReadOnlyList<SourceFile> GetSourceFiles(ILogger log)
IsNupkg = true,
FullNameInDestinationArchive = entry.FullName,
FullNameInSourceArchive = entry.FullName,
Hash = hasher.Hash(entry)
Hash = hasher.Hash(entry),
FileName = entry.FullName.Split("/").Last()
})
.ToArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,33 @@ public static void Create(IEnumerable<SourceFile> sourceFiles, string destinatio

private static void WriteUniqueFilesToZip(IEnumerable<SourceFile> sourceFiles, ZipArchive zip)
{
// This acts as a 'Distinct' - there may be multiples of a given file which are binary-identical
// therefore we only need to track ONE (aka the first) of these files during packing.
var uniqueFiles = sourceFiles
.GroupBy(sourceFile => new {sourceFile.FullNameInDestinationArchive, sourceFile.Hash})
.Select(g => new
{
g.Key.FullNameInDestinationArchive,
g.Key.Hash,
g.First().FullNameInSourceArchive,
g.First().ArchivePath
});
.DistinctBy(sourceFile => sourceFile.EntryNameInConsolidationArchive());

foreach (var groupedBySourceArchive in uniqueFiles.GroupBy(f => f.ArchivePath))
{
using (var sourceZip = ZipFile.OpenRead(groupedBySourceArchive.Key))
foreach (var uniqueFile in groupedBySourceArchive)
{
var entryName = Path.Combine(uniqueFile.Hash, uniqueFile.FullNameInDestinationArchive);
var entry = zip.CreateEntry(entryName, CompressionLevel.Fastest);

var entry = zip.CreateEntry(uniqueFile.EntryNameInConsolidationArchive(), CompressionLevel.Fastest);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could CompressionLevel.SmallestSize be a good way to go? We can afford extra creation time if it makes it a smaller archive

using (var destStream = entry.Open())
using (var sourceStream = sourceZip.Entries.First(e => e.FullName == uniqueFile.FullNameInSourceArchive).Open())
sourceStream.CopyTo(destStream);
}
}
}

private static void WriteIndexTo(Stream stream, IEnumerable<SourceFile> sourceFiles)
{
Dictionary<string, string[]> GroupByPlatform(IEnumerable<SourceFile> filesForPackage)
// SHould break out entryName to a function - make first class
Dictionary<string, FileTransfer[]> GroupByPlatform(IEnumerable<SourceFile> filesForPackage)
=> filesForPackage
.GroupBy(f => f.Platform)
.ToDictionary(
g => g.Key,
g => g.Select(f => f.Hash).OrderBy(h => h).ToArray()
.GroupBy(f => f.Platform)
.ToDictionary(
g => g.Key,
g => g.Select(f => new FileTransfer(f.EntryNameInConsolidationArchive(), f.FullNameInDestinationArchive)).ToArray()
);

var index = new ConsolidatedPackageIndex(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ public ConsolidatedPackageIndex(Dictionary<string, Package> packages)

public class Package
{
public Package(string packageId, string version, bool isNupkg, Dictionary<string, string[]> platformHashes)
public Package(string packageId, string version, bool isNupkg, Dictionary<string, FileTransfer[]> platformHashes)
{
PackageId = packageId;
Version = version;
IsNupkg = isNupkg;
PlatformHashes = new Dictionary<string, string[]>(platformHashes, StringComparer.OrdinalIgnoreCase);
PlatformHashes = new Dictionary<string, FileTransfer[]>(platformHashes, StringComparer.OrdinalIgnoreCase);
}

public string PackageId { get; }
public string Version { get; }
public bool IsNupkg { get; }
public Dictionary<string, string[]> PlatformHashes { get; }
public Dictionary<string, FileTransfer[]> PlatformHashes { get; }
}
}

public record FileTransfer(String Source, string Destination);
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ IReadOnlyList<SourceFile> ReadSashimiPackagedZip(string toolZipPath)
IsNupkg = false,
FullNameInDestinationArchive = string.Join("/", parts.Skip(1)),
FullNameInSourceArchive = entry.FullName,
Hash = hasher.Hash(entry)
Hash = hasher.Hash(entry),
FileName = parts.Last()
};
})
.ToArray();
Expand Down
5 changes: 5 additions & 0 deletions source/Calamari.ConsolidateCalamariPackages/SourceFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ class SourceFile
public string FullNameInDestinationArchive { get; set; }
public string FullNameInSourceArchive { get; set; }
public string Hash { get; set; }

public string FileName { get; set; }
public string EntryNameInConsolidationArchive() {
return $"{Hash}/{FileName}";
}
}
}