Skip to content

Commit

Permalink
Do not rewrite content of tasks.json file
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Nikitenko <rnikiten@redhat.com>
  • Loading branch information
RomanNikitenko committed Apr 2, 2019
1 parent f935dc7 commit 6ee2014
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
11 changes: 8 additions & 3 deletions packages/task/src/browser/quick-open-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { TaskInfo, TaskConfiguration } from '../common/task-protocol';
import { TaskConfigurations } from './task-configurations';
import URI from '@theia/core/lib/common/uri';
import { TaskActionProvider } from './task-action-provider';
import { LabelProvider } from '@theia/core/lib/browser';

@injectable()
export class QuickOpenTask implements QuickOpenModel, QuickOpenHandler {
Expand All @@ -44,6 +45,9 @@ export class QuickOpenTask implements QuickOpenModel, QuickOpenHandler {
@inject(TaskActionProvider)
protected readonly taskActionProvider: TaskActionProvider;

@inject(LabelProvider)
protected readonly labelProvider: LabelProvider;

/**
* @deprecated To be removed in 0.5.0
*/
Expand Down Expand Up @@ -142,7 +146,7 @@ export class QuickOpenTask implements QuickOpenModel, QuickOpenHandler {
}

providedTasks.forEach(task => {
this.items.push(new TaskConfigureQuickOpenItem(task, this.taskService));
this.items.push(new TaskConfigureQuickOpenItem(task, this.taskService, this.labelProvider));
});

this.quickOpenService.open(this, {
Expand Down Expand Up @@ -240,7 +244,8 @@ export class TaskConfigureQuickOpenItem extends QuickOpenGroupItem {

constructor(
protected readonly task: TaskConfiguration,
protected readonly taskService: TaskService
protected readonly taskService: TaskService,
protected readonly labelProvider: LabelProvider
) {
super();
}
Expand All @@ -251,7 +256,7 @@ export class TaskConfigureQuickOpenItem extends QuickOpenGroupItem {

getDescription(): string {
if (this.task._scope) {
return new URI(this.task._scope).path.toString();
return this.labelProvider.getLongName(new URI(this.task._scope));
}
return this.task._source;
}
Expand Down
33 changes: 20 additions & 13 deletions packages/task/src/browser/task-configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import * as jsoncparser from 'jsonc-parser';
import { ParseError } from 'jsonc-parser';
import { WorkspaceService } from '@theia/workspace/lib/browser';
import { open, OpenerService } from '@theia/core/lib/browser';
import { Resource } from '@theia/core';

export interface TaskConfigurationClient {
/**
Expand Down Expand Up @@ -248,11 +249,10 @@ export class TaskConfigurations implements Disposable {
return;
}

const tasks = this.getTasks();
tasks.push(task);

const configFileUri = this.getConfigFileUri(workspace.uri);
await this.saveTasks(configFileUri, tasks);
if (!this.getTasks().some(t => t.label === task.label)) {
await this.saveTask(configFileUri, task);
}

try {
await open(this.openerService, new URI(configFileUri));
Expand All @@ -261,20 +261,27 @@ export class TaskConfigurations implements Disposable {
}
}

/** Writes the tasks to a config file. Creates a config file if this one does not exist */
async saveTasks(configFileUri: string, tasks: TaskConfiguration[]): Promise<void> {
/** Writes the task to a config file. Creates a config file if this one does not exist */
async saveTask(configFileUri: string, task: TaskConfiguration): Promise<void> {
if (configFileUri && !await this.fileSystem.exists(configFileUri)) {
await this.fileSystem.createFile(configFileUri);
}

const preparedTasks = this.filterDuplicates(tasks).map(task => {
const { _source, $ident, ...preparedTask } = task;
return preparedTask;
});
const { _source, $ident, ...preparedTask } = task;
try {
const response = await this.fileSystem.resolveContent(configFileUri);
const content = response.content;

const resource = await this.resourceProvider(new URI(configFileUri));
if (resource.saveContents) {
await resource.saveContents(JSON.stringify({ tasks: preparedTasks }, undefined, 4));
const formattingOptions = { tabSize: 4, insertSpaces: true, eol: '' };
const edits = jsoncparser.modify(content, ['tasks', -1], preparedTask, { formattingOptions });
const result = jsoncparser.applyEdits(content, edits);

const resource = await this.resourceProvider(new URI(configFileUri));
Resource.save(resource, { content: result });
} catch (e) {
const message = `Failed to save task configuration for ${task.label} task.`;
console.error(`${message} ${e.toString()}`);
return;
}
}

Expand Down

0 comments on commit 6ee2014

Please sign in to comment.