提交 1558036e 编写于 作者: D Daniel Imms

Shuffle BackupMainService around

上级 b91bde29
......@@ -12,7 +12,7 @@ import { TPromise, TValueCallback } from 'vs/base/common/winjs.base';
import { ReadyState, VSCodeWindow } from 'vs/code/electron-main/window';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { IBackupMainService } from 'vs/platform/backup/node/backup';
import { IBackupMainService } from 'vs/platform/backup/common/backup';
import { ILogService } from 'vs/code/electron-main/log';
import { IStorageService } from 'vs/code/electron-main/storage';
......
......@@ -34,7 +34,8 @@ import { ServiceCollection } from 'vs/platform/instantiation/common/serviceColle
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
import { ILogService, MainLogService } from 'vs/code/electron-main/log';
import { IStorageService, StorageService } from 'vs/code/electron-main/storage';
import { IBackupMainService, BackupService } from 'vs/platform/backup/node/backup';
import { IBackupMainService } from 'vs/platform/backup/common/backup';
import { BackupMainService } from 'vs/platform/backup/node/backupMainService';
import { IEnvironmentService, ParsedArgs } from 'vs/platform/environment/common/environment';
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
......@@ -427,7 +428,7 @@ function createServices(args): IInstantiationService {
services.set(IConfigurationService, new SyncDescriptor(ConfigurationService));
services.set(IRequestService, new SyncDescriptor(RequestService));
services.set(IURLService, new SyncDescriptor(URLService, args['open-url']));
services.set(IBackupMainService, new SyncDescriptor(BackupService));
services.set(IBackupMainService, new SyncDescriptor(BackupMainService));
return new InstantiationService(services, true);
}
......
......@@ -17,7 +17,7 @@ import { IConfigurationService } from 'vs/platform/configuration/common/configur
import { parseArgs } from 'vs/platform/environment/node/argv';
import product from 'vs/platform/product';
import { getCommonHTTPHeaders } from 'vs/platform/environment/node/http';
import { IBackupMainService } from 'vs/platform/backup/node/backup';
import { IBackupMainService } from 'vs/platform/backup/common/backup';
import Uri from 'vs/base/common/uri';
export interface IWindowState {
......
......@@ -14,7 +14,7 @@ import * as paths from 'vs/base/common/paths';
import * as types from 'vs/base/common/types';
import * as arrays from 'vs/base/common/arrays';
import { assign, mixin } from 'vs/base/common/objects';
import { IBackupMainService } from 'vs/platform/backup/node/backup';
import { IBackupMainService } from 'vs/platform/backup/common/backup';
import { trim } from 'vs/base/common/strings';
import { IEnvironmentService, ParsedArgs } from 'vs/platform/environment/common/environment';
import { IStorageService } from 'vs/code/electron-main/storage';
......
......@@ -3,6 +3,53 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import Uri from 'vs/base/common/uri';
export interface IBackupWorkspacesFormat {
folderWorkspaces: string[];
}
export const IBackupMainService = createDecorator<IBackupMainService>('backupService');
export interface IBackupMainService {
_serviceBrand: any;
/**
* Gets the set of active workspace backup paths being tracked for restoration.
*
* @return The set of active workspace backup paths being tracked for restoration.
*/
getWorkspaceBackupPaths(): string[];
/**
* Pushes workspace backup paths to be tracked for restoration.
*
* @param workspaces The workspaces to add.
*/
pushWorkspaceBackupPathsSync(workspaces: Uri[]): void;
/**
* Removes a workspace backup path being tracked for restoration.
*
* @param workspace The workspace to remove.
*/
removeWorkspaceBackupPathSync(workspace: Uri): void;
/**
* Gets the set of untitled file backups for a particular workspace.
*
* @param workspace The workspace to get the backups for.
* @return The absolute paths for all the untitled file _backups_.
*/
getWorkspaceUntitledFileBackupsSync(workspace: Uri): string[];
/**
* Gets whether the workspace has backup(s) associated with it (ie. if the workspace backup
* directory exists).
*
* @param workspace The workspace to evaluate.
* @return Whether the workspace has backups.
*/
hasWorkspaceBackup(workspace: Uri): boolean;
}
......@@ -8,55 +8,10 @@ import * as fs from 'fs';
import * as path from 'path';
import Uri from 'vs/base/common/uri';
import { readdirSync } from 'vs/base/node/extfs';
import { IBackupWorkspacesFormat } from 'vs/platform/backup/common/backup';
import { IBackupWorkspacesFormat, IBackupMainService } from 'vs/platform/backup/common/backup';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
export const IBackupMainService = createDecorator<IBackupMainService>('backupService');
export interface IBackupMainService {
_serviceBrand: any;
/**
* Gets the set of active workspace backup paths being tracked for restoration.
*
* @return The set of active workspace backup paths being tracked for restoration.
*/
getWorkspaceBackupPaths(): string[];
/**
* Pushes workspace backup paths to be tracked for restoration.
*
* @param workspaces The workspaces to add.
*/
pushWorkspaceBackupPathsSync(workspaces: Uri[]): void;
/**
* Removes a workspace backup path being tracked for restoration.
*
* @param workspace The workspace to remove.
*/
removeWorkspaceBackupPathSync(workspace: Uri): void;
/**
* Gets the set of untitled file backups for a particular workspace.
*
* @param workspace The workspace to get the backups for.
* @return The absolute paths for all the untitled file _backups_.
*/
getWorkspaceUntitledFileBackupsSync(workspace: Uri): string[];
/**
* Gets whether the workspace has backup(s) associated with it (ie. if the workspace backup
* directory exists).
*
* @param workspace The workspace to evaluate.
* @return Whether the workspace has backups.
*/
hasWorkspaceBackup(workspace: Uri): boolean;
}
export class BackupService implements IBackupMainService {
export class BackupMainService implements IBackupMainService {
public _serviceBrand: any;
......
......@@ -15,10 +15,10 @@ import extfs = require('vs/base/node/extfs');
import pfs = require('vs/base/node/pfs');
import Uri from 'vs/base/common/uri';
import { TestEnvironmentService } from 'vs/test/utils/servicesTestUtils';
import { BackupService } from 'vs/platform/backup/node/backup';
import { BackupMainService } from 'vs/platform/backup/node/backupMainService';
import { IBackupWorkspacesFormat } from 'vs/platform/backup/common/backup';
class TestBackupService extends BackupService {
class TestBackupMainService extends BackupMainService {
constructor(backupHome: string, backupWorkspacesPath: string) {
super(TestEnvironmentService);
......@@ -30,7 +30,7 @@ class TestBackupService extends BackupService {
}
}
suite('BackupService', () => {
suite('BackupMainService', () => {
const parentDir = path.join(os.tmpdir(), 'vsctests', 'service');
const backupHome = path.join(parentDir, 'Backups');
const backupWorkspacesPath = path.join(backupHome, 'workspaces.json');
......@@ -40,10 +40,10 @@ suite('BackupService', () => {
const fooWorkspaceBackupDir = path.join(backupHome, crypto.createHash('md5').update(fooFile.fsPath).digest('hex'));
let backupService: BackupService;
let service: BackupMainService;
setup(done => {
backupService = new TestBackupService(backupHome, backupWorkspacesPath);
service = new TestBackupMainService(backupHome, backupWorkspacesPath);
// Delete any existing backups completely and then re-create it.
extfs.del(backupHome, os.tmpdir(), () => {
......@@ -58,41 +58,41 @@ suite('BackupService', () => {
});
test('getWorkspaceBackupPathsSync should return [] when workspaces.json doesn\'t exist', () => {
assert.deepEqual(backupService.getWorkspaceBackupPaths(), []);
assert.deepEqual(service.getWorkspaceBackupPaths(), []);
});
test('getWorkspaceBackupPathsSync should return [] when workspaces.json is not properly formed JSON', () => {
fs.writeFileSync(backupWorkspacesPath, '');
assert.deepEqual(backupService.getWorkspaceBackupPaths(), []);
assert.deepEqual(service.getWorkspaceBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{]');
assert.deepEqual(backupService.getWorkspaceBackupPaths(), []);
assert.deepEqual(service.getWorkspaceBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, 'foo');
assert.deepEqual(backupService.getWorkspaceBackupPaths(), []);
assert.deepEqual(service.getWorkspaceBackupPaths(), []);
});
test('getWorkspaceBackupPathsSync should return [] when folderWorkspaces in workspaces.json is absent', () => {
fs.writeFileSync(backupWorkspacesPath, '{}');
assert.deepEqual(backupService.getWorkspaceBackupPaths(), []);
assert.deepEqual(service.getWorkspaceBackupPaths(), []);
});
test('getWorkspaceBackupPathsSync should return [] when folderWorkspaces in workspaces.json is not a string array', () => {
fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{}}');
assert.deepEqual(backupService.getWorkspaceBackupPaths(), []);
assert.deepEqual(service.getWorkspaceBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{"foo": ["bar"]}}');
assert.deepEqual(backupService.getWorkspaceBackupPaths(), []);
assert.deepEqual(service.getWorkspaceBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{"foo": []}}');
assert.deepEqual(backupService.getWorkspaceBackupPaths(), []);
assert.deepEqual(service.getWorkspaceBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{"foo": "bar"}}');
assert.deepEqual(backupService.getWorkspaceBackupPaths(), []);
assert.deepEqual(service.getWorkspaceBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":"foo"}');
assert.deepEqual(backupService.getWorkspaceBackupPaths(), []);
assert.deepEqual(service.getWorkspaceBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":1}');
assert.deepEqual(backupService.getWorkspaceBackupPaths(), []);
assert.deepEqual(service.getWorkspaceBackupPaths(), []);
});
test('pushWorkspaceBackupPathsSync should persist paths to workspaces.json', () => {
backupService.pushWorkspaceBackupPathsSync([fooFile, barFile]);
assert.deepEqual(backupService.getWorkspaceBackupPaths(), [fooFile.fsPath, barFile.fsPath]);
service.pushWorkspaceBackupPathsSync([fooFile, barFile]);
assert.deepEqual(service.getWorkspaceBackupPaths(), [fooFile.fsPath, barFile.fsPath]);
});
test('getWorkspaceUntitledFileBackupsSync should return untitled file backup resources', done => {
......@@ -101,9 +101,9 @@ suite('BackupService', () => {
const untitledBackup2 = path.join(untitledBackupDir, 'foo');
pfs.mkdirp(untitledBackupDir).then(() => {
pfs.writeFile(untitledBackup1, 'test').then(() => {
assert.deepEqual(backupService.getWorkspaceUntitledFileBackupsSync(fooFile), [untitledBackup1]);
assert.deepEqual(service.getWorkspaceUntitledFileBackupsSync(fooFile), [untitledBackup1]);
pfs.writeFile(untitledBackup2, 'test').then(() => {
assert.deepEqual(backupService.getWorkspaceUntitledFileBackupsSync(fooFile), [untitledBackup1, untitledBackup2]);
assert.deepEqual(service.getWorkspaceUntitledFileBackupsSync(fooFile), [untitledBackup1, untitledBackup2]);
done();
});
});
......@@ -111,12 +111,12 @@ suite('BackupService', () => {
});
test('removeWorkspaceBackupPath should remove workspaces from workspaces.json', done => {
backupService.pushWorkspaceBackupPathsSync([fooFile, barFile]);
backupService.removeWorkspaceBackupPathSync(fooFile);
service.pushWorkspaceBackupPathsSync([fooFile, barFile]);
service.removeWorkspaceBackupPathSync(fooFile);
pfs.readFile(backupWorkspacesPath, 'utf-8').then(buffer => {
const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
assert.deepEqual(json.folderWorkspaces, [barFile.fsPath]);
backupService.removeWorkspaceBackupPathSync(barFile);
service.removeWorkspaceBackupPathSync(barFile);
pfs.readFile(backupWorkspacesPath, 'utf-8').then(content => {
const json2 = <IBackupWorkspacesFormat>JSON.parse(content);
assert.deepEqual(json2.folderWorkspaces, []);
......@@ -128,7 +128,7 @@ suite('BackupService', () => {
test('removeWorkspaceBackupPath should fail gracefully when removing a path that doesn\'t exist', done => {
const workspacesJson: IBackupWorkspacesFormat = { folderWorkspaces: [fooFile.fsPath] };
pfs.writeFileAndFlush(backupWorkspacesPath, JSON.stringify(workspacesJson)).then(() => {
backupService.removeWorkspaceBackupPathSync(barFile);
service.removeWorkspaceBackupPathSync(barFile);
pfs.readFile(backupWorkspacesPath, 'utf-8').then(content => {
const json = <IBackupWorkspacesFormat>JSON.parse(content);
assert.deepEqual(json.folderWorkspaces, [fooFile.fsPath]);
......@@ -138,8 +138,8 @@ suite('BackupService', () => {
});
test('doesWorkspaceHaveBackups should return whether the workspace\'s backup exists', () => {
assert.equal(backupService.hasWorkspaceBackup(fooFile), false);
assert.equal(service.hasWorkspaceBackup(fooFile), false);
fs.mkdirSync(fooWorkspaceBackupDir);
assert.equal(backupService.hasWorkspaceBackup(fooFile), true);
assert.equal(service.hasWorkspaceBackup(fooFile), true);
});
});
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册