Skip to content

Commit 36cfd8e

Browse files
committed
Factorize logic of getting a task by name or creating it
1 parent fefb9d3 commit 36cfd8e

File tree

2 files changed

+62
-61
lines changed

2 files changed

+62
-61
lines changed

integration/vscode/ada/src/gnattest.ts

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { escapeRegExp, exe, setTerminalEnvironment, slugify } from './helpers';
1313
import {
1414
DEFAULT_PROBLEM_MATCHERS,
1515
findTaskByName,
16+
getOrCreateTask,
1617
runTaskSequence,
1718
SimpleTaskDef,
1819
TASK_BUILD_TEST_DRIVER,
@@ -801,30 +802,14 @@ async function buildTestDriverAndReportErrors(
801802
) {
802803
const buildTasks = [];
803804
if (coverage) {
804-
const adaTP = adaExtState.getAdaTaskProvider();
805-
assert(adaTP);
806-
807-
const instTaskName = `GNATcoverage - Generate instrumented sources for coverage analysis`;
808805
/**
809806
* First try to fetch an existing task of the corresponding name. The
810807
* User may have defined a homonym in tasks.json to customize this
811808
* step.
812809
*/
813-
const instExistingTask = await findTaskByName(`${TASK_TYPE_ADA}: ${instTaskName}`).then(
814-
undefined,
815-
/**
816-
* Return undefined in case of errors when searching for the task.
817-
*/
818-
() => undefined,
819-
);
820-
let instTask;
821-
if (instExistingTask) {
822-
instTask = instExistingTask;
823-
} else {
824-
/**
825-
* If there's no existing task of that name, create one on the fly.
826-
*/
827-
const instTaskDef: SimpleTaskDef = {
810+
const instTask = await getOrCreateTask(
811+
`GNATcoverage - Generate instrumented sources for coverage analysis`,
812+
async () => ({
828813
type: TASK_TYPE_ADA,
829814
command: 'gnatcov',
830815
args: [
@@ -833,31 +818,12 @@ async function buildTestDriverAndReportErrors(
833818
'-P',
834819
await getGnatTestDriverProjectPath(),
835820
].concat(getScenarioArgs()),
836-
};
837-
instTask = (await adaTP.resolveTask(
838-
new vscode.Task(
839-
instTaskDef,
840-
vscode.TaskScope.Workspace,
841-
instTaskName,
842-
TASK_TYPE_ADA,
843-
undefined,
844-
DEFAULT_PROBLEM_MATCHERS,
845-
),
846-
))!;
847-
instTask.presentationOptions.reveal =
848-
instTask.presentationOptions.reveal ?? vscode.TaskRevealKind.Never;
849-
}
850-
851-
const buildTaskName = `GNATcoverage - Build GNATtest harness project in coverage mode`;
852-
const buildExistingTask = await findTaskByName(`${TASK_TYPE_ADA}: ${buildTaskName}`).then(
853-
undefined,
854-
() => undefined,
821+
}),
855822
);
856-
let buildTask;
857-
if (buildExistingTask) {
858-
buildTask = buildExistingTask;
859-
} else {
860-
const buildTaskDef: SimpleTaskDef = {
823+
824+
const buildTask = await getOrCreateTask(
825+
`GNATcoverage - Build GNATtest harness project in coverage mode`,
826+
async () => ({
861827
type: TASK_TYPE_ADA,
862828
command: 'gprbuild',
863829
args: [
@@ -877,20 +843,8 @@ async function buildTestDriverAndReportErrors(
877843
'-fdump-scos',
878844
'-fpreserve-control-flow',
879845
]),
880-
};
881-
buildTask = (await adaTP.resolveTask(
882-
new vscode.Task(
883-
buildTaskDef,
884-
vscode.TaskScope.Workspace,
885-
buildTaskName,
886-
TASK_TYPE_ADA,
887-
undefined,
888-
DEFAULT_PROBLEM_MATCHERS,
889-
),
890-
))!;
891-
buildTask.presentationOptions.reveal = vscode.TaskRevealKind.Never;
892-
}
893-
846+
}),
847+
);
894848
buildTasks.push(instTask, buildTask);
895849
} else {
896850
const task = await findTaskByName(`${TASK_TYPE_ADA}: ${TASK_BUILD_TEST_DRIVER}`);

integration/vscode/ada/src/taskProviders.ts

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ abstract class SequentialExecution extends vscode.CustomExecution {
10751075
* @throws an Error if the task is not found.
10761076
*/
10771077
export async function findTaskByName(
1078-
taskName: string,
1078+
taskFullName: string,
10791079
tasks?: vscode.Task[],
10801080
): Promise<vscode.Task> {
10811081
if (!tasks) {
@@ -1088,7 +1088,7 @@ export async function findTaskByName(
10881088
}
10891089

10901090
if (tasks.length == 0) {
1091-
throw Error('The task list is empty.' + ` Cannot find task '${taskName}'`);
1091+
throw Error('The task list is empty.' + ` Cannot find task '${taskFullName}'`);
10921092
}
10931093

10941094
// Sort the given tasks to put the workspace-defined ones first
@@ -1097,12 +1097,12 @@ export async function findTaskByName(
10971097
tasks = tasks.sort(workspaceTasksFirst);
10981098

10991099
const task = tasks.find((v) => {
1100-
return taskName == getConventionalTaskLabel(v) || taskName == v.name;
1100+
return taskFullName == getConventionalTaskLabel(v) || taskFullName == v.name;
11011101
});
11021102
if (task) {
11031103
return task;
11041104
} else {
1105-
const msg = `Could not find a task named '${taskName}' among the tasks:\n${tasks
1105+
const msg = `Could not find a task named '${taskFullName}' among the tasks:\n${tasks
11061106
.map((t) => t.name)
11071107
.join('\n')}`;
11081108
throw Error(msg);
@@ -1385,3 +1385,50 @@ export async function runTaskAndGetResult(task: vscode.Task): Promise<number | u
13851385
return Promise.reject(reason);
13861386
});
13871387
}
1388+
1389+
/**
1390+
*
1391+
* Search for a task of a given name, or create one if no matching existing task is found.
1392+
*
1393+
* @param taskPlainName - plain name of the task (without the task type prefix)
1394+
* @param taskDef - callback to create a task definition if a matching existing task is not found
1395+
* @param taskType - type identifier of the task
1396+
* @returns an existing task with a matching name if found, otherwise creates a
1397+
* task of that name using the callback to create the definition
1398+
*/
1399+
export async function getOrCreateTask(
1400+
taskPlainName: string,
1401+
taskDef: () => Promise<vscode.TaskDefinition>,
1402+
taskType = TASK_TYPE_ADA,
1403+
): Promise<vscode.Task> {
1404+
const adaTP = adaExtState.getAdaTaskProvider();
1405+
assert(adaTP);
1406+
const existingTask = await findTaskByName(`${taskType}: ${taskPlainName}`).then(
1407+
undefined,
1408+
/**
1409+
* Return undefined in case of errors when searching for the task.
1410+
*/
1411+
() => undefined,
1412+
);
1413+
let task;
1414+
if (existingTask) {
1415+
task = existingTask;
1416+
} else {
1417+
/**
1418+
* If there's no existing task of that name, create one on the fly.
1419+
*/
1420+
task = (await adaTP.resolveTask(
1421+
new vscode.Task(
1422+
await taskDef(),
1423+
vscode.TaskScope.Workspace,
1424+
taskPlainName,
1425+
taskType,
1426+
undefined,
1427+
DEFAULT_PROBLEM_MATCHERS,
1428+
),
1429+
))!;
1430+
task.presentationOptions.reveal =
1431+
task.presentationOptions.reveal ?? vscode.TaskRevealKind.Never;
1432+
}
1433+
return task;
1434+
}

0 commit comments

Comments
 (0)