Skip to content

Commit

Permalink
Merge pull request #2152 from rwblair/fix/set_ignore_for_dirs
Browse files Browse the repository at this point in the history
Pass value of ignore test to creation of filetree object.
  • Loading branch information
rwblair authored Oct 9, 2024
2 parents ae54efa + ae52740 commit 5991dbb
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 14 deletions.
6 changes: 5 additions & 1 deletion bids-validator/src/files/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ export async function fileListToTree(files: File[]): Promise<FileTree> {
const tree = filesToTree(files.map((f) => new BIDSFileBrowser(f, ignore, root)))
const bidsignore = tree.get('.bidsignore')
if (bidsignore) {
ignore.add(await readBidsIgnore(bidsignore as BIDSFile))
try {
ignore.add(await readBidsIgnore(bidsignore as BIDSFile))
} catch (err) {
console.log(`Failed to read '.bidsignore' file with the following error:\n${err}`)
}
}
return tree
}
21 changes: 15 additions & 6 deletions bids-validator/src/files/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as posix from '@std/path/posix'
import { type BIDSFile, FileTree } from '../types/filetree.ts'
import { requestReadPermission } from '../setup/requestPermissions.ts'
import { FileIgnoreRules, readBidsIgnore } from './ignore.ts'
import { logger } from '../utils/logger.ts'

/**
* Thrown when a text file is decoded as UTF-8 but contains UTF-16 characters
Expand Down Expand Up @@ -119,7 +120,7 @@ async function _readFileTree(
): Promise<FileTree> {
await requestReadPermission()
const name = basename(relativePath)
const tree = new FileTree(relativePath, name, parent)
const tree = new FileTree(relativePath, name, parent, ignore)

for await (const dirEntry of Deno.readDir(join(rootPath, relativePath))) {
if (dirEntry.isFile || dirEntry.isSymlink) {
Expand All @@ -129,10 +130,6 @@ async function _readFileTree(
ignore,
)
file.parent = tree
// For .bidsignore, read in immediately and add the rules
if (dirEntry.name === '.bidsignore') {
ignore.add(await readBidsIgnore(file))
}
tree.files.push(file)
}
if (dirEntry.isDirectory) {
Expand All @@ -151,7 +148,19 @@ async function _readFileTree(
/**
* Read in the target directory structure and return a FileTree
*/
export function readFileTree(rootPath: string): Promise<FileTree> {
export async function readFileTree(rootPath: string): Promise<FileTree> {
const ignore = new FileIgnoreRules([])
try {
const ignoreFile = new BIDSFileDeno(
rootPath,
'.bidsignore',
ignore,
)
ignore.add(await readBidsIgnore(ignoreFile))
} catch (err) {
if (!Object.hasOwn(err, 'code') || err.code !== 'ENOENT') {
logger.error(`Failed to read '.bidsignore' file with the following error:\n${err}`)
}
}
return _readFileTree(rootPath, '/', ignore)
}
2 changes: 1 addition & 1 deletion bids-validator/src/schema/applyRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ function evalJsonCheck(
}

if (sidecarRule && !(keyName in context.sidecarKeyOrigin)) {
logger.warning(
logger.warn(
`sidecarKeyOrigin map failed to initialize for ${context.path} on key ${keyName}. Validation caching not active for this key.`,
)
}
Expand Down
2 changes: 1 addition & 1 deletion bids-validator/src/schema/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export class BIDSContext implements Context {
if (error.key) {
this.dataset.issues.add({ code: error.key, location: this.file.path })
}
logger.warning(
logger.warn(
`tsv file could not be opened by loadColumns '${this.file.path}'`,
)
logger.debug(error)
Expand Down
12 changes: 9 additions & 3 deletions bids-validator/src/types/filetree.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { FileIgnoreRules } from '../files/ignore.ts'

export interface BIDSFile {
// Filename
name: string
Expand Down Expand Up @@ -29,18 +31,22 @@ export class FileTree {
name: string
files: BIDSFile[]
directories: FileTree[]
ignored: boolean
viewed: boolean
parent?: FileTree
#ignore: FileIgnoreRules

constructor(path: string, name: string, parent?: FileTree, ignored?: boolean) {
constructor(path: string, name: string, parent?: FileTree, ignore?: FileIgnoreRules) {
this.path = path
this.files = []
this.directories = []
this.name = name
this.parent = parent
this.viewed = false
this.ignored = ignored || false
this.#ignore = ignore ?? new FileIgnoreRules([])
}

get ignored(): boolean {
return this.#ignore.test(this.path)
}

_get(parts: string[]): BIDSFile | FileTree | undefined {
Expand Down
3 changes: 3 additions & 0 deletions bids-validator/src/validators/bids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export async function validate(
const bidsDerivatives: FileTree[] = []
const nonstdDerivatives: FileTree[] = []
fileTree.directories = fileTree.directories.filter((dir) => {
if (['sourcedata', 'code'].includes(dir.name)) {
return false
}
if (dir.name !== 'derivatives') {
return true
}
Expand Down
4 changes: 2 additions & 2 deletions bids-validator/src/validators/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const compile = memoize(metadataValidator.compile.bind(metadataValidator)

export function setCustomMetadataFormats(schema: Schema): void {
if (typeof schema.objects.formats !== 'object') {
logger.warning(
logger.warn(
`schema.objects.formats missing from schema, format validation disabled.`,
)
return
Expand All @@ -19,7 +19,7 @@ export function setCustomMetadataFormats(schema: Schema): void {
for (const key of Object.keys(schemaFormats)) {
const pattern = schemaFormats[key]['pattern']
if (typeof pattern !== 'string') {
logger.warning(
logger.warn(
`schema.objects.formats.${key} pattern missing or invalid. Skipping this format for addition to context json validator`,
)
continue
Expand Down

0 comments on commit 5991dbb

Please sign in to comment.