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

fix: add patch lockfile for multiple instances #500

Merged
merged 3 commits into from
Nov 14, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/background/Background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import path from 'path';

import vscode, { Disposable, l10n, Uri } from 'vscode';

import { utils } from '../utils';
import { ENCODING, EXTENSION_NAME, TOUCH_JSFILE_PATH, VERSION } from '../utils/constants';
import { vscodePath } from '../utils/vscodePath';
import { vsHelp } from '../utils/vsHelp';
Expand All @@ -19,6 +18,7 @@ type TConfigType = vscode.WorkspaceConfiguration & TPatchGeneratorConfig;

/**
* 插件逻辑类
* Extension logic
*
* @export
* @class Background
Expand All @@ -36,6 +36,7 @@ export class Background implements Disposable {
public jsFile = new JsPatchFile(vscodePath.jsPath);

/**
* Current config
* 当前用户配置
*
* @private
Expand Down Expand Up @@ -220,10 +221,6 @@ export class Background implements Disposable {
return;
}

// 50~550ms 的延时,对于可能的多实例,错开对于文件的操作
// 虽然有锁了,但这样更安心 =。=
await utils.sleep(50 + ~~(Math.random() * 500));

this.onConfigChange();
})
);
Expand Down
8 changes: 4 additions & 4 deletions src/background/CssFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import fs, { constants as fsConstants } from 'fs';
import { tmpdir } from 'os';
import path from 'path';

import { utils } from '../utils';
import { _ } from '../utils';
import { BACKGROUND_VER, ENCODING, VERSION } from '../utils/constants';
import { vsc } from '../utils/vsc';

Expand Down Expand Up @@ -116,7 +116,7 @@ export class CssFile {
try {
const mvcmd = process.platform === 'win32' ? 'move /Y' : 'mv -f';
const cmdarg = `${mvcmd} "${tempFilePath}" "${this.filePath}"`;
await utils.sudoExec(cmdarg, { name: 'Visual Studio Code Background Extension' });
await _.sudoExec(cmdarg, { name: 'Visual Studio Code Background Extension' });
return true;
} catch (e: any) {
await vsc.window.showErrorMessage(e.message);
Expand Down Expand Up @@ -177,7 +177,7 @@ export class CssFile {
*/
public async uninstall(): Promise<boolean> {
try {
await utils.lock();
await _.lock();
let content = await this.getContent();
content = this.clearContent(content);
// 异常case return
Expand All @@ -189,7 +189,7 @@ export class CssFile {
console.log(ex);
return false;
} finally {
await utils.unlock();
await _.unlock();
}
}
}
20 changes: 12 additions & 8 deletions src/background/PatchFile/PatchFile.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs, { constants as fsConstants } from 'fs';
import { tmpdir } from 'os';
import path from 'path';

import { utils } from '../../utils';
import { _ } from '../../utils';
import { BACKGROUND_VER, ENCODING, VERSION } from '../../utils/constants';
import { vsc } from '../../utils/vsc';

