|
| 1 | +function matnwb_exportTutorials(options) |
| 2 | +% matnwb_exportTutorials - Export mlx tutorial files to the specified output format |
| 3 | +% |
| 4 | +% Note: This function will ignore the following live scripts: |
| 5 | +% - basicUsage.mlx : depends on output from convertTrials.m |
| 6 | +% - read_demo.mlx : depends on external data, potentially slow |
| 7 | +% - remote_read.mlx : Uses nwbRead on s3 url, potentially very slow] |
| 8 | +% |
| 9 | +% To export all livescripts (assuming you have made sure the above-mentioned |
| 10 | +% files will run) call the function with IgnoreFiles set to empty, i.e: |
| 11 | +% matnwb_exportTutorials(..., "IgnoreFiles", string.empty) |
| 12 | + |
| 13 | + arguments |
| 14 | + options.ExportFormat (1,:) string {mustStartWithDot} = [".m", ".html"] |
| 15 | + options.Expression (1,1) string = "*" % Filter by expression |
| 16 | + options.FileNames (1,:) string = string.empty % Filter by file names |
| 17 | + options.FilePaths (1,:) string = string.empty % Export specified files |
| 18 | + options.IgnoreFiles (1,:) string = ["basicUsage", "read_demo", "remote_read"]; |
| 19 | + options.RunLivescript (1,1) logical = true |
| 20 | + end |
| 21 | + |
| 22 | + [exportFormat, targetFolderNames] = deal(options.ExportFormat); |
| 23 | + |
| 24 | + targetFolderNames = extractAfter(targetFolderNames, "."); |
| 25 | + targetFolderNames(strcmp(targetFolderNames, "m")) = fullfile("private", "mcode"); |
| 26 | + |
| 27 | + nwbTutorialDir = fullfile(misc.getMatnwbDir, "tutorials"); |
| 28 | + targetFolderPaths = fullfile(nwbTutorialDir, targetFolderNames); |
| 29 | + |
| 30 | + for folderPath = targetFolderPaths |
| 31 | + if ~isfolder(folderPath); mkdir(folderPath); end |
| 32 | + end |
| 33 | + |
| 34 | + if isempty(options.FilePaths) |
| 35 | + if endsWith(options.Expression, "*") |
| 36 | + expression = options.Expression + ".mlx"; |
| 37 | + else |
| 38 | + expression = options.Expression + "*.mlx"; |
| 39 | + end |
| 40 | + |
| 41 | + L = dir(fullfile(nwbTutorialDir, expression)); |
| 42 | + filePaths = string( fullfile({L.folder}, {L.name}) ); |
| 43 | + else |
| 44 | + filePaths = options.FilePaths; |
| 45 | + end |
| 46 | + |
| 47 | + [~, fileNames] = fileparts(filePaths); |
| 48 | + if ~isempty(options.FileNames) |
| 49 | + [fileNames, iA] = intersect(fileNames, options.FileNames, 'stable'); |
| 50 | + filePaths = filePaths(iA); |
| 51 | + end |
| 52 | + |
| 53 | + if ~isempty(options.IgnoreFiles) |
| 54 | + [~, fileNames] = fileparts(filePaths); |
| 55 | + [fileNames, iA] = setdiff(fileNames, options.IgnoreFiles, 'stable'); |
| 56 | + filePaths = filePaths(iA); |
| 57 | + end |
| 58 | + |
| 59 | + % Go to a temporary directory, so that tutorials are exported in a |
| 60 | + % temporary folder which is cleaned up afterwards |
| 61 | + currentDir = pwd(); |
| 62 | + cleanupWorkdir = onCleanup(@(fp) cd(currentDir)); |
| 63 | + |
| 64 | + tempDir = fullfile(tempdir, 'nwbTutorials'); |
| 65 | + if ~isfolder(tempDir); mkdir(tempDir); end |
| 66 | + disp('Changing into temporary directory:') |
| 67 | + cd(tempDir) |
| 68 | + |
| 69 | + cleanupDeleteTempFiles = onCleanup(@(fp) rmdir(tempDir, 's')); |
| 70 | + disp(tempDir) |
| 71 | + |
| 72 | + for i = 1:numel(filePaths) |
| 73 | + sourcePath = char( fullfile(filePaths(i)) ); |
| 74 | + if options.RunLivescript |
| 75 | + fprintf('Running livescript "%s"\n', fileNames(i)) |
| 76 | + |
| 77 | + matlab.internal.liveeditor.executeAndSave(sourcePath); |
| 78 | + end |
| 79 | + |
| 80 | + for j = 1:numel(exportFormat) |
| 81 | + targetPath = fullfile(targetFolderPaths(j), fileNames(i) + exportFormat(j)); |
| 82 | + fprintf('Exporting livescript "%s" to "%s"\n', fileNames(i), exportFormat(j)) |
| 83 | + export(sourcePath, strrep(targetPath, '.mlx', exportFormat(j))); |
| 84 | + end |
| 85 | + end |
| 86 | +end |
| 87 | + |
| 88 | +function mustStartWithDot(value) |
| 89 | + for i = 1:numel(value) |
| 90 | + assert(startsWith(value(i), '.'), ... |
| 91 | + 'Value must be a file extension starting with a period, e.g ".html"') |
| 92 | + end |
| 93 | +end |
0 commit comments