|
5 | 5 | # |
6 | 6 | # SPDX-License-Identifier: GPL-2.0-or-later |
7 | 7 |
|
| 8 | +# Given a string containing a ".", , return its suffix, |
| 9 | +# i.e. the bit after the last ".". For example, given "test.txt", |
| 10 | +# it returns "txt". |
| 11 | +BindGlobal( "AUTODOC_GetSuffix", |
| 12 | +function(str) |
| 13 | + local i; |
| 14 | + i := Length(str); |
| 15 | + while i > 0 and str[i] <> '.' do i := i - 1; od; |
| 16 | + if i = 0 then return ""; fi; |
| 17 | + return str{[i+1..Length(str)]}; |
| 18 | +end ); |
| 19 | + |
| 20 | +# Scan the given (by name) subdirs of a package dir for |
| 21 | +# files with one of the given extensions, and return the corresponding |
| 22 | +# filenames, as relative paths (relative to the package dir). |
| 23 | +# |
| 24 | +# For example, the invocation |
| 25 | +# AUTODOC_FindMatchingFiles(pkgdir, [ "gap/" ], [ "gi", "gd" ]); |
| 26 | +# might return a list looking like |
| 27 | +# [ "gap/AutoDocMainFunction.gd", "gap/AutoDocMainFunction.gi", ... ] |
| 28 | +BindGlobal( "AUTODOC_FindMatchingFiles", |
| 29 | +function (pkgdir, subdirs, extensions) |
| 30 | + local result, JoinRelativePath, AddMatchingFiles, d_rel; |
| 31 | + |
| 32 | + result := []; |
| 33 | + |
| 34 | + JoinRelativePath := function( dir, entry ) |
| 35 | + if dir = "" then |
| 36 | + return entry; |
| 37 | + fi; |
| 38 | + return Concatenation( dir, "/", entry ); |
| 39 | + end; |
| 40 | + |
| 41 | + AddMatchingFiles := function( abs_dir, rel_dir, recursive ) |
| 42 | + local abs_dir_obj, entries, entry, abs_entry, rel_entry; |
| 43 | + |
| 44 | + abs_dir_obj := Directory( abs_dir ); |
| 45 | + entries := DirectoryContents( abs_dir_obj ); |
| 46 | + Sort( entries ); |
| 47 | + for entry in entries do |
| 48 | + if entry = "." or entry = ".." then |
| 49 | + continue; |
| 50 | + fi; |
| 51 | + abs_entry := Filename( abs_dir_obj, entry ); |
| 52 | + rel_entry := JoinRelativePath( rel_dir, entry ); |
| 53 | + if IsDirectoryPath( abs_entry ) then |
| 54 | + if recursive then |
| 55 | + AddMatchingFiles( abs_entry, rel_entry, true ); |
| 56 | + fi; |
| 57 | + elif AUTODOC_GetSuffix( entry ) in extensions and |
| 58 | + IsReadableFile( abs_entry ) then |
| 59 | + Add( result, rel_entry ); |
| 60 | + fi; |
| 61 | + od; |
| 62 | + end; |
| 63 | + |
| 64 | + for d_rel in subdirs do |
| 65 | + if d_rel = "" or d_rel = "." then |
| 66 | + AddMatchingFiles( Filename( pkgdir, "" ), "", false ); |
| 67 | + elif not IsDirectoryPath( Filename( pkgdir, d_rel ) ) then |
| 68 | + continue; |
| 69 | + else |
| 70 | + AddMatchingFiles( Filename( pkgdir, d_rel ), d_rel, true ); |
| 71 | + fi; |
| 72 | + od; |
| 73 | + return result; |
| 74 | +end ); |
| 75 | + |
8 | 76 | # Ensure that the directory named by the given path string exists, creating any |
9 | 77 | # missing parent directories on the way. Relative paths are accepted and `.` / |
10 | 78 | # `..` components are normalized before creating directories. |
|
0 commit comments