| 1 | const UpdateSourceFiles = @This(); |
| 2 | |
| 3 | const std = @import("std"); |
| 4 | const Step = std.Build.Step; |
| 5 | const Configuration = std.Build.Configuration; |
| 6 | |
| 7 | step: Step, |
| 8 | embeds: std.ArrayList(Embed) = .empty, |
| 9 | copies: std.ArrayList(Copy) = .empty, |
| 10 | |
| 11 | pub const base_tag: Step.Tag = .update_source_files; |
| 12 | |
| 13 | pub const Embed = Step.WriteFile.Embed; |
| 14 | pub const Copy = Step.WriteFile.Copy; |
| 15 | |
| 16 | pub fn create(owner: *std.Build) *UpdateSourceFiles { |
| 17 | const graph = owner.graph; |
| 18 | const usf = graph.create(UpdateSourceFiles); |
| 19 | usf.* = .{ |
| 20 | .step = .init(.{ |
| 21 | .tag = base_tag, |
| 22 | .name = "UpdateSourceFiles", |
| 23 | .owner = owner, |
| 24 | }), |
| 25 | }; |
| 26 | return usf; |
| 27 | } |
| 28 | |
| 29 | /// Overwrites a path relative to the build root with the contents of another file. |
| 30 | /// |
| 31 | /// Because it updates source files, this should not be used as part of the |
| 32 | /// normal build process, but as a utility occasionally run by a developer with |
| 33 | /// intent to modify source files and then commit those changes to version |
| 34 | /// control. |
| 35 | pub fn addCopyFileToSource(usf: *UpdateSourceFiles, src_file: std.Build.LazyPath, sub_path: []const u8) void { |
| 36 | const graph = usf.step.owner.graph; |
| 37 | const wc = &graph.wip_configuration; |
| 38 | const arena = graph.arena; |
| 39 | |
| 40 | usf.copies.append(arena, .{ |
| 41 | .sub_path = wc.addString(sub_path) catch @panic("OOM"), |
| 42 | .src_file = src_file.dupe(graph), |
| 43 | }) catch @panic("OOM"); |
| 44 | |
| 45 | src_file.addStepDependencies(&usf.step); |
| 46 | } |
| 47 | |
| 48 | /// Overwrites a path relative to the package root with the provided bytes. |
| 49 | /// |
| 50 | /// Because it updates source files, this should not be used as part of the |
| 51 | /// normal build process, but as a utility occasionally run by a developer with |
| 52 | /// intent to modify source files and then commit those changes to version |
| 53 | /// control. |
| 54 | pub fn addBytesToSource(usf: *UpdateSourceFiles, contents: []const u8, sub_path: []const u8) void { |
| 55 | const graph = usf.step.owner.graph; |
| 56 | const wc = &graph.wip_configuration; |
| 57 | const arena = graph.arena; |
| 58 | |
| 59 | usf.embeds.append(arena, .{ |
| 60 | .sub_path = wc.addString(sub_path) catch @panic("OOM"), |
| 61 | .contents = wc.addBytes(contents) catch @panic("OOM"), |
| 62 | }) catch @panic("OOM"); |
| 63 | } |