|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VS Code Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021-2024 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 | +import * as fs from "fs/promises"; |
| 16 | +import * as vscode from "vscode"; |
| 17 | +import { SwiftOutputChannel } from "../ui/SwiftOutputChannel"; |
| 18 | +import { showReloadExtensionNotification } from "../ui/ReloadExtension"; |
| 19 | +import configuration from "../configuration"; |
| 20 | + |
| 21 | +export class SelectedXcodeWatcher implements vscode.Disposable { |
| 22 | + private xcodePath: string | undefined; |
| 23 | + private disposed: boolean = false; |
| 24 | + private interval: NodeJS.Timeout | undefined; |
| 25 | + private checkIntervalMs: number; |
| 26 | + private xcodeSymlink: () => Promise<string | undefined>; |
| 27 | + |
| 28 | + private static DEFAULT_CHECK_INTERVAL_MS = 2000; |
| 29 | + private static XCODE_SYMLINK_LOCATION = "/var/select/developer_dir"; |
| 30 | + |
| 31 | + constructor( |
| 32 | + private outputChannel: SwiftOutputChannel, |
| 33 | + testDependencies?: { |
| 34 | + checkIntervalMs?: number; |
| 35 | + xcodeSymlink?: () => Promise<string | undefined>; |
| 36 | + } |
| 37 | + ) { |
| 38 | + this.checkIntervalMs = |
| 39 | + testDependencies?.checkIntervalMs || SelectedXcodeWatcher.DEFAULT_CHECK_INTERVAL_MS; |
| 40 | + this.xcodeSymlink = |
| 41 | + testDependencies?.xcodeSymlink || |
| 42 | + (async () => { |
| 43 | + try { |
| 44 | + return await fs.readlink(SelectedXcodeWatcher.XCODE_SYMLINK_LOCATION); |
| 45 | + } catch (e) { |
| 46 | + return undefined; |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + if (!this.isValidXcodePlatform()) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // Deliberately not awaiting this, as we don't want to block the extension activation. |
| 55 | + this.setup(); |
| 56 | + } |
| 57 | + |
| 58 | + dispose() { |
| 59 | + this.disposed = true; |
| 60 | + clearInterval(this.interval); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Polls the Xcode symlink location checking if it has changed. |
| 65 | + * If the user has `swift.path` set in their settings this check is skipped. |
| 66 | + */ |
| 67 | + private async setup() { |
| 68 | + this.xcodePath = await this.xcodeSymlink(); |
| 69 | + this.interval = setInterval(async () => { |
| 70 | + if (this.disposed) { |
| 71 | + return clearInterval(this.interval); |
| 72 | + } |
| 73 | + |
| 74 | + const newXcodePath = await this.xcodeSymlink(); |
| 75 | + if (!configuration.path && newXcodePath && this.xcodePath !== newXcodePath) { |
| 76 | + this.outputChannel.appendLine( |
| 77 | + `Selected Xcode changed from ${this.xcodePath} to ${newXcodePath}` |
| 78 | + ); |
| 79 | + showReloadExtensionNotification( |
| 80 | + "The Swift Extension has detected a change in the selected Xcode. Please reload the extension to apply the changes." |
| 81 | + ); |
| 82 | + this.xcodePath = newXcodePath; |
| 83 | + } |
| 84 | + }, this.checkIntervalMs); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Xcode selection is a macOS only concept. |
| 89 | + */ |
| 90 | + private isValidXcodePlatform() { |
| 91 | + return process.platform === "darwin"; |
| 92 | + } |
| 93 | +} |
0 commit comments