iterateThroughDirSync function

void iterateThroughDirSync (String dir, List<String> extensions, List<String> dirsToExclude, void callback(File file))

Goes through the files

Implementation

void iterateThroughDirSync(final String dir,final List<String> extensions,  final List<String> dirsToExclude, void callback(final File file)) {
    String scanningMessage() => Intl.message("Scanning",desc: "In '_iterateThroughDirSync'");
    _logger.fine("${scanningMessage()}: $dir");

    // its OK if the path starts with packages but not if the path contains packages (avoid recursion)
    final RegExp regexp = new RegExp("^/*packages/*");

    final Directory directory = new Directory(dir);
    if (directory.existsSync()) {
        directory.listSync(recursive: true).where((final FileSystemEntity entity) {
            _logger.finer("Entity: ${entity}");

            bool isValidExtension(final String path)
                => extensions.any((final String extension) => path.toLowerCase().endsWith("${extension.toLowerCase()}"));

            bool isUsableFile = (entity != null
                && FileSystemEntity.isFileSync(entity.path)
                && isValidExtension(entity.path));

            if(!isUsableFile) {
                _logger.finer("...not usable!");
                return false;
            }
            if(entity.path.contains("packages")) {
                // return only true if the path starts!!!!! with packages
                return entity.path.contains(regexp);
            }

            if(entity.path.startsWith(".pub/") || entity.path.startsWith("./.pub/") ||
                entity.path.startsWith(".git/") || entity.path.startsWith("./.git/") ||
                entity.path.startsWith("build/") || entity.path.startsWith("./build/")){
                _logger.finer("...build, .pub or .git-Folder!");
                return false;
            }

            for(final String dirToExclude in dirsToExclude) {
                final String dir = dirToExclude.trim();
                if(entity.path.startsWith("${dir}/") || entity.path.startsWith("./${dir}/")) {
                    _logger.finer("...starts with ${dir} - not allowed!");
                    return false;
                }
            }

            return true;

        }).map((final FileSystemEntity entity) => new File(entity.path))
            .forEach((final File file) {
            _logger.fine("  Found: ${file}");
            callback(file);
        });
    }
}