Skip to content

Commit bdc2f21

Browse files
authored
Merge pull request #1 from ivanpovazan/main
Adding support for comparing object files of the generated app
2 parents 60846ae + 718957e commit bdc2f21

File tree

3 files changed

+102
-49
lines changed

3 files changed

+102
-49
lines changed

Comparer.cs

Lines changed: 72 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ namespace AppCompare;
77

88
class Comparer {
99

10-
public static DataTable GetTable (string app1path, string app2path, Dictionary<string, string> mappings)
10+
const string filter_all_files = "*.*";
11+
const string filter_obj_files = "*.o";
12+
13+
public static DataTable GetAppCompareTable (string app1path, string app2path, Dictionary<string, string> mappings)
1114
{
12-
DataTable dt = new ();
15+
DataTable dt = new ("App bundle compare");
1316
DataColumn files = new ("Files", typeof (string));
1417
dt.Columns.Add (files);
1518
dt.PrimaryKey = new [] { files };
@@ -21,7 +24,7 @@ public static DataTable GetTable (string app1path, string app2path, Dictionary<s
2124
dt.Columns.Add (new DataColumn (" ", typeof (string)));
2225

2326
try {
24-
Populate (dt, app1path, app2path, mappings);
27+
Populate (dt, app1path, app2path, mappings, filter_all_files);
2528
} catch (Exception ex) {
2629
dt.ExtendedProperties.Add ("Exception", ex);
2730
}
@@ -97,19 +100,45 @@ public static DataTable GetTable (string app1path, string app2path, Dictionary<s
97100
AddSummaryRow (dt, "Managed *.dll/exe", managed_a, managed_b);
98101
AddEmptyRow (dt);
99102
AddSummaryRow (dt, "TOTAL", size_a, size_b);
103+
100104
return dt;
101105
}
102106

103-
static void Populate (DataTable dt, string app1path, string app2path, Dictionary<string, string> mappings)
107+
public static DataTable GetObjCompareTable (string app1path, string app2path, Dictionary<string, string> mappings)
108+
{
109+
DataTable dt = new ("Object files compare");
110+
DataColumn files = new ("Files", typeof (string));
111+
dt.Columns.Add (files);
112+
dt.PrimaryKey = new [] { files };
113+
114+
dt.Columns.Add (new DataColumn ("Size A", typeof ((FileInfo, long))));
115+
dt.Columns.Add (new DataColumn ("Size B", typeof ((FileInfo, long))));
116+
dt.Columns.Add (new DataColumn ("diff", typeof (long)));
117+
dt.Columns.Add (new DataColumn ("%", typeof (double)));
118+
dt.Columns.Add (new DataColumn (" ", typeof (string)));
119+
120+
try {
121+
Populate (dt, app1path, app2path, mappings, filter_obj_files);
122+
} catch (Exception ex) {
123+
dt.ExtendedProperties.Add ("Exception", ex);
124+
}
125+
126+
dt.DefaultView.Sort = "Files ASC";
127+
dt = dt.DefaultView.ToTable ();
128+
dt.ExtendedProperties.Add ("AppA", app1path);
129+
dt.ExtendedProperties.Add ("AppB", app2path);
130+
131+
AddEmptyRow (dt);
132+
return dt;
133+
}
134+
135+
static void Populate (DataTable dt, string app1path, string app2path, Dictionary<string, string> mappings, string filter)
104136
{
105137
DirectoryInfo Directory1 = new (app1path);
106138
if (Directory1.Exists) {
107-
var len1 = app1path.Length;
108-
if (app1path [len1 - 1] != Path.DirectorySeparatorChar)
109-
len1++;
110-
foreach (var file in Directory1.GetFiles ("*.*", SearchOption.AllDirectories)) {
139+
foreach (var file in Directory1.GetFiles (filter, SearchOption.AllDirectories)) {
111140
dt.Rows.Add (new object? [] {
112-
file.FullName [len1..],
141+
file.Name,
113142
(file, file.Length),
114143
empty,
115144
-file.Length,
@@ -123,11 +152,8 @@ static void Populate (DataTable dt, string app1path, string app2path, Dictionary
123152
if (!Directory2.Exists)
124153
return;
125154

126-
var len2 = app2path.Length;
127-
if (app2path [len2 - 1] != Path.DirectorySeparatorChar)
128-
len2++;
129-
foreach (var file in Directory2.GetFiles ("*.*", SearchOption.AllDirectories)) {
130-
var name = file.FullName [len2..];
155+
foreach (var file in Directory2.GetFiles (filter, SearchOption.AllDirectories)) {
156+
var name = file.Name;
131157
var row = dt.Rows.Find (name);
132158
var remapped = false;
133159
if (row is null) {
@@ -180,54 +206,58 @@ static void AddSummaryRow (DataTable dt, string name, long a, long b)
180206
});
181207
}
182208

183-
static public string ExportMarkdown (DataTable table)
209+
static public string ExportMarkdown (List<DataTable> tables)
184210
{
185211
StringBuilder builder = new ();
186212
builder.AppendLine ("# Application Comparer");
187213
builder.AppendLine ();
188214

189-
builder.Append ("* App A: `").Append (table.ExtendedProperties ["AppA"]).AppendLine ("`");
190-
builder.Append ("* App B: `").Append (table.ExtendedProperties ["AppB"]).AppendLine ("`");
191-
builder.AppendLine ();
215+
foreach (var table in tables) {
216+
builder.AppendLine ($"## {table.TableName}");
217+
builder.AppendLine ();
218+
builder.Append ("* Path A: `").Append (table.ExtendedProperties ["AppA"]).AppendLine ("`");
219+
builder.Append ("* Path B: `").Append (table.ExtendedProperties ["AppB"]).AppendLine ("`");
220+
builder.AppendLine ();
192221

193-
var columns = table.Columns;
194-
// skip last "comment" column - it's for the tool itself, not for reporting
195-
for (int i = 0; i < columns.Count - 1; i++) {
196-
builder.Append ("| ").Append (columns [i].ColumnName).Append (' ');
197-
}
198-
builder.AppendLine ("|");
222+
var columns = table.Columns;
223+
// skip last "comment" column - it's for the tool itself, not for reporting
224+
for (int i = 0; i < columns.Count - 1; i++) {
225+
builder.Append ("| ").Append (columns [i].ColumnName).Append (' ');
226+
}
227+
builder.AppendLine ("|");
199228

200-
builder.AppendLine ("|:----|----:|----:|----:|----:|");
229+
builder.AppendLine ("|:----|----:|----:|----:|----:|");
201230

202-
foreach (DataRow row in table.Rows) {
203-
var file = row [0] as string;
204-
if (file!.Length == 0) {
205-
builder.AppendLine ("| | | | | |");
206-
} else {
207-
builder.Append ("| ").Append (row [0]);
208-
(_, long f1length) = ((FileInfo?, long)) row [1];
209-
builder.Append (" | ").AppendFormat ("{0:N0}", f1length);
210-
(_, long f2length) = ((FileInfo?, long)) row [2];
211-
builder.Append (" | ").AppendFormat ("{0:N0}", f2length);
212-
builder.Append (" | ").AppendFormat ("{0:N0}", row [3]);
213-
var percentage = (double) row [4];
214-
var display = Double.IsNaN (percentage) ? "-" : percentage.ToString ("P1");
215-
builder.Append (" | ").Append (display);
216-
builder.AppendLine (" |)");
231+
foreach (DataRow row in table.Rows) {
232+
var file = row [0] as string;
233+
if (file!.Length == 0) {
234+
builder.AppendLine ("| | | | | |");
235+
} else {
236+
builder.Append ("| ").Append (row [0]);
237+
(_, long f1length) = ((FileInfo?, long)) row [1];
238+
builder.Append (" | ").AppendFormat ("{0:N0}", f1length);
239+
(_, long f2length) = ((FileInfo?, long)) row [2];
240+
builder.Append (" | ").AppendFormat ("{0:N0}", f2length);
241+
builder.Append (" | ").AppendFormat ("{0:N0}", row [3]);
242+
var percentage = (double) row [4];
243+
var display = Double.IsNaN (percentage) ? "-" : percentage.ToString ("P1");
244+
builder.Append (" | ").Append (display);
245+
builder.AppendLine (" |)");
246+
}
217247
}
218248
}
219249
builder.AppendLine ();
220250
builder.AppendLine ("Generated by [appcompare](https://github.com/spouliot/appcompare)");
221251
return builder.ToString ();
222252
}
223253

224-
static public string Gist (DataTable table, bool openUrl = true)
254+
static public string Gist (List<DataTable> tables, bool openUrl = true)
225255
{
226256
GistRequest request = new () {
227257
Description = "Application Comparison Report",
228258
Public = false,
229259
};
230-
request.AddFile ("report.md", ExportMarkdown (table));
260+
request.AddFile ("report.md", ExportMarkdown (tables));
231261

232262
Task<GistResponse>? task = Task.Run (async () => await GistClient.CreateAsync (request));
233263
var url = "https://github.com/spouliot/SimpleGist/wiki#errors";

Program.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Data;
12
using Spectre.Console;
23
using Terminal.Gui;
34

@@ -12,11 +13,13 @@ class Program {
1213
/// <param name="outputMarkdown">Filename for the markdown output (optional).</param>
1314
/// <param name="gist">Gist the output.</param>
1415
/// <param name="mappingFile">File that describe a custom mapping between files from both application bundles/directories.</param>
16+
/// <param name="objDirs">Pair of directories for scanning for object files, separated with a colon (optional)</param>
1517
/// <returns>0 for success, 1 for invalid/incorrect arguments, 2 for unexpected failure.</returns>
16-
static int Main (string [] args, string? outputMarkdown, bool gist, string mappingFile)
18+
static int Main (string [] args, string? outputMarkdown, bool gist, string mappingFile, string? objDirs)
1719
{
1820
try {
1921
Dictionary<string, string>? mappings = null;
22+
List<DataTable> tables = new List<DataTable> ();
2023

2124
// if mappings are given then they must exists
2225
if (mappingFile is not null) {
@@ -57,15 +60,35 @@ static int Main (string [] args, string? outputMarkdown, bool gist, string mappi
5760
return 1;
5861
}
5962

60-
var table = Comparer.GetTable (app1, app2, mappings);
61-
string markdown = Comparer.ExportMarkdown (table);
63+
tables.Add (Comparer.GetAppCompareTable (app1, app2, mappings));
6264

65+
string? objDir1 = null;
66+
string? objDir2 = null;
67+
68+
if (!string.IsNullOrEmpty (objDirs)) {
69+
var objDirsSplitted = objDirs.Split (":");
70+
if (objDirsSplitted.Length != 2) {
71+
AnsiConsole.MarkupLine ($"[red]Error:[/] Missing or invalid path to obj directories.");
72+
return 1;
73+
}
74+
if (!CheckDirectory (objDirsSplitted [0], out objDir1)) {
75+
AnsiConsole.MarkupLine ($"[red]Error:[/] Cannot find obj directory at `{objDir1}`.");
76+
return 1;
77+
}
78+
if (!CheckDirectory (objDirsSplitted [1], out objDir2)) {
79+
AnsiConsole.MarkupLine ($"[red]Error:[/] Cannot find obj directory at `{objDir2}`.");
80+
return 1;
81+
}
82+
tables.Add (Comparer.GetObjCompareTable (objDir1, objDir2, mappings));
83+
}
84+
85+
string markdown = Comparer.ExportMarkdown (tables);
6386
if (outputMarkdown is not null) {
6487
File.WriteAllText (outputMarkdown, markdown);
6588
}
6689

6790
if (gist) {
68-
var url = Comparer.Gist (table, openUrl: false);
91+
var url = Comparer.Gist (tables, openUrl: false);
6992
Console.Out.WriteLine (url);
7093
}
7194

ProgramUI.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static void FileExport ()
174174
using SaveDialog d = new ("Export Table", "", new () { ".md" });
175175
Application.Run (d);
176176
if (!d.Canceled && (d.FilePath is not null)) {
177-
File.WriteAllText (d.FilePath.ToString ()!, Comparer.ExportMarkdown (tv.Table));
177+
File.WriteAllText (d.FilePath.ToString ()!, Comparer.ExportMarkdown (new List<DataTable> { tv.Table }));
178178
}
179179
}
180180

@@ -239,13 +239,13 @@ static void EditUnpairFiles ()
239239

240240
static void ViewGist ()
241241
{
242-
Comparer.Gist (tv.Table);
242+
Comparer.Gist (new List<DataTable> { tv.Table });
243243
}
244244

245245
static void ViewRefresh ()
246246
{
247247
if ((app1_path is not null) && (app2_path is not null)) {
248-
tv.Table = Comparer.GetTable (app1_path, app2_path, mappings);
248+
tv.Table = Comparer.GetAppCompareTable (app1_path, app2_path, mappings);
249249
if (tv.Table.ExtendedProperties ["Exception"] is Exception ex) {
250250
MessageBox.ErrorQuery (60, 13, "Error", ex.ToString (), "_Ok");
251251
}

0 commit comments

Comments
 (0)