From 2ebd2a7dd28a1afa89ad08bd0c429b1966ea048a Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Mon, 29 Jan 2018 22:04:27 +0100 Subject: [PATCH] builtin: manage extensions --- build/builtin/.eslintrc | 20 ++++++ build/builtin/browser-main.js | 125 +++++++++++++++++++++++++++++++++ build/builtin/index.html | 46 ++++++++++++ build/builtin/main.js | 20 ++++++ build/builtin/package.json | 5 ++ build/lib/builtInExtensions.js | 5 +- scripts/code.bat | 12 +++- scripts/code.sh | 6 ++ 8 files changed, 235 insertions(+), 4 deletions(-) create mode 100644 build/builtin/.eslintrc create mode 100644 build/builtin/browser-main.js create mode 100644 build/builtin/index.html create mode 100644 build/builtin/main.js create mode 100644 build/builtin/package.json diff --git a/build/builtin/.eslintrc b/build/builtin/.eslintrc new file mode 100644 index 00000000000..84e384941f3 --- /dev/null +++ b/build/builtin/.eslintrc @@ -0,0 +1,20 @@ +{ + "env": { + "node": true, + "es6": true, + "browser": true + }, + "rules": { + "no-console": 0, + "no-cond-assign": 0, + "no-unused-vars": 1, + "no-extra-semi": "warn", + "semi": "warn" + }, + "extends": "eslint:recommended", + "parserOptions": { + "ecmaFeatures": { + "experimentalObjectRestSpread": true + } + } +} \ No newline at end of file diff --git a/build/builtin/browser-main.js b/build/builtin/browser-main.js new file mode 100644 index 00000000000..160bac61968 --- /dev/null +++ b/build/builtin/browser-main.js @@ -0,0 +1,125 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +const fs = require('fs'); +const path = require('path'); +const os = require('os'); +const { remote } = require('electron'); +const dialog = remote.dialog; + +const builtInExtensionsPath = path.join(__dirname, '..', 'builtInExtensions.json'); +const controlFilePath = path.join(os.homedir(), '.vscode-oss-dev', 'extensions', 'control.json'); + +function readJson(filePath) { + return JSON.parse(fs.readFileSync(filePath)); +} + +function writeJson(filePath, obj) { + fs.writeFileSync(filePath, JSON.stringify(obj, null, 2)); +} + +function renderOption(form, id, title, value, checked) { + const input = document.createElement('input'); + input.type = 'radio'; + input.id = id; + input.name = 'choice'; + input.value = value; + input.checked = !!checked; + form.appendChild(input); + + const label = document.createElement('label'); + label.setAttribute('for', id); + label.textContent = title; + form.appendChild(label); + + return input; +} + +function render(el, state) { + function setState(state) { + try { + writeJson(controlFilePath, state.control); + } catch (err) { + console.error(err); + } + + el.innerHTML = ''; + render(el, state); + } + + const ul = document.createElement('ul'); + const { builtin, control } = state; + + for (const ext of builtin) { + const controlState = control[ext.name] || 'marketplace'; + + const li = document.createElement('li'); + ul.appendChild(li); + + const name = document.createElement('code'); + name.textContent = ext.name; + li.appendChild(name); + + const form = document.createElement('form'); + li.appendChild(form); + + const marketplaceInput = renderOption(form, `marketplace-${ext.name}`, 'Marketplace', 'marketplace', controlState === 'marketplace'); + marketplaceInput.onchange = function () { + control[ext.name] = 'marketplace'; + setState({ builtin, control }); + }; + + const disabledInput = renderOption(form, `disabled-${ext.name}`, 'Disabled', 'disabled', controlState === 'disabled'); + disabledInput.onchange = function () { + control[ext.name] = 'disabled'; + setState({ builtin, control }); + }; + + let local = undefined; + + if (controlState !== 'marketplace' && controlState !== 'disabled') { + local = controlState; + } + + const localInput = renderOption(form, `local-${ext.name}`, 'Local', 'local', !!local); + localInput.onchange = function () { + const result = dialog.showOpenDialog(remote.getCurrentWindow(), { + title: 'Choose Folder', + properties: ['openDirectory'] + }); + + if (result && result.length >= 1) { + control[ext.name] = result[0]; + } + + setState({ builtin, control }); + }; + + if (local) { + const localSpan = document.createElement('code'); + localSpan.className = 'local'; + localSpan.textContent = local; + form.appendChild(localSpan); + } + } + + el.appendChild(ul); +} + +function main() { + const el = document.getElementById('extensions'); + const builtin = readJson(builtInExtensionsPath); + let control; + + try { + control = readJson(controlFilePath); + } catch (err) { + control = {}; + } + + render(el, { builtin, control }); +} + +window.onload = main; \ No newline at end of file diff --git a/build/builtin/index.html b/build/builtin/index.html new file mode 100644 index 00000000000..13c84e0375c --- /dev/null +++ b/build/builtin/index.html @@ -0,0 +1,46 @@ + + + + + + + + + Manage Built-in Extensions + + + + + + +

