comparison m-toolbox/classes/+utils/@modules/buildModule.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % BUILDMODULE builds a new module structure in the location specified by
2 % the user.
3 %
4 % CALL: utils.modules.buildModule(dir, module_name)
5 %
6 % INPUTS:
7 % dir - the directory in which to build the module
8 % module_name - a name for the new module
9 %
10 % Example:
11 %
12 % utils.modules.buildModule('~/work', 'myNewModule')
13 %
14 %
15 % The resulting module structure on disk looks like:
16 %
17 % module/
18 % |- classes
19 % |- functions
20 % |- jar
21 % |- models
22 % |- tests
23 % |- classes
24 % |- models
25 %
26 %
27 % M Hewitson 21-01-11
28 %
29 % $Id: buildModule.m,v 1.4 2011/03/28 11:26:57 hewitson Exp $
30 %
31 function varargout = buildModule(varargin)
32
33 if nargin ~= 2
34 help(['utils.modules.' mfilename]);
35 error('incorrect inputs');
36 end
37
38 % Inputs
39 mdir = varargin{1};
40 if ~ischar(mdir)
41 error('The first input should be a string indicating a directory on disk');
42 end
43
44 mname = varargin{2};
45 if ~ischar(mname)
46 error('The second input should be a string indicating the module name');
47 end
48
49 % Make module
50 mpath = fullfile(mdir, mname);
51 paths = {'classes', 'functions', 'jar', 'models', 'pipelines', 'examples', 'tests', ...
52 fullfile('tests', 'classes'), fullfile('tests', 'models')};
53
54 % Check if the directory for the new module exists
55 if isdir(mpath)
56 r = input(sprintf('A directory exists at the chosen location (%s). \nDo you want to overwrite it? (yes/no) ', mpath), 's');
57 if ~strcmpi(r, 'yes')
58 return;
59 end
60 end
61
62 % Make module dir
63 [success,message,messageid] = mkdir(mdir,mname);
64 if ~success
65 error(messageid, 'Failed to make module directory. %s', message);
66 end
67
68 % Make all the sub directories
69 for kk=1:numel(paths)
70 p = paths{kk};
71 [success,message,messageid] = mkdir(mpath, p);
72 if ~success
73 error(messageid, 'Failed to make module directory %s.\n %s', fullfile(mpath, p), message);
74 end
75 end
76
77 % create README
78 fd = fopen(fullfile(mpath, 'README.txt'), 'w+');
79 fprintf(fd, 'LTPDA Module %s\n', mname);
80 fprintf(fd, '\n');
81 fprintf(fd, '\n');
82 fprintf(fd, 'For further details see the following README files:\n');
83 fprintf(fd, ' classes/README_classes.txt\n');
84 fprintf(fd, ' functions/README_functions.txt\n');
85 fprintf(fd, ' jar/README_jar.txt\n');
86 fprintf(fd, ' models/README_models.txt\n');
87 fprintf(fd, ' pipelines/README_pipelines.txt\n');
88 fprintf(fd, ' tests/README_tests.txt\n');
89 fprintf(fd, ' tests/classes/README_class_tests.txt\n');
90 fprintf(fd, ' tests/models/README_model_tests.txt\n');
91 fprintf(fd, '\n');
92 fprintf(fd, '\n');
93 % copy in the README files
94 src = fileparts(which('utils.modules.buildModule'));
95 installREADME(src, 'README_classes.txt', mpath, 'classes');
96 installREADME(src, 'README_functions.txt', mpath, 'functions');
97 installREADME(src, 'README_jar.txt', mpath, 'jar');
98 installREADME(src, 'README_models.txt', mpath, 'models');
99 installREADME(src, 'README_pipelines.txt', mpath, 'pipelines');
100 installREADME(src, 'README_tests.txt', mpath, 'tests');
101 installREADME(src, 'README_class_tests.txt', mpath, fullfile('tests', 'classes'));
102 installREADME(src, 'README_model_tests.txt', mpath, fullfile('tests', 'models'));
103
104
105 % Write moduleinfo.xml
106 writeModuleInfoXML(mpath, mname);
107
108 ls(mpath)
109 fprintf('* Module built successfully at %s\n', mpath);
110
111 end
112
113 function writeModuleInfoXML(mpath, name)
114
115 docNode = com.mathworks.xml.XMLUtils.createDocument('moduleinfo');
116 docRootNode = docNode.getDocumentElement;
117 docRootNode.setAttribute('name',name);
118 docRootNode.setAttribute('version','1.0');
119
120
121 % Save the sample XML document.
122 xmlFileName = fullfile(mpath,'moduleinfo.xml');
123 xmlwrite(xmlFileName,docNode);
124
125 end
126
127 function installREADME(src, name, mpath, mdir)
128
129 src = fullfile(src, name);
130 dest = fullfile(mpath, mdir);
131 [success,message,messageid] = copyfile(src, dest);
132 if ~success
133 error(messageid, 'Failed to copy %s to %s', src, dest);
134 end
135
136 end
137
138 % END