4
4
import * as fse from "fs-extra" ;
5
5
import * as _ from "lodash" ;
6
6
import * as minimatch from "minimatch" ;
7
+ import { platform } from "os" ;
7
8
import * as path from "path" ;
8
9
import { Disposable , ExtensionContext , Uri , window , workspace , WorkspaceFolder } from "vscode" ;
9
10
import { instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-wrapper" ;
@@ -20,6 +21,7 @@ export class LibraryController implements Disposable {
20
21
public constructor ( public readonly context : ExtensionContext ) {
21
22
this . disposable = Disposable . from (
22
23
instrumentOperationAsVsCodeCommand ( Commands . JAVA_PROJECT_ADD_LIBRARIES , ( ) => this . addLibraries ( ) ) ,
24
+ instrumentOperationAsVsCodeCommand ( Commands . JAVA_PROJECT_ADD_LIBRARY_FOLDERS , ( ) => this . addLibraries ( true ) ) ,
23
25
instrumentOperationAsVsCodeCommand ( Commands . JAVA_PROJECT_REMOVE_LIBRARY , ( node : DataNode ) =>
24
26
node . uri && this . removeLibrary ( Uri . parse ( node . uri ) . fsPath ) ) ,
25
27
instrumentOperationAsVsCodeCommand ( Commands . JAVA_PROJECT_REFRESH_LIBRARIES , ( ) =>
@@ -31,36 +33,36 @@ export class LibraryController implements Disposable {
31
33
this . disposable . dispose ( ) ;
32
34
}
33
35
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 [ ] ) {
58
37
const setting = Settings . referencedLibraries ( ) ;
59
38
setting . exclude = this . dedupAlreadyCoveredPattern ( libraryGlobs , ...setting . exclude ) ;
60
39
setting . include = this . updatePatternArray ( setting . include , ...libraryGlobs ) ;
61
40
Settings . updateReferencedLibraries ( setting ) ;
62
41
}
63
42
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
+
64
66
public async removeLibrary ( removalFsPath : string ) {
65
67
const setting = Settings . referencedLibraries ( ) ;
66
68
const removedPaths = _ . remove ( setting . include , ( include ) => {
0 commit comments