-
Notifications
You must be signed in to change notification settings - Fork 80
Add Swiftly toolchain management #1717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
roulpriya
wants to merge
7
commits into
swiftlang:main
Choose a base branch
from
roulpriya:list-available-toolchain
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+318
−103
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3d12c9f
Add Swiftly toolchain management
roulpriya 68790c8
refactoring changes
roulpriya a4c094d
refactoring changes
roulpriya fcd3803
refactoring changes
roulpriya bb13c81
refactoring changes
roulpriya 710da2f
refactoring changes
roulpriya 25a4344
refactoring changes
roulpriya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the VS Code Swift open source project | ||
// | ||
// Copyright (c) 2025 the VS Code Swift project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import * as path from "node:path"; | ||
import { SwiftlyConfig } from "./ToolchainVersion"; | ||
import * as fs from "node:fs/promises"; | ||
import { execFile, ExecFileError } from "../utilities/utilities"; | ||
import * as vscode from "vscode"; | ||
import { Version } from "../utilities/version"; | ||
import { z } from "zod"; | ||
|
||
const ListAvailableResult = z.object({ | ||
toolchains: z.array( | ||
z.object({ | ||
inUse: z.boolean(), | ||
installed: z.boolean(), | ||
isDefault: z.boolean(), | ||
name: z.string(), | ||
version: z.discriminatedUnion("type", [ | ||
z.object({ | ||
major: z.number(), | ||
minor: z.number(), | ||
patch: z.number().optional(), | ||
type: z.literal("stable"), | ||
}), | ||
z.object({ | ||
major: z.number(), | ||
minor: z.number(), | ||
branch: z.string(), | ||
date: z.string(), | ||
|
||
type: z.literal("snapshot"), | ||
}), | ||
]), | ||
}) | ||
), | ||
}); | ||
|
||
export class Swiftly { | ||
/** | ||
* Finds the version of Swiftly installed on the system. | ||
* | ||
* @returns the version of Swiftly as a `Version` object, or `undefined` | ||
* if Swiftly is not installed or not supported. | ||
*/ | ||
public static async version(): Promise<Version | undefined> { | ||
if (!Swiftly.isSupported()) { | ||
return undefined; | ||
} | ||
const { stdout } = await execFile("swiftly", ["--version"]); | ||
return Version.fromString(stdout.trim()); | ||
} | ||
|
||
/** | ||
* Finds the list of toolchains managed by Swiftly. | ||
* | ||
* @returns an array of toolchain paths | ||
*/ | ||
public static async listAvailableToolchains(): Promise<string[]> { | ||
if (!this.isSupported()) { | ||
return []; | ||
} | ||
const version = await Swiftly.version(); | ||
if (version?.isLessThan(new Version(1, 1, 0))) { | ||
return await Swiftly.getToolchainInstallLegacy(); | ||
} | ||
|
||
return await Swiftly.getListAvailableToolchains(); | ||
} | ||
|
||
private static async getListAvailableToolchains(): Promise<string[]> { | ||
try { | ||
const { stdout } = await execFile("swiftly", ["list-available", "--format=json"]); | ||
const response = ListAvailableResult.parse(JSON.parse(stdout)); | ||
return response.toolchains.map(t => t.name); | ||
} catch (error) { | ||
throw new Error( | ||
`Failed to retrieve Swiftly installations from disk: ${(error as Error).message}` | ||
); | ||
} | ||
} | ||
|
||
private static async getToolchainInstallLegacy() { | ||
try { | ||
const swiftlyHomeDir: string | undefined = process.env["SWIFTLY_HOME_DIR"]; | ||
if (!swiftlyHomeDir) { | ||
return []; | ||
} | ||
const swiftlyConfig = await Swiftly.getConfig(); | ||
if (!swiftlyConfig || !("installedToolchains" in swiftlyConfig)) { | ||
return []; | ||
} | ||
const installedToolchains = swiftlyConfig.installedToolchains; | ||
if (!Array.isArray(installedToolchains)) { | ||
return []; | ||
} | ||
return installedToolchains | ||
.filter((toolchain): toolchain is string => typeof toolchain === "string") | ||
.map(toolchain => path.join(swiftlyHomeDir, "toolchains", toolchain)); | ||
} catch (error) { | ||
throw new Error( | ||
`Failed to retrieve Swiftly installations from disk: ${(error as Error).message}` | ||
); | ||
} | ||
} | ||
|
||
private static isSupported() { | ||
return process.platform === "linux" || process.platform === "darwin"; | ||
} | ||
|
||
public static async inUseLocation(swiftlyPath: string, cwd?: vscode.Uri) { | ||
const { stdout: inUse } = await execFile(swiftlyPath, ["use", "--print-location"], { | ||
cwd: cwd?.fsPath, | ||
}); | ||
return inUse.trimEnd(); | ||
} | ||
|
||
/** | ||
* Determine if Swiftly is being used to manage the active toolchain and if so, return | ||
* the path to the active toolchain. | ||
* @returns The location of the active toolchain if swiftly is being used to manage it. | ||
*/ | ||
public static async toolchain(cwd?: vscode.Uri): Promise<string | undefined> { | ||
const swiftlyHomeDir: string | undefined = process.env["SWIFTLY_HOME_DIR"]; | ||
if (swiftlyHomeDir) { | ||
const { stdout: swiftLocation } = await execFile("which", ["swift"]); | ||
if (swiftLocation.startsWith(swiftlyHomeDir)) { | ||
// Print the location of the toolchain that swiftly is using. If there | ||
// is no cwd specified then it returns the global "inUse" toolchain otherwise | ||
// it respects the .swift-version file in the cwd and resolves using that. | ||
try { | ||
const inUse = await Swiftly.inUseLocation("swiftly", cwd); | ||
if (inUse.length > 0) { | ||
return path.join(inUse, "usr"); | ||
} | ||
} catch (err: unknown) { | ||
const error = err as ExecFileError; | ||
// Its possible the toolchain in .swift-version is misconfigured or doesn't exist. | ||
void vscode.window.showErrorMessage( | ||
`Failed to load toolchain from Swiftly: ${error.stderr}` | ||
); | ||
} | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
/** | ||
* Reads the Swiftly configuration file, if it exists. | ||
* | ||
* @returns A parsed Swiftly configuration. | ||
*/ | ||
private static async getConfig(): Promise<SwiftlyConfig | undefined> { | ||
const swiftlyHomeDir: string | undefined = process.env["SWIFTLY_HOME_DIR"]; | ||
if (!swiftlyHomeDir) { | ||
return; | ||
} | ||
const swiftlyConfigRaw = await fs.readFile( | ||
path.join(swiftlyHomeDir, "config.json"), | ||
"utf-8" | ||
); | ||
return JSON.parse(swiftlyConfigRaw); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.