4
4
import * as fse from "fs-extra" ;
5
5
import * as _ from "lodash" ;
6
6
import * as path from "path" ;
7
- import { commands , Disposable , ExtensionContext , Uri , window , workspace } from "vscode" ;
7
+ import { commands , Disposable , ExtensionContext , QuickPickItem , Uri , window , workspace } from "vscode" ;
8
8
import { instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-wrapper" ;
9
- import * as xml2js from "xml2js" ;
10
9
import { Commands } from "../commands" ;
10
+ import { Context } from "../constants" ;
11
+ import { contextManager } from "../contextManager" ;
11
12
import { Utility } from "../utility" ;
12
13
13
14
export class ProjectController implements Disposable {
@@ -25,10 +26,40 @@ export class ProjectController implements Disposable {
25
26
}
26
27
27
28
public async createJavaProject ( ) {
28
- const javaVersion : number = await this . getJavaVersion ( ) ;
29
- if ( ! javaVersion ) {
29
+ const projectKinds : QuickPickItem [ ] = [ {
30
+ label : BuildTool . None ,
31
+ detail : "A project without any build tools" ,
32
+ } ] ;
33
+ if ( contextManager . getContextValue ( Context . MAVEN_ENABLED ) ) {
34
+ projectKinds . push ( {
35
+ label : BuildTool . Maven ,
36
+ detail : "Use Maven to manage your project" ,
37
+ } ) ;
38
+ }
39
+ const choice : QuickPickItem | undefined = projectKinds . length === 1 ? projectKinds [ 0 ] :
40
+ await window . showQuickPick ( projectKinds , {
41
+ ignoreFocusOut : true ,
42
+ placeHolder : "Select the project build tool" ,
43
+ } ,
44
+ ) ;
45
+
46
+ if ( ! choice ) {
30
47
return ;
31
48
}
49
+
50
+ switch ( choice . label ) {
51
+ case BuildTool . Maven :
52
+ await commands . executeCommand ( Commands . JAVA_MAVEN_CREATE_PROJECT ) ;
53
+ break ;
54
+ case BuildTool . None :
55
+ await this . scaffoldSimpleProject ( ) ;
56
+ break ;
57
+ default :
58
+ break ;
59
+ }
60
+ }
61
+
62
+ private async scaffoldSimpleProject ( ) : Promise < void > {
32
63
const workspaceFolder = Utility . getDefaultWorkspaceFolder ( ) ;
33
64
const location : Uri [ ] = await window . showOpenDialog ( {
34
65
defaultUri : workspaceFolder && workspaceFolder . uri ,
@@ -39,73 +70,36 @@ export class ProjectController implements Disposable {
39
70
if ( ! location || ! location . length ) {
40
71
return ;
41
72
}
73
+
42
74
const basePath : string = location [ 0 ] . fsPath ;
43
75
const projectName : string = await window . showInputBox ( {
44
76
prompt : "Input a java project name" ,
45
- validateInput : ( name : string ) : string => {
77
+ ignoreFocusOut : true ,
78
+ validateInput : async ( name : string ) : Promise < string > => {
46
79
if ( name && ! name . match ( / ^ [ ^ * ~ / \\ ] + $ / ) ) {
47
80
return "Please input a valid project name" ;
48
81
}
49
- if ( name && fse . pathExistsSync ( path . join ( basePath , name ) ) ) {
82
+ if ( name && await fse . pathExists ( path . join ( basePath , name ) ) ) {
50
83
return "A project with this name already exists." ;
51
84
}
52
- return null ;
85
+ return "" ;
53
86
} ,
54
87
} ) ;
55
- if ( ! projectName ) {
56
- return ;
57
- }
58
- if ( await this . scaffoldJavaProject ( basePath , projectName , javaVersion ) ) {
59
- const openInNewWindow = workspace && ! _ . isEmpty ( workspace . workspaceFolders ) ;
60
- return commands . executeCommand ( "vscode.openFolder" , Uri . file ( path . join ( basePath , projectName ) ) , openInNewWindow ) ;
61
- }
62
- }
63
-
64
- private async scaffoldJavaProject ( basePath : string , projectName : string , javaVersion : number ) : Promise < boolean > {
65
88
const projectRoot : string = path . join ( basePath , projectName ) ;
66
- const templateRoot : string = path . join ( this . context . extensionPath , "templates" ) ;
67
- const projectFile : string = path . join ( projectRoot , ".project" ) ;
89
+ const templateRoot : string = path . join ( this . context . extensionPath , "templates" , "invisible-project" ) ;
68
90
try {
69
- let jdkSpecificTemplateRoot : string = path . join ( templateRoot , `Java${ javaVersion } ` ) ;
70
- if ( ! await fse . pathExists ( jdkSpecificTemplateRoot ) ) {
71
- // fall back to 8
72
- jdkSpecificTemplateRoot = path . join ( templateRoot , `Java8` ) ;
73
- }
74
91
await fse . ensureDir ( projectRoot ) ;
75
- await Promise . all ( [
76
- fse . copy ( path . join ( templateRoot , "App.java.sample" ) , path . join ( projectRoot , "src" , "app" , "App.java" ) ) ,
77
- fse . copy ( jdkSpecificTemplateRoot , projectRoot ) ,
78
- fse . copy ( path . join ( templateRoot , ".project" ) , path . join ( projectRoot , ".project" ) ) ,
79
- fse . ensureDir ( path . join ( projectRoot , "bin" ) ) ,
80
- ] ) ;
81
-
82
- // replace the project name with user input project name
83
- const xml : string = await fse . readFile ( projectFile , "utf8" ) ;
84
- const jsonObj : any = await Utility . parseXml ( xml ) ;
85
- jsonObj . projectDescription . name = projectName ;
86
- const builder : xml2js . Builder = new xml2js . Builder ( ) ;
87
- const newXml : string = builder . buildObject ( jsonObj ) ;
88
- await fse . writeFile ( projectFile , newXml ) ;
92
+ await fse . copy ( templateRoot , projectRoot ) ;
89
93
} catch ( error ) {
90
94
window . showErrorMessage ( error . message ) ;
91
95
return ;
92
96
}
93
- return true ;
97
+ const openInNewWindow = workspace && ! _ . isEmpty ( workspace . workspaceFolders ) ;
98
+ await commands . executeCommand ( Commands . VSCODE_OPEN_FOLDER , Uri . file ( path . join ( basePath , projectName ) ) , openInNewWindow ) ;
94
99
}
100
+ }
95
101
96
- private async getJavaVersion ( ) : Promise < number > {
97
- let javaVersion : number ;
98
- try {
99
- const javaHome : string = await Utility . checkJavaRuntime ( ) ;
100
- javaVersion = await Utility . checkJavaVersion ( javaHome ) ;
101
- } catch ( error ) {
102
- window . showErrorMessage ( error . message , error . label ) . then ( ( selection ) => {
103
- if ( error . label && error . label === selection && error . openUrl ) {
104
- commands . executeCommand ( "vscode.open" , error . openUrl ) ;
105
- }
106
- } ) ;
107
- return ;
108
- }
109
- return javaVersion ;
110
- }
102
+ enum BuildTool {
103
+ Maven = "Maven" ,
104
+ None = "No build tools" ,
111
105
}
0 commit comments