Add Project::addPath

This commit is contained in:
Max Brunsfeld
2015-02-10 11:53:50 -08:00
parent 8ab4ad54d8
commit 4ebfd22e3d
2 changed files with 46 additions and 16 deletions

View File

@@ -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]

View File

@@ -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