Expand Down Expand Up @@ -96,7 +96,7 @@ export abstract class AbsPatchFile {
try {
const mvcmd = process.platform === 'win32' ? 'move /Y' : 'mv -f';
const cmdarg = `${mvcmd} "${tempFilePath}" "${filePath}"`;
await utils.sudoExec(cmdarg, { name: 'Background Extension' });
await _.sudoExec(cmdarg, { name: 'Background Extension' });
return true;
} catch (e: any) {
vsc.window.showErrorMessage(e.message, { title: 'Common Issue' }).then(confirm => {
Expand Down Expand Up @@ -145,11 +145,15 @@ export abstract class AbsPatchFile {
protected abstract cleanPatches(content: string): string;

public async restore() {
await utils.lock();
let content = await this.getContent();
content = this.cleanPatches(content);
const ok = await this.write(content);
await utils.unlock();
return ok;
try {
await _.lock();
let content = await this.getContent();
content = this.cleanPatches(content);
return await this.write(content);
} catch {
return false;
} finally {
await _.unlock();
}
}
}
31 changes: 22 additions & 9 deletions src/background/PatchFile/PatchFile.javascript.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { _ } from '../../utils';
import { BACKGROUND_VER, VERSION } from '../../utils/constants';
import { AbsPatchFile } from './PatchFile.base';

Expand All @@ -13,16 +14,28 @@ import { AbsPatchFile } from './PatchFile.base';
*/
export class JsPatchFile extends AbsPatchFile {
public async applyPatches(patchContent: string): Promise<boolean> {
let content = await this.getContent();
content = this.cleanPatches(content);
content += [
//
`\n// vscode-background-start ${BACKGROUND_VER}.${VERSION}`,
patchContent,
'// vscode-background-end'
].join('\n');
try {
await _.lock();
const curContent = await this.getContent();
let content = this.cleanPatches(curContent);
content += [
//
`\n// vscode-background-start ${BACKGROUND_VER}.${VERSION}`,
patchContent,
'// vscode-background-end'
].join('\n');

return this.write(content);
// file unchanged
if (curContent === content) {
return true;
}

return await this.write(content);
} catch {
return false;
} finally {
await _.unlock();
}
}

protected cleanPatches(content: string): string {
Expand Down
4 changes: 2 additions & 2 deletions src/background/PatchGenerator/PatchGenerator.base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as stylis from 'stylis';
import vscode from 'vscode';

import { utils } from '../../utils';
import { _ } from '../../utils';

/**
* 用于触发开发工具 css in js 语言支持
Expand Down Expand Up @@ -126,7 +126,7 @@ container.appendChild(div);
script
]
.filter(n => !!n.length)
.map(n => utils.withIIFE(n))
.map(n => _.withIIFE(n))
.join(';');
}
}
4 changes: 2 additions & 2 deletions src/background/PatchGenerator/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import uglifyjs from 'uglify-js';

import { utils } from '../../utils';
import { _ } from '../../utils';
import { ChecksumsPatchGenerator } from './PatchGenerator.checksums';
import {
EditorPatchGenerator,
Expand Down Expand Up @@ -28,7 +28,7 @@ export class PatchGenerator {
new PanelPatchGenerator(options.panel).create(), // panel
new FullscreenPatchGenerator(options.fullscreen).create() // fullscreen
]
.map(n => utils.withIIFE(n))
.map(n => _.withIIFE(n))
.join(';');

// return script;
Expand Down
6 changes: 4 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import lockfile from 'lockfile';
import { LOCK_PATH } from './constants';
import { vsc } from './vsc';

export namespace utils {
export namespace _ {
/**
* if zh-CN
*/
Expand Down Expand Up @@ -43,7 +43,9 @@ export namespace utils {
lockfile.lock(
LOCK_PATH,
{
wait: 5000 // 应该能撑200的并发了,,,>_<#@!
// When multiple VSCode instances are running, all instances' commands need to be executed within the `wait` time
// 在打开了多个vscode实例时,需要所有实例的命令在`wait`时间内执行完毕
wait: 1000 * 30
},
err => {
if (err) {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/vscodePath.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import { utils } from './index';
import { _ } from './index';
import { vsc } from './vsc';

// 基础目录
Expand All @@ -18,7 +18,7 @@ const cssPath = (() => {
// https://github.com/microsoft/vscode/pull/141263
const webPath = getCssPath('workbench.web.main.css');

if (utils.isDesktop) {
if (_.isDesktop) {
return defPath;
}
return webPath;
Expand All @@ -29,7 +29,7 @@ const jsPath = (() => {

// desktop
// /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.desktop.main.js
if (utils.isDesktop) {
if (_.isDesktop) {
return path.join(base, 'vs/workbench/workbench.desktop.main.js');
}

Expand Down