Skip to content

Commit

Permalink
Safer file creation
Browse files Browse the repository at this point in the history
Also: disable multi-threading. Leads to errors when creating out dirs as this API is not threadsafe.
  • Loading branch information
max-leuthaeuser committed Nov 9, 2023
1 parent 0a9ed68 commit 17d3594
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions Sources/SwiftAstGenLib/SwiftAstGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ public class SwiftAstGenerator {
private var srcDir: URL
private var outputDir: URL
private var prettyPrint: Bool
private let availableProcessors = ProcessInfo.processInfo.activeProcessorCount

public init(srcDir: URL, outputDir: URL, prettyPrint: Bool) throws {
self.srcDir = srcDir
Expand Down Expand Up @@ -43,17 +42,20 @@ public class SwiftAstGenerator {
.appendingPathComponent(relativeFilePath)
.appendingPathExtension("json")
let outfileDirUrl = outFileUrl.deletingLastPathComponent()

try FileManager.default.createDirectory(
atPath: outfileDirUrl.path,
withIntermediateDirectories: true,
attributes: nil
)
FileManager.default.createFile(
atPath: outFileUrl.path,
contents: nil,
attributes: nil
)
if !FileManager.default.fileExists(atPath: outfileDirUrl.path) {
try FileManager.default.createDirectory(
atPath: outfileDirUrl.path,
withIntermediateDirectories: true,
attributes: nil
)
}
if !FileManager.default.fileExists(atPath: outFileUrl.path) {
FileManager.default.createFile(
atPath: outFileUrl.path,
contents: nil,
attributes: nil
)
}
try astJsonString.write(
to: outFileUrl,
atomically: true,
Expand All @@ -73,11 +75,6 @@ public class SwiftAstGenerator {
includingPropertiesForKeys: Array(resourceKeys),
options: [.skipsPackageDescendants])!

let queue = OperationQueue()
queue.name = "SwiftAstGen"
queue.qualityOfService = .userInitiated
queue.maxConcurrentOperationCount = availableProcessors

for case let fileUrl as URL in directoryEnumerator {
guard let resourceValues = try? fileUrl.resourceValues(forKeys: resourceKeys),
let isDirectory = resourceValues.isDirectory,
Expand All @@ -92,13 +89,9 @@ public class SwiftAstGenerator {
directoryEnumerator.skipDescendants()
}
} else if isRegularFile && name.hasSuffix(".swift") {
queue.addOperation {
self.parseFile(fileUrl: fileUrl)
}
parseFile(fileUrl: fileUrl)
}
}

queue.waitUntilAllOperationsAreFinished()
}

}

0 comments on commit 17d3594

Please sign in to comment.