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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2024 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 the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics
@_spi(FixItApplier) import SwiftIDEUtils
import SwiftSyntax
/// The result of editing a package, including any edits to the package
/// manifest and any new files that are introduced.
public struct PackageEditResult {
/// Edits to perform to the package manifest.
public var manifestEdits: [SourceEdit] = []
/// Auxiliary files to write.
public var auxiliaryFiles: [(RelativePath, SourceFileSyntax)] = []
}
extension PackageEditResult {
/// Apply the edits for the given manifest to the specified file system,
/// updating the manifest to the given manifest
public func applyEdits(
to filesystem: any FileSystem,
manifest: SourceFileSyntax,
manifestPath: AbsolutePath,
verbose: Bool
) throws {
let rootPath = manifestPath.parentDirectory
// Update the manifest
if verbose {
print("Updating package manifest at \(manifestPath.relative(to: rootPath))...", terminator: "")
}
let updatedManifestSource = FixItApplier.apply(
edits: manifestEdits,
to: manifest
)
try filesystem.writeFileContents(
manifestPath,
string: updatedManifestSource
)
if verbose {
print(" done.")
}
// Write all of the auxiliary files.
for (auxiliaryFileRelPath, auxiliaryFileSyntax) in auxiliaryFiles {
// If the file already exists, skip it.
let filePath = rootPath.appending(auxiliaryFileRelPath)
if filesystem.exists(filePath) {
if verbose {
print("Skipping \(filePath.relative(to: rootPath)) because it already exists.")
}
continue
}
// If the directory does not exist yet, create it.
let fileDir = filePath.parentDirectory
if !filesystem.exists(fileDir) {
if verbose {
print("Creating directory \(fileDir.relative(to: rootPath))...", terminator: "")
}
try filesystem.createDirectory(fileDir, recursive: true)
if verbose {
print(" done.")
}
}
// Write the file.
if verbose {
print("Writing \(filePath.relative(to: rootPath))...", terminator: "")
}
try filesystem.writeFileContents(
filePath,
string: auxiliaryFileSyntax.description
)
if verbose {
print(" done.")
}
}
}
}
|