Skip to content

Commit

Permalink
Use stringified URIs instead of raw string paths
Browse files Browse the repository at this point in the history
Signed-off-by: Mykola Morhun <mmorhun@redhat.com>
  • Loading branch information
mmorhun committed Feb 21, 2020
1 parent 7a4a762 commit b5122f6
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 48 deletions.
10 changes: 5 additions & 5 deletions packages/core/src/browser/test/mock-env-variables-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export class MockEnvVariablesServerImpl implements EnvVariablesServer {
return '.theia';
}

async getUserHomeFolderPath(): Promise<string> {
return '/home/test';
async getUserHomeFolder(): Promise<string> {
return 'file:///home/test';
}

async getUserDataFolderPath(): Promise<string> {
return '/home/test/.theia';
async getUserDataFolder(): Promise<string> {
return 'file:///home/test/.theia';
}

getExecPath(): Promise<string> {
Expand All @@ -40,7 +40,7 @@ export class MockEnvVariablesServerImpl implements EnvVariablesServer {
getValue(key: string): Promise<EnvVariable | undefined> {
throw new Error('Method not implemented.');
}
getAppDataPath(): Promise<string> {
getAppDataFolder(): Promise<string> {
throw new Error('Method not implemented.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ export interface EnvVariablesServer {
getExecPath(): Promise<string>
getVariables(): Promise<EnvVariable[]>
getValue(key: string): Promise<EnvVariable | undefined>
getUserHomeFolderPath(): Promise<string>
getUserHomeFolder(): Promise<string>
getDataFolderName(): Promise<string>
getUserDataFolderPath(): Promise<string>
getAppDataPath(): Promise<string>
getUserDataFolder(): Promise<string>
/** Windows specific. Returns system data folder of Theia. On other than Windows systems is the same as getUserDataFolder */
getAppDataFolder(): Promise<string>
}

export interface EnvVariable {
Expand Down
25 changes: 13 additions & 12 deletions packages/core/src/node/env-variables/env-variables-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import * as path from 'path';
import * as os from 'os';
import { injectable } from 'inversify';
import { EnvVariable, EnvVariablesServer } from '../../common/env-variables';
import { isWindows } from '../../common/os';
import URI from '../../common/uri';

const THEIA_DATA_FOLDER = '.theia';

const WINDOWS_APP_DATA_DIR = 'AppData';
const WINDOWS_ROAMING_DIR = 'Roaming';
const WINDOWS_DATA_FOLDERS = [WINDOWS_APP_DATA_DIR, WINDOWS_ROAMING_DIR];

@injectable()
export class EnvVariablesServerImpl implements EnvVariablesServer {
Expand Down Expand Up @@ -53,24 +52,26 @@ export class EnvVariablesServerImpl implements EnvVariablesServer {
return this.envs[key];
}

async getUserHomeFolderPath(): Promise<string> {
return os.homedir();
async getUserHomeFolder(): Promise<string> {
return new URI(os.homedir()).toString();
}

async getDataFolderName(): Promise<string> {
return THEIA_DATA_FOLDER;
}

async getUserDataFolderPath(): Promise<string> {
return path.join(await this.getUserHomeFolderPath(), await this.getDataFolderName());
async getUserDataFolder(): Promise<string> {
return new URI(await this.getUserHomeFolder()).resolve(await this.getDataFolderName()).toString();
}

async getAppDataPath(): Promise<string> {
return path.join(
await this.getUserHomeFolderPath(),
...(isWindows ? WINDOWS_DATA_FOLDERS : ['']),
await this.getDataFolderName()
);
async getAppDataFolder(): Promise<string> {
const dataFolderUriBuilder = new URI(await this.getUserHomeFolder());
if (isWindows) {
dataFolderUriBuilder.resolve(WINDOWS_APP_DATA_DIR);
dataFolderUriBuilder.resolve(WINDOWS_ROAMING_DIR);
}
dataFolderUriBuilder.resolve(await this.getDataFolderName());
return dataFolderUriBuilder.toString();
}

}
4 changes: 2 additions & 2 deletions packages/java/src/node/java-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { injectable, inject, named } from 'inversify';
import { Message, isRequestMessage } from 'vscode-ws-jsonrpc';
import { InitializeParams, InitializeRequest } from 'vscode-languageserver-protocol';
import { createSocketConnection } from 'vscode-ws-jsonrpc/lib/server';
import { DEBUG_MODE } from '@theia/core/lib/node';
import { DEBUG_MODE, FileUri } from '@theia/core/lib/node';
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
import { IConnection, BaseLanguageServerContribution, LanguageServerStartOptions } from '@theia/languages/lib/node';
import { JAVA_LANGUAGE_ID, JAVA_LANGUAGE_NAME, JavaStartParams } from '../common';
Expand Down Expand Up @@ -104,7 +104,7 @@ export class JavaContribution extends BaseLanguageServerContribution {
this.activeDataFolders.add(dataFolderSuffix);
clientConnection.onClose(() => this.activeDataFolders.delete(dataFolderSuffix));

const workspacePath = path.resolve(await this.envServer.getUserDataFolderPath(), 'jdt.ls', '_ws_' + dataFolderSuffix);
const workspacePath = path.resolve(FileUri.fsPath(await this.envServer.getUserDataFolder()), 'jdt.ls', '_ws_' + dataFolderSuffix);
const configuration = configurations.get(process.platform);
if (!configuration) {
throw new Error('Cannot find Java server configuration for ' + process.platform);
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-ext/src/hosted/browser/hosted-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,8 @@ export class HostedPluginSupport {
}

protected async getHostGlobalStoragePath(): Promise<string> {
const globalStorageFolderPath = new Path(await this.envServer.getUserDataFolderPath()).join('globalStorage').toString();
const userDataFolderPath: string = (await this.fileSystem.getFsPath(await this.envServer.getUserDataFolder()))!;
const globalStorageFolderPath = new Path(userDataFolderPath).join('globalStorage').toString();

// Make sure that folder by the path exists
if (! await this.fileSystem.exists(globalStorageFolderPath)) {
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-ext/src/main/browser/env-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ export class EnvMainImpl implements EnvMain {
}

$getUserHomeFolderPath(): Promise<string> {
return this.envVariableServer.getUserHomeFolderPath();
return this.envVariableServer.getUserHomeFolder();
}

$getDataFolderName(): Promise<string> {
return this.envVariableServer.getDataFolderName();
}

$getUserDataFolderPath(): Promise<string> {
return this.envVariableServer.getUserDataFolderPath();
return this.envVariableServer.getUserDataFolder();
}

$getAppDataPath(): Promise<string> {
return this.envVariableServer.getAppDataPath();
return this.envVariableServer.getAppDataFolder();
}
}

Expand Down
12 changes: 6 additions & 6 deletions packages/plugin-ext/src/main/node/paths/plugin-paths-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as path from 'path';
import { readdir, remove } from 'fs-extra';
import * as crypto from 'crypto';
import URI from '@theia/core/lib/common/uri';
import { FileUri } from '@theia/core/lib/node';
import { ILogger } from '@theia/core';
import { PluginPaths } from './const';
import { PluginPathsService } from '../../common/plugin-paths-protocol';
Expand Down Expand Up @@ -84,8 +85,7 @@ export class PluginPathsServiceImpl implements PluginPathsService {
}

protected async buildWorkspaceId(workspace: FileStat, roots: FileStat[]): Promise<string> {
const userDataDirPath = await this.envServer.getUserDataFolderPath();
const untitledWorkspace = getTemporaryWorkspaceFileUri(userDataDirPath);
const untitledWorkspace = getTemporaryWorkspaceFileUri(await this.envServer.getUserDataFolder());

if (untitledWorkspace.toString() === workspace.uri) {
// if workspace is temporary
Expand All @@ -105,13 +105,13 @@ export class PluginPathsServiceImpl implements PluginPathsService {
}

private async getLogsDirPath(): Promise<string> {
const theiaDir = await this.envServer.getUserDataFolderPath();
return path.join(theiaDir, PluginPaths.PLUGINS_LOGS_DIR);
const theiaDirPath = FileUri.fsPath(await this.envServer.getUserDataFolder());
return path.join(theiaDirPath, PluginPaths.PLUGINS_LOGS_DIR);
}

private async getWorkspaceStorageDirPath(): Promise<string> {
const theiaDir = await this.envServer.getUserDataFolderPath();
return path.join(theiaDir, PluginPaths.PLUGINS_WORKSPACE_STORAGE_DIR);
const theiaDirPath = FileUri.fsPath(await this.envServer.getUserDataFolder());
return path.join(theiaDirPath, PluginPaths.PLUGINS_WORKSPACE_STORAGE_DIR);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { injectable, inject, postConstruct } from 'inversify';
import * as fs from 'fs-extra';
import * as path from 'path';
import { FileUri } from '@theia/core/lib/node/file-uri';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { FileSystem } from '@theia/filesystem/lib/common';
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
Expand All @@ -42,9 +43,9 @@ export class PluginsKeyValueStorage {
@postConstruct()
protected async init(): Promise<void> {
try {
const theiaDirPath = await this.envServer.getUserDataFolderPath();
await this.fileSystem.createFolder(theiaDirPath);
const globalDataPath = path.join(theiaDirPath, PluginPaths.PLUGINS_GLOBAL_STORAGE_DIR, 'global-state.json');
const theiaDataFolderPath = FileUri.fsPath(await this.envServer.getUserDataFolder());
await this.fileSystem.createFolder(theiaDataFolderPath);
const globalDataPath = path.join(theiaDataFolderPath, PluginPaths.PLUGINS_GLOBAL_STORAGE_DIR, 'global-state.json');
await this.fileSystem.createFolder(path.dirname(globalDataPath));
this.deferredGlobalDataPath.resolve(globalDataPath);
} catch (e) {
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-ext/src/plugin/env-variables-server-ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ export class EnvVariablesServerExt implements EnvVariablesServer {
return { name, value };
}

getUserHomeFolderPath(): Promise<string> {
getUserHomeFolder(): Promise<string> {
return this.proxy.$getUserHomeFolderPath();
}

getDataFolderName(): Promise<string> {
return this.proxy.$getDataFolderName();
}

getUserDataFolderPath(): Promise<string> {
getUserDataFolder(): Promise<string> {
return this.proxy.$getUserDataFolderPath();
}

getAppDataPath(): Promise<string> {
getAppDataFolder(): Promise<string> {
return this.proxy.$getAppDataPath();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ export class UserStorageServiceFilesystemImpl implements UserStorageService {
@inject(ILogger) protected readonly logger: ILogger,
@inject(EnvVariablesServer) protected readonly envServer: EnvVariablesServer
) {
this.userStorageFolder = this.envServer.getUserDataFolderPath().then(userDataFolderPath => {
const userStorageFolderUri = new URI('file://' + userDataFolderPath);
watcher.watchFileChanges(userStorageFolderUri).then(disposable =>
this.userStorageFolder = this.envServer.getUserDataFolder().then(userDataFolder => {
const userDataFolderUri = new URI(userDataFolder);
watcher.watchFileChanges(userDataFolderUri).then(disposable =>
this.toDispose.push(disposable)
);
this.toDispose.push(this.watcher.onFilesChanged(changes => this.onDidFilesChanged(changes)));
return userStorageFolderUri;
return userDataFolderUri;
});

this.toDispose.push(this.onUserStorageChangedEmitter);
Expand Down
5 changes: 2 additions & 3 deletions packages/workspace/src/browser/quick-open-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ export class QuickOpenWorkspace implements QuickOpenModel {

async open(workspaces: string[]): Promise<void> {
this.items = [];
const homeDirPath = await this.envServer.getUserHomeFolderPath();
const userDataDirPath = await this.envServer.getUserDataFolderPath();
const tempWorkspaceFile = getTemporaryWorkspaceFileUri(userDataDirPath);
const homeDirPath: string = (await this.fileSystem.getFsPath(await this.envServer.getUserHomeFolder()))!;
const tempWorkspaceFile = getTemporaryWorkspaceFileUri(await this.envServer.getUserDataFolder());
await this.preferences.ready;
if (!workspaces.length) {
this.items.push(new QuickOpenGroupItem({
Expand Down
3 changes: 1 addition & 2 deletions packages/workspace/src/browser/workspace-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,7 @@ export class WorkspaceService implements FrontendApplicationContribution {
}

protected async getUntitledWorkspace(): Promise<URI | undefined> {
const userDataDirPath = await this.envServer.getUserDataFolderPath();
return getTemporaryWorkspaceFileUri(userDataDirPath);
return getTemporaryWorkspaceFileUri(await this.envServer.getUserDataFolder());
}

private async writeWorkspaceFile(workspaceFile: FileStat | undefined, workspaceData: WorkspaceData): Promise<FileStat | undefined> {
Expand Down
2 changes: 1 addition & 1 deletion packages/workspace/src/node/default-workspace-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class DefaultWorkspaceServer implements WorkspaceServer {
}

protected async getUserStoragePath(): Promise<string> {
return path.resolve(await this.envServer.getUserDataFolderPath(), 'recentworkspace.json');
return path.resolve(FileUri.fsPath(await this.envServer.getUserDataFolder()), 'recentworkspace.json');
}
}

Expand Down

0 comments on commit b5122f6

Please sign in to comment.