Skip to content

Commit

Permalink
fix: Gradle daemon view shows nothing when using local Gradle install…
Browse files Browse the repository at this point in the history
…ation (#957)
  • Loading branch information
CsCherrYY authored Aug 26, 2021
1 parent 091ed46 commit 01794c6
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 37 deletions.
1 change: 1 addition & 0 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@
},
{
"command": "gradle.stopDaemons",
"enablement": "!gradle:specificGradleVersion",
"title": "Stop Daemons",
"icon": {
"light": "resources/light/stop-daemons.svg",
Expand Down
10 changes: 10 additions & 0 deletions extension/src/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { FileWatcher } from './util/FileWatcher';
import { DependencyTreeItem } from './views/gradleTasks/DependencyTreeItem';
import { GRADLE_DEPENDENCY_REVEAL } from './views/gradleTasks/DependencyUtils';
import { GradleDependencyProvider } from './dependencies/GradleDependencyProvider';
import { getSpecificVersionStatus } from './views/gradleDaemons/util';

export class Extension {
private readonly client: GradleClient;
Expand Down Expand Up @@ -321,6 +322,15 @@ export class Extension {
Logger.setLogVerbosity(
debug ? LogVerbosity.DEBUG : LogVerbosity.INFO
);
} else if (
event.affectsConfiguration('java.import.gradle.home') ||
event.affectsConfiguration('java.import.gradle.version') ||
event.affectsConfiguration('java.import.gradle.wrapper.enabled')
) {
await this.refresh();
void this.gradleDaemonsTreeDataProvider.setSpecificVersion(
getSpecificVersionStatus()
);
}
}
),
Expand Down
31 changes: 29 additions & 2 deletions extension/src/views/gradleDaemons/GradleDaemonsTreeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import {
setShowStoppedDaemons,
} from '../../util/config';
import { Deferred } from '../../util/Deferred';
import { HintItem } from '../gradleTasks/HintItem';
import { getSpecificVersionStatus } from './util';

export class GradleDaemonsTreeDataProvider
implements vscode.TreeDataProvider<vscode.TreeItem> {
private cancelDeferred?: Deferred<vscode.TreeItem[]>;
private treeItems: vscode.TreeItem[] = [];
private specificVersion = false;
private readonly _onDidChangeTreeData: vscode.EventEmitter<vscode.TreeItem | null> = new vscode.EventEmitter<vscode.TreeItem | null>();
public readonly onDidChangeTreeData: vscode.Event<vscode.TreeItem | null> = this
._onDidChangeTreeData.event;
Expand All @@ -21,7 +24,22 @@ export class GradleDaemonsTreeDataProvider
private readonly context: vscode.ExtensionContext,
private readonly rootProjectsStore: RootProjectsStore,
private readonly client: GradleClient
) {}
) {
void this.setSpecificVersion(getSpecificVersionStatus());
}

public async setSpecificVersion(specificVersion: boolean): Promise<void> {
if (this.specificVersion === specificVersion) {
return;
}
this.specificVersion = specificVersion;
await vscode.commands.executeCommand(
'setContext',
'gradle:specificGradleVersion',
specificVersion
);
this.refresh();
}

public refresh(): void {
this.cancelDeferred?.resolve(this.treeItems);
Expand Down Expand Up @@ -75,7 +93,16 @@ export class GradleDaemonsTreeDataProvider
this.cancelDeferred.promise,
]);
this.cancelDeferred = undefined;
return this.treeItems;
if (this.treeItems.length) {
return this.treeItems;
}
return this.specificVersion
? [
new HintItem(
'Gradle Daemons view is not available when specifying a Gradle version'
),
]
: [new HintItem('No Gradle Daemons')];
}

