From 4ebfd22e3dfcda1a0c355179911997448e54f15e Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 10 Feb 2015 11:53:50 -0800 Subject: [PATCH] Add Project::addPath --- spec/project-spec.coffee | 23 +++++++++++++++++++++++ src/project.coffee | 39 +++++++++++++++++++++++---------------- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/spec/project-spec.coffee b/spec/project-spec.coffee index 3a9786242..77aad1a1c 100644 --- a/spec/project-spec.coffee +++ b/spec/project-spec.coffee @@ -259,6 +259,16 @@ describe "Project", -> expect(repo3.getShortHead()).toBe "master" expect(repo3.getPath()).toBe fs.realpathSync(path.join(directory3, ".git")) + it "calls callbacks registered with ::onDidChangePaths", -> + onDidChangePathsSpy = jasmine.createSpy('onDidChangePaths spy') + atom.project.onDidChangePaths(onDidChangePathsSpy) + + paths = [ temp.mkdirSync("dir1"), temp.mkdirSync("dir2") ] + atom.project.setPaths(paths) + + expect(onDidChangePathsSpy.callCount).toBe 1 + expect(onDidChangePathsSpy.mostRecentCall.args[0]).toEqual(paths) + describe "when path is null", -> it "sets its path and root directory to null", -> atom.project.setPaths([]) @@ -270,6 +280,19 @@ describe "Project", -> expect(atom.project.getPaths()[0]).toEqual path.dirname(require.resolve('./fixtures/dir/a')) expect(atom.project.getDirectories()[0].path).toEqual path.dirname(require.resolve('./fixtures/dir/a')) + describe ".addPath(path)", -> + it "calls callbacks registered with ::onDidChangePaths", -> + onDidChangePathsSpy = jasmine.createSpy('onDidChangePaths spy') + atom.project.onDidChangePaths(onDidChangePathsSpy) + + [oldPath] = atom.project.getPaths() + + newPath = temp.mkdirSync("dir") + atom.project.addPath(newPath) + + expect(onDidChangePathsSpy.callCount).toBe 1 + expect(onDidChangePathsSpy.mostRecentCall.args[0]).toEqual([oldPath, newPath]) + describe ".relativize(path)", -> it "returns the path, relative to whichever root directory it is inside of", -> rootPath = atom.project.getPaths()[0] diff --git a/src/project.coffee b/src/project.coffee index fed1d7bd3..6ce5a6fa2 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -165,22 +165,7 @@ class Project extends Model @rootDirectories = [] @repositories = [] - for projectPath, i in projectPaths - projectPath = path.normalize(projectPath) - - directoryPath = if fs.isDirectorySync(projectPath) - projectPath - else - path.dirname(projectPath) - - directory = new Directory(directoryPath) - @rootDirectories.push(directory) - - # For now, use only the repositoryProviders with a sync API. - repo = null - for provider in @repositoryProviders - break if repo = provider.repositoryForDirectorySync?(directory) - @repositories.push(repo ? null) + @addPath(projectPath, emitEvent: false) for projectPath in projectPaths @emit "path-changed" @emitter.emit 'did-change-paths', projectPaths @@ -189,6 +174,28 @@ class Project extends Model Grim.deprecate("Use ::setPaths instead") @setPaths([path]) + # Public: Add a path the project's list of root paths + # + # * `projectPath` {String} The path to the directory to add. + addPath: (projectPath, options) -> + projectPath = path.normalize(projectPath) + + directoryPath = if fs.isDirectorySync(projectPath) + projectPath + else + path.dirname(projectPath) + directory = new Directory(directoryPath) + @rootDirectories.push(directory) + + repo = null + for provider in @repositoryProviders + break if repo = provider.repositoryForDirectorySync?(directory) + @repositories.push(repo ? null) + + unless options?.emitEvent is false + @emit "path-changed" + @emitter.emit 'did-change-paths', @getPaths() + # Public: Get an {Array} of {Directory}s associated with this project. getDirectories: -> @rootDirectories