Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add "savedirtemp" generation option #540

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions +spec/generate.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
% optionally, include schema mapping as second argument OR path of specs
% schemaSource is either a path to a directory where the source is
% OR a containers.Map of filenames
namespaceText = convertStringsToChars(namespaceText);
schemaSource = convertStringsToChars(schemaSource);

Schema = spec.loadSchemaObject();
namespace = spec.schema2matlab(Schema.read(namespaceText));
Namespaces = spec.getNamespaceInfo(namespace);
Expand Down
33 changes: 14 additions & 19 deletions generateCore.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@ function generateCore(varargin)
% GENERATECORE(version) Generate classes for the
% core namespace of the listed version.
%
% A cache of schema data is generated in the 'namespaces' subdirectory in
% the current working directory. This is for allowing cross-referencing
% classes between multiple namespaces.
%
% Output files are generated placed in a '+types' subdirectory in the
% current working directory.
%
% GENERATECORE(__, 'savedir', saveDirectory) Generates the core class
% files in the specified directory.
%
% GENERATECORE(__, 'savedirtemp') Generates the core class files in the MATLAB temp directory.
%
% A cache of schema data is generated in the 'namespaces' subdirectory in
% the installation directory.
%
% Output files are generated placed in a '+types' subdirectory in the installation directory.
%
% Example:
% generateCore();
% generateCore('2.2.3');
%
% See also GENERATEEXTENSION

latestVersion = '2.6.0';

if nargin == 0 || strcmp(varargin{1}, 'savedir')

if isempty(varargin) ...
|| "savedir" == lower(varargin{1}) ...
|| "savedirtemp" == lower(varargin{1}) ...
|| "latest" == lower(varargin{1})
version = latestVersion;
else
version = varargin{1};
Expand All @@ -34,20 +37,12 @@ function generateCore(varargin)
varargin = varargin(2:end);
end

if strcmp(version, 'latest')
version = latestVersion;
end

schemaPath = fullfile(misc.getMatnwbDir(), 'nwb-schema', version);
corePath = fullfile(schemaPath, 'core', 'nwb.namespace.yaml');
commonPath = fullfile(schemaPath,...
'hdmf-common-schema', ...
'common',...
'namespace.yaml');
commonPath = fullfile(schemaPath, 'hdmf-common-schema', 'common', 'namespace.yaml');
assert(2 == exist(corePath, 'file'),...
'NWB:GenerateCore:MissingCoreSchema',...
'Cannot find suitable core namespace for schema version `%s`',...
version);
'Cannot find suitable core namespace for schema version `%s`', version);
if 2 == exist(commonPath, 'file')
generateExtension(commonPath, varargin{:});
end
Expand Down
94 changes: 54 additions & 40 deletions generateExtension.m
Original file line number Diff line number Diff line change
@@ -1,62 +1,76 @@
function generateExtension(varargin)
% GENERATEEXTENSION Generate Matlab classes from NWB extension schema file
% GENERATEEXTENSION(extension_path...) Generate classes
% GENERATEEXTENSION(ext1, ext2, ..., extn) Generate classes
% (Matlab m-files) from one or more NWB:N schema extension namespace
% files. A registry of already generated core types is used to resolve
% dependent types.
% files.
%
% GENERATEEXTENSION(__, 'savedir', location) Generates classes in a custom directory specified
% by file location.
%
% GENERATEEXTENSION(__, 'savedirtemp') Generates classes in the default MATLAB temp directory.
% This option will override any previously defined 'savedir' location.
%
% A cache of schema data is generated in the 'namespaces' subdirectory in
% the current working directory. This is for allowing cross-referencing
% classes between multiple namespaces.
% the installation directory (unless otherwise specified).
%
% Output files are generated placed in a '+types' subdirectory in the
% current working directory.
% By default, output files are generated placed in a '+types' subdirectory in the
% installation directory.
%
% Example:
% generateExtension('schema\myext\myextension.namespace.yaml', 'schema\myext2\myext2.namespace.yaml');
%
% See also GENERATECORE

