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

Fix add libraries dialog not working on Linux #434

Merged
merged 7 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
"category": "Java",
"icon": "$(add)"
},
{
"command": "java.project.addLibraryFolders",
"title": "%contributes.commands.java.project.addLibraryFolders%",
"category": "Java",
"icon": "$(new-folder)"
},
{
"command": "java.project.removeLibrary",
"title": "%contributes.commands.java.project.removeLibrary%",
Expand Down Expand Up @@ -285,6 +291,10 @@
"command": "java.project.addLibraries",
"when": "false"
},
{
"command": "java.project.addLibraryFolders",
"when": "false"
},
{
"command": "java.project.removeLibrary",
"when": "false"
Expand Down Expand Up @@ -465,6 +475,7 @@
},
{
"command": "java.project.addLibraries",
"alt": "java.project.addLibraryFolders",
"when": "view == javaProjectExplorer && viewItem =~ /java:container(?=.*?\\b\\+referencedLibrary\\b)/",
"group": "inline@0"
},
Expand Down
5 changes: 3 additions & 2 deletions package.nls.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"description": "Manage Java projects in Visual Studio Code",
"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.removeLibrary": "Remove jar file from project classpath",
"contributes.commands.java.project.addLibraries": "Add Jar Libraries to Project Classpath...",
"contributes.commands.java.project.addLibraryFolders": "Add Library Folders to Project Classpath...",
"contributes.commands.java.project.removeLibrary": "Remove Library from Project Classpath",
"contributes.commands.java.view.package.refresh": "Refresh",
"contributes.commands.java.project.build.workspace": "Build Workspace",
"contributes.commands.java.project.clean.workspace": "Clean Workspace",
Expand Down
5 changes: 3 additions & 2 deletions package.nls.zh.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"description": "在 Visual Studio Code 中管理 Java 项目",
"contributes.commands.java.project.create": "创建 Java 项目...",
"contributes.commands.java.project.addLibraries": "将一个 Jar 文件或一个目录添加到 Java 项目类路径中",
"contributes.commands.java.project.removeLibrary": "将该 Jar 文件从 Java 项目类路径中移除",
"contributes.commands.java.project.addLibraries": "TRANSLATION NEEDED...",
"contributes.commands.java.project.addLibraryFolders": "TRANSLATION NEEDED...",
"contributes.commands.java.project.removeLibrary": "TRANSLATION NEEDED? (English wording changed slightly, see previous commit)",
"contributes.commands.java.view.package.refresh": "刷新",
"contributes.commands.java.project.build.workspace": "构建工作空间",
"contributes.commands.java.project.clean.workspace": "清理工作空间",
Expand Down
2 changes: 2 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export namespace Commands {

export const JAVA_PROJECT_ADD_LIBRARIES = "java.project.addLibraries";

export const JAVA_PROJECT_ADD_LIBRARY_FOLDERS = "java.project.addLibraryFolders";

export const JAVA_PROJECT_REMOVE_LIBRARY = "java.project.removeLibrary";

export const JAVA_PROJECT_REFRESH_LIBRARIES = "java.project.refreshLibraries";
Expand Down
50 changes: 25 additions & 25 deletions src/controllers/libraryController.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import * as fse from "fs-extra";
import * as _ from "lodash";
import * as minimatch from "minimatch";
import { platform } from "os";
import * as path from "path";
import { Disposable, ExtensionContext, Uri, window, workspace, WorkspaceFolder } from "vscode";
import { instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-wrapper";
Expand All @@ -20,6 +20,7 @@ export class LibraryController implements Disposable {
public constructor(public readonly context: ExtensionContext) {
this.disposable = Disposable.from(
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_ADD_LIBRARIES, () => this.addLibraries()),
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_ADD_LIBRARY_FOLDERS, () => this.addLibraries(true)),
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_REMOVE_LIBRARY, (node: DataNode) =>
node.uri && this.removeLibrary(Uri.parse(node.uri).fsPath)),
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_REFRESH_LIBRARIES, () =>
Expand All @@ -31,36 +32,35 @@ export class LibraryController implements Disposable {
this.disposable.dispose();
}

public async addLibraries(libraryGlobs?: string[]) {
if (!libraryGlobs) {
libraryGlobs = [];
const workspaceFolder: WorkspaceFolder | undefined = Utility.getDefaultWorkspaceFolder();
const isWindows = process.platform.indexOf("win") === 0;
const results: Uri[] | undefined = await window.showOpenDialog({
defaultUri: workspaceFolder && workspaceFolder.uri,
canSelectFiles: true,
canSelectFolders: isWindows ? false : true,
canSelectMany: true,
openLabel: isWindows ? "Select jar files" : "Select jar files or directories",
filters: { Library: ["jar"] },
});
if (!results) {
return;
}
libraryGlobs = await Promise.all(results.map(async (uri: Uri) => {
// keep the param: `includeWorkspaceFolder` to false here
// since the multi-root is not supported well for invisible projects
const uriPath = workspace.asRelativePath(uri, false);
return (await fse.stat(uri.fsPath)).isDirectory() ? `${uriPath}/**/*.jar` : uriPath;
}));
}

public async addLibraryGlobs(libraryGlobs: string[]) {
const setting = Settings.referencedLibraries();
setting.exclude = this.dedupAlreadyCoveredPattern(libraryGlobs, ...setting.exclude);
setting.include = this.updatePatternArray(setting.include, ...libraryGlobs);
Settings.updateReferencedLibraries(setting);
}

public async addLibraries(canSelectFolders?: boolean) {
const workspaceFolder: WorkspaceFolder | undefined = Utility.getDefaultWorkspaceFolder();
const isMac = platform() === "darwin";
const results: Uri[] | undefined = await window.showOpenDialog({
defaultUri: workspaceFolder && workspaceFolder.uri,
canSelectFiles: !canSelectFolders,
canSelectFolders: canSelectFolders || isMac,
canSelectMany: true,
openLabel: canSelectFolders ? "Select Library Folders" : "Select Jar Libraries",
filters: canSelectFolders ? { Folders: ["*"] } : { "Jar Files": ["jar"] },
});
if (!results) {
return;
}
this.addLibraryGlobs(await Promise.all(results.map(async (uri: Uri) => {
// keep the param: `includeWorkspaceFolder` to false here
// since the multi-root is not supported well for invisible projects
const uriPath = workspace.asRelativePath(uri, false);
return canSelectFolders ? uriPath + "/**/*.jar" : uriPath;
Copy link
Member

@jdneo jdneo Jan 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When it's on MacOS, and triggered from addLibraries (canSelectFolders is undefined), if the user selects a folder, then it will not append /**/*.jar.

So maybe we still need the previous logic -- check if it is a folder or not and return different value accordingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I missed that since I don't have a mac to test on, but I'll add the old logic back.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2197367 (although I can't test if it works, since I don't have a mac).

})));
}

public async removeLibrary(removalFsPath: string) {
const setting = Settings.referencedLibraries();
const removedPaths = _.remove(setting.include, (include) => {
Expand Down