Skip to content

Commit

Permalink
Validate project isn't in OneDrive on creation (wpilibsuite#633)
Browse files Browse the repository at this point in the history
This provides an even earlier detection point compared to GradleRIO.
It should still be done in GradleRIO to catch things like moved and
cloned projects.
  • Loading branch information
wpineth committed Mar 29, 2024
1 parent aa6b8bd commit 2884c8a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
4 changes: 2 additions & 2 deletions vscode-wpilib/resources/webviews/gradle2020import.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ <h1>Welcome to WPILib 2020-2023 Project Importer</h1>
<input id="gradle2020Input" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px" />
<button id="gradle2020SelectButton">Select a gradle project.</button>
<br/>
<br/> <b>Base Folder.</b> Select a base folder to place the new project into. The imported project must be placed into a new folder. Importing in place is not supported.
<br/> <div id="projectfolderdiv" data-i18n-trans="ui" class="error" STYLE="font-weight:bold">Base Folder:</div> Select a base folder to place the new project into. The imported project must be placed into a new folder. Importing in place is not supported.
<br/>
<input id="projectFolder" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px" />
<input id="projectFolder" type="text" class="error" style="width: 100%; margin-top: 10px; margin-bottom: 10px" />
<br/>
<button id="projectSelectButton">Select a new project folder</button>
<br/>
Expand Down
4 changes: 2 additions & 2 deletions vscode-wpilib/resources/webviews/projectcreator.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ <h1 data-i18n-trans>Welcome to WPILib New Project Creator</h1>

<button id="projectBase" data-i18n-trans disabled>Select a project base</button>
<br/>
<br/> <span data-i18n-trans STYLE="font-weight:bold">Base Folder.</span> <span data-i18n-trans>Select a base folder to place the new project into.</span>
<br/> <div id="projectfolderdiv" data-i18n-trans="ui" class="error" STYLE="font-weight:bold">Base Folder:</div> <span data-i18n-trans>Select a base folder to place the new project into.</span>
<br/>
<input id="projectFolder" type="text" style="width: 100%; margin-top: 10px; margin-bottom: 10px" />
<input id="projectFolder" type="text" class="error" style="width: 100%; margin-top: 10px; margin-bottom: 10px" />
<br/>
<button id="projectSelectButton" data-i18n-trans>Select a new project folder</button>
<br/>
Expand Down
8 changes: 6 additions & 2 deletions vscode-wpilib/src/webviews/pages/gradle2020importpage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import { IGradle2020IPCReceive, IGradle2020IPCSend } from './gradle2020importpagetypes';
import { validateProject, validateTeamNumber } from './sharedpages';
import { validateProject, validateTeamNumber, validateProjectFolder } from './sharedpages';

interface IVsCodeApi {
postMessage(message: IGradle2020IPCReceive): void;
Expand All @@ -24,7 +24,8 @@ function projectSelectButtonClick() {
function importProjectButtonClick() {
const isValidTeam = validateTeamNumber();
const isValidProject = validateProject();
if (!isValidTeam || !isValidProject) {
const isValidFolder = validateProjectFolder();
if (!isValidTeam || !isValidProject || !isValidFolder) {
return;
}

Expand Down Expand Up @@ -58,6 +59,7 @@ window.addEventListener('message', (event) => {
case 'newproject':
const elem = document.getElementById('projectFolder') as HTMLInputElement;
elem.value = data.data;
validateProjectFolder();
break;
case 'teamnumber':
const tn = document.getElementById('teamNumber') as HTMLInputElement;
Expand All @@ -80,6 +82,8 @@ window.addEventListener('load', (_: Event) => {
document.getElementById('teamNumber')!.oninput = validateTeamNumber;
// tslint:disable-next-line:no-non-null-assertion
document.getElementById('importProject')!.onclick = importProjectButtonClick;
// tslint:disable-next-line:no-non-null-assertion
document.getElementById('projectFolder')!.oninput = validateProjectFolder;

vscode.postMessage({ type: 'loaded' });
});
8 changes: 6 additions & 2 deletions vscode-wpilib/src/webviews/pages/projectcreatorpage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import { IProjectIPCReceive, IProjectIPCSend, ProjectType } from './projectcreatorpagetypes';
import { validateProject, validateTeamNumber } from './sharedpages';
import { validateProject, validateTeamNumber, validateProjectFolder } from './sharedpages';

interface IVsCodeApi {
postMessage(message: IProjectIPCReceive): void;
Expand Down Expand Up @@ -74,7 +74,8 @@ function selectProjectFolder() {
function generateProject() {
const isValidTeam = validateTeamNumber();
const isValidProject = validateProject();
if (!isValidTeam || !isValidProject) {
const isValidFolder = validateProjectFolder();
if (!isValidTeam || !isValidProject || !isValidFolder) {
return;
}

Expand Down Expand Up @@ -103,6 +104,7 @@ window.addEventListener('message', (event) => {
case 'newproject':
const elem = document.getElementById('projectFolder') as HTMLInputElement;
elem.value = data.data as string;
validateProjectFolder();
break;
case 'projecttype':
projectType = data.data as ProjectType;
Expand Down Expand Up @@ -148,4 +150,6 @@ window.addEventListener('load', (_: Event) => {
document.getElementById('teamNumber')!.oninput = validateTeamNumber;
// tslint:disable-next-line:no-non-null-assertion
document.getElementById('generateProject')!.onclick = generateProject;
// tslint:disable-next-line:no-non-null-assertion
document.getElementById('projectFolder')!.onchange = validateProjectFolder;
});
22 changes: 22 additions & 0 deletions vscode-wpilib/src/webviews/pages/sharedpages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

// import { logger } from '../../logger';

declare global {
// tslint:disable-next-line:interface-name no-any
interface Window { i18nTrans: (domain: string, message: string, ...args: any[]) => string; }
Expand All @@ -8,6 +10,7 @@ declare global {
export function validateProject(): boolean {
const elem = document.getElementById('projectName') as HTMLButtonElement;
const s = elem.value;
// logger.log('Project Name: ' + s);
const match = s.match(/\w[\w-]*$/gm);
const pdiv = document.getElementById('projectnamediv') as HTMLDivElement;
if (match === null || match.length === 0) {
Expand All @@ -23,6 +26,25 @@ export function validateProject(): boolean {
}
}

export function validateProjectFolder(): boolean {
const elem = document.getElementById('projectFolder') as HTMLInputElement;
const s = elem.value;
// logger.log('Validate Project Folder: ' + s);
const oneDrive = s.includes('OneDrive');
const pdiv = document.getElementById('projectfolderdiv') as HTMLDivElement;
if (oneDrive === true || s.length === 0) {
pdiv.innerText = oneDrive === true ? window.i18nTrans('ui', 'Invalid Base Folder - Folder can\'t be in OneDrive') : window.i18nTrans('ui', 'Invalid Base Folder');
pdiv.classList.add('error');
elem.classList.add('error');
return false;
} else {
pdiv.innerText = window.i18nTrans('ui', 'Base Folder');
pdiv.classList.remove('error');
elem.classList.remove('error');
return true;
}
}

export function validateTeamNumber(): boolean {
const elem = document.getElementById('teamNumber') as HTMLInputElement;
const s = elem.value;
Expand Down

0 comments on commit 2884c8a

Please sign in to comment.