Skip to content

Commit 73d175f

Browse files
committed
dedupe in list, normalize
1 parent b8e2a12 commit 73d175f

3 files changed

Lines changed: 63 additions & 14 deletions

File tree

tools/sorcerer/build.zig

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,17 @@ fn generate_zig_schema_literal(allocator: std.mem.Allocator, schemas: []const Re
477477
var buf: std.ArrayList(u8) = .{};
478478
const writer = buf.writer(allocator);
479479

480+
// Helper to normalize paths (convert backslashes to forward slashes for Windows compatibility)
481+
const normalize_path = struct {
482+
fn call(alloc: std.mem.Allocator, path: []const u8) ![]const u8 {
483+
const result = try alloc.alloc(u8, path.len);
484+
for (path, 0..) |c, i| {
485+
result[i] = if (c == '\\') '/' else c;
486+
}
487+
return result;
488+
}
489+
}.call;
490+
480491
try writer.writeAll(
481492
\\// Auto-generated file - do not edit manually.
482493
\\// Generated by tools/sorcerer/build.zig
@@ -494,12 +505,40 @@ fn generate_zig_schema_literal(allocator: std.mem.Allocator, schemas: []const Re
494505
try writer.print(" .format = .{s},\n", .{@tagName(schema.format)});
495506

496507
// Chips
497-
try writer.writeAll(" .chips = &.{");
498-
for (schema.chips, 0..) |chip, i| {
499-
if (i > 0) try writer.writeAll(", ");
500-
try writer.print(".{{ .name = \"{s}\" }}", .{chip.name});
508+
try writer.writeAll(" .chips = &.{\n");
509+
for (schema.chips) |chip| {
510+
try writer.writeAll(" .{\n");
511+
try writer.print(" .name = \"{s}\",\n", .{chip.name});
512+
try writer.print(" .target_name = \"{s}\",\n", .{chip.target_name});
513+
// Patch files
514+
if (chip.patch_files.len > 0) {
515+
try writer.writeAll(" .patch_files = &.{\n");
516+
for (chip.patch_files) |patch_file| {
517+
switch (patch_file) {
518+
.src_path => |src| {
519+
const sub_path = try normalize_path(allocator, src.sub_path);
520+
const build_root = try normalize_path(allocator, src.build_root);
521+
try writer.writeAll(" .{ .src_path = .{\n");
522+
try writer.print(" .sub_path = \"{s}\",\n", .{sub_path});
523+
try writer.print(" .build_root = \"{s}\",\n", .{build_root});
524+
try writer.writeAll(" } },\n");
525+
},
526+
.dependency => |dep| {
527+
const sub_path = try normalize_path(allocator, dep.sub_path);
528+
const build_root = try normalize_path(allocator, dep.build_root);
529+
try writer.writeAll(" .{ .dependency = .{\n");
530+
try writer.print(" .sub_path = \"{s}\",\n", .{sub_path});
531+
try writer.print(" .build_root = \"{s}\",\n", .{build_root});
532+
try writer.print(" .dep_name = \"{s}\",\n", .{dep.dep_name});
533+
try writer.writeAll(" } },\n");
534+
},
535+
}
536+
}
537+
try writer.writeAll(" },\n");
538+
}
539+
try writer.writeAll(" },\n");
501540
}
502-
try writer.writeAll("},\n");
541+
try writer.writeAll(" },\n");
503542

504543
// Boards
505544
try writer.writeAll(" .boards = &.{");

tools/sorcerer/src/cli.zig

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,23 @@ fn run_list(allocator: Allocator, args: []const []const u8) !void {
149149
if (json_output) {
150150
try print_list_json(allocator, port_filter);
151151
} else {
152-
try print_list_table(port_filter);
152+
try print_list_table(allocator, port_filter);
153153
}
154154
}
155155

156-
fn print_list_table(port_filter: ?[]const u8) !void {
156+
fn print_list_table(allocator: Allocator, port_filter: ?[]const u8) !void {
157157
var stdout_writer: StdoutWriter = .{};
158158
const stdout = stdout_writer.writer();
159159

160+
// Track seen chip names to deduplicate display
161+
var seen_chips = std.StringHashMap(void).init(allocator);
162+
defer seen_chips.deinit();
163+
160164
// Print header
161165
stdout.print("{s:<24} {s:<24} {s}\n", .{ "CHIP", "PORT", "BOARD" }) catch |err| return handle_write_error(err);
162166
stdout.print("{s:-<24} {s:-<24} {s:-<32}\n", .{ "", "", "" }) catch |err| return handle_write_error(err);
163167

164-
// Print entries
168+
// Print entries (deduplicated by chip name)
165169
for (schemas.schemas) |schema| {
166170
const port_name = get_port_name(schema.location);
167171

@@ -173,6 +177,12 @@ fn print_list_table(port_filter: ?[]const u8) !void {
173177
}
174178

175179
for (schema.chips) |chip| {
180+
// Skip if we've already shown this chip name
181+
if (seen_chips.contains(chip.name)) {
182+
continue;
183+
}
184+
seen_chips.put(chip.name, {}) catch {};
185+
176186
const board_name = if (schema.boards.len > 0) schema.boards[0].name else "-";
177187
stdout.print("{s:<24} {s:<24} {s}\n", .{ chip.name, port_name, board_name }) catch |err| return handle_write_error(err);
178188

@@ -215,6 +225,7 @@ fn print_list_json(allocator: Allocator, port_filter: ?[]const u8) !void {
215225
for (schema.chips) |chip| {
216226
try entries.append(allocator, .{
217227
.chip = chip.name,
228+
.target = chip.target_name,
218229
.port = port_name,
219230
.format = @tagName(schema.format),
220231
.boards = schema.boards,
@@ -235,6 +246,7 @@ fn print_list_json(allocator: Allocator, port_filter: ?[]const u8) !void {
235246

236247
const JsonEntry = struct {
237248
chip: []const u8,
249+
target: []const u8,
238250
port: []const u8,
239251
format: []const u8,
240252
boards: []const RegisterSchemaUsage.Board,

tools/sorcerer/src/main.zig

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,7 @@ fn open_register_schema_submenu(m: *dvui.MenuWidget) !void {
266266
};
267267

268268
const path = try gpa.dupe(u8, f);
269-
const register_schemas = if (state.register_schema_usages) |rsu| rsu.value else null;
270-
const new_window = try RegzWindow.create(gpa, format, path, null, null, register_schemas);
269+
const new_window = try RegzWindow.create(gpa, format, path, null, null, schemas.schemas);
271270
errdefer new_window.destroy();
272271

273272
try state.regz_windows.put(gpa, f, new_window);
@@ -386,8 +385,7 @@ fn from_microzig_menu() void {
386385
else
387386
null;
388387

389-
const rsus = if (state.register_schema_usages) |r| r.value else null;
390-
const new_window = RegzWindow.create(gpa, format, path, null, chip_info, rsus) catch unreachable;
388+
const new_window = RegzWindow.create(gpa, format, path, null, chip_info, schemas.schemas) catch unreachable;
391389

392390
state.regz_windows.put(gpa, path, new_window) catch {
393391
new_window.destroy();
@@ -703,7 +701,7 @@ fn open_chip_target(rsu_idx: usize, chip_idx: usize) void {
703701
.patch_files = chip.patch_files,
704702
};
705703

706-
const new_window = RegzWindow.create(gpa, format, path, chip.name, chip_info, rsus.value) catch return;
704+
const new_window = RegzWindow.create(gpa, format, path, chip.name, chip_info, rsus) catch return;
707705

708706
state.regz_windows.put(gpa, path, new_window) catch {
709707
new_window.destroy();
@@ -1055,7 +1053,7 @@ fn open_board(rsu_idx: usize, board_idx: usize) void {
10551053
.targetdb => .targetdb,
10561054
};
10571055

1058-
const new_window = RegzWindow.create(gpa, format, path, null, chip_info, rsus.value) catch return;
1056+
const new_window = RegzWindow.create(gpa, format, path, null, chip_info, rsus) catch return;
10591057

10601058
state.regz_windows.put(gpa, path, new_window) catch {
10611059
new_window.destroy();

0 commit comments

Comments
 (0)