66import * as vscode from 'vscode' ;
77import * as path from 'path' ;
88import * as fs from 'fs' ;
9- import { CreateDirectory , CopyFiles } from '../utilities/directoryUtilities' ;
9+ import * as os from 'os' ;
10+ import {
11+ CreateDirectory ,
12+ CopyFiles ,
13+ checkFolderName ,
14+ } from '../utilities/directoryUtilities' ;
1015import { getUri } from '../utilities/getUri' ;
1116
1217export class NewProjectPanel {
@@ -15,13 +20,11 @@ export class NewProjectPanel {
1520 private readonly _panel : vscode . WebviewPanel ;
1621 private _disposables : vscode . Disposable [ ] = [ ] ;
1722
18- static readonly USER_INTPUT_ERR : number = - 2 ;
19- static readonly DIR_EXSITED_ERR : number = - 1 ;
2023 static readonly EXCUTION_SUCCESS : number = 0 ;
24+ static readonly DIR_EXSITED_ERR : number = - 1 ;
25+ static readonly USER_INTPUT_ERR : number = - 2 ;
26+ static readonly DIR_PATH_INVALID_ERR : number = - 3 ;
2127
22- /**
23- * @param context extension context from extension.ts active func
24- */
2528 constructor ( extensionUri : vscode . Uri , panel : vscode . WebviewPanel ) {
2629 this . _panel = panel ;
2730 this . _panel . webview . html = this . _getHtmlForWebview (
@@ -33,9 +36,6 @@ export class NewProjectPanel {
3336 this . _panel . onDidDispose ( this . dispose , null , this . _disposables ) ;
3437 }
3538
36- /**
37- * @param context
38- */
3939 public static render ( context : vscode . ExtensionContext ) {
4040 NewProjectPanel . USER_SET_WORKSPACE = vscode . workspace
4141 . getConfiguration ( )
@@ -55,18 +55,13 @@ export class NewProjectPanel {
5555 }
5656 ) ;
5757
58- /* create new project panel obj */
5958 NewProjectPanel . currentPanel = new NewProjectPanel (
6059 context . extensionUri ,
6160 panel
6261 ) ;
6362 }
6463 }
6564
66- /**
67- * @param projName project name input by user
68- * @param template
69- */
7065 private _creatNewProject (
7166 projName : string ,
7267 template : string ,
@@ -76,22 +71,23 @@ export class NewProjectPanel {
7671 return NewProjectPanel . USER_INTPUT_ERR ;
7772 }
7873
74+ if ( ! checkFolderName ( projName ) ) {
75+ return NewProjectPanel . DIR_PATH_INVALID_ERR ;
76+ }
77+
7978 let ROOT_PATH = path . join ( NewProjectPanel . USER_SET_WORKSPACE , projName ) ;
8079 let EXT_PATH = extensionUri . fsPath ;
8180
82- /* if the direcotry has exsited, then ignore the creation and return */
8381 if ( fs . existsSync ( ROOT_PATH ) ) {
8482 if ( fs . lstatSync ( ROOT_PATH ) . isDirectory ( ) ) {
8583 return NewProjectPanel . DIR_EXSITED_ERR ;
8684 }
8785 }
8886
89- /* create necessary floders under the project directory */
9087 CreateDirectory ( path . join ( ROOT_PATH , '.wamr' ) ) ;
9188 CreateDirectory ( path . join ( ROOT_PATH , 'include' ) ) ;
9289 CreateDirectory ( path . join ( ROOT_PATH , 'src' ) ) ;
9390
94- /* copy scripts files to project_root_path/.wamr */
9591 CopyFiles (
9692 path . join ( EXT_PATH , 'resource/scripts/CMakeLists.txt' ) ,
9793 path . join ( ROOT_PATH , '.wamr/CMakeLists.txt' )
@@ -110,7 +106,6 @@ export class NewProjectPanel {
110106 extensionUri : vscode . Uri ,
111107 templatePath : string
112108 ) {
113- /* get toolkit uri */
114109 const toolkitUri = getUri ( webview , extensionUri , [
115110 'node_modules' ,
116111 '@vscode' ,
@@ -147,29 +142,26 @@ export class NewProjectPanel {
147142 webview : vscode . Webview ,
148143 extensionUri : vscode . Uri
149144 ) {
150- // Handle messages from the webview
151145 webview . onDidReceiveMessage (
152146 message => {
153147 switch ( message . command ) {
154148 case 'create_new_project' :
149+ let createNewProjectStatus = this . _creatNewProject (
150+ message . projectName ,
151+ message . template ,
152+ extensionUri
153+ ) ;
155154 if (
156- this . _creatNewProject (
157- message . projectName ,
158- message . template ,
159- extensionUri
160- ) === NewProjectPanel . EXCUTION_SUCCESS
155+ createNewProjectStatus ===
156+ NewProjectPanel . EXCUTION_SUCCESS
161157 ) {
162- /* post message to page to inform the project creation has finished */
163158 webview . postMessage ( {
164159 command : 'proj_creation_finish' ,
165160 prjName : message . projectName ,
166161 } ) ;
167162 } else if (
168- this . _creatNewProject (
169- message . projectName ,
170- message . template ,
171- extensionUri
172- ) === NewProjectPanel . DIR_EXSITED_ERR
163+ createNewProjectStatus ===
164+ NewProjectPanel . DIR_EXSITED_ERR
173165 ) {
174166 vscode . window . showErrorMessage (
175167 'Project : ' +
@@ -178,16 +170,30 @@ export class NewProjectPanel {
178170 ) ;
179171 return ;
180172 } else if (
181- this . _creatNewProject (
182- message . projectName ,
183- message . template ,
184- extensionUri
185- ) === NewProjectPanel . USER_INTPUT_ERR
173+ createNewProjectStatus ===
174+ NewProjectPanel . USER_INTPUT_ERR
186175 ) {
187176 vscode . window . showErrorMessage (
188177 'Please fill chart before your submit!'
189178 ) ;
190179 return ;
180+ } else if (
181+ createNewProjectStatus ===
182+ NewProjectPanel . DIR_PATH_INVALID_ERR
183+ ) {
184+ if ( os . platform ( ) === 'win32' ) {
185+ vscode . window . showErrorMessage (
186+ "A file name can't contain any of the following characters: ' / \\ : * ? < > | ' and the length should be less than 255"
187+ ) ;
188+ } else if (
189+ os . platform ( ) === 'linux' ||
190+ os . platform ( ) === 'darwin'
191+ ) {
192+ vscode . window . showErrorMessage (
193+ "A file name can't contain following characters: '/' and the length should be less than 255"
194+ ) ;
195+ }
196+ return ;
191197 }
192198 return ;
193199
0 commit comments