| 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. |
| 4 | const Fmt = @This(); |
| 5 | |
| 6 | const std = @import("std"); |
| 7 | const Step = std.Build.Step; |
| 8 | const LazyPath = std.Build.LazyPath; |
| 9 | const Configuration = std.Build.Configuration; |
| 10 | |
| 11 | step: Step, |
| 12 | /// Intended to be read-only after the `Fmt` step is created. |
| 13 | paths: []const LazyPath, |
| 14 | /// Intended to be read-only after the `Fmt` step is created. |
| 15 | exclude_paths: []const LazyPath, |
| 16 | check: bool, |
| 17 | |
| 18 | pub const base_tag: Step.Tag = .fmt; |
| 19 | |
| 20 | pub 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 | |
| 27 | pub 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 | } |