|
| 1 | +// Workspace/ToolsVersionSpecificationRewriter.swift - Prepends/replaces Swift tools version specifications in manifest files. |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +// ----------------------------------------------------------------------------- |
| 12 | +/// |
| 13 | +/// This file implements a global function that rewrite the Swift tools version specification of a manifest file. |
| 14 | +/// |
| 15 | +// ----------------------------------------------------------------------------- |
| 16 | + |
| 17 | +import TSCBasic |
| 18 | +import PackageModel |
| 19 | +import PackageLoading |
| 20 | +import TSCUtility |
| 21 | + |
| 22 | +/// An error that causes the access to a manifest to fails. |
| 23 | +public struct ManifestAccessError: Error, CustomStringConvertible { |
| 24 | + public init(_ kind: Kind, at path: AbsolutePath) { |
| 25 | + self.kind = kind |
| 26 | + self.path = path |
| 27 | + } |
| 28 | + |
| 29 | + /// The kind of the error being raised. |
| 30 | + public enum Kind: Equatable { |
| 31 | + /// A component of a specified pathname did not exist, or the pathname was an empty string. |
| 32 | + /// |
| 33 | + /// This error is equivalent to `TSCBasic.FileSystemError.Kind.noEntry` and corresponds to the POSIX ENOENT error code, but is specialised for manifest access. |
| 34 | + case noSuchFileOrDirectory |
| 35 | + /// The path points to a directory. |
| 36 | + /// |
| 37 | + /// This error is equivalent to `TSCBasic.FileSystemError.Kind.isDirectory` and corresponds to rhe POSIX EISDIR error code, but is specialised for manifest access. |
| 38 | + case isADirectory |
| 39 | + /// The manifest cannot be accessed for an unknown reason. |
| 40 | + case unknown |
| 41 | + } |
| 42 | + |
| 43 | + /// The kind of the error being raised. |
| 44 | + public let kind: Kind |
| 45 | + |
| 46 | + /// The absolute path where the error occurred. |
| 47 | + public let path: AbsolutePath |
| 48 | + |
| 49 | + public var description: String { |
| 50 | + var reason: String { |
| 51 | + switch kind { |
| 52 | + case .noSuchFileOrDirectory: |
| 53 | + return "a component of the path does not exist, or the path is an empty string" |
| 54 | + case .isADirectory: |
| 55 | + return "the path is a directory; a file is expected" |
| 56 | + case .unknown: |
| 57 | + return "an unknown error occurred" |
| 58 | + } |
| 59 | + } |
| 60 | + return "no accessible Swift Package Manager manifest file found at '\(path)'; \(reason)" |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// Rewrites Swift tools version specification to the non-version-specific manifest file (`Package.swift`) in the given directory. |
| 65 | +/// |
| 66 | +/// If the main manifest file already contains a valid tools version specification (ignoring the validity of the version specifier and that of everything following it), then the existing specification is replaced by this new one. |
| 67 | +/// |
| 68 | +/// The version specifier in the specification does not contain any build metadata or pre-release identifier. The patch version is included if and only if it's not zero. |
| 69 | +/// |
| 70 | +/// A `FileSystemError` is thrown if the manifest file is unable to be read from or written to. |
| 71 | +/// |
| 72 | +/// - Precondition: `manifestDirectoryPath` must be a valid path to a directory that contains a `Package.swift` file. |
| 73 | +/// |
| 74 | +/// - Parameters: |
| 75 | +/// - manifestDirectoryPath: The absolute path to the given directory. |
| 76 | +/// - toolsVersion: The Swift tools version to specify as the lowest supported version. |
| 77 | +/// - fileSystem: The filesystem to read/write the manifest file on. |
| 78 | +public func rewriteToolsVersionSpecification(toDefaultManifestIn manifestDirectoryPath: AbsolutePath, specifying toolsVersion: ToolsVersion, fileSystem: FileSystem) throws { |
| 79 | + let manifestFilePath = manifestDirectoryPath.appending(component: Manifest.filename) |
| 80 | + |
| 81 | + guard fileSystem.isFile(manifestFilePath) else { |
| 82 | + guard fileSystem.exists(manifestFilePath) else { |
| 83 | + throw ManifestAccessError(.noSuchFileOrDirectory, at: manifestFilePath) |
| 84 | + } |
| 85 | + guard !fileSystem.isDirectory(manifestFilePath) else { |
| 86 | + throw ManifestAccessError(.isADirectory, at: manifestFilePath) |
| 87 | + } |
| 88 | + throw ManifestAccessError(.unknown, at: manifestFilePath) |
| 89 | + } |
| 90 | + |
| 91 | + /// The current contents of the file. |
| 92 | + let contents = try fileSystem.readFileContents(manifestFilePath) |
| 93 | + |
| 94 | + let stream = BufferedOutputByteStream() |
| 95 | + // Write out the tools version specification, including the patch version if and only if it's not zero. |
| 96 | + stream <<< toolsVersion.specification(roundedTo: .automatic) <<< "\n" |
| 97 | + |
| 98 | + // The following lines up to line 77 append the file contents except for the Swift tools version specification line. |
| 99 | + |
| 100 | + guard let contentsDecodedWithUTF8 = contents.validDescription else { |
| 101 | + throw ToolsVersionLoader.Error.nonUTF8EncodedManifest(path: manifestFilePath) |
| 102 | + } |
| 103 | + |
| 104 | + let manifestComponents = ToolsVersionLoader.split(contentsDecodedWithUTF8) |
| 105 | + |
| 106 | + let toolsVersionSpecificationComponents = manifestComponents.toolsVersionSpecificationComponents |
| 107 | + |
| 108 | + // Replace the Swift tools version specification line if and only if it's well-formed up to the version specifier. |
| 109 | + // This matches the behaviour of the old (now removed) [`ToolsVersionLoader.split(:_)`](https://github.com/WowbaggersLiquidLunch/swift-package-manager/blob/49cfc46bc5defd3ce8e0c0261e3e2cb475bcdb91/Sources/PackageLoading/ToolsVersionLoader.swift#L160). |
| 110 | + if toolsVersionSpecificationComponents.everythingUpToVersionSpecifierIsWellFormed { |
| 111 | + stream <<< ByteString(encodingAsUTF8: String(manifestComponents.contentsAfterToolsVersionSpecification)) |
| 112 | + } else { |
| 113 | + stream <<< contents |
| 114 | + } |
| 115 | + |
| 116 | + try fileSystem.writeFileContents(manifestFilePath, bytes: stream.bytes) |
| 117 | +} |
0 commit comments