From cbefdd6c5e9655719fc948135fe9c4bd9fb1abf5 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 24 Feb 2015 14:22:39 -0800 Subject: [PATCH] Make Workspace::scan work w/ multiple root directories --- spec/workspace-spec.coffee | 58 ++++++++++++++++++++++++++++++++++++++ src/scan-handler.coffee | 51 ++++++++++++++++++++++++++------- src/workspace.coffee | 3 +- 3 files changed, 100 insertions(+), 12 deletions(-) diff --git a/spec/workspace-spec.coffee b/spec/workspace-spec.coffee index 82526a299..e40ef1925 100644 --- a/spec/workspace-spec.coffee +++ b/spec/workspace-spec.coffee @@ -854,6 +854,64 @@ describe "Workspace", -> runs -> expect(results).toHaveLength 0 + describe "when the project has multiple root directories", -> + [dir1, dir2, file1, file2] = [] + + beforeEach -> + [dir1] = atom.project.getPaths() + file1 = path.join(dir1, "a-dir", "oh-git") + + dir2 = temp.mkdirSync("a-second-dir") + aDir2 = path.join(dir2, "a-dir") + file2 = path.join(aDir2, "a-file") + fs.mkdirSync(aDir2) + fs.writeFileSync(file2, "ccc aaaa") + + atom.project.addPath(dir2) + + it "searches matching files in all of the project's root directories", -> + resultPaths = [] + waitsForPromise -> + atom.workspace.scan /aaaa/, ({filePath}) -> + resultPaths.push(filePath) + + runs -> + expect(resultPaths.sort()).toEqual([file1, file2].sort()) + + describe "when an inclusion path starts with the basename of a root directory", -> + it "interprets the inclusion path as starting from that directory", -> + waitsForPromise -> + resultPaths = [] + atom.workspace + .scan /aaaa/, paths: ["dir"], ({filePath}) -> + resultPaths.push(filePath) unless filePath in resultPaths + .then -> + expect(resultPaths).toEqual([file1]) + + waitsForPromise -> + resultPaths = [] + atom.workspace + .scan /aaaa/, paths: [path.join("dir", "a-dir")], ({filePath}) -> + resultPaths.push(filePath) unless filePath in resultPaths + .then -> + expect(resultPaths).toEqual([file1]) + + waitsForPromise -> + resultPaths = [] + atom.workspace + .scan /aaaa/, paths: [path.basename(dir2)], ({filePath}) -> + resultPaths.push(filePath) unless filePath in resultPaths + .then -> + expect(resultPaths).toEqual([file2]) + + waitsForPromise -> + resultPaths = [] + atom.workspace + .scan /aaaa/, paths: [path.join(path.basename(dir2), "a-dir")], ({filePath}) -> + resultPaths.push(filePath) unless filePath in resultPaths + .then -> + expect(resultPaths).toEqual([file2]) + describe "::replace(regex, replacementText, paths, iterator)", -> [filePath, commentFilePath, sampleContent, sampleCommentContent] = [] diff --git a/src/scan-handler.coffee b/src/scan-handler.coffee index 7591ccd27..71917cc6e 100644 --- a/src/scan-handler.coffee +++ b/src/scan-handler.coffee @@ -1,13 +1,17 @@ +_ = require "underscore-plus" +path = require "path" +async = require "async" {PathSearcher, PathScanner, search} = require 'scandal' -module.exports = (rootPath, regexSource, options) -> +module.exports = (rootPaths, regexSource, options) -> callback = @async() + rootPath = rootPaths[0] + PATHS_COUNTER_SEARCHED_CHUNK = 50 pathsSearched = 0 searcher = new PathSearcher() - scanner = new PathScanner(rootPath, options) searcher.on 'file-error', ({code, path, message}) -> emit('scan:file-error', {code, path, message}) @@ -15,14 +19,41 @@ module.exports = (rootPath, regexSource, options) -> searcher.on 'results-found', (result) -> emit('scan:result-found', result) - scanner.on 'path-found', -> - pathsSearched++ - if pathsSearched % PATHS_COUNTER_SEARCHED_CHUNK == 0 - emit('scan:paths-searched', pathsSearched) - flags = "g" flags += "i" if options.ignoreCase regex = new RegExp(regexSource, flags) - search regex, scanner, searcher, -> - emit('scan:paths-searched', pathsSearched) - callback() + + async.each( + rootPaths, + (rootPath, next) -> + options2 = _.extend {}, options, + inclusions: processPaths(rootPath, options.inclusions) + exclusions: processPaths(rootPath, options.exclusions) + + scanner = new PathScanner(rootPath, options2) + + scanner.on 'path-found', -> + pathsSearched++ + if pathsSearched % PATHS_COUNTER_SEARCHED_CHUNK == 0 + emit('scan:paths-searched', pathsSearched) + + search regex, scanner, searcher, -> + emit('scan:paths-searched', pathsSearched) + next() + callback + ) + +processPaths = (rootPath, paths) -> + return paths unless paths?.length > 0 + rootPathBase = path.basename(rootPath) + results = [] + for givenPath in paths + segments = givenPath.split(path.sep) + firstSegment = segments.shift() + results.push(givenPath) + if firstSegment is rootPathBase + if segments.length is 0 + results.push(path.join("**", "*")) + else + results.push(path.join(segments...)) + results diff --git a/src/workspace.coffee b/src/workspace.coffee index d36abc270..f30d7534e 100644 --- a/src/workspace.coffee +++ b/src/workspace.coffee @@ -870,8 +870,7 @@ class Workspace extends Model exclusions: atom.config.get('core.ignoredNames') follow: atom.config.get('core.followSymlinks') - # TODO: need to support all paths in @getPaths() - task = Task.once require.resolve('./scan-handler'), atom.project.getPaths()[0], regex.source, searchOptions, -> + task = Task.once require.resolve('./scan-handler'), atom.project.getPaths(), regex.source, searchOptions, -> deferred.resolve() task.on 'scan:result-found', (result) ->