1//! This step has two modes:
2//! * Modify mode: directly modify source files, formatting them in place.
3//! * Check mode: fail the step if a non-conforming file is found.
4const Fmt = @This();
5
6const std = @import("std");
7const Step = std.Build.Step;
8const LazyPath = std.Build.LazyPath;
9const Configuration = std.Build.Configuration;
10
11step: Step,
12/// Intended to be read-only after the `Fmt` step is created.
13paths: []const LazyPath,
14/// Intended to be read-only after the `Fmt` step is created.
15exclude_paths: []const LazyPath,
16check: bool,
17
18pub const base_tag: Step.Tag = .fmt;
19
20pub const Options = struct {
21 paths: []const LazyPath = &.{},
22 exclude_paths: []const LazyPath = &.{},
23 /// If true, fails the build step when any non-conforming files are encountered.
24 check: bool = false,
25};
26
27pub fn create(owner: *std.Build, options: Options) *Fmt {
28 const graph = owner.graph;
29 const fmt = graph.create(Fmt);
30
31 fmt.* = .{
32 .step = .init(.{
33 .tag = base_tag,
34 .name = if (options.check) "zig fmt --check" else "zig fmt",
35 .owner = owner,
36 }),
37 .paths = LazyPath.dupeList(options.paths, graph),
38 .exclude_paths = LazyPath.dupeList(options.exclude_paths, graph),
39 .check = options.check,
40 };
41
42 for (options.paths) |lp| lp.addStepDependencies(&fmt.step);
43 for (options.exclude_paths) |lp| lp.addStepDependencies(&fmt.step);
44
45 return fmt;
46}