1
1
// PathKit - Effortless path operations
2
2
3
+ #if os(Linux)
4
+ import Glibc
5
+
6
+ let system_glob = Glibc . glob
7
+ #else
3
8
import Darwin
9
+
10
+ let system_glob = Darwin . glob
11
+ #endif
12
+
4
13
import Foundation
5
14
6
15
@@ -31,7 +40,13 @@ public struct Path {
31
40
path = " . "
32
41
} else if components. first == Path . separator && components. count > 1 {
33
42
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
34
47
path = p. substringFromIndex ( p. startIndex. successor ( ) )
48
+ #endif
49
+
35
50
} else {
36
51
path = components. joinWithSeparator ( Path . separator)
37
52
}
@@ -115,7 +130,7 @@ extension Path {
115
130
/// representation.
116
131
///
117
132
public func normalize( ) -> Path {
118
- return Path ( ( self . path as NSString ) . stringByStandardizingPath)
133
+ return Path ( NSString ( string : self . path) . stringByStandardizingPath)
119
134
}
120
135
121
136
/// De-normalizes the path, by replacing the current user home directory with "~".
@@ -124,7 +139,12 @@ extension Path {
124
139
/// representation.
125
140
///
126
141
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
128
148
}
129
149
130
150
/// Returns the path of the item pointed to by a symbolic link.
@@ -151,7 +171,7 @@ extension Path {
151
171
/// - Returns: the last path component
152
172
///
153
173
public var lastComponent : String {
154
- return ( path as NSString ) . lastPathComponent
174
+ return NSString ( string : path ) . lastPathComponent
155
175
}
156
176
157
177
/// The last path component without file extension
@@ -161,7 +181,7 @@ extension Path {
161
181
/// - Returns: the last path component without file extension
162
182
///
163
183
public var lastComponentWithoutExtension : String {
164
- return ( lastComponent as NSString ) . stringByDeletingPathExtension
184
+ return NSString ( string : lastComponent ) . stringByDeletingPathExtension
165
185
}
166
186
167
187
/// Splits the string representation on the directory separator.
@@ -170,15 +190,15 @@ extension Path {
170
190
/// - Returns: all path components
171
191
///
172
192
public var components : [ String ] {
173
- return ( path as NSString ) . pathComponents
193
+ return NSString ( string : path ) . pathComponents
174
194
}
175
195
176
196
/// The file extension behind the last dot of the last component.
177
197
///
178
198
/// - Returns: the file extension
179
199
///
180
200
public var `extension` : String ? {
181
- let pathExtension = ( path as NSString ) . pathExtension
201
+ let pathExtension = NSString ( string : path ) . pathExtension
182
202
if pathExtension. isEmpty {
183
203
return nil
184
204
}
@@ -391,13 +411,21 @@ extension Path {
391
411
/// depending on the platform.
392
412
///
393
413
public static var home : Path {
414
+ #if os(Linux)
415
+ return Path ( NSProcessInfo . processInfo ( ) . environment [ " HOME " ] ?? " / " )
416
+ #else
394
417
return Path ( NSHomeDirectory ( ) )
418
+ #endif
395
419
}
396
420
397
421
/// - Returns: the path of the temporary directory for the current user.
398
422
///
399
423
public static var temporary : Path {
424
+ #if os(Linux)
425
+ return Path ( NSProcessInfo . processInfo ( ) . environment [ " TMP " ] ?? " /tmp " )
426
+ #else
400
427
return Path ( NSTemporaryDirectory ( ) )
428
+ #endif
401
429
}
402
430
403
431
/// - Returns: the path of a temporary directory unique for the process.
@@ -441,7 +469,7 @@ extension Path {
441
469
/// - Returns: the contents of the file at the specified path as string.
442
470
///
443
471
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
445
473
}
446
474
447
475
/// Write a file.
@@ -468,7 +496,7 @@ extension Path {
468
496
/// - Returns: the contents of the file at the specified path as string.
469
497
///
470
498
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)
472
500
}
473
501
}
474
502
@@ -519,8 +547,13 @@ extension Path {
519
547
}
520
548
521
549
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
524
557
if let path = String . fromCString ( gt. gl_pathv [ index] ) {
525
558
return Path ( path)
526
559
}
@@ -635,8 +668,8 @@ internal func +(lhs: String, rhs: String) -> Path {
635
668
// Absolute paths replace relative paths
636
669
return Path ( rhs)
637
670
} 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
640
673
641
674
// Get rid of trailing "/" at the left side
642
675
if lSlice. count > 1 && lSlice. last == Path . separator {
0 commit comments