Skip to content

Commit 46305d8

Browse files
authored
Require a valid folder name and team number before allowing generation (#443)
1 parent a1b4177 commit 46305d8

File tree

12 files changed

+76
-26
lines changed

12 files changed

+76
-26
lines changed

vscode-wpilib/resources/webviews/eclipseimport.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ <h1>Welcome to WPILib 2018 Eclipse Project Importer</h1>
3434
<input id="newFolderCB" type="checkbox" checked/>
3535
<br/>
3636
<br/>
37-
<div id="projectnamediv">The following will be the project name. When you select an eclipse project above, it will insert the project name.</div>
38-
<input id="projectName" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px" disabled/>
37+
<div id="projectnamediv" class="error">The following will be the project name. When you select an eclipse project above, it will insert the project name.</div>
38+
<input id="projectName" type="text" class="error" style="width: 100%; margin-top: 10px; margin-bottom: 10px" disabled/>
3939
<br/>
40-
<div id="teamnumberdiv">Enter a team number</div>
41-
<input id="teamNumber" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px"
40+
<div id="teamnumberdiv" class="error">Enter a team number</div>
41+
<input id="teamNumber" type="text" class="error" style="width: 100%; margin-top: 10px; margin-bottom: 10px"
4242
/>
4343
<br/>
4444
<div id="desktopdiv">

vscode-wpilib/resources/webviews/gradle2020import.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ <h1>Welcome to WPILib 2020 Project Importer</h1>
3434
<button id="projectSelectButton">Select a new project folder</button>
3535
<br/>
3636
<br/>
37-
<div id="projectnamediv"><b>Project Name</b>
38-
<input id="projectName" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px" disabled/>
37+
<div id="projectnamediv" class="error"><b>Project Name</b>
38+
<input id="projectName" type="text" class="error" style="width: 100%; margin-top: 10px; margin-bottom: 10px" disabled/>
3939
When you select a gradle project above, it will insert the project name, however, it may be customized.
4040
<br/>
4141
<br/> <b>Create a new folder?</b>
@@ -45,8 +45,8 @@ <h1>Welcome to WPILib 2020 Project Importer</h1>
4545
<br/>
4646
</div>
4747
<br/>
48-
<div id="teamnumberdiv"><b>Team Number</b></div>
49-
<input id="teamNumber" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px"
48+
<div id="teamnumberdiv" class="error"><b>Team Number</b></div>
49+
<input id="teamNumber" class="error" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px"
5050
/>
5151
<br/>
5252
<div id="desktopdiv">

vscode-wpilib/resources/webviews/projectcreator.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ <h1 data-i18n-trans>Welcome to WPILib New Project Creator</h1>
3232
<button id="projectSelectButton" data-i18n-trans>Select a new project folder</button>
3333
<br/>
3434
<br/>
35-
<div id="projectnamediv" data-i18n-trans="ui" STYLE="font-weight:bold">Project Name</div>
36-
<input id="projectName" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px"
35+
<div id="projectnamediv" data-i18n-trans="ui" class="error" STYLE="font-weight:bold">Project Name</div>
36+
<input id="projectName" type="text" class="error" style="width: 100%; margin-top: 10px; margin-bottom: 10px"
3737
/>
3838
<br/>
3939
<br/> <span data-i18n-trans STYLE="font-weight:bold">Create a new folder?</span>
@@ -42,8 +42,8 @@ <h1 data-i18n-trans>Welcome to WPILib New Project Creator</h1>
4242
<span data-i18n-trans>This creates a new folder at <i>Base Folder\Project Name</i>. Highly recommended to be checked. Otherwise the folder will be placed at <i>Base Folder</i> and not utilize the Project Name</span>
4343
<br/>
4444
<br/>
45-
<div id="teamnumberdiv" data-i18n-trans="ui" STYLE="font-weight:bold">Team Number</div>
46-
<input id="teamNumber" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px"
45+
<div id="teamnumberdiv" class="error" data-i18n-trans="ui" STYLE="font-weight:bold">Team Number</div>
46+
<input id="teamNumber" class="error" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px"
4747
/>
4848
<br/>
4949
<div id="desktopdiv">

vscode-wpilib/src/webviews/pages/eclipseimportpage.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
import { IEclipseIPCReceive, IEclipseIPCSend } from './eclipseimportpagetypes';
4-
import { validateTeamNumber } from './sharedpages';
4+
import { validateProject, validateTeamNumber } from './sharedpages';
55

66
interface IVsCodeApi {
77
postMessage(message: IEclipseIPCReceive): void;
@@ -22,6 +22,12 @@ function projectSelectButtonClick() {
2222
}
2323

2424
function importProjectButtonClick() {
25+
const isValidTeam = validateTeamNumber();
26+
const isValidProject = validateProject();
27+
if (!isValidTeam || !isValidProject) {
28+
return;
29+
}
30+
2531
(document.activeElement as HTMLElement).blur();
2632
vscode.postMessage({
2733
data: {
@@ -64,5 +70,7 @@ window.addEventListener('load', (_: Event) => {
6470
// tslint:disable-next-line:no-non-null-assertion
6571
document.getElementById('teamNumber')!.oninput = validateTeamNumber;
6672
// tslint:disable-next-line:no-non-null-assertion
73+
document.getElementById('projectName')!.oninput = validateProject;
74+
// tslint:disable-next-line:no-non-null-assertion
6775
document.getElementById('importProject')!.onclick = importProjectButtonClick;
6876
});

vscode-wpilib/src/webviews/pages/gradle2020importpage.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
import { IGradle2020IPCReceive, IGradle2020IPCSend } from './gradle2020importpagetypes';
4-
import { validateTeamNumber } from './sharedpages';
4+
import { validateProject, validateTeamNumber } from './sharedpages';
55

66
interface IVsCodeApi {
77
postMessage(message: IGradle2020IPCReceive): void;
@@ -22,6 +22,12 @@ function projectSelectButtonClick() {
2222
}
2323

2424
function importProjectButtonClick() {
25+
const isValidTeam = validateTeamNumber();
26+
const isValidProject = validateProject();
27+
if (!isValidTeam || !isValidProject) {
28+
return;
29+
}
30+
2531
(document.activeElement as HTMLElement).blur();
2632
vscode.postMessage({
2733
data: {
@@ -66,6 +72,8 @@ window.addEventListener('load', (_: Event) => {
6672
// tslint:disable-next-line:no-non-null-assertion
6773
document.getElementById('projectSelectButton')!.onclick = projectSelectButtonClick;
6874
// tslint:disable-next-line:no-non-null-assertion
75+
document.getElementById('projectName')!.oninput = validateProject;
76+
// tslint:disable-next-line:no-non-null-assertion
6977
document.getElementById('teamNumber')!.oninput = validateTeamNumber;
7078
// tslint:disable-next-line:no-non-null-assertion
7179
document.getElementById('importProject')!.onclick = importProjectButtonClick;

vscode-wpilib/src/webviews/pages/projectcreatorpage.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ function selectProjectFolder() {
6060
}
6161

6262
function generateProject() {
63+
const isValidTeam = validateTeamNumber();
64+
const isValidProject = validateProject();
65+
if (!isValidTeam || !isValidProject) {
66+
return;
67+
}
68+
6369
(document.activeElement as HTMLElement).blur();
6470
vscode.postMessage({
6571
data: {

vscode-wpilib/src/webviews/pages/sharedpages.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ declare global {
55
interface Window { i18nTrans: (domain: string, message: string, ...args: any[]) => string; }
66
}
77

8-
export function validateProject() {
8+
export function validateProject(): boolean {
99
const elem = document.getElementById('projectName') as HTMLButtonElement;
1010
const s = elem.value;
1111
const match = s.match(/\w[\w-]*$/gm);
@@ -14,14 +14,16 @@ export function validateProject() {
1414
pdiv.innerText = window.i18nTrans('ui', 'Invalid project name');
1515
pdiv.classList.add('error');
1616
elem.classList.add('error');
17+
return false;
1718
} else {
1819
pdiv.innerText = window.i18nTrans('ui', 'Project Name');
1920
pdiv.classList.remove('error');
2021
elem.classList.remove('error');
22+
return true;
2123
}
2224
}
2325

24-
export function validateTeamNumber() {
26+
export function validateTeamNumber(): boolean {
2527
const elem = document.getElementById('teamNumber') as HTMLInputElement;
2628
const s = elem.value;
2729
const match = s.match(/^\d{1,5}$/gm);
@@ -30,9 +32,11 @@ export function validateTeamNumber() {
3032
pdiv.innerText = window.i18nTrans('ui', 'Invalid team number');
3133
pdiv.classList.add('error');
3234
elem.classList.add('error');
35+
return false;
3336
} else {
3437
pdiv.innerText = window.i18nTrans('ui', 'Team Number');
3538
pdiv.classList.remove('error');
3639
elem.classList.remove('error');
40+
return true;
3741
}
3842
}

wpilib-utility-standalone/eclipseimport.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ <h1>Welcome to WPILib Eclipse Project Importer</h1>
4141
<input id="newFolderCB" type="checkbox" checked/>
4242
<br/>
4343
<br/>
44-
<div id="projectnamediv">The following will be the project name. When you select an eclipse project above, it will insert the project name.</div>
45-
<input id="projectName" oninput="validators.validateProject(document.getElementById('projectName'), document.getElementById('projectnamediv'));" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px" disabled/>
44+
<div id="projectnamediv" class="error">The following will be the project name. When you select an eclipse project above, it will insert the project name.</div>
45+
<input id="projectName" class="error" oninput="validators.validateProject(document.getElementById('projectName'), document.getElementById('projectnamediv'));" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px" disabled/>
4646
<br/>
47-
<div id="teamnumberdiv">Enter a team number</div>
48-
<input id="teamNumber" oninput="validators.validateTeamNumber(document.getElementById('teamNumber'), document.getElementById('teamnumberdiv'));" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px"
47+
<div id="teamnumberdiv" class="error">Enter a team number</div>
48+
<input id="teamNumber" class="error" oninput="validators.validateTeamNumber(document.getElementById('teamNumber'), document.getElementById('teamnumberdiv'));" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px"
4949
/>
5050
<br/>
5151
<div id="desktopdiv">

wpilib-utility-standalone/projectcreator.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ <h1>Welcome to WPILib New Project Creator</h1>
5353
<input id="newFolderCB" type="checkbox" checked/>
5454
<br/>
5555
<br/>
56-
<div id="projectnamediv">Enter a project name</div>
57-
<input id="projectName" oninput="validators.validateProject(document.getElementById('projectName'), document.getElementById('projectnamediv'));" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px"
56+
<div id="projectnamediv" class="error">Enter a project name</div>
57+
<input id="projectName" class="error" oninput="validators.validateProject(document.getElementById('projectName'), document.getElementById('projectnamediv'));" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px"
5858
/>
5959
<br/>
60-
<div id="teamnumberdiv">Enter a team number</div>
61-
<input id="teamNumber" oninput="validators.validateTeamNumber(document.getElementById('teamNumber'), document.getElementById('teamnumberdiv'));" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px"
60+
<div id="teamnumberdiv" class="error">Enter a team number</div>
61+
<input id="teamNumber" class="error" oninput="validators.validateTeamNumber(document.getElementById('teamNumber'), document.getElementById('teamnumberdiv'));" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px"
6262
/>
6363
<br/>
6464
<div id="desktopdiv">

wpilib-utility-standalone/src/eclipseimport.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as os from 'os';
66
import * as path from 'path';
77
import { generateCopyCpp, generateCopyJava } from './shared/generator';
88
import { existsAsync, mkdirpAsync, readFileAsync, writeFileAsync } from './utilities';
9+
import { validateProject, validateTeamNumber } from './validators';
910

1011
const dialog = electron.remote.dialog;
1112
const bWindow = electron.remote.getCurrentWindow();
@@ -74,6 +75,15 @@ interface IImportProject {
7475
}
7576

7677
export async function importProjectButtonClick() {
78+
const isValidProject = validateProject((document.getElementById('projectName') as HTMLInputElement),
79+
(document.getElementById('projectnamediv') as HTMLInputElement));
80+
const isValidTeam = validateTeamNumber((document.getElementById('teamNumber') as HTMLInputElement),
81+
(document.getElementById('teamnumberdiv') as HTMLInputElement));
82+
if (!isValidTeam || !isValidProject) {
83+
alert('Project name and team number must be correct');
84+
return;
85+
}
86+
7787
(document.activeElement as HTMLElement).blur();
7888
const data: IImportProject = {
7989
fromProps: (document.getElementById('eclipseInput') as HTMLInputElement).value,

0 commit comments

Comments
 (0)