-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildfile.m
More file actions
419 lines (365 loc) · 13.2 KB
/
Copy pathbuildfile.m
File metadata and controls
419 lines (365 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
function plan = buildfile
%BUILDFILE LabKit build and validation entry points.
plan = buildplan(localfunctions);
plan.DefaultTasks = "test";
catalog = taskCatalog();
for k = 1:numel(catalog)
plan(catalog(k).Name).Description = catalog(k).Description;
end
end
function checkStyleTask(~)
runCatalogTask("checkStyle");
end
function testTask(~)
runCatalogTask("test");
end
function testUnitTask(~)
runCatalogTask("testUnit");
end
function testIntegrationTask(~)
runCatalogTask("testIntegration");
end
function testProjectTask(~)
runCatalogTask("testProject");
end
function testLabkitTask(~)
runCatalogTask("testLabkit");
end
function testLabkitGuiTask(~)
runCatalogTask("testLabkitGui");
end
function testAppsTask(~)
runCatalogTask("testApps");
end
function testAppsGuiTask(~)
runCatalogTask("testAppsGui");
end
function testGuiStructuralTask(~)
runCatalogTask("testGuiStructural");
end
function testGuiGestureTask(~)
runCatalogTask("testGuiGesture");
end
function coverageTask(~)
runCatalogTask("coverage");
end
function listTasksTask(~)
printTaskCatalog(taskCatalog());
end
function checkProjectTask(~)
root = fileparts(mfilename("fullpath"));
checkProjectDefinition(root);
end
function packageDryRunTask(~)
root = fileparts(mfilename("fullpath"));
packageCandidates = [ ...
"+labkit", ...
"apps", ...
"docs", ...
"scripts", ...
"README.md", ...
"labkit_launcher.m", ...
"buildfile.m", ...
"startup_labkit.m"];
validationOnly = [ ...
"tests", ...
"AGENTS.md"];
excludedGeneratedOrLocal = [ ...
"artifacts", ...
"photos", ...
"derived", ...
"project", ...
"resources/project", ...
"LabKit.prj", ...
".git", ...
"LABKIT_REFACTOR_ROADMAP.md"];
assertRelativePathsExist(root, packageCandidates);
assertRelativePathsExist(root, validationOnly);
report = struct( ...
"schemaVersion", 1, ...
"packageCandidates", {cellstr(packageCandidates)}, ...
"validationOnly", {cellstr(validationOnly)}, ...
"excludedGeneratedOrLocal", {cellstr(excludedGeneratedOrLocal)}, ...
"createsToolbox", false);
reportFile = writePackageDryRunReport(root, report);
fprintf("LabKit package dry run wrote:\n %s\n", reportFile);
fprintf("Package candidates: %d, validation-only roots/files: %d\n", ...
numel(packageCandidates), numel(validationOnly));
end
function catalog = taskCatalog()
catalog = [ ...
taskSpec("checkStyle", "Run project/style guardrails.", "Suites", "project", "Tags", "Style"), ...
taskSpec("test", "Run the full non-GUI test entry point.", "IncludeGui", false), ...
taskSpec("testUnit", "Run official unit tests.", "Tags", "Unit"), ...
taskSpec("testIntegration", "Run official contract tests.", "Tags", "Integration"), ...
taskSpec("testProject", "Run project guardrails.", "Suites", "project"), ...
taskSpec("testLabkit", "Run all reusable labkit non-GUI tests.", "Suites", "labkit", "IncludeGui", false), ...
taskSpec("testLabkitGui", "Run all reusable labkit GUI tests.", "Suites", "labkit", "IncludeGui", true), ...
taskSpec("testApps", "Run all app-owned non-GUI tests.", "Suites", "apps", "IncludeGui", false), ...
taskSpec("testAppsGui", "Run all app-owned GUI tests.", "Suites", "apps", "IncludeGui", true), ...
taskSpec("testGuiStructural", "Run noninteractive GUI structural tests.", "Suites", "gui", "Tags", "Structural", "IncludeGui", true), ...
taskSpec("testGuiGesture", "Run noninteractive GUI gesture tests.", "Tags", "Gesture", "IncludeGui", true), ...
taskSpec("coverage", "Run official tests with coverage artifacts.", "Tags", ["Unit", "Integration"], "IncludeCoverage", true), ...
taskSpec("listTasks", "List official LabKit build tasks.", "RunTests", false), ...
taskSpec("checkProject", "Verify optional local MATLAB Project metadata when present.", "RunTests", false), ...
taskSpec("packageDryRun", "Verify package boundary inventory without exporting.", "RunTests", false)];
end
function spec = taskSpec(name, description, varargin)
p = inputParser;
p.FunctionName = "taskSpec";
p.addParameter("RunTests", true, @isLogicalScalar);
p.addParameter("Suites", strings(1, 0), @isStringLikeList);
p.addParameter("Tags", strings(1, 0), @isStringLikeList);
p.addParameter("IncludeGui", [], @isEmptyOrLogicalScalar);
p.addParameter("IncludeCoverage", [], @isEmptyOrLogicalScalar);
p.addParameter("Required", true, @isLogicalScalar);
p.parse(varargin{:});
runTests = logical(p.Results.RunTests);
spec = struct( ...
"Name", string(name), ...
"Description", string(description), ...
"RunTests", runTests, ...
"Suites", normalizeTextList(p.Results.Suites), ...
"Tags", normalizeTextList(p.Results.Tags), ...
"IncludeGui", normalizeOptionalLogical(p.Results.IncludeGui), ...
"IncludeCoverage", normalizeOptionalLogical(p.Results.IncludeCoverage), ...
"Required", runTests && logical(p.Results.Required));
end
function runCatalogTask(runName)
spec = findTaskSpec(runName);
if ~spec.RunTests
error("LabKit:Build:CatalogTaskNotRunnable", ...
"Build task %s is not a test-runner task.", runName);
end
args = taskRunArguments(spec);
runBuildTests(spec.Name, args{:});
end
function spec = findTaskSpec(runName)
catalog = taskCatalog();
matches = [catalog.Name] == string(runName);
if ~any(matches)
error("LabKit:Build:UnknownCatalogTask", ...
"Unknown build task catalog entry: %s.", runName);
end
spec = catalog(matches);
end
function args = taskRunArguments(spec)
args = {};
if ~isempty(spec.Suites)
args = [args, {"Suites", spec.Suites}];
end
if ~isempty(spec.Tags)
args = [args, {"Tags", spec.Tags}];
end
if ~isempty(spec.IncludeGui)
args = [args, {"IncludeGui", spec.IncludeGui}];
end
if ~isempty(spec.IncludeCoverage)
args = [args, {"IncludeCoverage", spec.IncludeCoverage}];
end
end
function runBuildTests(runName, varargin)
root = fileparts(mfilename("fullpath"));
addpath(fullfile(root, "tests"));
runLabKitTests(varargin{:}, ...
"RunName", runName, ...
"ArtifactsRoot", fullfile(root, "artifacts"));
end
function printTaskCatalog(catalog)
fprintf("LabKit build tasks:\n");
for k = 1:numel(catalog)
fprintf(" %-30s %s\n", catalog(k).Name, catalog(k).Description);
end
end
function checkProjectDefinition(root)
projectFile = fullfile(root, "LabKit.prj");
if exist(projectFile, "file") ~= 2
fprintf("No local MATLAB Project file found at:\n %s\n", projectFile);
fprintf("Run labkit_ProjectGovernance_app to create one for local IDE use.\n");
return;
end
[proj, shouldCloseProject] = openLabKitProject(projectFile, root);
cleanup = onCleanup(@() closeProjectIfLoaded(proj, shouldCloseProject));
if string(proj.Name) ~= "LabKit"
error("LabKit:Build:ProjectName", ...
"Expected project name LabKit, found %s.", string(proj.Name));
end
if normalizePath(proj.RootFolder) ~= normalizePath(root)
error("LabKit:Build:ProjectRoot", ...
"Project root does not match repository root.");
end
expectedPaths = expectedProjectPaths(root);
actualPaths = normalizePaths(projectEntryPaths(proj.ProjectPath));
for k = 1:numel(expectedPaths)
if ~any(actualPaths == normalizePath(expectedPaths(k)))
error("LabKit:Build:ProjectPath", ...
"Project path is missing required folder: %s", expectedPaths(k));
end
end
assertNoHiddenProjectPath(root, actualPaths);
startupFiles = normalizePaths(projectEntryPaths(proj.StartupFiles));
if ~any(startupFiles == normalizePath(fullfile(root, "startup_labkit.m")))
error("LabKit:Build:ProjectStartup", ...
"Project startup files must include startup_labkit.m.");
end
fprintf("LabKit MATLAB Project metadata verified.\n");
clear cleanup
end
function [proj, shouldCloseProject] = openLabKitProject(projectFile, root)
shouldCloseProject = true;
try
proj = currentProject;
if normalizePath(proj.RootFolder) == normalizePath(root)
shouldCloseProject = false;
return;
end
catch
end
proj = openProject(projectFile);
end
function paths = expectedProjectPaths(root)
paths = string(root);
appsRoot = fullfile(root, "apps");
if exist(appsRoot, "dir") == 7
paths = [paths, string(appsRoot), appPathDirs(appsRoot)];
end
paths = unique(paths, "stable");
end
function dirs = appPathDirs(appRoot)
entries = dir(fullfile(appRoot, "**"));
entries = entries([entries.isdir]);
[~, order] = sort(string(fullfile({entries.folder}, {entries.name})));
entries = entries(order);
dirs = strings(1, numel(entries));
dirCount = 0;
for k = 1:numel(entries)
entry = entries(k);
if strcmp(entry.name, ".") || strcmp(entry.name, "..")
continue;
end
child = string(fullfile(entry.folder, entry.name));
if normalizePath(child) == normalizePath(appRoot) || ...
~isProjectPathCandidate(appRoot, child)
continue;
end
dirCount = dirCount + 1;
dirs(dirCount) = child;
end
dirs = dirs(1:dirCount);
end
function tf = isProjectPathCandidate(appRoot, folder)
rel = extractAfter(normalizePath(folder), strlength(normalizePath(appRoot)) + 1);
parts = split(rel, "/");
tf = ~any(startsWith(parts, ".") | startsWith(parts, "+") | ...
startsWith(parts, "@") | parts == "private");
end
function paths = projectEntryPaths(entries)
if isempty(entries)
paths = strings(1, 0);
elseif isstring(entries) || ischar(entries) || iscellstr(entries)
paths = string(entries);
else
paths = strings(1, numel(entries));
for k = 1:numel(entries)
if isprop(entries(k), "File")
paths(k) = string(entries(k).File);
else
paths(k) = string(entries(k));
end
end
end
end
function assertNoHiddenProjectPath(root, actualPaths)
rootPath = normalizePath(root);
for k = 1:numel(actualPaths)
path = actualPaths(k);
if path == rootPath
continue;
end
if startsWith(path, rootPath + "/")
relativePath = extractAfter(path, strlength(rootPath) + 1);
else
relativePath = path;
end
parts = split(relativePath, "/");
if any(parts == "private" | startsWith(parts, "+") | ...
startsWith(parts, "@") | startsWith(parts, "."))
error("LabKit:Build:ProjectHiddenPath", ...
"Project path includes private/package/hidden folder: %s", path);
end
end
end
function assertRelativePathsExist(root, relativePaths)
for k = 1:numel(relativePaths)
path = fullfile(root, relativePaths(k));
if exist(path, "file") ~= 2 && exist(path, "dir") ~= 7
error("LabKit:Build:PackageDryRunMissingPath", ...
"Package dry run expected path is missing: %s", relativePaths(k));
end
end
end
function reportFile = writePackageDryRunReport(root, report)
reportDir = fullfile(root, "artifacts", "package");
if exist(reportDir, "dir") ~= 7
mkdir(reportDir);
end
reportFile = fullfile(reportDir, "package-dry-run.json");
fid = fopen(reportFile, "w");
if fid < 0
error("LabKit:Build:PackageDryRunReport", ...
"Could not write package dry-run report: %s", reportFile);
end
cleanup = onCleanup(@() fclose(fid));
fwrite(fid, jsonencode(report), "char");
clear cleanup
end
function values = normalizeTextList(values)
if isempty(values)
values = strings(1, 0);
elseif ischar(values)
values = string({values});
elseif iscell(values)
values = string(values);
else
values = string(values);
end
values = values(:).';
values = values(strlength(values) > 0);
end
function value = normalizeOptionalLogical(value)
if isempty(value)
return;
end
value = logical(value);
end
function tf = isStringLikeList(value)
tf = ischar(value) || isstring(value) || iscellstr(value);
end
function tf = isLogicalScalar(value)
tf = (islogical(value) || isnumeric(value)) && isscalar(value);
end
function tf = isEmptyOrLogicalScalar(value)
tf = isempty(value) || isLogicalScalar(value);
end
function normalized = normalizePaths(paths)
normalized = strings(size(paths));
for k = 1:numel(paths)
normalized(k) = normalizePath(paths(k));
end
end
function normalized = normalizePath(path)
normalized = replace(string(path), "\", "/");
normalized = regexprep(normalized, "/+$", "");
normalized = lower(normalized);
end
function closeProjectIfLoaded(proj, shouldCloseProject)
if ~shouldCloseProject || isempty(proj) || ~isvalid(proj)
return;
end
try
if proj.isLoaded
proj.close;
end
catch
end
end