|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014-2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Basics |
| 14 | +@_spi(FixItApplier) import SwiftIDEUtils |
| 15 | +@_spi(PackageRefactor) import SwiftRefactor |
| 16 | +import SwiftSyntax |
| 17 | + |
| 18 | +package extension PackageEdit { |
| 19 | + /// Apply the edits for the given manifest to the specified file system, |
| 20 | + /// updating the manifest to the given manifest |
| 21 | + func applyEdits( |
| 22 | + to filesystem: any FileSystem, |
| 23 | + manifest: SourceFileSyntax, |
| 24 | + manifestPath: AbsolutePath, |
| 25 | + verbose: Bool |
| 26 | + ) throws { |
| 27 | + let rootPath = manifestPath.parentDirectory |
| 28 | + |
| 29 | + // Update the manifest |
| 30 | + if verbose { |
| 31 | + print("Updating package manifest at \(manifestPath.relative(to: rootPath))...", terminator: "") |
| 32 | + } |
| 33 | + |
| 34 | + let updatedManifestSource = FixItApplier.apply( |
| 35 | + edits: manifestEdits, |
| 36 | + to: manifest |
| 37 | + ) |
| 38 | + try filesystem.writeFileContents( |
| 39 | + manifestPath, |
| 40 | + string: updatedManifestSource |
| 41 | + ) |
| 42 | + if verbose { |
| 43 | + print(" done.") |
| 44 | + } |
| 45 | + |
| 46 | + // Write all of the auxiliary files. |
| 47 | + for (auxiliaryFileRelPath, auxiliaryFileSyntax) in auxiliaryFiles { |
| 48 | + // If the file already exists, skip it. |
| 49 | + let filePath = try rootPath.appending(RelativePath(validating: auxiliaryFileRelPath)) |
| 50 | + if filesystem.exists(filePath) { |
| 51 | + if verbose { |
| 52 | + print("Skipping \(filePath.relative(to: rootPath)) because it already exists.") |
| 53 | + } |
| 54 | + |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + // If the directory does not exist yet, create it. |
| 59 | + let fileDir = filePath.parentDirectory |
| 60 | + if !filesystem.exists(fileDir) { |
| 61 | + if verbose { |
| 62 | + print("Creating directory \(fileDir.relative(to: rootPath))...", terminator: "") |
| 63 | + } |
| 64 | + |
| 65 | + try filesystem.createDirectory(fileDir, recursive: true) |
| 66 | + |
| 67 | + if verbose { |
| 68 | + print(" done.") |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Write the file. |
| 73 | + if verbose { |
| 74 | + print("Writing \(filePath.relative(to: rootPath))...", terminator: "") |
| 75 | + } |
| 76 | + |
| 77 | + try filesystem.writeFileContents( |
| 78 | + filePath, |
| 79 | + string: auxiliaryFileSyntax.description |
| 80 | + ) |
| 81 | + |
| 82 | + if verbose { |
| 83 | + print(" done.") |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments