]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/blocks/rpp_update_doc.m
Extend rpp_update_doc to update block names
[pes-rpp/rpp-simulink.git] / rpp / blocks / rpp_update_doc.m
1 % Copyright (C) 2013-2014 Czech Technical University in Prague
2 %
3 % Authors:
4 %     - Carlos Jenkins <carlos@jenkins.co.cr>
5 %     - Michal Sojka <sojkam1@fel.cvut.cz>
6 %
7 % This document contains proprietary information belonging to Czech
8 % Technical University in Prague. Passing on and copying of this
9 % document, and communication of its contents is not permitted
10 % without prior written authorization.
11 %
12 % File : compile_blocks.m
13 % Abstract:
14 %     Compile all the C-MEX S-Function in current folder.
15 %
16 % References:
17 %     http://www.mathworks.com/help/matlab/ref/mex.html
18 %     http://www.mathworks.com/help/matlab/matlab_external/custom-building-mex-files.html
19
20 function updated = rpp_update_doc(varargin)
21 % RPP_UPDATE_DOC Update built-in S-functions documentation.
22 %    The documentation is updated based on the YAML comment in .c files.
23 %    The number of updated blocks is returned.
24 if nargin > 0,
25     op = varargin{1};
26 else
27     op = 'update';
28 end
29 open('rpp_lib');
30 set_param('rpp_lib', 'Lock', 'off');
31 blocks = find_system('rpp_lib', 'Type', 'block');
32 updated = 0;
33 for i=1:length(blocks),
34     block = blocks{i};
35     params = get_param(block, 'ObjectParameters');
36     fields = fieldnames(params);
37
38     disp(['Processing ' blocks{i}]);
39
40     updated = updated + process_param(op, block, 'MaskType', '--masktype');
41     updated = updated + process_param(op, block, 'MaskDescription', '--html --printdesc');
42     updated = updated + process_param(op, block, 'MaskPromptString', '--maskpromptstring');
43     updated = updated + process_param(op, block, 'MaskHelp', '--html --printhelp');
44     updated = updated + process_param(op, block, 'Name', '--name'); % Name must be changed last!!!
45 end
46 set_param('rpp_lib', 'Lock', 'on');
47
48 function output = run_doc_parse(fn, opts)
49     % We have to unset Matlab's library path in order to run pandoc - it
50     % needs newer libc than the one shipped with Matlab
51     [rc, output] = system(['unset LD_LIBRARY_PATH; scripts/doc_parse.py ' opts ' ' fn '.c']);
52     if rc ~= 0,
53         disp(output)
54         throw(MException('rpp:pandoc_failure', ['Failed to extract doc from ' fn '.c']))
55     end
56
57 function print_diff(fn, param, old, new)
58     fnold = [fn '.' param '.old'];
59     f = fopen(fnold, 'w');
60     fprintf(f, '%s', old);
61     fclose(f);
62
63     fnnew = [fn '.' param '.new'];
64     f = fopen(fnnew, 'w');
65     fprintf(f, '%s', new);
66     fclose(f);
67
68     [rc, diff] = system(['diff -u ' fnold ' ' fnnew]);
69     disp(diff);
70     delete(fnold);
71     delete(fnnew);
72
73 function updated = process_param(op, block, param, opts)
74     fn = get_param(block, 'FunctionName');
75     new_content = run_doc_parse(fn, opts);
76     old_content = get_param(block, param);
77
78     if strcmp(param, 'MaskType') || ...
79        strcmp(param, 'MaskPromptString') || ...
80        strcmp(param, 'Name'),
81         new_content = deblank(new_content);
82     end
83
84     if ~strcmp(old_content, new_content),
85         if strcmp(op, 'diff'),
86             disp([block ': ' param ' not up-to-date'])
87             print_diff(fn, param, old_content, new_content)
88         else
89             set_param(block, param, new_content)
90         end
91         updated = 1;
92     else
93         updated = 0;
94     end