Do not create temporary directory

This commit is contained in:
Yuya Tanaka 2016-04-06 18:23:39 +09:00
parent 219b4699cf
commit 58d40869da

View file

@ -13,15 +13,13 @@ final class Formatter {
static var sharedInstance = Formatter() static var sharedInstance = Formatter()
private static let pathExtension = "SwiftLintXcode" private static let pathExtension = "SwiftLintXcode"
let fileManager = NSFileManager.defaultManager() private let fileManager = NSFileManager.defaultManager()
let tempDirURL: NSURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("SwiftLintXcode-\(NSUUID().UUIDString)")
struct CursorPosition { private struct CursorPosition {
let line: Int let line: Int
let column: Int let column: Int
} }
class func isFormattableDocument(document: NSDocument) -> Bool { class func isFormattableDocument(document: NSDocument) -> Bool {
return (document.fileURL?.pathExtension?.lowercaseString == "swift") ?? false return (document.fileURL?.pathExtension?.lowercaseString == "swift") ?? false
} }
@ -123,7 +121,6 @@ final class Formatter {
} }
private func withTempporaryFile<T>(@noescape callback: (filePath: String) throws -> T) throws -> T { private func withTempporaryFile<T>(@noescape callback: (filePath: String) throws -> T) throws -> T {
try ensureTemporaryDirectory()
let filePath = createTemporaryPath() let filePath = createTemporaryPath()
if fileManager.fileExistsAtPath(filePath) { if fileManager.fileExistsAtPath(filePath) {
throw NSError(domain: "net.ypresto.SwiftLintXcode", code: 0, userInfo: [ throw NSError(domain: "net.ypresto.SwiftLintXcode", code: 0, userInfo: [
@ -135,11 +132,7 @@ final class Formatter {
} }
private func createTemporaryPath() -> String { private func createTemporaryPath() -> String {
return tempDirURL.URLByAppendingPathComponent(NSUUID().UUIDString).path! + ".swift" return NSURL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
} .URLByAppendingPathComponent("SwiftLintXcode_\(NSUUID().UUIDString).swift").path!
private func ensureTemporaryDirectory() throws {
if fileManager.fileExistsAtPath(tempDirURL.path!) { return }
try fileManager.createDirectoryAtURL(tempDirURL, withIntermediateDirectories: true, attributes: nil)
} }
} }