Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faet: Add dependency to Maven projects #230

Merged
merged 1 commit into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@
"light": "images/light/icon-add.svg"
}
},
{
"command": "java.project.maven.addDependency",
"title": "%contributes.commands.java.project.maven.addDependency%",
"category": "Java",
"icon": {
"dark": "images/dark/icon-add.svg",
"light": "images/light/icon-add.svg"
}
},
{
"command": "java.project.removeLibrary",
"title": "%contributes.commands.java.project.removeLibrary%",
Expand Down Expand Up @@ -176,6 +185,14 @@
"command": "java.view.package.copyRelativeFilePath",
"when": "never"
},
{
"command": "java.project.addLibraries",
"when": "never"
},
{
"command": "java.project.maven.addDependency",
"when": "never"
},
{
"command": "java.project.removeLibrary",
"when": "never"
Expand Down Expand Up @@ -242,6 +259,11 @@
"command": "java.project.refreshLibraries",
"when": "view == javaDependencyExplorer && viewItem =~ /java:container\/referenced-libraries$/",
"group": "inline@1"
},
{
"command": "java.project.maven.addDependency",
"when": "view == javaDependencyExplorer && viewItem =~ /container\/maven-dependencies/",
"group": "inline@0"
}
]
},
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"description": "Manage Java Dependencies in VSCode",
"contributes.commands.java.project.create": "Create Java Project",
"contributes.commands.java.project.addLibraries": "Add a jar file or a folder to project classpath",
"contributes.commands.java.project.maven.addDependency": "Add a new dependency to the Maven project",
"contributes.commands.java.project.removeLibrary": "Remove jar file from project classpath",
"contributes.commands.java.view.package.refresh": "Refresh",
"contributes.commands.java.view.package.changeRepresentation": "Change package representation",
Expand Down
1 change: 1 addition & 0 deletions package.nls.zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"description": "在 VSCode 中管理 Java 依赖项",
"contributes.commands.java.project.create": "创建 Java 项目",
"contributes.commands.java.project.addLibraries": "将一个 Jar 文件或一个目录添加到 Java 项目类路径中",
"contributes.commands.java.project.maven.addDependency": "为该 Maven 项目增加依赖库",
"contributes.commands.java.project.removeLibrary": "将该 Jar 文件从 Java 项目类路径中移除",
"contributes.commands.java.view.package.refresh": "刷新",
"contributes.commands.java.view.package.changeRepresentation": "更改包展示形式",
Expand Down
2 changes: 2 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export namespace Commands {

export const JAVA_PROJECT_REFRESH_LIBRARIES = "java.project.refreshLibraries";

export const JAVA_MAVEN_PROJECT_ADD_DEPENDENCY = "java.project.maven.addDependency";

export const JAVA_PROJECT_LIST = "java.project.list";

export const JAVA_PROJECT_REFRESH_LIB_SERVER = "java.project.refreshLib";
Expand Down
14 changes: 13 additions & 1 deletion src/controllers/libraryController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import * as fse from "fs-extra";
import * as _ from "lodash";
import * as minimatch from "minimatch";
import * as path from "path";
import { Disposable, ExtensionContext, Uri, window, workspace, WorkspaceFolder } from "vscode";
import { commands, Disposable, ExtensionContext, Uri, window, workspace, WorkspaceFolder } from "vscode";
import { instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-wrapper";
import { Commands } from "../commands";
import { Jdtls } from "../java/jdtls";
import { Settings } from "../settings";
import { Utility } from "../utility";
import { ContainerNode } from "../views/containerNode";
import { DataNode } from "../views/dataNode";

export class LibraryController implements Disposable {
Expand All @@ -24,13 +25,24 @@ export class LibraryController implements Disposable {
this.removeLibrary(Uri.parse(node.uri).fsPath)),
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_REFRESH_LIBRARIES, () =>
this.refreshLibraries()),
instrumentOperationAsVsCodeCommand(Commands.JAVA_MAVEN_PROJECT_ADD_DEPENDENCY, (node: ContainerNode) =>
this.addMavenDependency(node)),
);
}

public dispose() {
this.disposable.dispose();
}

public async addMavenDependency(node: ContainerNode) {
const pomPath: string = path.join(node.projectBasePath, "pom.xml");
if (await fse.pathExists(pomPath)) {
commands.executeCommand("maven.project.addDependency", { pomPath });
} else {
commands.executeCommand("maven.project.addDependency");
}
}

public async addLibraries(libraryGlobs?: string[]) {
if (!libraryGlobs) {
libraryGlobs = [];
Expand Down
5 changes: 5 additions & 0 deletions src/views/containerNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import { Uri } from "vscode";
import { Jdtls } from "../java/jdtls";
import { INodeData, NodeKind } from "../java/nodeData";
import { DataNode } from "./dataNode";
Expand All @@ -13,6 +14,10 @@ export class ContainerNode extends DataNode {
super(nodeData, parent);
}

public get projectBasePath() {
return Uri.parse(this._project.uri).fsPath;
}

protected loadData(): Thenable<INodeData[]> {
return Jdtls.getPackageData({ kind: NodeKind.Container, projectUri: this._project.uri, path: this.path });
}
Expand Down