use rmsync

This commit is contained in:
arily 2023-03-10 15:38:31 +09:00
parent 044fba244f
commit d5b7572ca6

View File

@ -15,33 +15,33 @@ const localize = require('./i18n');
const os = require('./platform');
var themeStylePaths = {
'Default Dark': '../themes/Default Dark.css',
'Dark (Exclude Tab Line)': '../themes/Dark (Exclude Tab Line).css',
'Dark (Only Subbar)': '../themes/Dark (Only Subbar).css',
'Default Light': '../themes/Default Light.css',
'Light (Only Subbar)': '../themes/Light (Only Subbar).css',
'Tokyo Night Storm': '../themes/Tokyo Night Storm.css',
'Tokyo Night Storm (Outer)': '../themes/Tokyo Night Storm (Outer).css',
'Noir et blanc': '../themes/Noir et blanc.css',
'Solarized Dark+': '../themes/Solarized Dark+.css',
'Default Dark': '../themes/Default Dark.css',
'Dark (Exclude Tab Line)': '../themes/Dark (Exclude Tab Line).css',
'Dark (Only Subbar)': '../themes/Dark (Only Subbar).css',
'Default Light': '../themes/Default Light.css',
'Light (Only Subbar)': '../themes/Light (Only Subbar).css',
'Tokyo Night Storm': '../themes/Tokyo Night Storm.css',
'Tokyo Night Storm (Outer)': '../themes/Tokyo Night Storm (Outer).css',
'Noir et blanc': '../themes/Noir et blanc.css',
'Solarized Dark+': '../themes/Solarized Dark+.css',
}
const themeConfigPaths = {
'Default Dark': '../themes/Default Dark.json',
'Dark (Exclude Tab Line)': '../themes/Dark (Exclude Tab Line).json',
'Dark (Only Subbar)': '../themes/Dark (Only Subbar).json',
'Default Light': '../themes/Default Light.json',
'Light (Only Subbar)': '../themes/Light (Only Subbar).json',
'Tokyo Night Storm': '../themes/Tokyo Night Storm.json',
'Tokyo Night Storm (Outer)': '../themes/Tokyo Night Storm (Outer).json',
'Noir et blanc': '../themes/Noir et blanc.json',
'Solarized Dark+': '../themes/Solarized Dark+.json',
'Default Dark': '../themes/Default Dark.json',
'Dark (Exclude Tab Line)': '../themes/Dark (Exclude Tab Line).json',
'Dark (Only Subbar)': '../themes/Dark (Only Subbar).json',
'Default Light': '../themes/Default Light.json',
'Light (Only Subbar)': '../themes/Light (Only Subbar).json',
'Tokyo Night Storm': '../themes/Tokyo Night Storm.json',
'Tokyo Night Storm (Outer)': '../themes/Tokyo Night Storm (Outer).json',
'Noir et blanc': '../themes/Noir et blanc.json',
'Solarized Dark+': '../themes/Solarized Dark+.json',
}
var defaultTheme = 'Default Dark';
function getCurrentTheme(config) {
return config.theme in themeStylePaths ? config.theme : defaultTheme;
return config.theme in themeStylePaths ? config.theme : defaultTheme;
}
async function changeTerminalRendererType() {
@ -126,248 +126,248 @@ function deepEqual(obj1, obj2) {
//check if value is primitive
function isPrimitive(obj) {
return (obj !== Object(obj));
return (obj !== Object(obj));
}
function isFirstload() {
try {
fs.readFileSync(lockPath);
return false
} catch (err) {
return true
}
try {
fs.readFileSync(lockPath);
return false
} catch (err) {
return true
}
}
function lockFirstload() {
fs.writeFileSync(lockPath, '', () => { });
fs.writeFileSync(lockPath, '', () => { });
}
function activate(context) {
console.log('vscode-vibrancy is active!');
console.log('vscode-vibrancy is active!');
var appDir = path.dirname(require.main.filename);
var appDir = path.dirname(require.main.filename);
var HTMLFile = appDir + '/vs/code/electron-sandbox/workbench/workbench.html';
var JSFile = appDir + '/main.js';
var HTMLFile = appDir + '/vs/code/electron-sandbox/workbench/workbench.html';
var JSFile = appDir + '/main.js';
var runtimeVersion = 'v6';
var runtimeDir = appDir + '/vscode-vibrancy-runtime-' + runtimeVersion;
var runtimeVersion = 'v6';
var runtimeDir = appDir + '/vscode-vibrancy-runtime-' + runtimeVersion;
async function installRuntime() {
// if runtimeDir exists, recurse through it and delete all files
// (recursive fs.rm does not work properly on Windows)
if (fs.existsSync(runtimeDir)) {
fs.readdirSync(runtimeDir).forEach((file, index) => {
const curPath = path.join(runtimeDir, file);
fs.unlinkSync(curPath);
});
fs.rmdirSync(runtimeDir);
}
async function installRuntime() {
// if runtimeDir exists, recurse through it and delete all files
// (recursive fs.rm does not work properly on Windows)
if (fs.existsSync(runtimeDir)) {
// fs.readdirSync(runtimeDir).forEach((file, index) => {
// const curPath = path.join(runtimeDir, file);
// fs.unlinkSync(curPath);
// });
fs.rmSync(runtimeDir, { recursive: true, force: true });
}
await fs.mkdir(runtimeDir);
await fsExtra.copy(path.resolve(__dirname, '../runtime'), path.resolve(runtimeDir));
}
await fs.mkdir(runtimeDir);
await fsExtra.copy(path.resolve(__dirname, '../runtime'), path.resolve(runtimeDir));
}
async function installJS() {
const config = vscode.workspace.getConfiguration("vscode_vibrancy");
const currentTheme = getCurrentTheme(config);
const themeConfig = require(path.resolve(__dirname, themeConfigPaths[currentTheme]));
const themeCSS = await fs.readFile(path.join(__dirname, themeStylePaths[currentTheme]), 'utf-8');
async function installJS() {
const config = vscode.workspace.getConfiguration("vscode_vibrancy");
const currentTheme = getCurrentTheme(config);
const themeConfig = require(path.resolve(__dirname, themeConfigPaths[currentTheme]));
const themeCSS = await fs.readFile(path.join(__dirname, themeStylePaths[currentTheme]), 'utf-8');
const JS = await fs.readFile(JSFile, 'utf-8');
const JS = await fs.readFile(JSFile, 'utf-8');
// generate imports by reading all files in config.imports
const imports = {
css: "",
js: "",
};
for (let i = 0; i < config.imports.length; i++) {
if (config.imports[i] === "/path/to/file") continue;
// generate imports by reading all files in config.imports
const imports = {
css: "",
js: "",
};
for (let i = 0; i < config.imports.length; i++) {
if (config.imports[i] === "/path/to/file") continue;
try {
const importContent = await fs.readFile(config.imports[i], 'utf-8');
if (config.imports[i].endsWith('.css')) {
imports.css += `<style>${importContent}</style>`;
} else {
imports.js += `<script>${importContent}</script>`;
}
} catch (err) {
vscode.window.showWarningMessage(localize('messages.importError').replace('%1', config.imports[i]));
}
}
try {
const importContent = await fs.readFile(config.imports[i], 'utf-8');
const injectData = {
os: os,
config: config,
theme: themeConfig,
themeCSS: themeCSS,
imports: imports,
}
if (config.imports[i].endsWith('.css')) {
imports.css += `<style>${importContent}</style>`;
} else {
imports.js += `<script>${importContent}</script>`;
}
} catch (err) {
vscode.window.showWarningMessage(localize('messages.importError').replace('%1', config.imports[i]));
}
}
const base = __filename;
const injectData = {
os: os,
config: config,
theme: themeConfig,
themeCSS: themeCSS,
imports: imports,
}
const newJS = JS.replace(/\n\/\* !! VSCODE-VIBRANCY-START !! \*\/[\s\S]*?\/\* !! VSCODE-VIBRANCY-END !! \*\//, '')
+ '\n/* !! VSCODE-VIBRANCY-START !! */\n;(function(){\n'
+ `if (!require(\'fs\').existsSync(${JSON.stringify(base)})) return;\n`
+ `global.vscode_vibrancy_plugin = ${JSON.stringify(injectData)}; try{ require(${JSON.stringify(runtimeDir)}); } catch (err) {console.error(err)}\n`
+ '})()\n/* !! VSCODE-VIBRANCY-END !! */';
await fs.writeFile(JSFile, newJS, 'utf-8');
}
const base = __filename;
async function installHTML() {
const HTML = await fs.readFile(HTMLFile, 'utf-8');
const newJS = JS.replace(/\n\/\* !! VSCODE-VIBRANCY-START !! \*\/[\s\S]*?\/\* !! VSCODE-VIBRANCY-END !! \*\//, '')
+ '\n/* !! VSCODE-VIBRANCY-START !! */\n;(function(){\n'
+ `if (!require(\'fs\').existsSync(${JSON.stringify(base)})) return;\n`
+ `global.vscode_vibrancy_plugin = ${JSON.stringify(injectData)}; try{ require(${JSON.stringify(runtimeDir)}); } catch (err) {console.error(err)}\n`
+ '})()\n/* !! VSCODE-VIBRANCY-END !! */';
await fs.writeFile(JSFile, newJS, 'utf-8');
}
const newHTML = HTML.replace(
/<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'; trusted-types (.+);">/g,
(_, trustedTypes) => {
return `<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'; trusted-types ${trustedTypes} VscodeVibrancy;">`;
}
);
async function installHTML() {
const HTML = await fs.readFile(HTMLFile, 'utf-8');
if (HTML !== newHTML) {
await fs.writeFile(HTMLFile, newHTML, 'utf-8');
}
}
const newHTML = HTML.replace(
/<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'; trusted-types (.+);">/g,
(_, trustedTypes) => {
return `<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'; trusted-types ${trustedTypes} VscodeVibrancy;">`;
}
);
async function uninstallJS() {
const JS = await fs.readFile(JSFile, 'utf-8');
const needClean = /\n\/\* !! VSCODE-VIBRANCY-START !! \*\/[\s\S]*?\/\* !! VSCODE-VIBRANCY-END !! \*\//.test(JS);
if (needClean) {
const newJS = JS
.replace(/\n\/\* !! VSCODE-VIBRANCY-START !! \*\/[\s\S]*?\/\* !! VSCODE-VIBRANCY-END !! \*\//, '')
await fs.writeFile(JSFile, newJS, 'utf-8');
}
}
if (HTML !== newHTML) {
await fs.writeFile(HTMLFile, newHTML, 'utf-8');
}
}
async function uninstallHTML() {
const HTML = await fs.readFile(HTMLFile, 'utf-8');
const needClean = / VscodeVibrancy;/.test(HTML);
if (needClean) {
const newHTML = HTML.replace(" VscodeVibrancy;", ";").replace("; trusted-types", "; trusted-types")
await fs.writeFile(HTMLFile, newHTML, 'utf-8');
}
}
async function uninstallJS() {
const JS = await fs.readFile(JSFile, 'utf-8');
const needClean = /\n\/\* !! VSCODE-VIBRANCY-START !! \*\/[\s\S]*?\/\* !! VSCODE-VIBRANCY-END !! \*\//.test(JS);
if (needClean) {
const newJS = JS
.replace(/\n\/\* !! VSCODE-VIBRANCY-START !! \*\/[\s\S]*?\/\* !! VSCODE-VIBRANCY-END !! \*\//, '')
await fs.writeFile(JSFile, newJS, 'utf-8');
}
}
function enabledRestart() {
vscode.window.showInformationMessage(localize('messages.enabled'), { title: localize('messages.restartIde') })
.then(function (msg) {
msg && promptRestart();
});
}
async function uninstallHTML() {
const HTML = await fs.readFile(HTMLFile, 'utf-8');
const needClean = / VscodeVibrancy;/.test(HTML);
if (needClean) {
const newHTML = HTML.replace(" VscodeVibrancy;", ";").replace("; trusted-types", "; trusted-types")
await fs.writeFile(HTMLFile, newHTML, 'utf-8');
}
}
function disabledRestart() {
vscode.window.showInformationMessage(localize('messages.disabled'), { title: localize('messages.restartIde') })
.then(function (msg) {
msg && promptRestart();
});
}
function enabledRestart() {
vscode.window.showInformationMessage(localize('messages.enabled'), { title: localize('messages.restartIde') })
.then(function (msg) {
msg && promptRestart();
});
}
// #### main commands ######################################################
function disabledRestart() {
vscode.window.showInformationMessage(localize('messages.disabled'), { title: localize('messages.restartIde') })
.then(function (msg) {
msg && promptRestart();
});
}
async function Install() {
// #### main commands ######################################################
if (os === 'unknown') {
vscode.window.showInformationMessage(localize('messages.unsupported'));
throw new Error('unsupported');
}
async function Install() {
try {
await fs.stat(JSFile);
await fs.stat(HTMLFile);
if (os === 'unknown') {
vscode.window.showInformationMessage(localize('messages.unsupported'));
throw new Error('unsupported');
}
await installRuntime();
await installJS();
await installHTML();
await changeTerminalRendererType();
} catch (error) {
if (error && (error.code === 'EPERM' || error.code === 'EACCES')) {
vscode.window.showInformationMessage(localize('messages.admin') + error);
}
else {
vscode.window.showInformationMessage(localize('messages.smthingwrong') + error);
}
throw error;
}
}
try {
await fs.stat(JSFile);
await fs.stat(HTMLFile);
async function Uninstall() {
try {
// uninstall old version
await fs.stat(HTMLFile);
await uninstallHTML();
} finally {
await installRuntime();
await installJS();
await installHTML();
await changeTerminalRendererType();
} catch (error) {
if (error && (error.code === 'EPERM' || error.code === 'EACCES')) {
vscode.window.showInformationMessage(localize('messages.admin') + error);
}
else {
vscode.window.showInformationMessage(localize('messages.smthingwrong') + error);
}
throw error;
}
}
}
async function Uninstall() {
try {
// uninstall old version
await fs.stat(HTMLFile);
await uninstallHTML();
} finally {
try {
await fs.stat(JSFile);
}
await uninstallJS();
} catch (error) {
if (error && (error.code === 'EPERM' || error.code === 'EACCES')) {
vscode.window.showInformationMessage(localize('messages.admin') + error);
}
else {
vscode.window.showInformationMessage(localize('messages.smthingwrong') + error);
}
throw error;
}
}
try {
await fs.stat(JSFile);
async function Update() {
await Uninstall();
await Install();
}
await uninstallJS();
} catch (error) {
if (error && (error.code === 'EPERM' || error.code === 'EACCES')) {
vscode.window.showInformationMessage(localize('messages.admin') + error);
}
else {
vscode.window.showInformationMessage(localize('messages.smthingwrong') + error);
}
throw error;
}
}
var installVibrancy = vscode.commands.registerCommand('extension.installVibrancy', async () => {
await Install();
enabledRestart();
});
var uninstallVibrancy = vscode.commands.registerCommand('extension.uninstallVibrancy', async () => {
await Uninstall()
disabledRestart();
});
var updateVibrancy = vscode.commands.registerCommand('extension.updateVibrancy', async () => {
await Update();
enabledRestart();
});
async function Update() {
await Uninstall();
await Install();
}
context.subscriptions.push(installVibrancy);
context.subscriptions.push(uninstallVibrancy);
context.subscriptions.push(updateVibrancy);
var installVibrancy = vscode.commands.registerCommand('extension.installVibrancy', async () => {
await Install();
enabledRestart();
});
var uninstallVibrancy = vscode.commands.registerCommand('extension.uninstallVibrancy', async () => {
await Uninstall()
disabledRestart();
});
var updateVibrancy = vscode.commands.registerCommand('extension.updateVibrancy', async () => {
await Update();
enabledRestart();
});
if (isFirstload()) {
vscode.window.showInformationMessage(localize('messages.firstload'), { title: localize('messages.installIde') })
.then(async (msg) => {
if (msg) {
await Update();
await checkColorTheme();
enabledRestart();
}
});
lockFirstload();
}
context.subscriptions.push(installVibrancy);
context.subscriptions.push(uninstallVibrancy);
context.subscriptions.push(updateVibrancy);
var lastConfig = vscode.workspace.getConfiguration("vscode_vibrancy");
if (isFirstload()) {
vscode.window.showInformationMessage(localize('messages.firstload'), { title: localize('messages.installIde') })
.then(async (msg) => {
if (msg) {
await Update();
await checkColorTheme();
enabledRestart();
}
});
lockFirstload();
}
vscode.workspace.onDidChangeConfiguration(() => {
newConfig = vscode.workspace.getConfiguration("vscode_vibrancy");
if (!deepEqual(lastConfig, newConfig)) {
lastConfig = newConfig;
vscode.window.showInformationMessage(localize('messages.configupdate'), { title: localize('messages.reloadIde') })
.then(async (msg) => {
if (msg) {
await Update();
if (newConfig.theme !== vscode.workspace.getConfiguration("vscode_vibrancy")) {
await checkColorTheme();
}
enabledRestart();
}
});
lockFirstload();
}
});
var lastConfig = vscode.workspace.getConfiguration("vscode_vibrancy");
vscode.workspace.onDidChangeConfiguration(() => {
newConfig = vscode.workspace.getConfiguration("vscode_vibrancy");
if (!deepEqual(lastConfig, newConfig)) {
lastConfig = newConfig;
vscode.window.showInformationMessage(localize('messages.configupdate'), { title: localize('messages.reloadIde') })
.then(async (msg) => {
if (msg) {
await Update();
if (newConfig.theme !== vscode.workspace.getConfiguration("vscode_vibrancy")) {
await checkColorTheme();
}
enabledRestart();
}
});
lockFirstload();
}
});
}
exports.activate = activate;