Skip to content

Commit fd8d3e0

Browse files
committed
Show assemblies and shared libraries total differences in summary
1 parent 130598f commit fd8d3e0

File tree

4 files changed

+75
-11
lines changed

4 files changed

+75
-11
lines changed

ApkDescription.cs

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class ApkDescription {
2727
[DataMember]
2828
readonly Dictionary<string, FileProperties> Entries = new Dictionary<string, FileProperties> ();
2929

30+
Dictionary<string, (long Difference, long OriginalTotal)> totalDifferences = new Dictionary<string, (long, long)> ();
31+
3032
public static ApkDescription Load (string path)
3133
{
3234
if (!File.Exists (path)) {
@@ -103,12 +105,63 @@ void SaveDescription (string path)
103105
}
104106
}
105107

106-
static public void PrintDifference (string key, long diff, string comment = null, string padding = null)
108+
static ConsoleColor PrintDifferenceStart (string key, long diff, string comment = null, string padding = null)
107109
{
108-
var color = diff > 0 ? ConsoleColor.Red : ConsoleColor.Green;
109-
Program.ColorWrite ($"{padding} {diff:+;-;+}{Math.Abs (diff),12}", color);
110+
var color = diff == 0 ? ConsoleColor.Gray : diff > 0 ? ConsoleColor.Red : ConsoleColor.Green;
111+
Program.ColorWrite ($"{padding} {diff:+;-;+}{Math.Abs (diff),12:#,0}", color);
110112
Program.ColorWrite ($" {key}", ConsoleColor.Gray);
111-
Program.ColorWriteLine (comment, color);
113+
Program.ColorWrite (comment, color);
114+
115+
return color;
116+
}
117+
118+
static public void PrintDifference (string key, long diff, string comment = null, string padding = null)
119+
{
120+
PrintDifferenceStart (key, diff, comment, padding);
121+
Console.WriteLine ();
122+
}
123+
124+
static public void PrintDifference (string key, long diff, long orig, string comment = null, string padding = null)
125+
{
126+
var color = PrintDifferenceStart (key, diff, comment, padding);
127+
128+
if (orig != 0)
129+
Program.ColorWrite ($" {(float)diff/orig:0.00%} (of {orig:#,0})", color);
130+
131+
Console.WriteLine ();
132+
}
133+
134+
void AddToTotal (string entry, long size)
135+
{
136+
var entryDiff = EntryDiff.ForExtension (Path.GetExtension (entry));
137+
if (entryDiff == null)
138+
return;
139+
140+
var diffType = entryDiff.Name;
141+
if (!totalDifferences.ContainsKey (diffType))
142+
totalDifferences.Add (diffType, (0, size));
143+
else {
144+
var info = totalDifferences [diffType];
145+
totalDifferences [diffType] = (info.Difference, info.OriginalTotal + size);
146+
}
147+
}
148+
149+
bool AddToDifference (string entry, long diff, out EntryDiff entryDiff)
150+
{
151+
entryDiff = EntryDiff.ForExtension (Path.GetExtension (entry));
152+
153+
if (entryDiff == null)
154+
return false;
155+
156+
var diffType = entryDiff.Name;
157+
if (!totalDifferences.ContainsKey (diffType))
158+
totalDifferences.Add (diffType, (diff, 0));
159+
else {
160+
var info = totalDifferences [diffType];
161+
totalDifferences [diffType] = (info.Difference + diff, info.OriginalTotal);
162+
}
163+
164+
return true;
112165
}
113166

114167
public void Compare (ApkDescription other)
@@ -129,6 +182,8 @@ public void Compare (ApkDescription other)
129182
differences [key] = -Entries [key].Size;
130183
singles.Add (key);
131184
}
185+
186+
AddToTotal (key, Entries [key].Size);
132187
}
133188

134189
foreach (var key in other.Entries.Keys) {
@@ -147,24 +202,26 @@ public void Compare (ApkDescription other)
147202

148203
PrintDifference (diff.Key, diff.Value, single ? $" *{(diff.Value > 0 ? 2 : 1)}" : null);
149204

205+
EntryDiff entryDiff;
206+
if (!AddToDifference (diff.Key, diff.Value, out entryDiff))
207+
continue;
208+
150209
if (comparingApks && !single)
151-
CompareEntries (new KeyValuePair<string, FileProperties> (diff.Key, Entries [diff.Key]), new KeyValuePair<string, FileProperties> (diff.Key, other.Entries [diff.Key]), other);
210+
CompareEntries (new KeyValuePair<string, FileProperties> (diff.Key, Entries [diff.Key]), new KeyValuePair<string, FileProperties> (diff.Key, other.Entries [diff.Key]), other, entryDiff);
152211
}
153212

154213
Program.ColorWriteLine ("Summary:", ConsoleColor.Green);
155214
if (Program.Verbose)
156215
Program.ColorWriteLine ($" apk1: {PackageSize,12} {PackagePath}\n apk2: {other.PackageSize,12} {other.PackagePath}", ConsoleColor.Gray);
157216

217+
foreach (var total in totalDifferences)
218+
PrintDifference ($"{total.Key} ", total.Value.Difference, total.Value.OriginalTotal);
219+
158220
PrintDifference ("Package size difference", other.PackageSize - PackageSize);
159221
}
160222

161-
void CompareEntries (KeyValuePair<string, FileProperties> entry, KeyValuePair<string, FileProperties> other, ApkDescription otherApk)
223+
void CompareEntries (KeyValuePair<string, FileProperties> entry, KeyValuePair<string, FileProperties> other, ApkDescription otherApk, EntryDiff diff)
162224
{
163-
var diff = EntryDiff.ForExtension (Path.GetExtension (entry.Key));
164-
165-
if (diff == null)
166-
return;
167-
168225
var tmpDir = Path.Combine (Path.GetTempPath (), Path.GetRandomFileName ());
169226
var tmpDirOther = Path.Combine (Path.GetTempPath (), Path.GetRandomFileName ());
170227

AssemblyDiff.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public AssemblyDiff ()
1414
{
1515
}
1616

17+
public override string Name { get { return "Assemblies"; } }
18+
1719
TypeDefinition GetTypeDefinition (MetadataReader reader, TypeDefinitionHandle handle, out string fullName)
1820
{
1921
var typeDef = reader.GetTypeDefinition (handle);

EntryDiff.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace apkdiff {
44
public abstract class EntryDiff {
5+
6+
public abstract string Name { get; }
7+
58
public static EntryDiff ForExtension (string extension)
69
{
710
switch (extension) {

SharedLibraryDiff.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public SharedLibraryDiff ()
88
{
99
}
1010

11+
public override string Name { get { return "Shared libraries"; } }
12+
1113
string RunCommand (string cmd, string arguments)
1214
{
1315
String output;

0 commit comments

Comments
 (0)