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 all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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 @@ -286,6 +292,10 @@
"command": "java.project.addLibraries",
"when": "false"
},
{
"command": "java.project.addLibraryFolders",
"when": "false"
},
{
"command": "java.project.removeLibrary",
"when": "false"
Expand Down Expand Up @@ -466,6 +476,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 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": "添加 Jar 文件至项目 Classpath...",
"contributes.commands.java.project.addLibraryFolders": "添加文件夹至项目 Classpath...",
"contributes.commands.java.project.removeLibrary": "从项目 Classpath 中移除",
"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: 26 additions & 24 deletions src/controllers/libraryController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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 +21,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 +33,36 @@ 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);
const isLibraryFolder = canSelectFolders || isMac && (await fse.stat(uri.fsPath)).isDirectory();
return isLibraryFolder ? uriPath + "/**/*.jar" : uriPath;
})));
}

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