44import * as fse from "fs-extra" ;
55import * as _ from "lodash" ;
66import * 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" ;
88import { instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-wrapper" ;
9- import * as xml2js from "xml2js" ;
109import { Commands } from "../commands" ;
10+ import { Context } from "../constants" ;
11+ import { contextManager } from "../contextManager" ;
1112import { Utility } from "../utility" ;
1213
1314export class ProjectController implements Disposable {
@@ -25,10 +26,40 @@ export class ProjectController implements Disposable {
2526 }
2627
2728 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 ) {
3047 return ;
3148 }
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 > {
3263 const workspaceFolder = Utility . getDefaultWorkspaceFolder ( ) ;
3364 const location : Uri [ ] = await window . showOpenDialog ( {
3465 defaultUri : workspaceFolder && workspaceFolder . uri ,
@@ -39,73 +70,36 @@ export class ProjectController implements Disposable {
3970 if ( ! location || ! location . length ) {
4071 return ;
4172 }
73+
4274 const basePath : string = location [ 0 ] . fsPath ;
4375 const projectName : string = await window . showInputBox ( {
4476 prompt : "Input a java project name" ,
45- validateInput : ( name : string ) : string => {
77+ ignoreFocusOut : true ,
78+ validateInput : async ( name : string ) : Promise < string > => {
4679 if ( name && ! name . match ( / ^ [ ^ * ~ / \\ ] + $ / ) ) {
4780 return "Please input a valid project name" ;
4881 }
49- if ( name && fse . pathExistsSync ( path . join ( basePath , name ) ) ) {
82+ if ( name && await fse . pathExists ( path . join ( basePath , name ) ) ) {
5083 return "A project with this name already exists." ;
5184 }
52- return null ;
85+ return "" ;
5386 } ,
5487 } ) ;
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 > {
6588 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" ) ;
6890 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- }
7491 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 ) ;
8993 } catch ( error ) {
9094 window . showErrorMessage ( error . message ) ;
9195 return ;
9296 }
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 ) ;
9499 }
100+ }
95101
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" ,
111105}
0 commit comments