|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VS Code Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 the VS Code Swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of VS Code Swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +export interface SwiftlyConfig { |
| 16 | + installedToolchains: string[]; |
| 17 | + inUse: string; |
| 18 | + version: string; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * This code is a port of the toolchain version parsing in Swiftly. |
| 23 | + * Until Swiftly can report the location of the toolchains under its management |
| 24 | + * use `ToolchainVersion.parse(versionString)` to reconstruct the directory name of the toolchain on disk. |
| 25 | + * https://github.com/swiftlang/swiftly/blob/bd6884316817e400a0ec512599f046fa437e9760/Sources/SwiftlyCore/ToolchainVersion.swift# |
| 26 | + */ |
| 27 | +// |
| 28 | +// Enum representing a fully resolved toolchain version (e.g. 5.6.7 or 5.7-snapshot-2022-07-05). |
| 29 | +export class ToolchainVersion { |
| 30 | + private type: "stable" | "snapshot"; |
| 31 | + private value: StableRelease | Snapshot; |
| 32 | + |
| 33 | + constructor( |
| 34 | + value: |
| 35 | + | { |
| 36 | + type: "stable"; |
| 37 | + major: number; |
| 38 | + minor: number; |
| 39 | + patch: number; |
| 40 | + } |
| 41 | + | { |
| 42 | + type: "snapshot"; |
| 43 | + branch: Branch; |
| 44 | + date: string; |
| 45 | + } |
| 46 | + ) { |
| 47 | + if (value.type === "stable") { |
| 48 | + this.type = "stable"; |
| 49 | + this.value = new StableRelease(value.major, value.minor, value.patch); |
| 50 | + } else { |
| 51 | + this.type = "snapshot"; |
| 52 | + this.value = new Snapshot(value.branch, value.date); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private static stableRegex = /^(?:Swift )?(\d+)\.(\d+)\.(\d+)$/; |
| 57 | + private static mainSnapshotRegex = /^main-snapshot-(\d{4}-\d{2}-\d{2})$/; |
| 58 | + private static releaseSnapshotRegex = /^(\d+)\.(\d+)-snapshot-(\d{4}-\d{2}-\d{2})$/; |
| 59 | + |
| 60 | + /** |
| 61 | + * Parse a toolchain version from the provided string |
| 62 | + **/ |
| 63 | + static parse(string: string): ToolchainVersion { |
| 64 | + let match: RegExpMatchArray | null; |
| 65 | + |
| 66 | + // Try to match as stable release |
| 67 | + match = string.match(this.stableRegex); |
| 68 | + if (match) { |
| 69 | + const major = parseInt(match[1], 10); |
| 70 | + const minor = parseInt(match[2], 10); |
| 71 | + const patch = parseInt(match[3], 10); |
| 72 | + |
| 73 | + if (isNaN(major) || isNaN(minor) || isNaN(patch)) { |
| 74 | + throw new Error(`invalid stable version: ${string}`); |
| 75 | + } |
| 76 | + |
| 77 | + return new ToolchainVersion({ |
| 78 | + type: "stable", |
| 79 | + major, |
| 80 | + minor, |
| 81 | + patch, |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + // Try to match as main snapshot |
| 86 | + match = string.match(this.mainSnapshotRegex); |
| 87 | + if (match) { |
| 88 | + return new ToolchainVersion({ |
| 89 | + type: "snapshot", |
| 90 | + branch: Branch.main(), |
| 91 | + date: match[1], |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + // Try to match as release snapshot |
| 96 | + match = string.match(this.releaseSnapshotRegex); |
| 97 | + if (match) { |
| 98 | + const major = parseInt(match[1], 10); |
| 99 | + const minor = parseInt(match[2], 10); |
| 100 | + |
| 101 | + if (isNaN(major) || isNaN(minor)) { |
| 102 | + throw new Error(`invalid release snapshot version: ${string}`); |
| 103 | + } |
| 104 | + |
| 105 | + return new ToolchainVersion({ |
| 106 | + type: "snapshot", |
| 107 | + branch: Branch.release(major, minor), |
| 108 | + date: match[3], |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + throw new Error(`invalid toolchain version: "${string}"`); |
| 113 | + } |
| 114 | + |
| 115 | + get name(): string { |
| 116 | + if (this.type === "stable") { |
| 117 | + const release = this.value as StableRelease; |
| 118 | + return `${release.major}.${release.minor}.${release.patch}`; |
| 119 | + } else { |
| 120 | + const snapshot = this.value as Snapshot; |
| 121 | + if (snapshot.branch.type === "main") { |
| 122 | + return `main-snapshot-${snapshot.date}`; |
| 123 | + } else { |
| 124 | + return `${snapshot.branch.major}.${snapshot.branch.minor}-snapshot-${snapshot.date}`; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + get identifier(): string { |
| 130 | + if (this.type === "stable") { |
| 131 | + const release = this.value as StableRelease; |
| 132 | + if (release.patch === 0) { |
| 133 | + if (release.minor === 0) { |
| 134 | + return `swift-${release.major}-RELEASE`; |
| 135 | + } |
| 136 | + return `swift-${release.major}.${release.minor}-RELEASE`; |
| 137 | + } |
| 138 | + return `swift-${release.major}.${release.minor}.${release.patch}-RELEASE`; |
| 139 | + } else { |
| 140 | + const snapshot = this.value as Snapshot; |
| 141 | + if (snapshot.branch.type === "main") { |
| 142 | + return `swift-DEVELOPMENT-SNAPSHOT-${snapshot.date}-a`; |
| 143 | + } else { |
| 144 | + return `swift-${snapshot.branch.major}.${snapshot.branch.minor}-DEVELOPMENT-SNAPSHOT-${snapshot.date}-a`; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + get description(): string { |
| 150 | + return this.value.description; |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +class Branch { |
| 155 | + static main(): Branch { |
| 156 | + return new Branch("main", null, null); |
| 157 | + } |
| 158 | + |
| 159 | + static release(major: number, minor: number): Branch { |
| 160 | + return new Branch("release", major, minor); |
| 161 | + } |
| 162 | + |
| 163 | + private constructor( |
| 164 | + public type: "main" | "release", |
| 165 | + public _major: number | null, |
| 166 | + public _minor: number | null |
| 167 | + ) {} |
| 168 | + |
| 169 | + get description(): string { |
| 170 | + switch (this.type) { |
| 171 | + case "main": |
| 172 | + return "main"; |
| 173 | + case "release": |
| 174 | + return `${this._major}.${this._minor} development`; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + get name(): string { |
| 179 | + switch (this.type) { |
| 180 | + case "main": |
| 181 | + return "main"; |
| 182 | + case "release": |
| 183 | + return `${this._major}.${this._minor}`; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + get major(): number | null { |
| 188 | + return this._major; |
| 189 | + } |
| 190 | + |
| 191 | + get minor(): number | null { |
| 192 | + return this._minor; |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +// Snapshot class |
| 197 | +class Snapshot { |
| 198 | + // Branch enum |
| 199 | + |
| 200 | + branch: Branch; |
| 201 | + date: string; |
| 202 | + |
| 203 | + constructor(branch: Branch, date: string) { |
| 204 | + this.branch = branch; |
| 205 | + this.date = date; |
| 206 | + } |
| 207 | + |
| 208 | + get description(): string { |
| 209 | + if (this.branch.type === "main") { |
| 210 | + return `main-snapshot-${this.date}`; |
| 211 | + } else { |
| 212 | + return `${this.branch.major}.${this.branch.minor}-snapshot-${this.date}`; |
| 213 | + } |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +class StableRelease { |
| 218 | + major: number; |
| 219 | + minor: number; |
| 220 | + patch: number; |
| 221 | + |
| 222 | + constructor(major: number, minor: number, patch: number) { |
| 223 | + this.major = major; |
| 224 | + this.minor = minor; |
| 225 | + this.patch = patch; |
| 226 | + } |
| 227 | + |
| 228 | + get description(): string { |
| 229 | + return `Swift ${this.major}.${this.minor}.${this.patch}`; |
| 230 | + } |
| 231 | +} |
0 commit comments