44import * as fse from "fs-extra" ;
55import * as _ from "lodash" ;
66import * as minimatch from "minimatch" ;
7+ import { platform } from "os" ;
78import * as path from "path" ;
89import { Disposable , ExtensionContext , Uri , window , workspace , WorkspaceFolder } from "vscode" ;
910import { instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-wrapper" ;
@@ -20,6 +21,7 @@ export class LibraryController implements Disposable {
2021 public constructor ( public readonly context : ExtensionContext ) {
2122 this . disposable = Disposable . from (
2223 instrumentOperationAsVsCodeCommand ( Commands . JAVA_PROJECT_ADD_LIBRARIES , ( ) => this . addLibraries ( ) ) ,
24+ instrumentOperationAsVsCodeCommand ( Commands . JAVA_PROJECT_ADD_LIBRARY_FOLDERS , ( ) => this . addLibraries ( true ) ) ,
2325 instrumentOperationAsVsCodeCommand ( Commands . JAVA_PROJECT_REMOVE_LIBRARY , ( node : DataNode ) =>
2426 node . uri && this . removeLibrary ( Uri . parse ( node . uri ) . fsPath ) ) ,
2527 instrumentOperationAsVsCodeCommand ( Commands . JAVA_PROJECT_REFRESH_LIBRARIES , ( ) =>
@@ -31,36 +33,36 @@ export class LibraryController implements Disposable {
3133 this . disposable . dispose ( ) ;
3234 }
3335
34- public async addLibraries ( libraryGlobs ?: string [ ] ) {
35- if ( ! libraryGlobs ) {
36- libraryGlobs = [ ] ;
37- const workspaceFolder : WorkspaceFolder | undefined = Utility . getDefaultWorkspaceFolder ( ) ;
38- const isWindows = process . platform . indexOf ( "win" ) === 0 ;
39- const results : Uri [ ] | undefined = await window . showOpenDialog ( {
40- defaultUri : workspaceFolder && workspaceFolder . uri ,
41- canSelectFiles : true ,
42- canSelectFolders : isWindows ? false : true ,
43- canSelectMany : true ,
44- openLabel : isWindows ? "Select jar files" : "Select jar files or directories" ,
45- filters : { Library : [ "jar" ] } ,
46- } ) ;
47- if ( ! results ) {
48- return ;
49- }
50- libraryGlobs = await Promise . all ( results . map ( async ( uri : Uri ) => {
51- // keep the param: `includeWorkspaceFolder` to false here
52- // since the multi-root is not supported well for invisible projects
53- const uriPath = workspace . asRelativePath ( uri , false ) ;
54- return ( await fse . stat ( uri . fsPath ) ) . isDirectory ( ) ? `${ uriPath } /**/*.jar` : uriPath ;
55- } ) ) ;
56- }
57-
36+ public async addLibraryGlobs ( libraryGlobs : string [ ] ) {
5837 const setting = Settings . referencedLibraries ( ) ;
5938 setting . exclude = this . dedupAlreadyCoveredPattern ( libraryGlobs , ...setting . exclude ) ;
6039 setting . include = this . updatePatternArray ( setting . include , ...libraryGlobs ) ;
6140 Settings . updateReferencedLibraries ( setting ) ;
6241 }
6342
43+ public async addLibraries ( canSelectFolders ?: boolean ) {
44+ const workspaceFolder : WorkspaceFolder | undefined = Utility . getDefaultWorkspaceFolder ( ) ;
45+ const isMac = platform ( ) === "darwin" ;
46+ const results : Uri [ ] | undefined = await window . showOpenDialog ( {
47+ defaultUri : workspaceFolder && workspaceFolder . uri ,
48+ canSelectFiles : ! canSelectFolders ,
49+ canSelectFolders : canSelectFolders || isMac ,
50+ canSelectMany : true ,
51+ openLabel : canSelectFolders ? "Select Library Folders" : "Select Jar Libraries" ,
52+ filters : canSelectFolders ? { Folders : [ "*" ] } : { "Jar Files" : [ "jar" ] } ,
53+ } ) ;
54+ if ( ! results ) {
55+ return ;
56+ }
57+ this . addLibraryGlobs ( await Promise . all ( results . map ( async ( uri : Uri ) => {
58+ // keep the param: `includeWorkspaceFolder` to false here
59+ // since the multi-root is not supported well for invisible projects
60+ const uriPath = workspace . asRelativePath ( uri , false ) ;
61+ const isLibraryFolder = canSelectFolders || isMac && ( await fse . stat ( uri . fsPath ) ) . isDirectory ( ) ;
62+ return isLibraryFolder ? uriPath + "/**/*.jar" : uriPath ;
63+ } ) ) ) ;
64+ }
65+
6466 public async removeLibrary ( removalFsPath : string ) {
6567 const setting = Settings . referencedLibraries ( ) ;
6668 const removedPaths = _ . remove ( setting . include , ( include ) => {
0 commit comments