Skip to content

Commit

Permalink
Enable user-level task configurations
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Mäder <tmader@redhat.com>
  • Loading branch information
tsmaeder committed May 11, 2020
1 parent fa7fb7d commit 415d602
Show file tree
Hide file tree
Showing 29 changed files with 549 additions and 339 deletions.
10 changes: 9 additions & 1 deletion packages/core/src/browser/preferences/preference-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ export interface PreferenceProviderDataChange {
readonly domain?: string[];
}

export namespace PreferenceProviderDataChange {
export function affects(change: PreferenceProviderDataChange, resourceUri?: string): boolean {
const resourcePath = resourceUri && new URI(resourceUri).path;
const domain = change.domain;
return !resourcePath || !domain || domain.some(uri => new URI(uri).path.relativity(resourcePath) >= 0);
}
}

export interface PreferenceProviderDataChanges {
[preferenceName: string]: PreferenceProviderDataChange
[preferenceName: string]: PreferenceProviderDataChange;
}

export interface PreferenceResolveResult<T> {
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions packages/debug/src/browser/preferences/launch-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import { interfaces } from 'inversify';
import { PreferenceContribution, PreferenceSchema } from '@theia/core/lib/browser/preferences/preference-contribution';
import { launchSchemaId } from '../debug-schema-updater';
import { LaunchFolderPreferenceProvider } from './launch-folder-preference-provider';
import { FolderPreferenceProvider } from '@theia/preferences/lib/browser';
import { PreferenceConfiguration } from '@theia/core/lib/browser/preferences/preference-configurations';

export const launchPreferencesSchema: PreferenceSchema = {
Expand All @@ -35,6 +33,5 @@ export const launchPreferencesSchema: PreferenceSchema = {

export function bindLaunchPreferences(bind: interfaces.Bind): void {
bind(PreferenceContribution).toConstantValue({ schema: launchPreferencesSchema });
bind(FolderPreferenceProvider).to(LaunchFolderPreferenceProvider).inTransientScope().whenTargetNamed('launch');
bind(PreferenceConfiguration).toConstantValue({ name: 'launch' });
}
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ export interface TaskDto {
type: string;
label: string;
source?: string;
scope?: string;
scope: string | number;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}
Expand Down
22 changes: 15 additions & 7 deletions packages/plugin-ext/src/main/browser/tasks-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class TasksMainImpl implements TasksMain, Disposable {
this.toDispose.push(this.taskWatcher.onTaskCreated((event: TaskInfo) => {
this.proxy.$onDidStartTask({
id: event.taskId,
task: event.config
task: this.fromTaskConfiguration(event.config)
});
}));

Expand All @@ -64,7 +64,7 @@ export class TasksMainImpl implements TasksMain, Disposable {
if (event.processId !== undefined) {
this.proxy.$onDidStartTaskProcess(event.processId, {
id: event.taskId,
task: event.config
task: this.fromTaskConfiguration(event.config)
});
}
}));
Expand Down Expand Up @@ -133,19 +133,19 @@ export class TasksMainImpl implements TasksMain, Disposable {
if (taskInfo) {
return {
id: taskInfo.taskId,
task: taskInfo.config
task: this.fromTaskConfiguration(taskInfo.config)
};
}
}

async $taskExecutions(): Promise<{
id: number;
task: TaskConfiguration;
task: TaskDto;
}[]> {
const runningTasks = await this.taskService.getRunningTasks();
return runningTasks.map(taskInfo => ({
id: taskInfo.taskId,
task: taskInfo.config
task: this.fromTaskConfiguration(taskInfo.config)
}));
}

Expand All @@ -167,16 +167,24 @@ export class TasksMainImpl implements TasksMain, Disposable {
protected createTaskResolver(handle: number): TaskResolver {
return {
resolveTask: taskConfig =>
this.proxy.$resolveTask(handle, taskConfig).then(v =>
this.proxy.$resolveTask(handle, this.fromTaskConfiguration(taskConfig)).then(v =>
this.toTaskConfiguration(v!)
)
};
}

protected toTaskConfiguration(taskDto: TaskDto): TaskConfiguration {
return Object.assign(taskDto, {
_source: taskDto.source || 'plugin',
_source: taskDto.source,
_scope: taskDto.scope
});
}

protected fromTaskConfiguration(task: TaskConfiguration): TaskDto {
return Object.assign(task, {
source: task._source,
scope: task._scope
});
}

}
7 changes: 5 additions & 2 deletions packages/plugin-ext/src/plugin/type-converters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ describe('Type converters:', () => {
type: shellType,
label,
source,
scope: undefined,
scope: 1,
command,
args,
options: { cwd },
Expand All @@ -194,7 +194,7 @@ describe('Type converters:', () => {
type: shellType,
label,
source,
scope: undefined,
scope: 2,
command: commandLine,
options: { cwd },
additionalProperty
Expand All @@ -203,6 +203,7 @@ describe('Type converters:', () => {
const shellPluginTask: theia.Task = {
name: label,
source,
scope: 1,
definition: {
type: shellType,
additionalProperty
Expand All @@ -219,6 +220,7 @@ describe('Type converters:', () => {
const pluginTaskWithCommandLine: theia.Task = {
name: label,
source,
scope: 2,
definition: {
type: shellType,
additionalProperty
Expand All @@ -245,6 +247,7 @@ describe('Type converters:', () => {
const customPluginTaskWithCommandLine: theia.Task = {
name: label,
source,
scope: 2,
definition: {
type: customType,
additionalProperty
Expand Down
10 changes: 8 additions & 2 deletions packages/plugin-ext/src/plugin/type-converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,11 @@ export function fromTask(task: theia.Task): TaskDto | undefined {
const taskDto = {} as TaskDto;
taskDto.label = task.name;
taskDto.source = task.source;
taskDto.scope = typeof task.scope === 'object' ? task.scope.uri.toString() : undefined;
if (typeof task.scope === 'object') {
taskDto.scope = task.scope.uri.toString();
} else if (typeof task.scope === 'number') {
taskDto.scope = task.scope;
}

const taskDefinition = task.definition;
if (!taskDefinition) {
Expand Down Expand Up @@ -748,13 +752,15 @@ export function toTask(taskDto: TaskDto): theia.Task {
const result = {} as theia.Task;
result.name = label;
result.source = source;
if (scope) {
if (typeof scope === 'string') {
const uri = URI.parse(scope);
result.scope = {
uri,
name: uri.toString(),
index: 0
};
} else {
result.scope = scope;
}

const taskType = type;
Expand Down
20 changes: 7 additions & 13 deletions packages/preferences/src/browser/folder-preference-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,35 @@
import { inject, injectable } from 'inversify';
import URI from '@theia/core/lib/common/uri';
import { PreferenceScope } from '@theia/core/lib/browser';
import { AbstractResourcePreferenceProvider } from './abstract-resource-preference-provider';
import { FileStat } from '@theia/filesystem/lib/common';
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
import { SectionPreferenceProvider } from './section-preference-provider';

export const FolderPreferenceProviderFactory = Symbol('FolderPreferenceProviderFactory');
export interface FolderPreferenceProviderFactory {
(options: FolderPreferenceProviderOptions): FolderPreferenceProvider;
(uri: URI, section: string, folder: FileStat): FolderPreferenceProvider;
}

export const FolderPreferenceProviderOptions = Symbol('FolderPreferenceProviderOptions');
export const FolderPreferenceProviderFolder = Symbol('FolderPreferenceProviderFolder');
export interface FolderPreferenceProviderOptions {
readonly folder: FileStat;
readonly configUri: URI;
readonly sectionName: string | undefined;
}

@injectable()
export class FolderPreferenceProvider extends AbstractResourcePreferenceProvider {
export class FolderPreferenceProvider extends SectionPreferenceProvider {

@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService;
@inject(FolderPreferenceProviderOptions) protected readonly options: FolderPreferenceProviderOptions;
@inject(FolderPreferenceProviderFolder) protected readonly folder: FileStat;

private _folderUri: URI;

get folderUri(): URI {
if (!this._folderUri) {
this._folderUri = new URI(this.options.folder.uri);
this._folderUri = new URI(this.folder.uri);
}
return this._folderUri;
}

protected getUri(): URI {
return this.options.configUri;
}

protected getScope(): PreferenceScope {
if (!this.workspaceService.isMultiRootWorkspaceOpened) {
// when FolderPreferenceProvider is used as a delegate of WorkspacePreferenceProvider in a one-folder workspace
Expand All @@ -62,5 +57,4 @@ export class FolderPreferenceProvider extends AbstractResourcePreferenceProvider
getDomain(): string[] {
return [this.folderUri.toString()];
}

}
25 changes: 15 additions & 10 deletions packages/preferences/src/browser/folders-preferences-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import URI from '@theia/core/lib/common/uri';
import { PreferenceProvider, PreferenceResolveResult } from '@theia/core/lib/browser/preferences/preference-provider';
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
import { PreferenceConfigurations } from '@theia/core/lib/browser/preferences/preference-configurations';
import { FolderPreferenceProvider, FolderPreferenceProviderFactory, FolderPreferenceProviderOptions } from './folder-preference-provider';
import { FolderPreferenceProvider, FolderPreferenceProviderFactory } from './folder-preference-provider';
import { FileStat } from '@theia/filesystem/lib/common';

@injectable()
export class FoldersPreferencesProvider extends PreferenceProvider {
Expand Down Expand Up @@ -57,12 +58,12 @@ export class FoldersPreferencesProvider extends PreferenceProvider {
for (const folder of roots) {
for (const configPath of this.configurations.getPaths()) {
for (const configName of [...this.configurations.getSectionNames(), this.configurations.getConfigName()]) {
const configUri = this.configurations.createUri(new URI(folder.uri), configPath, configName);
const key = configUri.toString();
toDelete.delete(key);
if (!this.providers.has(key)) {
const provider = this.createProvider({ folder, configUri });
this.providers.set(key, provider);
const sectionUri = this.configurations.createUri(new URI(folder.uri), configPath, configName);
const sectionKey = sectionUri.toString();
toDelete.delete(sectionKey);
if (!this.providers.has(sectionKey)) {
const provider = this.createProvider(sectionUri, configName, folder);
this.providers.set(sectionKey, provider);
}
}
}
Expand Down Expand Up @@ -202,6 +203,7 @@ export class FoldersPreferencesProvider extends PreferenceProvider {
folderProviders.push(provider);
providers.set(uri, folderProviders);

// in case we have nested folders mounted as workspace roots, select the innermost enclosing folder
const relativity = provider.folderUri.path.relativity(resourcePath);
if (relativity >= 0 && folder.relativity > relativity) {
folder = { relativity, uri };
Expand All @@ -210,10 +212,13 @@ export class FoldersPreferencesProvider extends PreferenceProvider {
return folder.uri && providers.get(folder.uri) || [];
}

protected createProvider(options: FolderPreferenceProviderOptions): FolderPreferenceProvider {
const provider = this.folderPreferenceProviderFactory(options);
protected createProvider(uri: URI, section: string, folder: FileStat): FolderPreferenceProvider {
const provider = this.folderPreferenceProviderFactory(uri, section, folder);
this.toDispose.push(provider);
this.toDispose.push(provider.onDidPreferencesChanged(change => this.onDidPreferencesChangedEmitter.fire(change)));
this.toDispose.push(provider.onDidPreferencesChanged(change => {
this.onDidPreferencesChangedEmitter.fire(change);
}
));
return provider;
}

Expand Down
1 change: 1 addition & 0 deletions packages/preferences/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export * from './user-preference-provider';
export * from './workspace-preference-provider';
export * from './folders-preferences-provider';
export * from './folder-preference-provider';
export * from './user-configs-preference-provider';
Loading

0 comments on commit 415d602

Please sign in to comment.