Skip to content

Commit 62dca49

Browse files
committed
Merge pull request #10 from neonichu/linux-support
Support for Linux
2 parents 225ee44 + 65d5e3b commit 62dca49

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

Sources/PathKit.swift

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
// PathKit - Effortless path operations
22

3+
#if os(Linux)
4+
import Glibc
5+
6+
let system_glob = Glibc.glob
7+
#else
38
import Darwin
9+
10+
let system_glob = Darwin.glob
11+
#endif
12+
413
import Foundation
514

615

@@ -31,7 +40,13 @@ public struct Path {
3140
path = "."
3241
} else if components.first == Path.separator && components.count > 1 {
3342
let p = components.joinWithSeparator(Path.separator)
43+
#if os(Linux)
44+
let index = p.startIndex.distanceTo(p.startIndex.successor())
45+
path = NSString(string: p).substringFromIndex(index)
46+
#else
3447
path = p.substringFromIndex(p.startIndex.successor())
48+
#endif
49+
3550
} else {
3651
path = components.joinWithSeparator(Path.separator)
3752
}
@@ -115,7 +130,7 @@ extension Path {
115130
/// representation.
116131
///
117132
public func normalize() -> Path {
118-
return Path((self.path as NSString).stringByStandardizingPath)
133+
return Path(NSString(string: self.path).stringByStandardizingPath)
119134
}
120135

121136
/// De-normalizes the path, by replacing the current user home directory with "~".
@@ -124,7 +139,12 @@ extension Path {
124139
/// representation.
125140
///
126141
public func abbreviate() -> Path {
127-
return Path((self.path as NSString).stringByAbbreviatingWithTildeInPath)
142+
#if os(Linux)
143+
// TODO: actually de-normalize the path
144+
return self
145+
#else
146+
return Path(NSString(string: self.path).stringByAbbreviatingWithTildeInPath)
147+
#endif
128148
}
129149

130150
/// Returns the path of the item pointed to by a symbolic link.
@@ -151,7 +171,7 @@ extension Path {
151171
/// - Returns: the last path component
152172
///
153173
public var lastComponent: String {
154-
return (path as NSString).lastPathComponent
174+
return NSString(string: path).lastPathComponent
155175
}
156176

157177
/// The last path component without file extension
@@ -161,7 +181,7 @@ extension Path {
161181
/// - Returns: the last path component without file extension
162182
///
163183
public var lastComponentWithoutExtension: String {
164-
return (lastComponent as NSString).stringByDeletingPathExtension
184+
return NSString(string: lastComponent).stringByDeletingPathExtension
165185
}
166186

167187
/// Splits the string representation on the directory separator.
@@ -170,15 +190,15 @@ extension Path {
170190
/// - Returns: all path components
171191
///
172192
public var components: [String] {
173-
return (path as NSString).pathComponents
193+
return NSString(string: path).pathComponents
174194
}
175195

176196
/// The file extension behind the last dot of the last component.
177197
///
178198
/// - Returns: the file extension
179199
///
180200
public var `extension`: String? {
181-
let pathExtension = (path as NSString).pathExtension
201+
let pathExtension = NSString(string: path).pathExtension
182202
if pathExtension.isEmpty {
183203
return nil
184204
}
@@ -391,13 +411,21 @@ extension Path {
391411
/// depending on the platform.
392412
///
393413
public static var home: Path {
414+
#if os(Linux)
415+
return Path(NSProcessInfo.processInfo().environment["HOME"] ?? "/")
416+
#else
394417
return Path(NSHomeDirectory())
418+
#endif
395419
}
396420

397421
/// - Returns: the path of the temporary directory for the current user.
398422
///
399423
public static var temporary: Path {
424+
#if os(Linux)
425+
return Path(NSProcessInfo.processInfo().environment["TMP"] ?? "/tmp")
426+
#else
400427
return Path(NSTemporaryDirectory())
428+
#endif
401429
}
402430

403431
/// - Returns: the path of a temporary directory unique for the process.
@@ -441,7 +469,7 @@ extension Path {
441469
/// - Returns: the contents of the file at the specified path as string.
442470
///
443471
public func read(encoding: NSStringEncoding = NSUTF8StringEncoding) throws -> String {
444-
return try NSString(contentsOfFile: path, encoding: encoding) as String
472+
return try NSString(contentsOfFile: path, encoding: encoding).substringFromIndex(0) as String
445473
}
446474

447475
/// Write a file.
@@ -468,7 +496,7 @@ extension Path {
468496
/// - Returns: the contents of the file at the specified path as string.
469497
///
470498
public func write(string: String, encoding: NSStringEncoding = NSUTF8StringEncoding) throws {
471-
try string.writeToFile(normalize().path, atomically: true, encoding: encoding)
499+
try NSString(string: string).writeToFile(normalize().path, atomically: true, encoding: encoding)
472500
}
473501
}
474502

@@ -519,8 +547,13 @@ extension Path {
519547
}
520548

521549
let flags = GLOB_TILDE | GLOB_BRACE | GLOB_MARK
522-
if Darwin.glob(cPattern, flags, nil, &gt) == 0 {
523-
return (0..<Int(gt.gl_matchc)).flatMap { index in
550+
if system_glob(cPattern, flags, nil, &gt) == 0 {
551+
#if os(Linux)
552+
let matchc = gt.gl_pathc
553+
#else
554+
let matchc = gt.gl_matchc
555+
#endif
556+
return (0..<Int(matchc)).flatMap { index in
524557
if let path = String.fromCString(gt.gl_pathv[index]) {
525558
return Path(path)
526559
}
@@ -635,8 +668,8 @@ internal func +(lhs: String, rhs: String) -> Path {
635668
// Absolute paths replace relative paths
636669
return Path(rhs)
637670
} else {
638-
var lSlice = (lhs as NSString).pathComponents.fullSlice
639-
var rSlice = (rhs as NSString).pathComponents.fullSlice
671+
var lSlice = NSString(string: lhs).pathComponents.fullSlice
672+
var rSlice = NSString(string: rhs).pathComponents.fullSlice
640673

641674
// Get rid of trailing "/" at the left side
642675
if lSlice.count > 1 && lSlice.last == Path.separator {

0 commit comments

Comments
 (0)