@@ -2,23 +2,12 @@ import * as vscode from "vscode";
2
2
import * as util from "../util/util" ;
3
3
import * as prompts from "../util/vscodePrompts" ;
4
4
import * as path from "path" ;
5
- import fetch from "node-fetch " ;
6
- import { MP_STARTER_API_ROOT , OPEN_NEW_PROJECT_OPTIONS , EXTENSION_USER_AGENT } from "../constants " ;
5
+ import { OPEN_NEW_PROJECT_OPTIONS , ERRORS } from "../constants " ;
6
+ import * as mpStarterApi from "../util/mpStarterApi " ;
7
7
8
8
export async function generateProject ( ) : Promise < void > {
9
9
try {
10
- const mpSupportResponse = await fetch ( `${ MP_STARTER_API_ROOT } /supportMatrix` , {
11
- method : "GET" ,
12
- headers : {
13
- "User-Agent" : EXTENSION_USER_AGENT ,
14
- } ,
15
- } ) ;
16
- if ( mpSupportResponse . status >= 400 && mpSupportResponse . status < 600 ) {
17
- throw new Error ( `Bad response ${ mpSupportResponse . status } : ${ mpSupportResponse . statusText } ` ) ;
18
- }
19
-
20
- const mpSupportMatrix = await mpSupportResponse . json ( ) ;
21
-
10
+ const mpSupportMatrix = await mpStarterApi . getSupportMatrix ( ) ;
22
11
// mpConfigurations is a map of mp version -> mp configuration
23
12
const mpConfigurations = mpSupportMatrix . configs ;
24
13
const allMpVersions = Object . keys ( mpConfigurations ) ;
@@ -44,18 +33,20 @@ export async function generateProject(): Promise<void> {
44
33
return ;
45
34
}
46
35
47
- const javaSEVersion = await prompts . askForJavaSEVersion ( mpVersion , mpServer ) ;
36
+ // gets support information about which JavaSE versions / microprofile specs are supported by the
37
+ // users selected mp server / mp version combination
38
+ const { javaSEVersions, mpSpecs } = await mpStarterApi . getSupportedJavaAndSpecs (
39
+ mpServer ,
40
+ mpVersion
41
+ ) ;
42
+
43
+ const javaSEVersion = await prompts . askForJavaSEVersion ( javaSEVersions ) ;
48
44
if ( javaSEVersion === undefined ) {
49
45
return ;
50
46
}
51
47
52
- // ask user to pick a list of mp specifications to use for the given version of mp they selected
53
- const allSupportedSpecs = mpConfigurations [ mpVersion ] . specs ;
54
48
const specDescriptions = mpSupportMatrix . descriptions ;
55
- const mpSpecifications = await prompts . askForMPSpecifications (
56
- allSupportedSpecs ,
57
- specDescriptions
58
- ) ;
49
+ const mpSpecifications = await prompts . askForMPSpecifications ( mpSpecs , specDescriptions ) ;
59
50
if ( mpSpecifications === undefined ) {
60
51
return ;
61
52
}
@@ -67,7 +58,7 @@ export async function generateProject(): Promise<void> {
67
58
68
59
const targetDirString = targetFolder . fsPath ;
69
60
70
- const requestPayload = {
61
+ const projectOptions = {
71
62
groupId : groupId ,
72
63
artifactId : artifactId ,
73
64
mpVersion : mpVersion ,
@@ -80,61 +71,54 @@ export async function generateProject(): Promise<void> {
80
71
// location to download the zip file
81
72
const zipPath = path . join ( targetDirString , zipName ) ;
82
73
83
- const requestOptions = {
84
- url : `${ MP_STARTER_API_ROOT } /project` ,
85
- method : "POST" ,
86
- headers : {
87
- "Content-Type" : "application/json" ,
88
- "User-Agent" : EXTENSION_USER_AGENT ,
89
- } ,
90
- body : JSON . stringify ( requestPayload ) ,
91
- } ;
92
-
93
74
// show a progress bar as the zip file is being downloaded
94
75
await vscode . window . withProgress (
95
76
{
96
77
location : vscode . ProgressLocation . Notification ,
97
78
title : "Generating the MicroProfile Starter project..." ,
98
79
cancellable : false ,
99
80
} ,
100
- ( ) => util . downloadFile ( requestOptions , zipPath )
81
+ ( ) => mpStarterApi . downloadMPStarterProjectZip ( projectOptions , zipPath )
101
82
) ;
102
83
84
+ const targetDirFolder = path . join ( targetDirString , artifactId ) ;
85
+
103
86
try {
104
- const targetDirFolder = path . join ( targetDirString , artifactId ) ;
105
87
await util . unzipFile ( zipPath , targetDirString , targetDirFolder ) ;
106
- try {
107
- await util . deleteFile ( zipPath ) ;
108
- } catch ( e ) {
109
- console . error ( e ) ;
110
- vscode . window . showErrorMessage ( `Failed to delete file ${ zipName } ` ) ;
111
- }
112
-
113
- // open the unzipped folder in a new VS Code window
114
- const uriPath = vscode . Uri . file ( targetDirFolder ) ;
115
- // prompt user whether they want to add project to current workspace or open in a new window
116
- const selection = await vscode . window . showInformationMessage (
117
- "MicroProfile Starter project generated. Would you like to add your project to the current workspace or open it in a new window?" ,
118
- ... [
119
- OPEN_NEW_PROJECT_OPTIONS . ADD_CURRENT_WORKSPACE ,
120
- OPEN_NEW_PROJECT_OPTIONS . OPEN_NEW_WINDOW ,
121
- ]
122
- ) ;
123
- if ( selection === OPEN_NEW_PROJECT_OPTIONS . ADD_CURRENT_WORKSPACE ) {
124
- vscode . workspace . updateWorkspaceFolders ( 0 , 0 , { uri : uriPath } ) ;
125
- } else if ( selection === OPEN_NEW_PROJECT_OPTIONS . OPEN_NEW_WINDOW ) {
126
- await vscode . commands . executeCommand ( "vscode.openFolder" , uriPath , true ) ;
127
- }
128
- } catch ( err ) {
129
- console . error ( err ) ;
130
- vscode . window . showErrorMessage ( "Failed to extract the MicroProfile Starter project." ) ;
88
+ } catch ( e ) {
89
+ console . error ( e ) ;
90
+ const err = new Error ( "Unable to extract MicroProfile Starter project" ) ;
91
+ err . name = ERRORS . EXTRACT_PROJECT_ERROR ;
92
+ throw err ;
93
+ }
94
+
95
+ // if failed to delete the zip, no need to error out but show a warning to users
96
+ try {
97
+ await util . deleteFile ( zipPath ) ;
98
+ } catch ( e ) {
99
+ console . error ( e ) ;
100
+ vscode . window . showErrorMessage ( `Failed to delete file ${ zipName } ` ) ;
101
+ }
102
+
103
+ const uriPath = vscode . Uri . file ( targetDirFolder ) ;
104
+ // prompt user whether they want to add project to current workspace or open in a new window
105
+ const selection = await vscode . window . showInformationMessage (
106
+ "MicroProfile Starter project generated. Add your project to the current workspace or open it in a new window?" ,
107
+ ... [ OPEN_NEW_PROJECT_OPTIONS . ADD_CURRENT_WORKSPACE , OPEN_NEW_PROJECT_OPTIONS . OPEN_NEW_WINDOW ]
108
+ ) ;
109
+ if ( selection === OPEN_NEW_PROJECT_OPTIONS . ADD_CURRENT_WORKSPACE ) {
110
+ vscode . workspace . updateWorkspaceFolders ( 0 , 0 , { uri : uriPath } ) ;
111
+ } else if ( selection === OPEN_NEW_PROJECT_OPTIONS . OPEN_NEW_WINDOW ) {
112
+ await vscode . commands . executeCommand ( "vscode.openFolder" , uriPath , true ) ;
131
113
}
132
114
} catch ( e ) {
133
115
console . error ( e ) ;
134
- if ( e . name === "FetchError" ) {
116
+ if ( e . name === ERRORS . FETCH_ERROR ) {
135
117
vscode . window . showErrorMessage (
136
118
"Failed to connect to the MicroProfile Starter. Please check your network connection and try again."
137
119
) ;
120
+ } else if ( e . name === ERRORS . EXTRACT_PROJECT_ERROR ) {
121
+ vscode . window . showErrorMessage ( "Failed to extract the MicroProfile Starter project" ) ;
138
122
} else {
139
123
vscode . window . showErrorMessage ( "Failed to generate a MicroProfile Starter project" ) ;
140
124
}
0 commit comments