-
Notifications
You must be signed in to change notification settings - Fork 86
macOS: Auto-install CLI to ~/.local/bin #2937
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
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
67ac233
Refactor macOS CLI installation to use ~/.local/bin
ivan-ottinger 314afbd
Respect user preference when CLI is explicitly disabled
ivan-ottinger d03e18f
Fix test paths for cross-platform CI compatibility
ivan-ottinger 8b7186a
Skip macOS-specific tests on Windows CI
ivan-ottinger 20e9dae
Remove legacy symlink cleanup
ivan-ottinger 9f5f263
Skip CLI auto-install in development mode
ivan-ottinger e78e351
Merge remote-tracking branch 'origin/trunk' into stu-1363-auto-instal…
ivan-ottinger 270df7f
Remove stale legacy cleanup comment from test
ivan-ottinger bf34da1
Fix uninstall ENOENT and narrow test skip to Windows only
ivan-ottinger 514943d
Remove redundant isCliInstalled check from autoInstallIfNeeded
ivan-ottinger 5eb12ec
Check PATH in isCliInstalled and normalize PATH entries
ivan-ottinger 816d532
Use process.env.SHELL for shell profile detection
ivan-ottinger 1f5f619
Merge remote-tracking branch 'origin/trunk' into stu-1363-auto-instal…
ivan-ottinger 54f7759
Merge remote-tracking branch 'origin/trunk' into stu-1363-auto-instal…
ivan-ottinger 5a0bc63
Check profile file instead of runtime PATH for CLI status
ivan-ottinger ec95c5a
Merge remote-tracking branch 'origin/trunk' into stu-1363-auto-instal…
ivan-ottinger be0d28a
Remove stale PATH setup from test beforeEach
ivan-ottinger 3f4fdb3
Simplify test mocks for readFile
ivan-ottinger 74cb728
Merge remote-tracking branch 'origin/trunk' into stu-1363-auto-instal…
ivan-ottinger 7e5f09e
Only treat ENOENT as empty profile, rethrow other errors
ivan-ottinger 645ba1f
Only set cliAutoInstalled flag after verifying install succeeded
ivan-ottinger 1ee857d
Revert isCliInstalled check after install on macOS
ivan-ottinger bf746d8
Merge branch 'trunk' into stu-1363-auto-install-cli-macos
fredrikekelund 520af96
Tweaks
fredrikekelund 681f640
Clean up legacy CLI symlink when uninstalling, if needed
fredrikekelund 820334c
Simplification
fredrikekelund 5fc4568
Move functions to methods
fredrikekelund 9339dfa
`os.userInfo()` and shell basenames instead of paths
fredrikekelund 1270007
Minor tweak
fredrikekelund fb6f9dc
Remove "CLI Installed" modal
fredrikekelund 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file was deleted.
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { dialog } from 'electron'; | ||
| import { mkdir, readlink, symlink, unlink, lstat } from 'node:fs/promises'; | ||
| import fs from 'node:fs'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import * as Sentry from '@sentry/electron/main'; | ||
| import { isErrnoException } from '@studio/common/lib/is-errno-exception'; | ||
|
|
@@ -8,18 +9,26 @@ import { sudoExec } from 'src/lib/sudo-exec'; | |
| import { getMainWindow } from 'src/main-window'; | ||
| import { StudioCliInstallationManager } from 'src/modules/cli/lib/ipc-handlers'; | ||
| import { getResourcesPath } from 'src/storage/paths'; | ||
| import { loadUserData, updateAppdata } from 'src/storage/user-data'; | ||
| import packageJson from '../../../../package.json'; | ||
|
|
||
| const cliSymlinkPath = '/usr/local/bin/studio'; | ||
| const legacyCliSymlinkPath = '/usr/local/bin/studio'; | ||
| const cliSymlinkPath = path.join( os.homedir(), '.local', 'bin', 'studio' ); | ||
|
|
||
| const binPath = path.join( getResourcesPath(), 'bin' ); | ||
| const cliPackagedPath = path.join( binPath, 'studio-cli.sh' ); | ||
| const installScriptPath = path.join( binPath, 'install-studio-cli.sh' ); | ||
| const uninstallScriptPath = path.join( binPath, 'uninstall-studio-cli.sh' ); | ||
|
|
||
| const SUPPORTED_SHELLS = [ 'bash', 'zsh' ] as const; | ||
| const SHELL_PROFILE_MAP: Record< ( typeof SUPPORTED_SHELLS )[ number ], string > = { | ||
| bash: '.bash_profile', | ||
| zsh: '.zshrc', | ||
| }; | ||
| const DEFAULT_PROFILE = SHELL_PROFILE_MAP[ 'zsh' ]; | ||
| const PATH_DEFINITION = '$HOME/.local/bin'; | ||
| const PATH_EXPORT_LINE = `export PATH="${ PATH_DEFINITION }:$PATH"`; | ||
|
|
||
| const ERROR_FILE_ALREADY_EXISTS = 'Studio CLI symlink path already occupied by non-symlink'; | ||
| // Defined in @vscode/sudo-prompt | ||
| const ERROR_PERMISSION = 'User did not grant permission.'; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we don't run sudo, the error message is no longer needed. |
||
|
|
||
| export class MacOSCliInstallationManager implements StudioCliInstallationManager { | ||
| constructor() { | ||
|
|
@@ -29,36 +38,18 @@ export class MacOSCliInstallationManager implements StudioCliInstallationManager | |
| } | ||
|
|
||
| async isCliInstalled(): Promise< boolean > { | ||
| const currentSymlinkDestination = await this.getCurrentSymlinkDestination(); | ||
|
|
||
| // Return true if we are running the development version of the app and the production CLI is installed | ||
| if ( process.env.NODE_ENV !== 'production' ) { | ||
| const prodCliPackagedPath = path.join( | ||
| path.sep, | ||
| 'Applications', | ||
| 'Studio.app', | ||
| 'Contents', | ||
| 'Resources', | ||
| 'bin', | ||
| 'studio-cli.sh' | ||
| ); | ||
| if ( currentSymlinkDestination === prodCliPackagedPath ) { | ||
| return true; | ||
| } | ||
| const existingContent = await this.readShellProfileContent(); | ||
|
|
||
| if ( ! existingContent.includes( PATH_DEFINITION ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| return currentSymlinkDestination === cliPackagedPath; | ||
| return await this.doesSymlinkLeadToPackagedCli( cliSymlinkPath ); | ||
| } | ||
|
|
||
| async installCliWithConfirmation(): Promise< void > { | ||
| try { | ||
| await this.installCli(); | ||
| const mainWindow = await getMainWindow(); | ||
| await dialog.showMessageBox( mainWindow, { | ||
| type: 'info', | ||
| title: __( 'CLI Installed' ), | ||
| message: __( 'The CLI has been installed successfully.' ), | ||
| } ); | ||
| } catch ( error ) { | ||
| console.error( 'Failed to install CLI', error ); | ||
|
|
||
|
|
@@ -76,8 +67,6 @@ export class MacOSCliInstallationManager implements StudioCliInstallationManager | |
| ), | ||
| cliSymlinkPath | ||
| ); | ||
| } else if ( error.message === ERROR_PERMISSION ) { | ||
| message = __( 'Please ensure you grant Studio admin permissions when prompted.' ); | ||
| } else { | ||
| // Only report unexpected errors to Sentry | ||
| Sentry.captureException( error ); | ||
|
|
@@ -98,6 +87,7 @@ export class MacOSCliInstallationManager implements StudioCliInstallationManager | |
| async uninstallCliWithConfirmation(): Promise< void > { | ||
| try { | ||
| await this.uninstallCli(); | ||
| await this.uninstallLegacyCliIfNeeded(); | ||
| const mainWindow = await getMainWindow(); | ||
| await dialog.showMessageBox( mainWindow, { | ||
| type: 'info', | ||
|
|
@@ -125,9 +115,21 @@ export class MacOSCliInstallationManager implements StudioCliInstallationManager | |
| } | ||
| } | ||
|
|
||
| async autoInstallIfNeeded(): Promise< void > { | ||
| // Only auto-install on first launch. If the flag is already set but the CLI isn't | ||
| // installed, the user must have explicitly disabled it — respect their choice. | ||
| const userData = await loadUserData(); | ||
| if ( userData.cliAutoInstalled ) { | ||
| return; | ||
| } | ||
|
|
||
| await this.installCli(); | ||
| await updateAppdata( { cliAutoInstalled: true } ); | ||
| } | ||
|
ivan-ottinger marked this conversation as resolved.
|
||
|
|
||
| private async installCli(): Promise< void > { | ||
| try { | ||
| const stats = await lstat( cliSymlinkPath ); | ||
| const stats = await fs.promises.lstat( cliSymlinkPath ); | ||
|
|
||
| if ( ! stats.isSymbolicLink() ) { | ||
| throw new Error( ERROR_FILE_ALREADY_EXISTS ); | ||
|
|
@@ -144,59 +146,131 @@ export class MacOSCliInstallationManager implements StudioCliInstallationManager | |
| return; | ||
| } | ||
|
|
||
| try { | ||
| const directoryPath = path.dirname( cliSymlinkPath ); | ||
| const directoryPath = path.dirname( cliSymlinkPath ); | ||
|
|
||
| await unlink( cliSymlinkPath ); | ||
| await mkdir( directoryPath, { recursive: true } ); | ||
| await symlink( cliPackagedPath, cliSymlinkPath ); | ||
| } catch ( e ) { | ||
| // `/usr/local/bin` is not typically writable by non-root users, so in most cases, we run | ||
| // this install script with admin privileges to create the symlink. | ||
| await sudoExec( `/bin/sh "${ installScriptPath }"`, { | ||
| name: packageJson.productName, | ||
| env: { | ||
| CLI_SYMLINK_PATH: cliSymlinkPath, | ||
| CLI_PACKAGED_PATH: cliPackagedPath, | ||
| }, | ||
| } ); | ||
| try { | ||
| await fs.promises.unlink( cliSymlinkPath ); | ||
| } catch ( error ) { | ||
| if ( ! isErrnoException( error ) || error.code !== 'ENOENT' ) { | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| await fs.promises.mkdir( directoryPath, { recursive: true } ); | ||
| await fs.promises.symlink( cliPackagedPath, cliSymlinkPath ); | ||
| await this.ensurePathInProfile(); | ||
| } | ||
|
|
||
| private async uninstallCli(): Promise< void > { | ||
| try { | ||
| const stats = await lstat( cliSymlinkPath ); | ||
| const stats = await fs.promises.lstat( cliSymlinkPath ); | ||
|
|
||
| if ( ! stats.isSymbolicLink() ) { | ||
| throw new Error( ERROR_FILE_ALREADY_EXISTS ); | ||
| } | ||
| } catch ( error ) { | ||
| if ( isErrnoException( error ) && error.code === 'ENOENT' ) { | ||
| // File does not exist, which means we can proceed | ||
| } else { | ||
| throw error; | ||
| // File does not exist, nothing to uninstall. | ||
| return; | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| await fs.promises.unlink( cliSymlinkPath ); | ||
| } | ||
|
ivan-ottinger marked this conversation as resolved.
|
||
|
|
||
| private async uninstallLegacyCliIfNeeded(): Promise< void > { | ||
| const legacyCliExists = await this.doesSymlinkLeadToPackagedCli( legacyCliSymlinkPath ); | ||
| if ( ! legacyCliExists ) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| await unlink( cliSymlinkPath ); | ||
| await fs.promises.unlink( legacyCliSymlinkPath ); | ||
| } catch ( error ) { | ||
| // `/usr/local/bin` is not typically writable by non-root users, so in most cases, we run | ||
| // this uninstall script with admin privileges to remove the symlink. | ||
| await sudoExec( `/bin/sh "${ uninstallScriptPath }"`, { | ||
| name: packageJson.productName, | ||
| env: { | ||
| CLI_SYMLINK_PATH: cliSymlinkPath, | ||
| CLI_SYMLINK_PATH: legacyCliSymlinkPath, | ||
| }, | ||
| } ); | ||
| } | ||
| } | ||
|
|
||
| private async getCurrentSymlinkDestination(): Promise< string | null > { | ||
| private async ensurePathInProfile(): Promise< void > { | ||
| const existingContent = await this.readShellProfileContent(); | ||
|
|
||
| if ( existingContent.includes( PATH_DEFINITION ) ) { | ||
| return; | ||
| } | ||
|
|
||
| const profilePath = this.getShellProfilePath(); | ||
|
|
||
| const lineToAppend = | ||
| existingContent.endsWith( '\n' ) || existingContent === '' | ||
| ? `${ PATH_EXPORT_LINE }\n` | ||
| : `\n${ PATH_EXPORT_LINE }\n`; | ||
|
|
||
| await fs.promises.writeFile( profilePath, existingContent + lineToAppend, 'utf-8' ); | ||
| } | ||
|
|
||
| private getShellProfilePath(): string { | ||
| const shell = path.basename( os.userInfo().shell ?? process.env.SHELL ?? '' ); | ||
| const supportedShell = SUPPORTED_SHELLS.find( ( candidate ) => candidate === shell ); | ||
| const profileFile = supportedShell ? SHELL_PROFILE_MAP[ supportedShell ] : DEFAULT_PROFILE; | ||
| return path.join( os.homedir(), profileFile ); | ||
| } | ||
|
|
||
| private async readShellProfileContent(): Promise< string > { | ||
| try { | ||
| return await readlink( cliSymlinkPath ); | ||
| return await fs.promises.readFile( this.getShellProfilePath(), 'utf-8' ); | ||
| } catch ( error ) { | ||
| if ( isErrnoException( error ) && error.code === 'ENOENT' ) { | ||
| return ''; | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| private async doesSymlinkLeadToPackagedCli( symlinkPath: string ): Promise< boolean > { | ||
| try { | ||
| const symlinkDestination = await fs.promises.readlink( symlinkPath ); | ||
|
|
||
| if ( process.env.NODE_ENV !== 'production' ) { | ||
| const prodCliPackagedPath = path.join( | ||
| path.sep, | ||
| 'Applications', | ||
| 'Studio.app', | ||
| 'Contents', | ||
| 'Resources', | ||
| 'bin', | ||
| 'studio-cli.sh' | ||
| ); | ||
|
|
||
| if ( symlinkDestination === prodCliPackagedPath ) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return symlinkDestination === cliPackagedPath; | ||
| } catch { | ||
| return null; | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export async function autoInstallMacOSCliIfNeeded(): Promise< void > { | ||
| if ( process.platform !== 'darwin' || process.env.NODE_ENV !== 'production' ) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const manager = new MacOSCliInstallationManager(); | ||
| await manager.autoInstallIfNeeded(); | ||
| } catch ( error ) { | ||
| console.error( 'Failed to auto-install macOS CLI', error ); | ||
| Sentry.captureException( error ); | ||
| } | ||
| } | ||
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.