]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/blocks/rpp_update_doc.m
1475a204d73f0e977e22e572d2d47e939f9b5894
[pes-rpp/rpp-simulink.git] / rpp / blocks / rpp_update_doc.m
1 % Copyright (C) 2013-2015 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 block_libs = rpp_get_blocks();
30 updated = 0;
31 for i=1:length(block_libs),
32     block_lib = block_libs{i};
33     last_updated = updated;
34     load_system(block_lib);
35     set_param(block_lib, 'Lock', 'off');
36     blocks = find_system(block_lib, 'Type', 'block');
37     for j=1:length(blocks),
38         block = blocks{j};
39         params = get_param(block, 'ObjectParameters');
40         fields = fieldnames(params);
41
42         disp(['Processing ' blocks{j}]);
43         updated = updated + process_param(op, block, 'MaskType', '--masktype');
44         updated = updated + process_param(op, block, 'MaskDescription', '--html --printdesc');
45         updated = updated + process_param(op, block, 'MaskPromptString', '--maskpromptstring');
46         updated = updated + process_param(op, block, 'MaskHelp', '--html --printhelp');
47         updated = updated + process_param(op, block, 'Name', '--name'); % Name must be changed last!!!
48     end
49     set_param(block_lib, 'Lock', 'on');
50     if strcmp(op, 'update') && updated ~= last_updated,
51         save_system(block_lib);
52     end
53     close_system(block_lib);
54 end
55
56 function output = run_doc_parse(fn, opts)
57     % We have to unset Matlab's library path in order to run pandoc - it
58     % needs newer libc than the one shipped with Matlab
59     [rc, output] = system(['unset LD_LIBRARY_PATH; scripts/doc_parse.py ' opts ' ' fn '.c']);
60     if rc ~= 0,
61         disp(output)
62         throw(MException('rpp:pandoc_failure', ['Failed to extract doc from ' fn '.c']))
63     end
64
65 function print_diff(fn, param, old, new, diffopts)
66     fnold = [fn '.' param '.old'];
67     f = fopen(fnold, 'w');
68     fprintf(f, '%s', old);
69     fclose(f);
70
71     fnnew = [fn '.' param '.new'];
72     f = fopen(fnnew, 'w');
73     fprintf(f, '%s', new);
74     fclose(f);
75
76     [rc, diff] = system(['git --no-pager diff --no-index ' diffopts ' --word-diff=plain ' fnold ' ' fnnew]);
77     disp(diff);
78     delete(fnold);
79     delete(fnnew);
80
81 function updated = process_param(op, block, param, opts)
82     fn = get_param(block, 'FunctionName');
83     new_content = run_doc_parse(fn, opts);
84     old_content = get_param(block, param);
85
86     if strcmp(param, 'MaskType') || ...
87        strcmp(param, 'MaskPromptString') || ...
88        strcmp(param, 'Name'),
89         new_content = deblank(new_content);
90     end
91
92     if ~strcmp(old_content, new_content),
93         if strcmp(op, 'diff'),
94             disp([block ': ' param ' not up-to-date'])
95             print_diff(fn, param, old_content, new_content, '')
96         else
97             set_param(block, param, new_content)
98             print_diff(fn, param, old_content, new_content, '--no-color')
99         end
100         updated = 1;
101     else
102         updated = 0;
103     end