|
1 | 1 | const std = @import("std"); |
2 | 2 |
|
3 | | -pub fn build(b: *std.Build) void { |
| 3 | +pub fn build(b: *std.Build) !void { |
| 4 | + checkGitLfsContent() catch { |
| 5 | + try ensureGit(b.allocator); |
| 6 | + try ensureGitLfs(b.allocator, "install"); |
| 7 | + try ensureGitLfs(b.allocator, "pull"); |
| 8 | + try checkGitLfsContent(); |
| 9 | + }; |
| 10 | + |
4 | 11 | const zopenvr = b.addModule("root", .{ |
5 | 12 | .root_source_file = b.path("src/openvr.zig"), |
6 | 13 | }); |
@@ -120,3 +127,81 @@ pub fn installOpenVR( |
120 | 127 | else => {}, |
121 | 128 | } |
122 | 129 | } |
| 130 | + |
| 131 | +fn ensureGit(allocator: std.mem.Allocator) !void { |
| 132 | + const printErrorMsg = (struct { |
| 133 | + fn impl() void { |
| 134 | + std.log.err("\n" ++ |
| 135 | + \\--------------------------------------------------------------------------- |
| 136 | + \\ |
| 137 | + \\'git version' failed. Is Git not installed? |
| 138 | + \\ |
| 139 | + \\--------------------------------------------------------------------------- |
| 140 | + \\ |
| 141 | + , .{}); |
| 142 | + } |
| 143 | + }).impl; |
| 144 | + const argv = &[_][]const u8{ "git", "version" }; |
| 145 | + const result = std.process.Child.run(.{ |
| 146 | + .allocator = allocator, |
| 147 | + .argv = argv, |
| 148 | + }) catch { // e.g. FileNotFound |
| 149 | + printErrorMsg(); |
| 150 | + return error.GitNotFound; |
| 151 | + }; |
| 152 | + defer { |
| 153 | + allocator.free(result.stderr); |
| 154 | + allocator.free(result.stdout); |
| 155 | + } |
| 156 | + if (result.term.Exited != 0) { |
| 157 | + printErrorMsg(); |
| 158 | + return error.GitNotFound; |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +fn ensureGitLfs(allocator: std.mem.Allocator, cmd: []const u8) !void { |
| 163 | + const printNoGitLfs = (struct { |
| 164 | + fn impl() void { |
| 165 | + std.log.err("\n" ++ |
| 166 | + \\--------------------------------------------------------------------------- |
| 167 | + \\ |
| 168 | + \\Please install Git LFS (Large File Support) extension and run 'zig build' again. |
| 169 | + \\ |
| 170 | + \\For more info about Git LFS see: https://git-lfs.github.com/ |
| 171 | + \\ |
| 172 | + \\--------------------------------------------------------------------------- |
| 173 | + \\ |
| 174 | + , .{}); |
| 175 | + } |
| 176 | + }).impl; |
| 177 | + const argv = &[_][]const u8{ "git", "lfs", cmd }; |
| 178 | + const result = std.process.Child.run(.{ |
| 179 | + .allocator = allocator, |
| 180 | + .argv = argv, |
| 181 | + }) catch { // e.g. FileNotFound |
| 182 | + printNoGitLfs(); |
| 183 | + return error.GitLfsNotFound; |
| 184 | + }; |
| 185 | + defer { |
| 186 | + allocator.free(result.stderr); |
| 187 | + allocator.free(result.stdout); |
| 188 | + } |
| 189 | + if (result.term.Exited != 0) { |
| 190 | + printNoGitLfs(); |
| 191 | + return error.GitLfsNotFound; |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +fn checkGitLfsContent() !void { |
| 196 | + const expected_contents = |
| 197 | + \\DO NOT EDIT OR DELETE |
| 198 | + \\This file is used to check if Git LFS content has been downloaded |
| 199 | + ; |
| 200 | + var buf: [expected_contents.len]u8 = undefined; |
| 201 | + _ = std.fs.cwd().readFile(".lfs-content-token", &buf) catch { |
| 202 | + return error.GitLfsContentTokenNotFound; |
| 203 | + }; |
| 204 | + if (!std.mem.eql(u8, expected_contents, &buf)) { |
| 205 | + return error.GitLfsContentCheckFailed; |
| 206 | + } |
| 207 | +} |
0 commit comments