private async getProjectRootFolders(): Promise<string[]> {
Expand Down
16 changes: 16 additions & 0 deletions extension/src/views/gradleDaemons/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import * as vscode from 'vscode';

export function getSpecificVersionStatus(): boolean {
const versionConfig = vscode.workspace
.getConfiguration('java')
.get<string | null>('import.gradle.version');
const wrapperConfig = vscode.workspace
.getConfiguration('java')
.get<boolean | undefined>('import.gradle.wrapper.enabled');
return (
wrapperConfig === false && versionConfig !== null && versionConfig !== ''
);
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package com.github.badsyntax.gradle;

import com.github.badsyntax.gradle.exceptions.GradleWrapperException;
import com.github.badsyntax.gradle.exceptions.GradleExecutionException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class DaemonStatus {
private GradleWrapper gradleWrapper;
private GradleExecution gradleExecution;

// 56783 IDLE 6.4
// 39762 STOPPED (other compatible daemons were started ...)
private static final Pattern STATUS_REGEX =
Pattern.compile("^\\s+([0-9]+)\\s+([A-Z]+)\\s+([\\p{ASCII}]+)$");

public DaemonStatus(GradleWrapper gradleWrapper) {
this.gradleWrapper = gradleWrapper;
public DaemonStatus(GradleExecution gradleExecution) {
this.gradleExecution = gradleExecution;
}

public synchronized List<DaemonInfo> get() throws GradleWrapperException {
public synchronized List<DaemonInfo> get() throws GradleExecutionException {
ArrayList<DaemonInfo> daemonStatus = new ArrayList<>();
String processOutput = gradleWrapper.exec("--status", "--quiet");
String processOutput = gradleExecution.exec("--status", "--quiet");
Stream.of(processOutput.split("\n"))
.forEach(
line -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*******************************************************************************
* Copyright (c) 2021 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/
package com.github.badsyntax.gradle;

import com.github.badsyntax.gradle.exceptions.GradleExecutionException;

public interface GradleExecution {
public String exec(String... args) throws GradleExecutionException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* Copyright (c) 2021 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/
package com.github.badsyntax.gradle;

import com.github.badsyntax.gradle.exceptions.GradleExecutionException;
import com.github.badsyntax.gradle.exceptions.ProcessException;
import com.github.badsyntax.gradle.process.Process;
import com.github.badsyntax.gradle.process.ProcessOutput;
import java.io.File;
import java.io.IOException;
import java.util.stream.Collectors;

public class GradleLocalInstallation implements GradleExecution {
private File localInstallation;
private static final String GRADLE_EXECUTION_UNIX = "gradle";
private static final String GRADLE_EXECUTION_WINDOWS = "gradle.bat";

public GradleLocalInstallation(File localInstallation) {
this.localInstallation = localInstallation;
}

public synchronized String exec(String... args) throws GradleExecutionException {
try {
if (args.length == 0) {
throw new GradleExecutionException("No gradle args supplied");
}
File binFolder = localInstallation.toPath().resolve("bin").toFile();
Process process = new Process(binFolder);
process.setUnixCommand(GRADLE_EXECUTION_UNIX);
process.setWindowsCommand(GRADLE_EXECUTION_WINDOWS);
process.exec(args);
ProcessOutput processOutput = process.getProcessOutput();
String stdErrString = processOutput.getStdErr().lines().collect(Collectors.joining("\n"));
String stdOutString = processOutput.getStdOut().lines().collect(Collectors.joining("\n"));
process.close();
if (stdErrString.length() > 0) {
throw new GradleExecutionException(String.format("Error running gradle: %s", stdErrString));
}
return stdOutString;
} catch (IOException | ProcessException e) {
throw new GradleExecutionException(String.format("Error running gradle: %s", e.getMessage()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*******************************************************************************
* Copyright (c) 2021 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/
package com.github.badsyntax.gradle;

public enum GradleProjectConnectionType {
WRAPPER,
SPECIFICVERSION,
LOCALINSTALLATION,
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class GradleProjectConnector {

private GradleProjectConnector() {}

private static GradleProjectConnectionType connectionType = GradleProjectConnectionType.WRAPPER;
private static String localInstallation;

public static GradleConnector build(String projectDir, GradleConfig config)
throws GradleConnectionException {
GradleConnector connector =
Expand All @@ -27,13 +30,18 @@ private static void setConnectorConfig(
}
if (!config.getWrapperEnabled()) {
if (!Strings.isNullOrEmpty(config.getVersion())) {
GradleProjectConnector.connectionType = GradleProjectConnectionType.SPECIFICVERSION;
gradleConnector.useGradleVersion(config.getVersion());
} else if (!Strings.isNullOrEmpty(config.getGradleHome())) {
gradleConnector.useInstallation(new File(config.getGradleHome()));
GradleProjectConnector.connectionType = GradleProjectConnectionType.LOCALINSTALLATION;
GradleProjectConnector.localInstallation = config.getGradleHome();
gradleConnector.useInstallation(new File(GradleProjectConnector.localInstallation));
} else {
throw new GradleConnectionException(
"java.import.gradle.home is invalid, please check it again.");
}
} else {
GradleProjectConnector.connectionType = GradleProjectConnectionType.WRAPPER;
}
}

Expand All @@ -44,4 +52,12 @@ private static File buildGradleUserHomeFile(String gradleUserHome, String projec
: Paths.get(projectDir, gradleUserHome).toAbsolutePath().toString();
return new File(gradleUserHomePath);
}

public static GradleProjectConnectionType getConnectionType() {
return GradleProjectConnector.connectionType;
}

public static String getLocalInstallation() {
return GradleProjectConnector.localInstallation;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.github.badsyntax.gradle;

import com.github.badsyntax.gradle.exceptions.GradleWrapperException;
import com.github.badsyntax.gradle.exceptions.GradleExecutionException;
import com.github.badsyntax.gradle.exceptions.ProcessException;
import com.github.badsyntax.gradle.process.Process;
import com.github.badsyntax.gradle.process.ProcessOutput;
import java.io.File;
import java.io.IOException;
import java.util.stream.Collectors;

public class GradleWrapper {
public class GradleWrapper implements GradleExecution {
private File projectRoot;
private static final String GRADLE_WRAPPER_UNIX = "gradlew";
private static final String GRADLE_WRAPPER_WINDOWS = "gradlew.bat";
Expand All @@ -17,10 +17,10 @@ public GradleWrapper(File projectRoot) {
this.projectRoot = projectRoot;
}

public synchronized String exec(String... args) throws GradleWrapperException {
public synchronized String exec(String... args) throws GradleExecutionException {
try {
if (args.length == 0) {
throw new GradleWrapperException("No wrapper args supplied");
throw new GradleExecutionException("No wrapper args supplied");
}
Process process = new Process(projectRoot);
process.setUnixCommand(GRADLE_WRAPPER_UNIX);
Expand All @@ -31,12 +31,12 @@ public synchronized String exec(String... args) throws GradleWrapperException {
String stdOutString = processOutput.getStdOut().lines().collect(Collectors.joining("\n"));
process.close();
if (stdErrString.length() > 0) {
throw new GradleWrapperException(
throw new GradleExecutionException(
String.format("Error running gradle wrapper: %s", stdErrString));
}
return stdOutString;
} catch (IOException | ProcessException e) {
throw new GradleWrapperException(
throw new GradleExecutionException(
String.format("Error running gradle wrapper: %s", e.getMessage()));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.badsyntax.gradle.exceptions;

public class GradleExecutionException extends Exception {
private static final long serialVersionUID = 1L;

public GradleExecutionException(String message) {
super(message);
}

public GradleExecutionException(String message, Throwable cause) {
super(message, cause);
}
}

This file was deleted.

Loading

0 comments on commit 01794c6

Please sign in to comment.