Built-in Extensions

+
+ + + \ No newline at end of file diff --git a/build/builtin/main.js b/build/builtin/main.js new file mode 100644 index 00000000000..849027ad2b9 --- /dev/null +++ b/build/builtin/main.js @@ -0,0 +1,20 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +const { app, BrowserWindow } = require('electron'); +const url = require('url'); +const path = require('path'); + +let window = null; + +app.once('ready', () => { + window = new BrowserWindow({ width: 800, height: 600 }); + window.setMenuBarVisibility(false); + window.loadURL(url.format({ pathname: path.join(__dirname, 'index.html'), protocol: 'file:', slashes: true })); + // window.webContents.openDevTools(); + window.once('closed', () => window = null); +}); + +app.on('window-all-closed', () => app.quit()); \ No newline at end of file diff --git a/build/builtin/package.json b/build/builtin/package.json new file mode 100644 index 00000000000..6843791a5d8 --- /dev/null +++ b/build/builtin/package.json @@ -0,0 +1,5 @@ +{ + "name": "builtin", + "version": "0.1.0", + "main": "main.js" +} \ No newline at end of file diff --git a/build/lib/builtInExtensions.js b/build/lib/builtInExtensions.js index 13bbe368c4a..7cbf31c12aa 100644 --- a/build/lib/builtInExtensions.js +++ b/build/lib/builtInExtensions.js @@ -75,7 +75,7 @@ function syncExtension(extension, controlState) { return es.readArray([]); } - util.log(util.colors.blue('[local]'), `${extension.name}: ${controlState}`, util.colors.green('✔︎')); + util.log(util.colors.blue('[local]'), `${extension.name}: ${util.colors.cyan(controlState)}`, util.colors.green('✔︎')); return es.readArray([]); } } @@ -95,7 +95,7 @@ function writeControlFile(control) { function main() { util.log('Syncronizing built-in extensions...'); - util.log('Control file:', controlFilePath); + util.log(`You can manage built-in extensions with the ${util.colors.cyan('--builtin')} flag`); const control = readControlFile(); const streams = []; @@ -115,7 +115,6 @@ function main() { process.exit(1); }) .on('end', () => { - util.log(`${streams.length} built-in extensions processed.`); process.exit(0); }); } diff --git a/scripts/code.bat b/scripts/code.bat index 018577357d7..05d482c6176 100644 --- a/scripts/code.bat +++ b/scripts/code.bat @@ -17,6 +17,9 @@ set CODE=".build\electron\%NAMESHORT%" node build\lib\electron.js if %errorlevel% neq 0 node .\node_modules\gulp\bin\gulp.js electron +:: Manage build-in extensions +if "%1"=="--builtin" goto builtin + :: Sync built-in extensions node build\lib\builtInExtensions.js @@ -37,6 +40,13 @@ set ELECTRON_ENABLE_STACK_DUMPING=1 :: %CODE% --js-flags="--trace-hydrogen --trace-phase=Z --trace-deopt --code-comments --hydrogen-track-positions --redirect-code-traces" . %* %CODE% . %* +goto end + +:builtin +%CODE% build/builtin + +:end + popd -endlocal +endlocal \ No newline at end of file diff --git a/scripts/code.sh b/scripts/code.sh index 0d9402a6eec..f6d103ceda5 100755 --- a/scripts/code.sh +++ b/scripts/code.sh @@ -24,6 +24,12 @@ function code() { # Get electron node build/lib/electron.js || ./node_modules/.bin/gulp electron + # Manage built-in extensions + if [[ "$1" == "--builtin" ]]; then + exec "$CODE" build/builtin + return + fi + # Sync built-in extensions node build/lib/builtInExtensions.js