From 84ca9b76c76405ec03be1f2c5f444bf9b4d9f57d Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 8 Feb 2016 21:59:49 -0800 Subject: [PATCH] Move arg parsing to launcher.js --- resources/common/bin/code.sh | 56 ++++++++------------- resources/common/bin/launcher.js | 47 +++++++++++++++++ src/vs/workbench/electron-main/bootstrap.js | 27 ---------- 3 files changed, 68 insertions(+), 62 deletions(-) create mode 100644 resources/common/bin/launcher.js diff --git a/resources/common/bin/code.sh b/resources/common/bin/code.sh index 1908d9cb752..72ab09ff3fc 100755 --- a/resources/common/bin/code.sh +++ b/resources/common/bin/code.sh @@ -1,46 +1,32 @@ #!/usr/bin/env bash -VSCODE_CWD=$(pwd) - -while getopts ":hv-:" opt; do - case $opt in - -) - case $OPTARG in - help|version) - ENABLE_OUTPUT=1 - ;; - esac - ;; - h|v) - ENABLE_OUTPUT=1 - ;; - esac -done +# +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. if [[ "$OSTYPE" == "darwin"* ]]; then - if [ $ENABLE_OUTPUT ]; then - if [ -x "/Applications/Visual Studio Code.app" ]; then - VSCODE_PATH="/Applications/Visual Studio Code.app" - elif [ -x "$HOME/Applications/Visual Studio Code.app" ]; then - VSCODE_PATH="$HOME/Applications/Visual Studio Code.app" - else - echo "Could not locate Visual Studio Code.app" - exit 1 - fi - "$VSCODE_PATH/Contents/MacOS/Electron" "$@" + if [ -x "/Applications/Visual Studio Code.app" ]; then + VSCODE_DIR="/Applications/Visual Studio Code.app/Contents/MacOS" + elif [ -x "$HOME/Applications/Visual Studio Code.app" ]; then + VSCODE_DIR="$HOME/Applications/Visual Studio Code.app/Contents/MacOS" else - open -n -b "com.microsoft.VSCode" --args $* + echo "Could not locate Visual Studio Code.app" + exit 1 fi + ELECTRON_FILE="Electron" else - VSCODE_PATH="/usr/share/code/Code" - if [ -x $VSCODE_PATH ]; then - if [ $ENABLE_OUTPUT ]; then - "$VSCODE_PATH" "$@" - exit $? - else - nohup $VSCODE_PATH "$@" > ~/.vscode/nohup.out 2>&1 & - fi + VSCODE_DIR="/usr/share/code" + if [ -x "$VSCODE_DIR/Code" ]; then + ELECTRON_FILE="Code" + elif [ -x "$VSCODE_DIR/Code - OSS" ]; then + ELECTRON_FILE="Code - OSS" else echo "Could not locate Visual Studio Code executable." exit 1 fi fi + +VSCODE_LAUNCHER="$VSCODE_DIR/launcher.js" + +ATOM_SHELL_INTERNAL_RUN_AS_NODE=1 VSCODE_PATH="$VSCODE_DIR/$ELECTRON_FILE" \ + "$VSCODE_DIR/$ELECTRON_FILE" $VSCODE_LAUNCHER "$@" +exit $? diff --git a/resources/common/bin/launcher.js b/resources/common/bin/launcher.js new file mode 100644 index 00000000000..d69f1b39f90 --- /dev/null +++ b/resources/common/bin/launcher.js @@ -0,0 +1,47 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +/* global process */ + +var packageJson = require('../../../package.json'); +var os = require('os'); +var spawn = require('child_process').spawn; +var yargs = require('yargs'); + +function parseArgs() { + var executable = 'code' + (os.platform() == 'win32' ? '.exe' : ''); + var options = yargs(process.argv.slice(1)); + options.usage( + 'Visual Studio Code v' + packageJson.version + '\n' + + '\n' + + 'Usage: ' + executable + ' [arguments] [path]'); + options.alias('h', 'help').boolean('h').describe('h', 'Print usage.'); + options.string('locale').describe('locale', 'Use a specific locale.'); + options.boolean('n').describe('n', 'Force a new instance of code.'); + options.alias('v', 'version').boolean('v').describe('v', 'Print version.'); + + var args = options.argv; + if (args.help) { + process.stdout.write(options.help()); + process.exit(0); + } + if (args.version) { + process.stdout.write(packageJson.version + '\n'); + process.exit(0); + } +} + +function launchCode() { + delete process.env['ATOM_SHELL_INTERNAL_RUN_AS_NODE']; + spawn(process.env['VSCODE_PATH'], process.argv.slice(2), { detached: true, stdio: 'ignore' }); +} + +function main() { + parseArgs(); + launchCode(); + process.exit(0); +} + +main(); diff --git a/src/vs/workbench/electron-main/bootstrap.js b/src/vs/workbench/electron-main/bootstrap.js index 23272ae01b8..2cffa36b261 100644 --- a/src/vs/workbench/electron-main/bootstrap.js +++ b/src/vs/workbench/electron-main/bootstrap.js @@ -10,8 +10,6 @@ global.vscodeStart = Date.now(); var app = require('electron').app; var path = require('path'); -var process = require('process'); -var os = require('os'); var fs = require('fs'); // Change cwd if given via env variable @@ -21,31 +19,6 @@ try { // noop } -function parseArgs() { - var executable = 'code' + (os.platform() == 'win32' ? '.exe' : ''); - var options = require('yargs')(process.argv.slice(1)); - options.usage( - 'Visual Studio Code v' + app.getVersion() + '\n' + - '\n' + - 'Usage: ' + executable + ' [arguments] [path]'); - options.alias('h', 'help').boolean('h').describe('h', 'Print usage.'); - options.string('locale').describe('locale', 'Use a specific locale.'); - options.boolean('n').describe('n', 'Force a new instance of code.'); - options.alias('v', 'version').boolean('v').describe('v', 'Print version.'); - - var args = options.argv; - if (args.help) { - process.stdout.write(options.help()); - process.exit(0); - } - if (args.version) { - process.stdout.write(app.getVersion() + '\n'); - process.exit(0); - } -} - -parseArgs(); - // Set path according to being built or not if (!!process.env.VSCODE_DEV) { var appData = app.getPath('appData');