mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
Initial VS Code 1.19 source merge (#571)
* Initial 1.19 xcopy * Fix yarn build * Fix numerous build breaks * Next batch of build break fixes * More build break fixes * Runtime breaks * Additional post merge fixes * Fix windows setup file * Fix test failures. * Update license header blocks to refer to source eula
This commit is contained in:
18
src/vs/platform/state/common/state.ts
Normal file
18
src/vs/platform/state/common/state.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
|
||||
|
||||
export const IStateService = createDecorator<IStateService>('stateService');
|
||||
|
||||
export interface IStateService {
|
||||
_serviceBrand: any;
|
||||
|
||||
getItem<T>(key: string, defaultValue?: T): T;
|
||||
setItem(key: string, data: any): void;
|
||||
removeItem(key: string): void;
|
||||
}
|
||||
110
src/vs/platform/state/node/stateService.ts
Normal file
110
src/vs/platform/state/node/stateService.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import * as path from 'path';
|
||||
import * as fs from 'original-fs';
|
||||
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
|
||||
import { writeFileAndFlushSync } from 'vs/base/node/extfs';
|
||||
import { isUndefined, isUndefinedOrNull } from 'vs/base/common/types';
|
||||
import { IStateService } from 'vs/platform/state/common/state';
|
||||
import { ILogService } from 'vs/platform/log/common/log';
|
||||
|
||||
export class FileStorage {
|
||||
|
||||
private database: object = null;
|
||||
|
||||
constructor(private dbPath: string, private onError: (error) => void) { }
|
||||
|
||||
private ensureLoaded(): void {
|
||||
if (!this.database) {
|
||||
this.database = this.loadSync();
|
||||
}
|
||||
}
|
||||
|
||||
public getItem<T>(key: string, defaultValue?: T): T {
|
||||
this.ensureLoaded();
|
||||
|
||||
const res = this.database[key];
|
||||
if (isUndefinedOrNull(res)) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public setItem(key: string, data: any): void {
|
||||
this.ensureLoaded();
|
||||
|
||||
// Remove an item when it is undefined or null
|
||||
if (isUndefinedOrNull(data)) {
|
||||
return this.removeItem(key);
|
||||
}
|
||||
|
||||
// Shortcut for primitives that did not change
|
||||
if (typeof data === 'string' || typeof data === 'number' || typeof data === 'boolean') {
|
||||
if (this.database[key] === data) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this.database[key] = data;
|
||||
this.saveSync();
|
||||
}
|
||||
|
||||
public removeItem(key: string): void {
|
||||
this.ensureLoaded();
|
||||
|
||||
// Only update if the key is actually present (not undefined)
|
||||
if (!isUndefined(this.database[key])) {
|
||||
this.database[key] = void 0;
|
||||
this.saveSync();
|
||||
}
|
||||
}
|
||||
|
||||
private loadSync(): object {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(this.dbPath).toString()); // invalid JSON or permission issue can happen here
|
||||
} catch (error) {
|
||||
if (error && error.code !== 'ENOENT') {
|
||||
this.onError(error);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
private saveSync(): void {
|
||||
try {
|
||||
writeFileAndFlushSync(this.dbPath, JSON.stringify(this.database, null, 4)); // permission issue can happen here
|
||||
} catch (error) {
|
||||
this.onError(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class StateService implements IStateService {
|
||||
|
||||
_serviceBrand: any;
|
||||
|
||||
private fileStorage: FileStorage;
|
||||
|
||||
constructor( @IEnvironmentService environmentService: IEnvironmentService, @ILogService logService: ILogService) {
|
||||
this.fileStorage = new FileStorage(path.join(environmentService.userDataPath, 'storage.json'), error => logService.error(error));
|
||||
}
|
||||
|
||||
public getItem<T>(key: string, defaultValue?: T): T {
|
||||
return this.fileStorage.getItem(key, defaultValue);
|
||||
}
|
||||
|
||||
public setItem(key: string, data: any): void {
|
||||
this.fileStorage.setItem(key, data);
|
||||
}
|
||||
|
||||
public removeItem(key: string): void {
|
||||
this.fileStorage.removeItem(key);
|
||||
}
|
||||
}
|
||||
56
src/vs/platform/state/test/node/state.test.ts
Normal file
56
src/vs/platform/state/test/node/state.test.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import * as assert from 'assert';
|
||||
import os = require('os');
|
||||
import path = require('path');
|
||||
import extfs = require('vs/base/node/extfs');
|
||||
import { getRandomTestPath } from 'vs/workbench/test/workbenchTestServices';
|
||||
import { writeFileAndFlushSync, mkdirp } from 'vs/base/node/extfs';
|
||||
import { FileStorage } from 'vs/platform/state/node/stateService';
|
||||
|
||||
suite('StateService', () => {
|
||||
const parentDir = getRandomTestPath(os.tmpdir(), 'vsctests', 'stateservice');
|
||||
const storageFile = path.join(parentDir, 'storage.json');
|
||||
|
||||
teardown(done => {
|
||||
extfs.del(parentDir, os.tmpdir(), done);
|
||||
});
|
||||
|
||||
test('Basics', done => {
|
||||
return mkdirp(parentDir).then(() => {
|
||||
writeFileAndFlushSync(storageFile, '');
|
||||
|
||||
let service = new FileStorage(storageFile, () => null);
|
||||
|
||||
service.setItem('some.key', 'some.value');
|
||||
assert.equal(service.getItem('some.key'), 'some.value');
|
||||
|
||||
service.removeItem('some.key');
|
||||
assert.equal(service.getItem('some.key', 'some.default'), 'some.default');
|
||||
|
||||
assert.ok(!service.getItem('some.unknonw.key'));
|
||||
|
||||
service.setItem('some.other.key', 'some.other.value');
|
||||
|
||||
service = new FileStorage(storageFile, () => null);
|
||||
|
||||
assert.equal(service.getItem('some.other.key'), 'some.other.value');
|
||||
|
||||
service.setItem('some.other.key', 'some.other.value');
|
||||
assert.equal(service.getItem('some.other.key'), 'some.other.value');
|
||||
|
||||
service.setItem('some.undefined.key', void 0);
|
||||
assert.equal(service.getItem('some.undefined.key', 'some.default'), 'some.default');
|
||||
|
||||
service.setItem('some.null.key', null);
|
||||
assert.equal(service.getItem('some.null.key', 'some.default'), 'some.default');
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user