-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDirname.swift
79 lines (72 loc) · 2.3 KB
/
Dirname.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// Dirname.swift
// Macro
//
// Created by Helge Hess.
// Copyright © 2020 ZeeZide GmbH. All rights reserved.
//
import struct Foundation.URL
import class Foundation.FileManager
/**
* An attempt to emulate the `__dirname` variable in Node modules,
* requires a function in Swift.
* `__dirname` gives the directory location of the current Swift file, commonly
* used to lookup resources that live alongside the Swift source file.
*
* Note: Do not confuse w/ `process.cwd()`, which returns the current directory
* of the process.
*
* Note: Can do synchronous I/O, be careful when to call this!
*
* ### Implementation
*
* The complicated thing is that SPM does not have proper resource locations.
* A workaround is to use the `#file` compiler directive, which contains the
* location of the Swift sourcefile _calling_ `__dirname()`.
*
* Now the difficult part is, that the environment may not have access to the
* source file anymore (because just the library is being deployed).
* In this case, we return `process.cwd`.
*
* ### `swift sh`
*
* There are extra issues w/ [swift-sh](https://github.com/mxcl/swift-sh):
*
* https://github.com/mxcl/swift-sh/issues/101
*
* So we catch this and (try to) use the CWD in that situation.
* Note: This does not yet work properly for nested modules!
*/
public func ___dirname(caller: String) -> String {
// The check for `swift sh`
let fm = FileManager.default
if caller.contains("swift-sh.cache"), let toolname = process.env["_"] {
let dirURL = URL(fileURLWithPath: process.cwd(), isDirectory: true)
let toolURL : URL = {
if #available(macOS 10.11, iOS 11, *) {
return URL(fileURLWithPath: toolname, relativeTo: dirURL)
}
else {
return dirURL.appendingPathComponent(toolname) // TBD
}
}()
if fm.fileExists(atPath: toolURL.path) {
return toolURL.deletingLastPathComponent().path
}
}
if fm.fileExists(atPath: caller) {
return URL(fileURLWithPath: caller).deletingLastPathComponent().path
}
return process.cwd()
}
#if swift(>=5.3)
@inlinable
public func __dirname(caller: String = #filePath) -> String {
return ___dirname(caller: caller)
}
#else
@inlinable
public func __dirname(caller: String = #file) -> String {
return ___dirname(caller: caller)
}
#endif