From 19bf64f3cd99cfbc4cfa5e1509f0ffc6b5a93e2d Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 9 Jan 2015 13:58:01 -0800 Subject: [PATCH] Revert "Deprecate Project::resolve" This reverts commit 3c5bd9f10a07ab4d5dacee481779d37483632039. --- package.json | 2 +- spec/package-spec.coffee | 16 ++++++++-------- spec/project-spec.coffee | 25 ++++++++++++++++++++++-- spec/theme-manager-spec.coffee | 8 ++++---- spec/workspace-spec.coffee | 34 ++++++++++++++++----------------- spec/workspace-view-spec.coffee | 12 ++++++------ src/project.coffee | 30 ++++++++++++++++++++++------- src/workspace.coffee | 6 +++--- 8 files changed, 85 insertions(+), 48 deletions(-) diff --git a/package.json b/package.json index e2b99bb1c..e27bd4e36 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "nslog": "^1.0.1", "oniguruma": "^3.0.4", "optimist": "0.4.0", - "pathwatcher": "^2.5.0", + "pathwatcher": "^2.3.7", "property-accessors": "^1", "q": "^1.0.1", "random-words": "0.0.1", diff --git a/spec/package-spec.coffee b/spec/package-spec.coffee index 87d243ce2..8f968bb9f 100644 --- a/spec/package-spec.coffee +++ b/spec/package-spec.coffee @@ -9,14 +9,14 @@ describe "Package", -> spyOn(atom, 'inDevMode').andReturn(false) it "does not activate it", -> - packagePath = atom.project.getDirectories()[0]?.resolve('packages/package-with-incompatible-native-module') + packagePath = atom.project.resolve('packages/package-with-incompatible-native-module') pack = new Package(packagePath) expect(pack.isCompatible()).toBe false expect(pack.incompatibleModules[0].name).toBe 'native-module' expect(pack.incompatibleModules[0].path).toBe path.join(packagePath, 'node_modules', 'native-module') it "caches the incompatible native modules in local storage", -> - packagePath = atom.project.getDirectories()[0]?.resolve('packages/package-with-incompatible-native-module') + packagePath = atom.project.resolve('packages/package-with-incompatible-native-module') cacheKey = null cacheItem = null @@ -46,14 +46,14 @@ describe "Package", -> describe "when the theme contains a single style file", -> it "loads and applies css", -> expect($("atom-text-editor").css("padding-bottom")).not.toBe "1234px" - themePath = atom.project.getDirectories()[0]?.resolve('packages/theme-with-index-css') + themePath = atom.project.resolve('packages/theme-with-index-css') theme = new ThemePackage(themePath) theme.activate() expect($("atom-text-editor").css("padding-top")).toBe "1234px" it "parses, loads and applies less", -> expect($("atom-text-editor").css("padding-bottom")).not.toBe "1234px" - themePath = atom.project.getDirectories()[0]?.resolve('packages/theme-with-index-less') + themePath = atom.project.resolve('packages/theme-with-index-less') theme = new ThemePackage(themePath) theme.activate() expect($("atom-text-editor").css("padding-top")).toBe "4321px" @@ -64,7 +64,7 @@ describe "Package", -> expect($("atom-text-editor").css("padding-right")).not.toBe("102px") expect($("atom-text-editor").css("padding-bottom")).not.toBe("103px") - themePath = atom.project.getDirectories()[0]?.resolve('packages/theme-with-package-file') + themePath = atom.project.resolve('packages/theme-with-package-file') theme = new ThemePackage(themePath) theme.activate() expect($("atom-text-editor").css("padding-top")).toBe("101px") @@ -77,7 +77,7 @@ describe "Package", -> expect($("atom-text-editor").css("padding-right")).not.toBe "20px" expect($("atom-text-editor").css("padding-bottom")).not.toBe "30px" - themePath = atom.project.getDirectories()[0]?.resolve('packages/theme-without-package-file') + themePath = atom.project.resolve('packages/theme-without-package-file') theme = new ThemePackage(themePath) theme.activate() expect($("atom-text-editor").css("padding-top")).toBe "10px" @@ -86,7 +86,7 @@ describe "Package", -> describe "reloading a theme", -> beforeEach -> - themePath = atom.project.getDirectories()[0]?.resolve('packages/theme-with-package-file') + themePath = atom.project.resolve('packages/theme-with-package-file') theme = new ThemePackage(themePath) theme.activate() @@ -97,7 +97,7 @@ describe "Package", -> describe "events", -> beforeEach -> - themePath = atom.project.getDirectories()[0]?.resolve('packages/theme-with-package-file') + themePath = atom.project.resolve('packages/theme-with-package-file') theme = new ThemePackage(themePath) theme.activate() diff --git a/spec/project-spec.coffee b/spec/project-spec.coffee index ba91fbde7..0583a0749 100644 --- a/spec/project-spec.coffee +++ b/spec/project-spec.coffee @@ -8,7 +8,7 @@ BufferedProcess = require '../src/buffered-process' describe "Project", -> beforeEach -> - atom.project.setPaths([atom.project.getDirectories()[0]?.resolve('dir')]) + atom.project.setPaths([atom.project.resolve('dir')]) describe "serialization", -> deserializedProject = null @@ -109,7 +109,7 @@ describe "Project", -> expect(newBufferHandler).toHaveBeenCalledWith(editor.buffer) it "returns number of read bytes as progress indicator", -> - filePath = atom.project.getDirectories()[0]?.resolve 'a' + filePath = atom.project.resolve 'a' totalBytes = 0 promise = atom.project.open(filePath) promise.progress (bytesRead) -> totalBytes = bytesRead @@ -148,6 +148,27 @@ describe "Project", -> atom.project.bufferForPath("b").then (anotherBuffer) -> expect(anotherBuffer).not.toBe buffer + describe ".resolve(uri)", -> + describe "when passed an absolute or relative path", -> + it "returns an absolute path based on the atom.project's root", -> + absolutePath = require.resolve('./fixtures/dir/a') + expect(atom.project.resolve('a')).toBe absolutePath + expect(atom.project.resolve(absolutePath + '/../a')).toBe absolutePath + expect(atom.project.resolve('a/../a')).toBe absolutePath + expect(atom.project.resolve()).toBeUndefined() + + describe "when passed a uri with a scheme", -> + it "does not modify uris that begin with a scheme", -> + expect(atom.project.resolve('http://zombo.com')).toBe 'http://zombo.com' + + describe "when the project has no path", -> + it "returns undefined for relative URIs", -> + atom.project.setPaths([]) + expect(atom.project.resolve('test.txt')).toBeUndefined() + expect(atom.project.resolve('http://github.com')).toBe 'http://github.com' + absolutePath = fs.absolute(__dirname) + expect(atom.project.resolve(absolutePath)).toBe absolutePath + describe ".setPaths(path)", -> describe "when path is a file", -> it "sets its path to the files parent directory and updates the root directory", -> diff --git a/spec/theme-manager-spec.coffee b/spec/theme-manager-spec.coffee index a9bacfe90..9d5ae218d 100644 --- a/spec/theme-manager-spec.coffee +++ b/spec/theme-manager-spec.coffee @@ -170,7 +170,7 @@ describe "ThemeManager", -> themeManager.onDidChangeStylesheets stylesheetsChangedHandler = jasmine.createSpy("stylesheetsChangedHandler") themeManager.onDidAddStylesheet stylesheetAddedHandler = jasmine.createSpy("stylesheetAddedHandler") - cssPath = atom.project.getDirectories()[0]?.resolve('css.css') + cssPath = atom.project.resolve('css.css') lengthBefore = $('head style').length themeManager.requireStylesheet(cssPath) @@ -194,7 +194,7 @@ describe "ThemeManager", -> $('head style[id*="css.css"]').remove() it "synchronously loads and parses less files at the given path and installs a style tag for it in the head", -> - lessPath = atom.project.getDirectories()[0]?.resolve('sample.less') + lessPath = atom.project.resolve('sample.less') lengthBefore = $('head style').length themeManager.requireStylesheet(lessPath) expect($('head style').length).toBe lengthBefore + 1 @@ -218,9 +218,9 @@ describe "ThemeManager", -> it "supports requiring css and less stylesheets without an explicit extension", -> themeManager.requireStylesheet path.join(__dirname, 'fixtures', 'css') - expect($('head style[source-path*="css.css"]').attr('source-path')).toBe themeManager.stringToId(atom.project.getDirectories()[0]?.resolve('css.css')) + expect($('head style[source-path*="css.css"]').attr('source-path')).toBe themeManager.stringToId(atom.project.resolve('css.css')) themeManager.requireStylesheet path.join(__dirname, 'fixtures', 'sample') - expect($('head style[source-path*="sample.less"]').attr('source-path')).toBe themeManager.stringToId(atom.project.getDirectories()[0]?.resolve('sample.less')) + expect($('head style[source-path*="sample.less"]').attr('source-path')).toBe themeManager.stringToId(atom.project.resolve('sample.less')) $('head style[id*="css.css"]').remove() $('head style[id*="sample.less"]').remove() diff --git a/spec/workspace-spec.coffee b/spec/workspace-spec.coffee index e520300d9..b96bc79af 100644 --- a/spec/workspace-spec.coffee +++ b/spec/workspace-spec.coffee @@ -11,7 +11,7 @@ describe "Workspace", -> workspace = null beforeEach -> - atom.project.setPaths([atom.project.getDirectories()[0]?.resolve('dir')]) + atom.project.setPaths([atom.project.resolve('dir')]) atom.workspace = workspace = new Workspace describe "::open(uri, options)", -> @@ -70,19 +70,19 @@ describe "Workspace", -> expect(openEvents).toEqual [ { - uri: atom.project.getDirectories()[0]?.resolve('a') + uri: atom.project.resolve('a') item: editor1 pane: atom.workspace.getActivePane() index: 0 } { - uri: atom.project.getDirectories()[0]?.resolve('b') + uri: atom.project.resolve('b') item: editor2 pane: atom.workspace.getActivePane() index: 1 } { - uri: atom.project.getDirectories()[0]?.resolve('a') + uri: atom.project.resolve('a') item: editor1 pane: atom.workspace.getActivePane() index: 0 @@ -96,7 +96,7 @@ describe "Workspace", -> workspace.open('a').then (o) -> editor = o runs -> - expect(editor.getUri()).toBe atom.project.getDirectories()[0]?.resolve('a') + expect(editor.getUri()).toBe atom.project.resolve('a') expect(workspace.getActivePaneItem()).toBe editor expect(workspace.getActivePane().items).toEqual [editor] expect(workspace.getActivePane().activate).toHaveBeenCalled() @@ -230,7 +230,7 @@ describe "Workspace", -> workspace.addOpener(barOpener) waitsForPromise -> - pathToOpen = atom.project.getDirectories()[0]?.resolve('a.foo') + pathToOpen = atom.project.resolve('a.foo') workspace.open(pathToOpen, hey: "there").then (item) -> expect(item).toEqual { foo: pathToOpen, options: {hey: "there"} } @@ -271,11 +271,11 @@ describe "Workspace", -> expect(workspace.getActivePaneItem().getUri()).not.toBeUndefined() # destroy all items - expect(workspace.getActivePaneItem().getUri()).toBe atom.project.getDirectories()[0]?.resolve('file1') + expect(workspace.getActivePaneItem().getUri()).toBe atom.project.resolve('file1') pane.destroyActiveItem() - expect(workspace.getActivePaneItem().getUri()).toBe atom.project.getDirectories()[0]?.resolve('b') + expect(workspace.getActivePaneItem().getUri()).toBe atom.project.resolve('b') pane.destroyActiveItem() - expect(workspace.getActivePaneItem().getUri()).toBe atom.project.getDirectories()[0]?.resolve('a') + expect(workspace.getActivePaneItem().getUri()).toBe atom.project.resolve('a') pane.destroyActiveItem() # reopens items with uris @@ -285,20 +285,20 @@ describe "Workspace", -> workspace.reopenItem() runs -> - expect(workspace.getActivePaneItem().getUri()).toBe atom.project.getDirectories()[0]?.resolve('a') + expect(workspace.getActivePaneItem().getUri()).toBe atom.project.resolve('a') # does not reopen items that are already open waitsForPromise -> workspace.open('b') runs -> - expect(workspace.getActivePaneItem().getUri()).toBe atom.project.getDirectories()[0]?.resolve('b') + expect(workspace.getActivePaneItem().getUri()).toBe atom.project.resolve('b') waitsForPromise -> workspace.reopenItem() runs -> - expect(workspace.getActivePaneItem().getUri()).toBe atom.project.getDirectories()[0]?.resolve('file1') + expect(workspace.getActivePaneItem().getUri()).toBe atom.project.resolve('file1') describe "::increase/decreaseFontSize()", -> it "increases/decreases the font size without going below 1", -> @@ -568,7 +568,7 @@ describe "Workspace", -> runs -> expect(results).toHaveLength(3) - expect(results[0].filePath).toBe atom.project.getDirectories()[0]?.resolve('a') + expect(results[0].filePath).toBe atom.project.resolve('a') expect(results[0].matches).toHaveLength(3) expect(results[0].matches[0]).toEqual matchText: 'aaa' @@ -585,7 +585,7 @@ describe "Workspace", -> expect(results.length).toBe 1 {filePath, matches} = results[0] - expect(filePath).toBe atom.project.getDirectories()[0]?.resolve('a') + expect(filePath).toBe atom.project.resolve('a') expect(matches).toHaveLength 1 expect(matches[0]).toEqual matchText: '$bill' @@ -746,10 +746,10 @@ describe "Workspace", -> [filePath, commentFilePath, sampleContent, sampleCommentContent] = [] beforeEach -> - atom.project.setPaths([atom.project.getDirectories()[0]?.resolve('../')]) + atom.project.setPaths([atom.project.resolve('../')]) - filePath = atom.project.getDirectories()[0]?.resolve('sample.js') - commentFilePath = atom.project.getDirectories()[0]?.resolve('sample-with-comments.js') + filePath = atom.project.resolve('sample.js') + commentFilePath = atom.project.resolve('sample-with-comments.js') sampleContent = fs.readFileSync(filePath).toString() sampleCommentContent = fs.readFileSync(commentFilePath).toString() diff --git a/spec/workspace-view-spec.coffee b/spec/workspace-view-spec.coffee index ade0dcc4d..09de0bb26 100644 --- a/spec/workspace-view-spec.coffee +++ b/spec/workspace-view-spec.coffee @@ -13,8 +13,8 @@ describe "WorkspaceView", -> beforeEach -> jasmine.snapshotDeprecations() - atom.project.setPaths([atom.project.getDirectories()[0]?.resolve('dir')]) - pathToOpen = atom.project.getDirectories()[0]?.resolve('a') + atom.project.setPaths([atom.project.resolve('dir')]) + pathToOpen = atom.project.resolve('a') atom.workspace = new Workspace atom.workspaceView = atom.views.getView(atom.workspace).__spacePenView atom.workspaceView.enableKeymap() @@ -93,11 +93,11 @@ describe "WorkspaceView", -> editorView2 = atom.workspaceView.panes.find('atom-pane-axis.horizontal > atom-pane-axis.vertical > atom-pane atom-text-editor:eq(0)').view() editorView4 = atom.workspaceView.panes.find('atom-pane-axis.horizontal > atom-pane-axis.vertical > atom-pane atom-text-editor:eq(1)').view() - expect(editorView1.getEditor().getPath()).toBe atom.project.getDirectories()[0]?.resolve('a') - expect(editorView2.getEditor().getPath()).toBe atom.project.getDirectories()[0]?.resolve('b') - expect(editorView3.getEditor().getPath()).toBe atom.project.getDirectories()[0]?.resolve('../sample.js') + expect(editorView1.getEditor().getPath()).toBe atom.project.resolve('a') + expect(editorView2.getEditor().getPath()).toBe atom.project.resolve('b') + expect(editorView3.getEditor().getPath()).toBe atom.project.resolve('../sample.js') expect(editorView3.getEditor().getCursorScreenPosition()).toEqual [2, 4] - expect(editorView4.getEditor().getPath()).toBe atom.project.getDirectories()[0]?.resolve('../sample.txt') + expect(editorView4.getEditor().getPath()).toBe atom.project.resolve('../sample.txt') expect(editorView4.getEditor().getCursorScreenPosition()).toEqual [0, 2] # ensure adjust pane dimensions is called diff --git a/src/project.coffee b/src/project.coffee index e8e22ba89..9a8040a22 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -139,9 +139,25 @@ class Project extends Model Grim.deprecate("Use ::getDirectories instead") @rootDirectory + # Public: Given a uri, this resolves it relative to the project directory. If + # the path is already absolute or if it is prefixed with a scheme, it is + # returned unchanged. + # + # * `uri` The {String} name of the path to convert. + # + # Returns a {String} or undefined if the uri is not missing or empty. resolve: (uri) -> - Grim.deprecate("Use `Project::getDirectories()[0]?.resolve()` instead") - @rootDirectory?.resolve(uri) + return unless uri + + if uri?.match(/[A-Za-z0-9+-.]+:\/\//) # leave path alone if it has a scheme + uri + else + if fs.isAbsolute(uri) + path.normalize(fs.absolute(uri)) + else if projectPath = @getPaths()[0] + path.normalize(fs.absolute(path.join(projectPath, uri))) + else + undefined # Public: Make the given path relative to the project directory. # @@ -204,14 +220,14 @@ class Project extends Model # # Returns a promise that resolves to an {TextEditor}. open: (filePath, options={}) -> - filePath = @rootDirectory?.resolve(filePath) + filePath = @resolve(filePath) @bufferForPath(filePath).then (buffer) => @buildEditorForBuffer(buffer, options) # Deprecated openSync: (filePath, options={}) -> deprecate("Use Project::open instead") - filePath = @rootDirectory?.resolve(filePath) + filePath = @resolve(filePath) @buildEditorForBuffer(@bufferForPathSync(filePath), options) # Retrieves all the {TextBuffer}s in the project; that is, the @@ -223,14 +239,14 @@ class Project extends Model # Is the buffer for the given path modified? isPathModified: (filePath) -> - @findBufferForPath(@rootDirectory?.resolve(filePath))?.isModified() + @findBufferForPath(@resolve(filePath))?.isModified() findBufferForPath: (filePath) -> _.find @buffers, (buffer) -> buffer.getPath() == filePath # Only to be used in specs bufferForPathSync: (filePath) -> - absoluteFilePath = @rootDirectory?.resolve(filePath) + absoluteFilePath = @resolve(filePath) existingBuffer = @findBufferForPath(absoluteFilePath) if filePath existingBuffer ? @buildBufferSync(absoluteFilePath) @@ -243,7 +259,7 @@ class Project extends Model # # Returns a promise that resolves to the {TextBuffer}. bufferForPath: (filePath) -> - absoluteFilePath = @rootDirectory?.resolve(filePath) + absoluteFilePath = @resolve(filePath) existingBuffer = @findBufferForPath(absoluteFilePath) if absoluteFilePath Q(existingBuffer ? @buildBuffer(absoluteFilePath)) diff --git a/src/workspace.coffee b/src/workspace.coffee index 34685128a..084734abd 100644 --- a/src/workspace.coffee +++ b/src/workspace.coffee @@ -383,7 +383,7 @@ class Workspace extends Model open: (uri, options={}) -> searchAllPanes = options.searchAllPanes split = options.split - uri = atom.project.getDirectories()[0]?.resolve(uri) + uri = atom.project.resolve(uri) pane = @paneContainer.paneForUri(uri) if searchAllPanes pane ?= switch split @@ -422,7 +422,7 @@ class Workspace extends Model {initialLine, initialColumn} = options activatePane = options.activatePane ? true - uri = atom.project.getDirectories()[0]?.resolve(uri) + uri = atom.project.resolve(uri) item = @getActivePane().itemForUri(uri) if uri @@ -445,7 +445,7 @@ class Workspace extends Model if uri? item = pane.itemForUri(uri) - item ?= opener(atom.project.getDirectories()[0]?.resolve(uri), options) for opener in @getOpeners() when !item + item ?= opener(atom.project.resolve(uri), options) for opener in @getOpeners() when !item item ?= atom.project.open(uri, options) Q(item)