From 8cbf4331e425af9dd6a646aabc00e6bcac02a3cb Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 27 Feb 2012 10:17:40 -0800 Subject: [PATCH] Remove references to OSX from fs.coffee and make fs specs pass. --- Classes/native_handler.mm | 63 +++++++++++++++++++++++++++++------ spec/atom/project-spec.coffee | 2 +- spec/spec-suite.coffee | 2 +- spec/stdlib/fs-spec.coffee | 26 +++++++-------- src/stdlib/fs.coffee | 29 ++++++++-------- 5 files changed, 82 insertions(+), 40 deletions(-) diff --git a/Classes/native_handler.mm b/Classes/native_handler.mm index 722b928c3..b69d6f598 100644 --- a/Classes/native_handler.mm +++ b/Classes/native_handler.mm @@ -10,7 +10,7 @@ NSString *stringFromCefV8Value(const CefRefPtr& value) { NativeHandler::NativeHandler() : CefV8Handler() { m_object = CefV8Value::CreateObject(NULL); - const char *functionNames[] = {"exists", "read", "absolute", "list", "isFile", "isDirectory", "remove", "open", "terminate"}; + const char *functionNames[] = {"exists", "read", "write", " absolute", "list", "isFile", "isDirectory", "remove", "asyncList", "open", "quit"}; NSUInteger arrayLength = sizeof(functionNames) / sizeof(const char *); for (NSUInteger i = 0; i < arrayLength; i++) { const char *functionName = functionNames[i]; @@ -48,6 +48,23 @@ bool NativeHandler::Execute(const CefString& name, return true; } + else if (name == "write") { + NSString *path = stringFromCefV8Value(arguments[0]); + NSString *content = stringFromCefV8Value(arguments[1]); + + + NSError *error = nil; + BOOL success = [content writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]; + + if (error) { + exception = [[error localizedDescription] UTF8String]; + } + else if (!success) { + std::string exception = "Cannot write to '"; + exception += [path UTF8String]; + exception += "'"; + } + } else if (name == "absolute") { NSString *path = stringFromCefV8Value(arguments[0]); @@ -96,15 +113,6 @@ bool NativeHandler::Execute(const CefString& name, return true; } - else if (name == "isFile") { - NSString *path = stringFromCefV8Value(arguments[0]); - - BOOL isDir = false; - BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir]; - retval = CefV8Value::CreateBool(exists && !isDir); - - return true; - } else if (name == "remove") { NSString *path = stringFromCefV8Value(arguments[0]); @@ -117,6 +125,41 @@ bool NativeHandler::Execute(const CefString& name, return true; } + else if (name == "asyncList") { + NSString *path = stringFromCefV8Value(arguments[0]); + bool recursive = arguments[1]->GetBoolValue(); + + NSFileManager *fm = [NSFileManager defaultManager]; + NSArray *relativePaths = [NSArray array]; + NSError *error = nil; + + if (recursive) { + relativePaths = [fm subpathsOfDirectoryAtPath:path error:&error]; + } + else { + relativePaths = [fm contentsOfDirectoryAtPath:path error:&error]; + } + + if (error) { + exception = [[error localizedDescription] UTF8String]; + } + else { + CefRefPtr paths = CefV8Value::CreateArray(); + for (NSUInteger i = 0; i < relativePaths.count; i++) { + NSString *relativePath = [relativePaths objectAtIndex:i]; + NSString *fullPath = [path stringByAppendingPathComponent:relativePath]; + paths->SetValue(i, CefV8Value::CreateString([fullPath UTF8String])); + } + + CefV8ValueList args; + args.push_back(paths); + CefRefPtr e; + arguments[2]->ExecuteFunction(arguments[2], args, retval, e, true); + exception = e->GetMessage(); + } + + return true; + } else if (name == "open") { NSString *path = stringFromCefV8Value(arguments[0]); [NSApp open:path]; diff --git a/spec/atom/project-spec.coffee b/spec/atom/project-spec.coffee index 6550de079..ece27ba2d 100644 --- a/spec/atom/project-spec.coffee +++ b/spec/atom/project-spec.coffee @@ -8,7 +8,7 @@ describe "Project", -> describe ".getFilePaths()", -> it "returns a promise which resolves to a list of all file urls in the project, recursively", -> - expectedPaths = (url.replace(project.url, '') for url in fs.list(project.url, true) when fs.isFile url) + expectedPaths = (url.replace(project.url, '') for url in fs.listTree(project.url) when fs.isFile url) waitsForPromise -> project.getFilePaths().done (result) -> diff --git a/spec/spec-suite.coffee b/spec/spec-suite.coffee index 08524f51c..4479e1c12 100644 --- a/spec/spec-suite.coffee +++ b/spec/spec-suite.coffee @@ -1,4 +1,4 @@ fs = require 'fs' require 'spec-helper' -require path for path in fs.listDirectoryTree($atom.loadPath + "/spec") when /-spec\.coffee$/.test path +require path for path in fs.listTree($atom.loadPath + "/spec") when /-spec\.coffee$/.test path diff --git a/spec/stdlib/fs-spec.coffee b/spec/stdlib/fs-spec.coffee index 1e43de342..cac346606 100644 --- a/spec/stdlib/fs-spec.coffee +++ b/spec/stdlib/fs-spec.coffee @@ -19,19 +19,19 @@ describe "fs", -> expect(fs.join('a', 'b/c/', 'd/')).toBe 'a/b/c/d/' describe ".async", -> - describe ".listFiles(directoryPath, recursive)", -> - directoryPath = null - beforeEach -> directoryPath = require.resolve 'fixtures/dir' + directoryPath = null + beforeEach -> + directoryPath = require.resolve 'fixtures/dir' - describe "when recursive is true", -> - it "returns a promise that resolves to the recursive contents of that directory that are files", -> - waitsForPromise -> - fs.async.listFiles(directoryPath, true).done (result) -> - expect(result).toEqual (path for path in fs.list(directoryPath, true) when fs.isFile(path)) + describe ".listTree(directoryPath)", -> + it "returns a promise that resolves to the recursive contents of that directory", -> + waitsForPromise -> + fs.async.listTree(directoryPath).done (result) -> + expect(result).toEqual fs.listTree(directoryPath) - describe "when recursive is false", -> - it "returns a promise that resolves to the contents of that directory that are files", -> - waitsForPromise -> - fs.async.listFiles(directoryPath).done (result) -> - expect(result).toEqual (path for path in fs.list(directoryPath) when fs.isFile(path)) + describe ".listFiles(directoryPath)", -> + it "returns a promise that resolves to the contents of that directory", -> + waitsForPromise -> + fs.async.listFiles(directoryPath).done (result) -> + expect(result).toEqual fs.list(directoryPath) diff --git a/src/stdlib/fs.coffee b/src/stdlib/fs.coffee index 2c99fb363..7c0fabe61 100644 --- a/src/stdlib/fs.coffee +++ b/src/stdlib/fs.coffee @@ -21,10 +21,10 @@ module.exports = # Return the dirname of the given path. That is the path with any trailing # non-directory component removed. directory: (path) -> - if @isDirectory(absPath) - absPath.replace(/\/?$/, '/') + if @isDirectory(path) + path.replace(/\/?$/, '/') else - absPath.replace(new RegExp("/#{@base(path)}$"), '/') + path.replace(new RegExp("/#{@base(path)}$"), '/') # Returns true if the file specified by path exists exists: (path) -> @@ -43,19 +43,14 @@ module.exports = # Returns true if the file specified by path exists and is a # regular file. isFile: (path) -> - $native.isFile path + not $native.isDirectory path # Returns an array with all the names of files contained # in the directory path. list: (path) -> $native.list(path, false) - # Returns an Array that starts with the given directory, and all the - # directories relative to the given path, discovered by a depth first - # traversal of every directory in any visited directory, not traversing - # symbolic links to directories, in lexically sorted order within - # directories. - listDirectoryTree: (path) -> + listTree: (path) -> $native.list(path, true) # Remove a file at the given path. Throws an error if path is not a @@ -69,14 +64,18 @@ module.exports = # Open, write, flush, and close a file, writing the given content. write: (path, content) -> - str = OSX.NSString.stringWithUTF8String content - enc = OSX.NSUTF8StringEncoding - str.writeToFile_atomically_encoding_error path, true, enc, null + $native.write(path, content) async: - listFiles: (path, recursive) -> + listFiles: (path) -> deferred = $.Deferred() - $atomController.fs.listFilesAtPath_recursive_onComplete path, recursive, (subpaths) -> + $native.asyncList path, false, (subpaths) -> + deferred.resolve subpaths + deferred + + listTree: (path) -> + deferred = $.Deferred() + $native.asyncList path, true, (subpaths) -> deferred.resolve subpaths deferred