@@ -62,7 +62,8 @@ extension CEWorkspaceFileManager {
6262 func addFile(
6363 fileName: String ,
6464 toFile file: CEWorkspaceFile ,
65- useExtension: String ? = nil
65+ useExtension: String ? = nil ,
66+ contents: Data ? = nil
6667 ) throws -> CEWorkspaceFile {
6768 // check the folder for other files, and see what the most common file extension is
6869 do {
@@ -95,7 +96,7 @@ extension CEWorkspaceFileManager {
9596 // Create the file
9697 guard fileManager. createFile (
9798 atPath: fileUrl. path,
98- contents: nil ,
99+ contents: contents ,
99100 attributes: [ FileAttributeKey . creationDate: Date ( ) ]
100101 ) else {
101102 throw CocoaError . error ( . fileWriteUnknown, url: fileUrl)
@@ -116,57 +117,6 @@ extension CEWorkspaceFileManager {
116117 }
117118 }
118119
119- /// This function is used to create a file with contents
120- func addFileWithContents(
121- fileName: String ,
122- toFile file: CEWorkspaceFile ,
123- useExtension: String ? = nil ,
124- contents: Data ?
125- ) throws -> CEWorkspaceFile {
126- do {
127- var fileExtension : String
128- if fileName. contains ( " . " ) {
129- fileExtension = " "
130- } else {
131- fileExtension = useExtension ?? findCommonFileExtension ( for: file)
132- if !fileExtension. isEmpty && !fileExtension. starts ( with: " . " ) {
133- fileExtension = " . " + fileExtension
134- }
135- }
136-
137- var fileUrl = file. nearestFolder. appendingPathComponent ( " \( fileName) \( fileExtension) " )
138- var fileNumber = 0
139- while fileManager. fileExists ( atPath: fileUrl. path) {
140- fileNumber += 1
141- fileUrl = fileUrl. deletingLastPathComponent ( )
142- . appendingPathComponent ( " \( fileName) \( fileNumber) \( fileExtension) " )
143- }
144-
145- guard fileUrl. fileName. isValidFilename else {
146- throw FileManagerError . invalidFileName
147- }
148-
149- guard fileManager. createFile (
150- atPath: fileUrl. path,
151- contents: contents,
152- attributes: [ FileAttributeKey . creationDate: Date ( ) ]
153- ) else {
154- throw CocoaError . error ( . fileWriteUnknown, url: fileUrl)
155- }
156-
157- try rebuildFiles ( fromItem: file. isFolder ? file : file. parent ?? file)
158- notifyObservers ( updatedItems: [ file. isFolder ? file : file. parent ?? file] )
159-
160- guard let newFile = getFile ( fileUrl. path, createIfNotFound: true ) else {
161- throw FileManagerError . fileNotIndexed
162- }
163- return newFile
164- } catch {
165- logger. error ( " Failed to add file with contents: \( error, privacy: . auto) " )
166- throw error
167- }
168- }
169-
170120 /// Finds a common file extension in the same directory as a file. Defaults to `txt` if no better alternatives
171121 /// are found.
172122 /// - Parameter file: The file to use to determine a common extension.
0 commit comments