Skip to content

Commit 11312ed

Browse files
authored
Merge pull request #32 from badsyntax/fix-gradle-stdout-parsing
Fix gradle tasks parsing. Fixes #19
2 parents 514b3ce + 3c79828 commit 11312ed

File tree

7 files changed

+96
-15
lines changed

7 files changed

+96
-15
lines changed

.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@
9595
],
9696
"outFiles": ["${workspaceFolder}/out/test/**/*.js"],
9797
"preLaunchTask": "npm: watch"
98+
},
99+
{
100+
"name": "Test: Unit",
101+
"type": "extensionHost",
102+
"request": "launch",
103+
"runtimeExecutable": "${execPath}",
104+
"args": [
105+
"--disable-extensions",
106+
"--extensionDevelopmentPath=${workspaceFolder}",
107+
"--extensionTestsPath=${workspaceFolder}/out/test/unit/",
108+
"${workspaceFolder}/test-fixtures/gradle-groovy-default-build-file"
109+
],
110+
"outFiles": ["${workspaceFolder}/out/test/**/*.js"],
111+
"preLaunchTask": "npm: watch"
98112
}
99113
]
100114
}

src/extension.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,7 @@ export async function activate(
113113
): Promise<ExtensionApi> {
114114
registerTaskProvider(context);
115115
treeDataProvider = registerExplorer(context);
116-
const hasBuildFile = await hasGradleBuildFile();
117-
if (!hasBuildFile) {
118-
window.showWarningMessage('No gradle build file found');
119-
} else if (getIsTasksExplorerEnabled()) {
116+
if ((await hasGradleBuildFile()) && getIsTasksExplorerEnabled()) {
120117
commands.executeCommand('setContext', 'gradle:showTasksExplorer', true);
121118
}
122119
return {};

src/tasks.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,23 +334,18 @@ async function exists(file: string): Promise<boolean> {
334334

335335
type StringMap = { [s: string]: string };
336336

337-
const TASK_REGEX: RegExp = /$\s*([a-z0-9]+)(\s-\s(.*))?$/gim;
337+
const TASK_REGEX: RegExp = /$\s*([a-z]+[A-Z0-9]?[a-z0-9]*[A-Za-z0-9]*)(\s-\s(.*))?/gm;
338338

339-
function parseGradleTasks(buffer: Buffer | string): StringMap {
339+
export function parseGradleTasks(buffer: Buffer | string): StringMap {
340340
const tasks: StringMap = {};
341341
let match: RegExpExecArray | null = null;
342342
while ((match = TASK_REGEX.exec(buffer.toString())) !== null) {
343-
const [, name, description] = match;
343+
const [, name, , description] = match;
344344
tasks[name] = description;
345345
}
346346
return tasks;
347347
}
348348

349-
interface ProcessOutput {
350-
readonly stdout: string | Buffer;
351-
readonly stderr: string | Buffer;
352-
}
353-
354349
function spawn(
355350
command: string,
356351
args?: ReadonlyArray<string>,

src/test/runTest.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ import { runTests } from 'vscode-test';
44

55
const extensionDevelopmentPath = path.resolve(__dirname, '../../');
66

7+
async function runUnitTests() {
8+
await runTests({
9+
extensionDevelopmentPath,
10+
extensionTestsPath: path.resolve(__dirname, 'unit'),
11+
launchArgs: [
12+
path.resolve(
13+
__dirname,
14+
'../../test-fixtures/gradle-groovy-default-build-file'
15+
),
16+
'--disable-extensions'
17+
]
18+
});
19+
}
20+
721
async function runTestsWithGradle() {
822
const fixtures = [
923
'gradle-groovy-default-build-file',
@@ -13,7 +27,7 @@ async function runTestsWithGradle() {
1327
for (const fixture of fixtures) {
1428
await runTests({
1529
extensionDevelopmentPath,
16-
extensionTestsPath: path.resolve(__dirname, './gradle'),
30+
extensionTestsPath: path.resolve(__dirname, 'gradle'),
1731
launchArgs: [
1832
path.resolve(__dirname, `../../test-fixtures/${fixture}`),
1933
'--disable-extensions'
@@ -28,7 +42,7 @@ async function runTestsWithGradle() {
2842
async function runTestsWithoutGradle() {
2943
await runTests({
3044
extensionDevelopmentPath,
31-
extensionTestsPath: path.resolve(__dirname, './no-gradle'),
45+
extensionTestsPath: path.resolve(__dirname, 'no-gradle'),
3246
launchArgs: [
3347
path.resolve(__dirname, '../../test-fixtures/no-gradle'),
3448
'--disable-extensions'
@@ -39,7 +53,7 @@ async function runTestsWithoutGradle() {
3953
async function runTestsWithMultiRoot() {
4054
await runTests({
4155
extensionDevelopmentPath,
42-
extensionTestsPath: path.resolve(__dirname, './multi-root'),
56+
extensionTestsPath: path.resolve(__dirname, 'multi-root'),
4357
launchArgs: [
4458
path.resolve(
4559
__dirname,
@@ -55,6 +69,7 @@ async function runTestsWithMultiRoot() {
5569

5670
async function main() {
5771
try {
72+
await runUnitTests();
5873
await runTestsWithGradle();
5974
await runTestsWithMultiRoot();
6075
await runTestsWithoutGradle();

src/test/unit/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { createTestRunner } from '../testUtil';
2+
3+
export const run = createTestRunner('**/**.test.js');

src/test/unit/tasks.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
import * as assert from 'assert';
4+
import * as vscode from 'vscode';
5+
6+
import { parseGradleTasks } from '../../tasks';
7+
8+
suite('tasks', () => {
9+
suite('gradle tasks output parsing', () => {
10+
test('should correctly parse tasks', async () => {
11+
const stdout = fs.readFileSync(
12+
path.resolve(
13+
__dirname,
14+
'../../../test-fixtures/unit/gradle-tasks-stdout'
15+
),
16+
'utf8'
17+
);
18+
const tasks = parseGradleTasks(stdout);
19+
20+
assert.equal(Object.keys(tasks).length, 4);
21+
assert.equal('help' in tasks, true);
22+
assert.equal('run' in tasks, true);
23+
assert.equal('runShadow' in tasks, true);
24+
assert.equal('startShadowScripts' in tasks, true);
25+
26+
assert.equal(tasks.help, undefined);
27+
assert.equal(tasks.run, 'Runs a task');
28+
assert.equal(tasks.runShadow, 'Runs a shadow task');
29+
assert.equal(tasks.startShadowScripts, 'Starts a shadow script');
30+
});
31+
});
32+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Starting a Gradle Daemon, 12 busy Daemons could not be reused, use --status for details
2+
3+
> Task :tasks
4+
5+
------------------------------------------------------------
6+
Tasks runnable from root project
7+
------------------------------------------------------------
8+
9+
Group Description
10+
-----------------
11+
run - Runs a task
12+
runShadow - Runs a shadow task
13+
startShadowScripts - Starts a shadow script
14+
help
15+
16+
Rules
17+
-----
18+
Pattern: clean<TaskName>: Cleans the output files of a task.
19+
20+
To see all tasks and more detail, run gradlew tasks --all
21+
22+
To see more detail about a task, run gradlew help --task <task>
23+
24+
BUILD SUCCESSFUL in 5s
25+
1 actionable task: 1 executed

0 commit comments

Comments
 (0)