deploy: current vibn theia state
Made-with: Cursor
This commit is contained in:
117
packages/preferences/src/node/backend-preference-storage.ts
Normal file
117
packages/preferences/src/node/backend-preference-storage.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2025 STMicroelectronics and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
|
||||
import { Listener, ListenerList, URI } from '@theia/core';
|
||||
import { JSONValue } from '@theia/core/shared/@lumino/coreutils';
|
||||
import { FileContentStatus, PreferenceStorage } from '../common/abstract-resource-preference-provider';
|
||||
import { EncodingService } from '@theia/core/lib/common/encoding-service';
|
||||
import { BinaryBuffer } from '@theia/core/lib/common/buffer';
|
||||
import { Deferred } from '@theia/core/lib/common/promise-util';
|
||||
import debounce = require('@theia/core/shared/lodash.debounce');
|
||||
import { JSONCEditor } from '../common/jsonc-editor';
|
||||
import { DiskFileSystemProvider } from '@theia/filesystem/lib/node/disk-file-system-provider';
|
||||
import { UTF8 } from '@theia/core/lib/common/encodings';
|
||||
|
||||
interface WriteOperation {
|
||||
key: string,
|
||||
path: string[],
|
||||
value: JSONValue
|
||||
}
|
||||
|
||||
export class BackendPreferenceStorage implements PreferenceStorage {
|
||||
|
||||
protected pendingWrites: WriteOperation[] = [];
|
||||
protected writeDeferred = new Deferred<boolean>();
|
||||
protected writeFile = debounce(() => {
|
||||
this.doWrite();
|
||||
}, 10);
|
||||
|
||||
protected currentContent: string | undefined = undefined;
|
||||
protected encoding: string = UTF8;
|
||||
|
||||
constructor(
|
||||
protected readonly fileSystem: DiskFileSystemProvider,
|
||||
protected readonly uri: URI,
|
||||
protected readonly encodingService: EncodingService,
|
||||
protected readonly jsonEditor: JSONCEditor) {
|
||||
|
||||
this.fileSystem.watch(uri, { excludes: [], recursive: false });
|
||||
this.fileSystem.onDidChangeFile(events => {
|
||||
for (const e of events) {
|
||||
if (e.resource.isEqual(uri)) {
|
||||
this.read().then(content => this.onDidChangeFileContentListeners.invoke({ content, fileOK: true }, () => { }))
|
||||
.catch(() => this.onDidChangeFileContentListeners.invoke({ content: '', fileOK: false }, () => { }));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
writeValue(key: string, path: string[], value: JSONValue): Promise<boolean> {
|
||||
this.pendingWrites.push({
|
||||
key, path, value
|
||||
});
|
||||
return this.waitForWrite();
|
||||
}
|
||||
|
||||
waitForWrite(): Promise<boolean> {
|
||||
const result = this.writeDeferred.promise;
|
||||
this.writeFile();
|
||||
return result;
|
||||
}
|
||||
|
||||
async doWrite(): Promise<void> {
|
||||
try {
|
||||
if (this.currentContent === undefined) {
|
||||
await this.read();
|
||||
}
|
||||
let newContent = this.currentContent || '';
|
||||
for (const op of this.pendingWrites) {
|
||||
newContent = this.jsonEditor.setValue(newContent, op.path, op.value);
|
||||
}
|
||||
await this.fileSystem.writeFile(this.uri, this.encodingService.encode(newContent, {
|
||||
encoding: this.encoding,
|
||||
hasBOM: false
|
||||
}).buffer, {
|
||||
create: true,
|
||||
overwrite: true
|
||||
});
|
||||
this.currentContent = newContent;
|
||||
this.pendingWrites = [];
|
||||
await Listener.awaitAll({ content: newContent, fileOK: true }, this.onDidChangeFileContentListeners);
|
||||
this.writeDeferred.resolve(true);
|
||||
} catch (e) {
|
||||
this.currentContent = undefined;
|
||||
console.error(e);
|
||||
this.writeDeferred.resolve(false);
|
||||
} finally {
|
||||
this.writeDeferred = new Deferred();
|
||||
}
|
||||
}
|
||||
|
||||
protected readonly onDidChangeFileContentListeners = new ListenerList<FileContentStatus, Promise<boolean>>();
|
||||
onDidChangeFileContent: Listener.Registration<FileContentStatus, Promise<boolean>> = this.onDidChangeFileContentListeners.registration;
|
||||
|
||||
async read(): Promise<string> {
|
||||
const contents = BinaryBuffer.wrap(await this.fileSystem.readFile(this.uri));
|
||||
this.encoding = (await this.encodingService.detectEncoding(contents)).encoding || this.encoding;
|
||||
this.currentContent = this.encodingService.decode(contents, this.encoding);
|
||||
return this.currentContent;
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
}
|
||||
|
||||
}
|
||||
49
packages/preferences/src/node/preference-backend-module.ts
Normal file
49
packages/preferences/src/node/preference-backend-module.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2024 Typefox and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
|
||||
import { ContainerModule } from '@theia/core/shared/inversify';
|
||||
import { CliContribution } from '@theia/core/lib/node/cli';
|
||||
import { PreferenceCliContribution } from './preference-cli-contribution';
|
||||
import { ConnectionContainerModule } from '@theia/core/lib/node/messaging/connection-container-module';
|
||||
import { CliPreferences, CliPreferencesPath } from '../common/cli-preferences';
|
||||
import { bindPreferenceProviders } from './preference-bindings';
|
||||
import { PreferenceStorageFactory } from '../common/abstract-resource-preference-provider';
|
||||
import { PreferenceScope, URI } from '@theia/core';
|
||||
import { BackendPreferenceStorage } from './backend-preference-storage';
|
||||
import { JSONCEditor } from '../common/jsonc-editor';
|
||||
import { EncodingService } from '@theia/core/lib/common/encoding-service';
|
||||
import { DiskFileSystemProvider } from '@theia/filesystem/lib/node/disk-file-system-provider';
|
||||
|
||||
const preferencesConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService }) => {
|
||||
bindBackendService(CliPreferencesPath, CliPreferences);
|
||||
});
|
||||
|
||||
export default new ContainerModule(bind => {
|
||||
bind(PreferenceCliContribution).toSelf().inSingletonScope();
|
||||
bind(CliPreferences).toService(PreferenceCliContribution);
|
||||
bind(CliContribution).toService(PreferenceCliContribution);
|
||||
bind(JSONCEditor).toSelf().inSingletonScope();
|
||||
|
||||
bind(PreferenceStorageFactory).toFactory(({ container }) => (uri: URI, scope: PreferenceScope) => new BackendPreferenceStorage(
|
||||
container.get(DiskFileSystemProvider),
|
||||
uri,
|
||||
container.get(EncodingService),
|
||||
container.get(JSONCEditor)
|
||||
));
|
||||
|
||||
bind(ConnectionContainerModule).toConstantValue(preferencesConnectionModule);
|
||||
bindPreferenceProviders(bind);
|
||||
});
|
||||
31
packages/preferences/src/node/preference-bindings.ts
Normal file
31
packages/preferences/src/node/preference-bindings.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2025 STMicroelectronics and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
|
||||
import { interfaces } from '@theia/core/shared/inversify';
|
||||
import { UserPreferenceProvider, UserPreferenceProviderFactory } from '../common/user-preference-provider';
|
||||
import { SectionPreferenceProviderUri, SectionPreferenceProviderSection } from '../common/section-preference-provider';
|
||||
import { bindFactory, PreferenceProvider, PreferenceScope, URI } from '@theia/core';
|
||||
import { UserConfigsPreferenceProvider, UserStorageLocationProvider } from '../common/user-configs-preference-provider';
|
||||
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
|
||||
|
||||
export function bindPreferenceProviders(bind: interfaces.Bind): void {
|
||||
bind(UserStorageLocationProvider).toDynamicValue(context => async () => {
|
||||
const env: EnvVariablesServer = context.container.get(EnvVariablesServer);
|
||||
return new URI(await env.getConfigDirUri());
|
||||
});
|
||||
bind(PreferenceProvider).to(UserConfigsPreferenceProvider).inSingletonScope().whenTargetNamed(PreferenceScope.User);
|
||||
bindFactory(bind, UserPreferenceProviderFactory, UserPreferenceProvider, SectionPreferenceProviderUri, SectionPreferenceProviderSection);
|
||||
}
|
||||
48
packages/preferences/src/node/preference-cli-contribution.ts
Normal file
48
packages/preferences/src/node/preference-cli-contribution.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2024 Typefox and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
|
||||
import { injectable } from '@theia/core/shared/inversify';
|
||||
import { Argv } from '@theia/core/shared/yargs';
|
||||
import { CliContribution } from '@theia/core/lib/node/cli';
|
||||
import { CliPreferences } from '../common/cli-preferences';
|
||||
|
||||
@injectable()
|
||||
export class PreferenceCliContribution implements CliContribution, CliPreferences {
|
||||
|
||||
protected preferences: [string, unknown][] = [];
|
||||
|
||||
configure(conf: Argv<{}>): void {
|
||||
conf.option('set-preference', {
|
||||
nargs: 1,
|
||||
desc: 'sets the specified preference'
|
||||
});
|
||||
}
|
||||
|
||||
setArguments(args: Record<string, unknown>): void {
|
||||
if (args.setPreference) {
|
||||
const preferences: string[] = args.setPreference instanceof Array ? args.setPreference : [args.setPreference];
|
||||
for (const preference of preferences) {
|
||||
const firstEqualIndex = preference.indexOf('=');
|
||||
this.preferences.push([preference.substring(0, firstEqualIndex), JSON.parse(preference.substring(firstEqualIndex + 1))]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async getPreferences(): Promise<[string, unknown][]> {
|
||||
return this.preferences;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user