Skip to content

Commit 01cfa5c

Browse files
committed
publish: functionalize
1 parent 93faa01 commit 01cfa5c

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

private/coverage_run.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
%% COVERAGE_RUN
2+
% called from buildfile.m
3+
4+
function coverage_run(pkg, test_dir)
5+
6+
import matlab.unittest.TestRunner
7+
import matlab.unittest.Verbosity
8+
import matlab.unittest.plugins.CodeCoveragePlugin
9+
import matlab.unittest.plugins.XMLPlugin
10+
import matlab.unittest.plugins.codecoverage.CoberturaFormat
11+
12+
suite = testsuite(test_dir);
13+
14+
% not import to allow use of rest of buildfile with R2022b
15+
format = matlab.unittest.plugins.codecoverage.CoverageResult;
16+
17+
18+
runner = TestRunner.withTextOutput();
19+
runner.addPlugin(CodeCoveragePlugin.forPackage(pkg, Producing=format))
20+
21+
% runner.addPlugin(XMLPlugin.producingJUnitFormat('test-results.xml'))
22+
% runner.addPlugin(CodeCoveragePlugin.forPackage(pkg, 'Producing', ...
23+
% CoberturaFormat('test-coverage.xml')))
24+
25+
r = runner.run(suite);
26+
assert(~isempty(r), "no tests found")
27+
28+
assertSuccess(r)
29+
30+
generateHTMLReport(format.Result)
31+
32+
end

private/publish_gen_index_html.m

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
%% PUBLISH_GEN_INDEX_HTML generate index.html for package docs
2+
% publish (generate) docs from Matlab project
3+
% called from buildfile.m
4+
%
5+
% Ref:
6+
% * https://www.mathworks.com/help/matlab/ref/publish.html
7+
% * https://www.mathworks.com/help/matlab/matlab_prog/marking-up-matlab-comments-for-publishing.html
8+
%
9+
% for package code -- assumes no classes and depth == 1
10+
function publish_gen_index_html(pkg_name, tagline, outdir)
11+
12+
pkg = what("+" + pkg_name);
13+
% "+" avoids picking up cwd of same name
14+
assert(~isempty(pkg), pkg_name + " is not detected as a Matlab directory or package")
15+
16+
%% generate docs
17+
readme = fullfile(outdir, "index.html");
18+
19+
if ~isfolder(outdir)
20+
mkdir(outdir);
21+
end
22+
23+
txt = ["<!DOCTYPE html> <head> <title>" + pkg_name + " API</title> <body>", ...
24+
"<h1>" + pkg_name + " API</h1>", ...
25+
tagline, ...
26+
"<h2>API Reference</h2>"];
27+
fid = fopen(readme, 'w');
28+
fprintf(fid, join(txt, "\n"));
29+
30+
for sub = pkg.m.'
31+
32+
s = sub{1};
33+
[~, name] = fileparts(s);
34+
doc_fn = publish(pkg_name + "." + name, evalCode=false, outputDir=outdir);
35+
disp(doc_fn)
36+
37+
% inject summary into Readme.md
38+
summary = split(string(help(pkg_name + "." + name)), newline);
39+
words = split(strip(summary(1)), " ");
40+
41+
% purposefully this will error if no docstring
42+
fname = words(1);
43+
if(lower(fname) ~= lower(name))
44+
error("fname %s does not match name %s", fname, name)
45+
end
46+
47+
line = "<a href=" + name + ".html>" + fname + "</a> ";
48+
if(length(words) > 1)
49+
line = line + join(words(2:end));
50+
end
51+
52+
fprintf(fid, line + "<br>\n");
53+
54+
end
55+
56+
fprintf(fid, "</body> </html>");
57+
58+
fclose(fid);
59+
60+
end

0 commit comments

Comments
 (0)