Skip to content

Commit c56dca2

Browse files
committed
[Foundation] Implemented mkdtemp
1 parent f5728c6 commit c56dca2

File tree

7 files changed

+41
-48
lines changed

7 files changed

+41
-48
lines changed

Sources/POSIX/mkdtemp.swift

Lines changed: 0 additions & 40 deletions
This file was deleted.

Sources/Utility/mkdtemp.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright 2015 - 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import Foundation
12+
13+
/**
14+
Creates a temporary directory for the duration of the provided closure.
15+
16+
- Note: the contents of the temporary directory is recursively removed.
17+
*/
18+
public func mkdtemp<T>(_ template: String, prefix: String! = nil, body: @noescape(String) throws -> T) throws -> T {
19+
20+
let dirname = "template.\(NSProcessInfo.processInfo().globallyUniqueString)"
21+
22+
#if os(OSX)
23+
let path = NSURL(fileURLWithPath: NSTemporaryDirectory())
24+
.appendingPathComponent(dirname, isDirectory: true)
25+
#else
26+
let path = NSURL(fileURLWithPath: NSTemporaryDirectory())
27+
.URLByAppendingPathComponent(dirname, isDirectory: true)!
28+
#endif
29+
30+
try NSFileManager.`default`().createDirectory(at: path, withIntermediateDirectories: true, attributes: [:])
31+
defer { _ = try? NSFileManager.`default`().removeItem(at: path) }
32+
33+
return try body(path.path!)
34+
}

Tests/Functional/Utilities.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import func POSIX.getenv
1616
import func POSIX.popen
1717
import func Utility.realpath
1818
import enum POSIX.Error
19-
import func POSIX.mkdtemp
19+
import func Utility.mkdtemp
2020

2121
#if os(OSX)
22-
import class Foundation.NSBundle
22+
import class Foundation.NSBundle
2323
#endif
2424

2525

@@ -30,7 +30,7 @@ func fixture(name fixtureName: String, tags: [String] = [], file: StaticString =
3030
}
3131

3232
do {
33-
try POSIX.mkdtemp(gsub(fixtureName)) { prefix in
33+
try Utility.mkdtemp(gsub(fixtureName)) { prefix in
3434
defer { _ = try? unlink(prefix) }
3535

3636
let rootd = Path.join(#file, "../../../Fixtures", fixtureName).normpath
@@ -166,7 +166,7 @@ func executeSwiftBuild(_ chdir: String, configuration: Configuration = .Debug, p
166166

167167
func mktmpdir(_ file: StaticString = #file, line: UInt = #line, body: @noescape(String) throws -> Void) {
168168
do {
169-
try POSIX.mkdtemp("spm-tests") { dir in
169+
try Utility.mkdtemp("spm-tests") { dir in
170170
defer { _ = try? unlink(dir) }
171171
try body(dir)
172172
}

Tests/Utility/MiscTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
import func Utility.realpath
12-
import func POSIX.mkdtemp
12+
import func Utility.mkdtemp
1313
import func POSIX.mkdir
1414
import func POSIX.symlink
1515
import func Utility.unlink

Tests/Utility/PathTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import XCTest
1515
import func POSIX.getenv
1616
import func POSIX.getcwd
17-
import func POSIX.mkdtemp
17+
import func Utility.mkdtemp
1818
import func Utility.unlink
1919
import func POSIX.symlink
2020
import func POSIX.mkdir

Tests/Xcodeproj/FunctionalTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
import func POSIX.mkdtemp
11+
import func Utility.mkdtemp
1212
import PackageType
1313
import Xcodeproj
1414
import Utility

Tests/Xcodeproj/GenerateXcodeprojTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
import func POSIX.mkdtemp
1211
import PackageType
1312
import Xcodeproj
1413
import Utility

0 commit comments

Comments
 (0)