for iOption = 1:length(varargin)
option = varargin{iOption};
validateattributes(option, {'char', 'string'}, {'scalartext'} ...
, 'generateExtension', 'extension name', iOption);
if isstring(option)
varargin{iOption} = char(option);
end

invalidArgumentErrorCode = 'NWB:GenerateExtension:InvalidArguments';

iOptionLocation = find(strcmp(varargin, 'savedir') | strcmp(varargin, 'savedirtemp'), 1);
if isempty(iOptionLocation)
iOptionLocation = length(varargin) + 1;

Check warning on line 28 in generateExtension.m

View check run for this annotation

Codecov / codecov/patch

generateExtension.m#L28

Added line #L28 was not covered by tests
end

saveDirMask = strcmp(varargin, 'savedir');
if any(saveDirMask)
assert(~saveDirMask(end),...
'NWB:GenerateExtenion:InvalidParameter',...
'savedir must be paired with the desired save directory.');
saveDir = varargin{find(saveDirMask, 1, 'last') + 1};
saveDirParametersMask = saveDirMask | circshift(saveDirMask, 1);
sourceList = varargin(~saveDirParametersMask);

assert(0 < min(iOptionLocation, length(varargin)), invalidArgumentErrorCode ...
, 'generateExtension requires at least one extension to generate from.');

options = varargin(iOptionLocation:end);
extensions = varargin(1:(iOptionLocation-1));

hasSaveDirTemp = any(strcmpi(varargin, 'savedirtemp'));
if hasSaveDirTemp
saveDir = fullfile(tempdir(), 'MatNWB');

Check warning on line 39 in generateExtension.m

View check run for this annotation

Codecov / codecov/patch

generateExtension.m#L39

Added line #L39 was not covered by tests
else
saveDir = misc.getMatnwbDir();
sourceList = varargin;
iSaveDir = find(strcmpi(options, 'savedir'));
if isempty(iSaveDir)
saveDir = misc.getMatnwbDir();

Check warning on line 43 in generateExtension.m

View check run for this annotation

Codecov / codecov/patch

generateExtension.m#L43

Added line #L43 was not covered by tests
elseif iSaveDir(end) < length(options)
saveDir = options{iSaveDir(end) + 1};
assert(1 ~= isfile(saveDir) ...
, invalidArgumentErrorCode ...
, 'provided save directory "%s" must be a valid directory path.', saveDir);
else
error(invalidArgumentErrorCode ...
, 'incomplete or erroneous savedir argument order.');

Check warning on line 51 in generateExtension.m

View check run for this annotation

Codecov / codecov/patch

generateExtension.m#L50-L51

Added lines #L50 - L51 were not covered by tests
end
end

for iNamespaceFiles = 1:length(sourceList)
source = sourceList{iNamespaceFiles};
validateattributes(source, {'char', 'string'}, {'scalartext'});

[localpath, ~, ~] = fileparts(source);
assert(2 == exist(source, 'file'),...
'NWB:GenerateExtension:FileNotFound', 'Path to file `%s` could not be found.', source);

assert(all(cellfun('isclass', extensions, 'char') | cellfun('isclass', extensions, 'string')) ...
, invalidArgumentErrorCode ...
, 'extensions must be character arrays or strings.');

for iExtension = 1:length(extensions)
source = string(extensions{iExtension});
assert(1 == isfile(source) ...
, invalidArgumentErrorCode ...
, 'extension file "%s" not found.', source);
fid = fopen(source);
namespaceText = fread(fid, '*char') .';
fclose(fid);

Namespaces = spec.generate(namespaceText, localpath);

Namespaces = spec.generate(namespaceText, fileparts(source));
for iNamespace = 1:length(Namespaces)
Namespace = Namespaces(iNamespace);
spec.saveCache(Namespace, saveDir);
file.writeNamespace(Namespace.name, saveDir);
N = Namespaces(iNamespace);
spec.saveCache(N, saveDir);
file.writeNamespace(N.name, saveDir);
rehash();
end
end
addpath(saveDir);
end