forked from swiftlang/swift-package-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem+Extensions.swift
More file actions
247 lines (211 loc) · 10.5 KB
/
Copy pathFileSystem+Extensions.swift
File metadata and controls
247 lines (211 loc) · 10.5 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
This source file is part of the Swift.org open source project
Copyright (c) 2020-2021 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import class Foundation.FileManager
import struct Foundation.Data
import struct Foundation.UUID
import SystemPackage
import TSCBasic
// MARK: - user level
extension FileSystem {
/// SwiftPM directory under user's home directory (~/.swiftpm)
public var dotSwiftPM: AbsolutePath {
self.homeDirectory.appending(component: ".swiftpm")
}
fileprivate var idiomaticSwiftPMDirectory: AbsolutePath? {
return FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first.flatMap { AbsolutePath($0.path) }?.appending(component: "org.swift.swiftpm")
}
}
// MARK: - cache
extension FileSystem {
private var idiomaticUserCacheDirectory: AbsolutePath? {
// in TSC: FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)
self.cachesDirectory
}
/// SwiftPM cache directory under user's caches directory (if exists)
public var swiftPMCacheDirectory: AbsolutePath {
if let path = self.idiomaticUserCacheDirectory {
return path.appending(component: "org.swift.swiftpm")
} else {
return self.dotSwiftPMCachesDirectory
}
}
fileprivate var dotSwiftPMCachesDirectory: AbsolutePath {
return self.dotSwiftPM.appending(component: "cache")
}
}
extension FileSystem {
public func getOrCreateSwiftPMCacheDirectory() throws -> AbsolutePath {
let idiomaticCacheDirectory = self.swiftPMCacheDirectory
// Create idiomatic if necessary
if !self.exists(idiomaticCacheDirectory) {
try self.createDirectory(idiomaticCacheDirectory, recursive: true)
}
// Create ~/.swiftpm if necessary
if !self.exists(self.dotSwiftPM) {
try self.createDirectory(self.dotSwiftPM, recursive: true)
}
// Create ~/.swiftpm/cache symlink if necessary
if !self.exists(self.dotSwiftPMCachesDirectory, followSymlink: false) {
try self.createSymbolicLink(dotSwiftPMCachesDirectory, pointingAt: idiomaticCacheDirectory, relative: false)
}
return idiomaticCacheDirectory
}
}
// MARK: - configuration
extension FileSystem {
/// SwiftPM config directory under user's config directory (if exists)
public var swiftPMConfigurationDirectory: AbsolutePath {
if let path = self.idiomaticSwiftPMDirectory {
return path.appending(component: "configuration")
} else {
return self.dotSwiftPMConfigurationDirectory
}
}
fileprivate var dotSwiftPMConfigurationDirectory: AbsolutePath {
return self.dotSwiftPM.appending(component: "configuration")
}
}
extension FileSystem {
public func getOrCreateSwiftPMConfigurationDirectory(observabilityScope: ObservabilityScope?) throws -> AbsolutePath {
let idiomaticConfigurationDirectory = self.swiftPMConfigurationDirectory
// temporary 5.6, remove on next version: transition from previous configuration location
if !self.exists(idiomaticConfigurationDirectory) {
try self.createDirectory(idiomaticConfigurationDirectory, recursive: true)
}
// in the case where ~/.swiftpm/configuration is not the idiomatic location (eg on macOS where its /Users/<user>/Library/org.swift.swiftpm/configuration)
if idiomaticConfigurationDirectory != self.dotSwiftPMConfigurationDirectory {
// copy the configuration files from old location (eg /Users/<user>/Library/org.swift.swiftpm) to new one (eg /Users/<user>/Library/org.swift.swiftpm/configuration)
// but leave them there for backwards compatibility (eg older xcode)
let oldConfigDirectory = idiomaticConfigurationDirectory.parentDirectory
if self.exists(oldConfigDirectory, followSymlink: false) && self.isDirectory(oldConfigDirectory) {
let configurationFiles = try self.getDirectoryContents(oldConfigDirectory)
.map{ oldConfigDirectory.appending(component: $0) }
.filter{ self.isFile($0) && !self.isSymlink($0) && $0.extension != "lock"}
for file in configurationFiles {
let destination = idiomaticConfigurationDirectory.appending(component: file.basename)
observabilityScope?.emit(warning: "Usage of \(file) has been deprecated. Please delete it and use the new \(destination) instead.")
if !self.exists(destination) {
try self.copy(from: file, to: destination)
}
}
}
// in the case where ~/.swiftpm/configuration is the idiomatic location (eg on Linux)
} else {
// copy the configuration files from old location (~/.swiftpm/config) to new one (~/.swiftpm/configuration)
// but leave them there for backwards compatibility (eg older toolchain)
let oldConfigDirectory = self.dotSwiftPM.appending(component: "config")
if self.exists(oldConfigDirectory, followSymlink: false) && self.isDirectory(oldConfigDirectory) {
let configurationFiles = try self.getDirectoryContents(oldConfigDirectory)
.map{ oldConfigDirectory.appending(component: $0) }
.filter{ self.isFile($0) && !self.isSymlink($0) && $0.extension != "lock"}
for file in configurationFiles {
let destination = idiomaticConfigurationDirectory.appending(component: file.basename)
observabilityScope?.emit(warning: "Usage of \(file) has been deprecated. Please delete it and use the new \(destination) instead.")
if !self.exists(destination) {
try self.copy(from: file, to: destination)
}
}
}
}
// ~temporary 5.6 migration
// Create idiomatic if necessary
if !self.exists(idiomaticConfigurationDirectory) {
try self.createDirectory(idiomaticConfigurationDirectory, recursive: true)
}
// Create ~/.swiftpm if necessary
if !self.exists(self.dotSwiftPM) {
try self.createDirectory(self.dotSwiftPM, recursive: true)
}
// Create ~/.swiftpm/configuration symlink if necessary
if !self.exists(self.dotSwiftPMConfigurationDirectory, followSymlink: false) {
try self.createSymbolicLink(dotSwiftPMConfigurationDirectory, pointingAt: idiomaticConfigurationDirectory, relative: false)
}
return idiomaticConfigurationDirectory
}
}
// MARK: - security
extension FileSystem {
/// SwiftPM security directory under user's security directory (if exists)
public var swiftPMSecurityDirectory: AbsolutePath {
if let path = self.idiomaticSwiftPMDirectory {
return path.appending(component: "security")
} else {
return self.dotSwiftPMSecurityDirectory
}
}
fileprivate var dotSwiftPMSecurityDirectory: AbsolutePath {
return self.dotSwiftPM.appending(component: "security")
}
}
extension FileSystem {
public func getOrCreateSwiftPMSecurityDirectory() throws -> AbsolutePath {
let idiomaticSecurityDirectory = self.swiftPMSecurityDirectory
// temporary 5.6, remove on next version: transition from ~/.swiftpm/security to idiomatic location + symbolic link
if idiomaticSecurityDirectory != self.dotSwiftPMSecurityDirectory &&
self.exists(self.dotSwiftPMSecurityDirectory) &&
self.isDirectory(self.dotSwiftPMSecurityDirectory) {
try self.removeFileTree(self.dotSwiftPMSecurityDirectory)
}
// ~temporary 5.6 migration
// Create idiomatic if necessary
if !self.exists(idiomaticSecurityDirectory) {
try self.createDirectory(idiomaticSecurityDirectory, recursive: true)
}
// Create ~/.swiftpm if necessary
if !self.exists(self.dotSwiftPM) {
try self.createDirectory(self.dotSwiftPM, recursive: true)
}
// Create ~/.swiftpm/security symlink if necessary
if !self.exists(self.dotSwiftPMSecurityDirectory, followSymlink: false) {
try self.createSymbolicLink(dotSwiftPMSecurityDirectory, pointingAt: idiomaticSecurityDirectory, relative: false)
}
return idiomaticSecurityDirectory
}
}
// MARK: - Utilities
extension FileSystem {
public func readFileContents(_ path: AbsolutePath) throws -> Data {
return try Data(self.readFileContents(path).contents)
}
public func readFileContents(_ path: AbsolutePath) throws -> String {
return try String(decoding: self.readFileContents(path), as: UTF8.self)
}
public func writeFileContents(_ path: AbsolutePath, data: Data) throws {
return try self.writeFileContents(path, bytes: .init(data))
}
public func writeFileContents(_ path: AbsolutePath, string: String) throws {
return try self.writeFileContents(path, bytes: .init(encodingAsUTF8: string))
}
public func writeFileContents(_ path: AbsolutePath, provider: () -> String) throws {
return try self.writeFileContents(path, string: provider())
}
}
extension FileSystem {
public func forceCreateDirectory(at path: AbsolutePath) throws {
try self.createDirectory(path.parentDirectory, recursive: true)
if self.exists(path) {
try self.removeFileTree(path)
}
try self.createDirectory(path, recursive: true)
}
}
extension FileSystem {
public func stripFirstLevel(of path: AbsolutePath) throws {
let topLevelContents = try self.getDirectoryContents(path)
guard topLevelContents.count == 1, let rootPath = topLevelContents.first.map({ path.appending(component: $0) }), self.isDirectory(rootPath) else {
throw StringError("stripFirstLevel requires single top level directory")
}
let tempDirectory = path.parentDirectory.appending(component: UUID().uuidString)
try self.move(from: rootPath, to: tempDirectory)
let rootContents = try self.getDirectoryContents(tempDirectory)
for entry in rootContents {
try self.move(from: tempDirectory.appending(component: entry), to: path.appending(component: entry))
}
try self.removeFileTree(tempDirectory)
}
}