Skip to content

Commit

Permalink
Get project type by its natures (#1282)
Browse files Browse the repository at this point in the history
Signed-off-by: Sheng Chen <sheche@microsoft.com>
  • Loading branch information
jdneo authored Jan 29, 2024
1 parent 7c9fc75 commit 4f23b6d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/classpath/classpathConfigurationView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import compareVersions from "compare-versions";
let classpathConfigurationPanel: vscode.WebviewPanel | undefined;
let lsApi: LanguageServerAPI | undefined;
let currentProjectRoot: vscode.Uri;
const Nature_IDS: string = "org.eclipse.jdt.ls.core.natureIds"
const SOURCE_PATH_KEY: string = "org.eclipse.jdt.ls.core.sourcePaths";
const OUTPUT_PATH_KEY: string = "org.eclipse.jdt.ls.core.outputPath";
const VM_LOCATION_KEY: string = "org.eclipse.jdt.ls.core.vm.location";
Expand Down Expand Up @@ -490,6 +491,7 @@ async function getVmInstallsFromLS(): Promise<VmInstall[]> {

async function getProjectClasspathFromLS(uri: vscode.Uri): Promise<ClasspathComponent> {
const queryKeys: string[] = [
Nature_IDS,
SOURCE_PATH_KEY,
OUTPUT_PATH_KEY,
VM_LOCATION_KEY,
Expand All @@ -501,7 +503,7 @@ async function getProjectClasspathFromLS(uri: vscode.Uri): Promise<ClasspathComp
queryKeys
);
const classpath: ClasspathComponent = {
projectType: await getProjectType(uri.fsPath),
projectType: getProjectType(uri.fsPath, response[Nature_IDS] as string[]),
sourcePaths: response[SOURCE_PATH_KEY] as string[],
defaultOutputPath: response[OUTPUT_PATH_KEY] as string,
jdkPath: response[VM_LOCATION_KEY] as string,
Expand Down
10 changes: 7 additions & 3 deletions src/java-runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ async function getProjectRuntimesFromPM(): Promise<ProjectRuntimeEntry[]> {

for (const project of projects) {
const runtimeSpec = await getRuntimeSpec(project.uri);
const projectType: ProjectType = await getProjectType(vscode.Uri.parse(project.uri).fsPath);
const projectType: ProjectType = getProjectType(vscode.Uri.parse(project.uri).fsPath, runtimeSpec.natureIds);
ret.push({
name: project.displayName || project.name,
rootPath: project.uri,
Expand All @@ -293,7 +293,7 @@ async function getProjectRuntimesFromLS(): Promise<ProjectRuntimeEntry[]> {

for (const projectRoot of projects) {
const runtimeSpec = await getRuntimeSpec(projectRoot);
const projectType: ProjectType = await getProjectType(vscode.Uri.parse(projectRoot).fsPath);
const projectType: ProjectType = await getProjectType(vscode.Uri.parse(projectRoot).fsPath, runtimeSpec.natureIds);
ret.push({
name: getProjectNameFromUri(projectRoot),
rootPath: projectRoot,
Expand All @@ -306,14 +306,17 @@ async function getProjectRuntimesFromLS(): Promise<ProjectRuntimeEntry[]> {
}

async function getRuntimeSpec(projectRootUri: string) {
let natureIds;
let runtimePath;
let sourceLevel;
const javaExt = vscode.extensions.getExtension("redhat.java");
if (javaExt && javaExt.isActive) {
const NATURE_IDS = "org.eclipse.jdt.ls.core.natureIds";
const SOURCE_LEVEL_KEY = "org.eclipse.jdt.core.compiler.source";
const VM_INSTALL_PATH = "org.eclipse.jdt.ls.core.vm.location";
try {
const settings: any = await javaExt.exports.getProjectSettings(projectRootUri, [SOURCE_LEVEL_KEY, VM_INSTALL_PATH]);
const settings: any = await javaExt.exports.getProjectSettings(projectRootUri, [NATURE_IDS, SOURCE_LEVEL_KEY, VM_INSTALL_PATH]);
natureIds = settings[NATURE_IDS];
runtimePath = settings[VM_INSTALL_PATH];
sourceLevel = settings[SOURCE_LEVEL_KEY];
} catch (error) {
Expand All @@ -322,6 +325,7 @@ async function getRuntimeSpec(projectRootUri: string) {
}

return {
natureIds,
runtimePath,
sourceLevel
};
Expand Down
15 changes: 6 additions & 9 deletions src/utils/jdt.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import * as fse from "fs-extra";
import * as path from "path";
import { ProjectType } from "./webview";
import { NatureId, ProjectType } from "./webview";
import * as vscode from "vscode";

export async function getProjectType(fsPath: string): Promise<ProjectType> {
export function getProjectType(fsPath: string, natureIds: string[]): ProjectType {
if (isDefaultProject(fsPath)) {
return ProjectType.Default;
}
const buildDotGradleFile = path.join(fsPath, "build.gradle");
if (await fse.pathExists(buildDotGradleFile)) {

if (natureIds.includes(NatureId.Gradle) || natureIds.includes(NatureId.GradleBs)) {
return ProjectType.Gradle;
}
const pomDotXmlFile = path.join(fsPath, "pom.xml");
if (await fse.pathExists(pomDotXmlFile)) {
if (natureIds.includes(NatureId.Maven)){
return ProjectType.Maven;
}
const dotProjectFile = path.join(fsPath, ".project");
if (!await fse.pathExists(dotProjectFile)) { // for invisible projects, .project file is located in workspace storage.
if (natureIds.includes(NatureId.UnmanagedFolder)) {
return ProjectType.UnmanagedFolder;
}
return ProjectType.Others;
Expand Down
3 changes: 2 additions & 1 deletion src/utils/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ export enum ProjectType {
export enum NatureId {
Maven = "org.eclipse.m2e.core.maven2Nature",
Gradle = "org.eclipse.buildship.core.gradleprojectnature",
Java = "org.eclipse.jdt.core.javanature",
GradleBs = "com.microsoft.gradle.bs.importer.GradleBuildServerProjectNature",
UnmanagedFolder = "org.eclipse.jdt.ls.unmanagedFolderNature",
}

0 comments on commit 4f23b6d

Please sign in to comment.