2828//
2929
3030import Foundation
31- import PackageDSLKit
3231
33- @available ( * , deprecated, message: " Migrate to separate protocol. " )
34- extension FileManager {
35- internal func swiftVersion( from directoryURL: URL ) -> SwiftVersion ? {
32+ extension FileManager : PackageFilesInterface {
33+ public var currentDirectoryURL : URL {
34+ URL ( fileURLWithPath: currentDirectoryPath)
35+ }
36+
37+ private func readDirectoryContents( at path: String , fileExtension: String = " swift " ) throws
38+ -> [ String ]
39+ {
40+ var contents : [ String ] = [ ]
41+ let items = try contentsOfDirectory ( atPath: path)
42+
43+ // Process subdirectories (post-order)
44+ for item in items {
45+ let itemPath = ( path as NSString ) . appendingPathComponent ( item)
46+ var isDirectory : ObjCBool = false
47+ let fileExists = fileExists ( atPath: itemPath, isDirectory: & isDirectory)
48+
49+ if fileExists && isDirectory. boolValue {
50+ contents += try readDirectoryContents ( at: itemPath, fileExtension: fileExtension)
51+ }
52+ }
53+
54+ // Process files
55+ for item in items where item. hasSuffix ( " . \( fileExtension) " ) {
56+ let itemPath = ( path as NSString ) . appendingPathComponent ( item)
57+
58+ let fileContents = try String ( contentsOfFile: itemPath, encoding: . utf8)
59+ contents. append ( fileContents)
60+ }
61+
62+ return contents
63+ }
64+ public func writePackageSwiftFile(
65+ swiftVersion: SwiftVersion ,
66+ from dslSourcesURL: URL ,
67+ to pathURL: URL
68+ ) throws {
69+ let contents = try self . readDirectoryContents (
70+ at: dslSourcesURL. path ( ) ,
71+ fileExtension: " swift "
72+ )
73+
74+ let packageFileURL = pathURL. appendingPathComponent ( " Package.swift " )
75+ let strings =
76+ [
77+ " // swift-tools-version: \( swiftVersion) " ,
78+ SupportCodeBlock . syntaxNode. trimmedDescription,
79+ ] + contents
80+ let data = Data ( strings. joined ( separator: " \n " ) . utf8)
81+ self . createFile ( atPath: packageFileURL. path ( ) , contents: data)
82+ // TODO: log error if file creation fails
83+ }
84+
85+ public func createDirectory( at url: URL , withIntermediateDirectories createIntermediates: Bool )
86+ throws
87+ {
88+ try self . createDirectory (
89+ at: url,
90+ withIntermediateDirectories: createIntermediates,
91+ attributes: nil
92+ )
93+ }
94+
95+ public func createFile( at url: URL , text: String ) {
96+ self . createFile ( atPath: url. path ( ) , contents: Data ( text. utf8) )
97+ }
98+ public func swiftVersion( from directoryURL: URL ) -> SwiftVersion ? {
3699 let swiftVersionURL = directoryURL. appending ( component: " .swift-version " )
37100 let packageSwiftURL = directoryURL. appending ( component: " Package.swift " )
38101
@@ -53,7 +116,7 @@ extension FileManager {
53116
54117 return . readFrom( packageSwiftFileURL: packageSwiftURL)
55118 }
56- internal func createTargetSourceAt(
119+ public func createTargetSourceAt(
57120 _ pathURL: URL , productName: String , _ productType: ProductType
58121 ) throws {
59122 let sourcesDirURL = pathURL. appendingPathComponent ( " Sources/ \( productName) " )
@@ -82,27 +145,24 @@ extension FileManager {
82145 contents: Data ( sourceCode. utf8)
83146 )
84147 }
85- internal func createTargetSourceAt(
86- _ pathURL: URL , productName: String , _ packageType: PackageType
148+ public func createFileStructure(
149+ forPackageType packageType: PackageType ,
150+ forProductName productName: String ,
151+ at pathURL: URL
87152 ) throws {
88- let productType : ProductType ?
89-
90- switch packageType {
91- case . empty:
92- productType = nil
93- case . library:
94- productType = . library
95- case . executable:
96- productType = . executable
153+ guard packageType != . empty else {
154+ return
97155 }
98- assert ( productType != nil , " Unknown package type \( packageType) " )
99- guard let productType else {
156+
157+ try self . createTargetSourceAt ( pathURL, productName: productName, packageType)
158+
159+ guard packageType == . library else {
100160 return
101161 }
102- try self . createTargetSourceAt ( pathURL, productName: productName, productType)
103- }
104162
105- fileprivate func createTestTargetAt( _ pathURL: URL , _ productName: String ) throws {
163+ try createTestTargetAt ( pathURL, productName)
164+ }
165+ private func createTestTargetAt( _ pathURL: URL , _ productName: String ) throws {
106166 let testingDirURL = pathURL. appendingPathComponent ( " Tests/ \( productName) Tests " )
107167 try self . createDirectory ( at: testingDirURL, withIntermediateDirectories: true )
108168
@@ -117,68 +177,23 @@ extension FileManager {
117177 """
118178 self . createFile ( atPath: testFileURL. path ( ) , contents: Data ( testCode. utf8) )
119179 }
120-
121- internal func createFileStructure(
122- forPackageType packageType: PackageType ,
123- forProductName productName: String ,
124- at pathURL: URL
125- ) throws {
126- guard packageType != . empty else {
127- return
128- }
129-
130- try self . createTargetSourceAt ( pathURL, productName: productName, packageType)
131-
132- guard packageType == . library else {
133- return
134- }
135-
136- try createTestTargetAt ( pathURL, productName)
137- }
138- internal func writePackageSwiftFile(
139- swiftVersion: SwiftVersion ,
140- from dslSourcesURL: URL ,
141- to pathURL: URL
180+ private func createTargetSourceAt(
181+ _ pathURL: URL , productName: String , _ packageType: PackageType
142182 ) throws {
143- let contents = try self . readDirectoryContents (
144- at: dslSourcesURL. path ( ) ,
145- fileExtension: " swift "
146- )
147-
148- let packageFileURL = pathURL. appendingPathComponent ( " Package.swift " )
149- let strings =
150- [
151- " // swift-tools-version: \( swiftVersion) " ,
152- SupportCodeBlock . syntaxNode. trimmedDescription,
153- ] + contents
154- let data = Data ( strings. joined ( separator: " \n " ) . utf8)
155- self . createFile ( atPath: packageFileURL. path ( ) , contents: data)
156- }
157- internal func readDirectoryContents( at path: String , fileExtension: String = " swift " ) throws
158- -> [ String ]
159- {
160- var contents : [ String ] = [ ]
161- let items = try contentsOfDirectory ( atPath: path)
162-
163- // Process subdirectories (post-order)
164- for item in items {
165- let itemPath = ( path as NSString ) . appendingPathComponent ( item)
166- var isDirectory : ObjCBool = false
167- fileExists ( atPath: itemPath, isDirectory: & isDirectory)
183+ let productType : ProductType ?
168184
169- if isDirectory. boolValue {
170- contents += try readDirectoryContents ( at: itemPath, fileExtension: fileExtension)
171- }
185+ switch packageType {
186+ case . empty:
187+ productType = nil
188+ case . library:
189+ productType = . library
190+ case . executable:
191+ productType = . executable
172192 }
173-
174- // Process files
175- for item in items where item. hasSuffix ( " . \( fileExtension) " ) {
176- let itemPath = ( path as NSString ) . appendingPathComponent ( item)
177-
178- let fileContents = try String ( contentsOfFile: itemPath, encoding: . utf8)
179- contents. append ( fileContents)
193+ assert ( productType != nil , " Unknown package type \( packageType) " )
194+ guard let productType else {
195+ return
180196 }
181-
182- return contents
197+ try self . createTargetSourceAt ( pathURL, productName: productName, productType)
183198 }
184199}
0 commit comments