| 1 | const Fmt = @This(); |
| 2 | |
| 3 | const std = @import("std"); |
| 4 | const Configuration = std.Build.Configuration; |
| 5 | |
| 6 | const Step = @import("../Step.zig"); |
| 7 | const Maker = @import("../../Maker.zig"); |
| 8 | |
| 9 | /// Persisted to reuse memory on subsequent calls to `make`. |
| 10 | argv: std.ArrayList([]const u8) = .empty, |
| 11 | |
| 12 | pub fn make( |
| 13 | fmt: *Fmt, |
| 14 | step_index: Configuration.Step.Index, |
| 15 | maker: *Maker, |
| 16 | progress_node: std.Progress.Node, |
| 17 | ) Step.ExtendedMakeError!void { |
| 18 | const graph = maker.graph; |
| 19 | const step = maker.stepByIndex(step_index); |
| 20 | const gpa = maker.gpa; |
| 21 | const arena = graph.arena; // TODO don't leak into the process arena |
| 22 | const argv = &fmt.argv; |
| 23 | const conf = &maker.scanned_config.configuration; |
| 24 | const conf_step = step_index.ptr(conf); |
| 25 | const conf_fmt = conf_step.extended.get(conf.extra).fmt; |
| 26 | const paths = conf_fmt.paths.slice; |
| 27 | const exclude_paths = conf_fmt.exclude_paths.slice; |
| 28 | |
| 29 | argv.clearRetainingCapacity(); |
| 30 | try argv.ensureUnusedCapacity(gpa, 2 + 1 + paths.len + 2 * exclude_paths.len); |
| 31 | |
| 32 | argv.appendAssumeCapacity(graph.zig_exe); |
| 33 | argv.appendAssumeCapacity("fmt"); |
| 34 | |
| 35 | if (conf_fmt.flags.check) |
| 36 | argv.appendAssumeCapacity("--check"); |
| 37 | |
| 38 | for (paths) |lp| |
| 39 | argv.appendAssumeCapacity(try maker.resolveLazyPathIndexAbs(arena, lp, step_index)); |
| 40 | |
| 41 | for (exclude_paths) |lp| { |
| 42 | argv.appendAssumeCapacity("--exclude"); |
| 43 | argv.appendAssumeCapacity(try maker.resolveLazyPathIndexAbs(arena, lp, step_index)); |
| 44 | } |
| 45 | |
| 46 | const run_result = step.captureChildProcess(maker, arena, .{ |
| 47 | .progress_node = progress_node, |
| 48 | .argv = argv.items, |
| 49 | .allow_failure = false, |
| 50 | }) catch |err| switch (err) { |
| 51 | error.FileNotFound => unreachable, |
| 52 | else => |e| return e, |
| 53 | }; |
| 54 | |
| 55 | if (conf_fmt.flags.check) switch (run_result.term) { |
| 56 | .exited => |code| if (code != 0 and run_result.stdout.len != 0) { |
| 57 | var it = std.mem.tokenizeScalar(u8, run_result.stdout, '\n'); |
| 58 | while (it.next()) |bad_file_name| { |
| 59 | try step.addError(maker, "{s}: non-conforming formatting", .{bad_file_name}); |
| 60 | } |
| 61 | }, |
| 62 | else => {}, |
| 63 | }; |
| 64 | try step.handleChildProcessTerm(maker, run_result.term); |
| 65 | } |