Skip to content
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

sandbox - make CLI commands a native host thing #123899

Merged
merged 5 commits into from May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/vs/platform/native/common/native.ts
Expand Up @@ -129,6 +129,10 @@ export interface ICommonNativeHostService {
toggleWindowTabsBar(): Promise<void>;
updateTouchBar(items: ISerializableCommandAction[][]): Promise<void>;

// macOS Shell command
installShellCommand(): Promise<void>;
uninstallShellCommand(): Promise<void>;

// Lifecycle
notifyReady(): Promise<void>
relaunch(options?: { addArgs?: string[], removeArgs?: string[] }): Promise<void>;
Expand Down
102 changes: 100 additions & 2 deletions src/vs/platform/native/electron-main/nativeHostMainService.ts
Expand Up @@ -3,6 +3,11 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as fs from 'fs';
import { exec } from 'child_process';
import { promisify } from 'util';
import { localize } from 'vs/nls';
import { realpath } from 'vs/base/node/extpath';
import { Emitter, Event } from 'vs/base/common/event';
import { IWindowsMainService, ICodeWindow, OpenContext } from 'vs/platform/windows/electron-main/windows';
import { MessageBoxOptions, MessageBoxReturnValue, shell, OpenDevToolsOptions, SaveDialogOptions, SaveDialogReturnValue, OpenDialogOptions, OpenDialogReturnValue, Menu, BrowserWindow, app, clipboard, powerMonitor, nativeTheme, screen, Display } from 'electron';
Expand All @@ -15,15 +20,15 @@ import { ISerializableCommandAction } from 'vs/platform/actions/common/actions';
import { IEnvironmentMainService } from 'vs/platform/environment/electron-main/environmentMainService';
import { AddFirstParameterToFunctions } from 'vs/base/common/types';
import { IDialogMainService } from 'vs/platform/dialogs/electron-main/dialogMainService';
import { SymlinkSupport } from 'vs/base/node/pfs';
import { exists, SymlinkSupport } from 'vs/base/node/pfs';
import { URI } from 'vs/base/common/uri';
import { ITelemetryData, ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { MouseInputEvent } from 'vs/base/parts/sandbox/common/electronTypes';
import { arch, totalmem, release, platform, type, loadavg, freemem, cpus } from 'os';
import { virtualMachineHint } from 'vs/base/node/id';
import { ILogService } from 'vs/platform/log/common/log';
import { dirname, join } from 'vs/base/common/path';
import { dirname, join, resolve } from 'vs/base/common/path';
import { IProductService } from 'vs/platform/product/common/productService';
import { memoize } from 'vs/base/common/decorators';
import { Disposable } from 'vs/base/common/lifecycle';
Expand Down Expand Up @@ -256,6 +261,99 @@ export class NativeHostMainService extends Disposable implements INativeHostMain
//#endregion


//#region macOS Shell Command

async installShellCommand(windowId: number | undefined): Promise<void> {
const { source, target } = await this.getShellCommandLink();

// Only install unless already existing
try {
const { symbolicLink } = await SymlinkSupport.stat(source);
if (symbolicLink && !symbolicLink.dangling) {
const linkTargetRealPath = await realpath(source);
if (target === linkTargetRealPath) {
return;
}
}

// Different source, delete it first
await fs.promises.unlink(source);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error; // throw on any error but file not found
}
}

try {
await fs.promises.symlink(target, source);
} catch (error) {
if (error.code !== 'EACCES' && error.code !== 'ENOENT') {
throw error;
}

const { response } = await this.showMessageBox(windowId, {
type: 'info',
message: localize('warnEscalation', "{0} will now prompt with 'osascript' for Administrator privileges to install the shell command.", this.productService.nameShort),
buttons: [localize('ok', "OK"), localize('cancel', "Cancel")],
cancelId: 1
});

if (response === 0 /* OK */) {
try {
const command = `osascript -e "do shell script \\"mkdir -p /usr/local/bin && ln -sf \'${target}\' \'${source}\'\\" with administrator privileges"`;
await promisify(exec)(command);
} catch (error) {
throw new Error(localize('cantCreateBinFolder', "Unable to install the shell command '{0}'.", source));
}
}
}
}

async uninstallShellCommand(windowId: number | undefined): Promise<void> {
const { source } = await this.getShellCommandLink();

try {
await fs.promises.unlink(source);
} catch (error) {
switch (error.code) {
case 'EACCES':
const { response } = await this.showMessageBox(windowId, {
type: 'info',
message: localize('warnEscalationUninstall', "{0} will now prompt with 'osascript' for Administrator privileges to uninstall the shell command.", this.productService.nameShort),
buttons: [localize('ok', "OK"), localize('cancel', "Cancel")],
cancelId: 1
});

if (response === 0 /* OK */) {
try {
const command = `osascript -e "do shell script \\"rm \'${source}\'\\" with administrator privileges"`;
await promisify(exec)(command);
} catch (error) {
throw new Error(localize('cantUninstall', "Unable to uninstall the shell command '{0}'.", source));
}
}
break;
case 'ENOENT':
break; // ignore file not found
default:
throw error;
}
}
}

private async getShellCommandLink(): Promise<{ readonly source: string, readonly target: string }> {
const target = resolve(this.environmentMainService.appRoot, 'bin', 'code');
const source = `/usr/local/bin/${this.productService.applicationName}`;

// Ensure source exists
const sourceExists = await exists(target);
if (!sourceExists) {
throw new Error(localize('sourceMissing', "Unable to find shell script in '{0}'", target));
}

return { source, target };
}

//#region Dialog

async showMessageBox(windowId: number | undefined, options: MessageBoxOptions): Promise<MessageBoxReturnValue> {
Expand Down
193 changes: 0 additions & 193 deletions src/vs/workbench/contrib/cli/node/cli.contribution.ts

This file was deleted.