Skip to content

Commit 86ce70e

Browse files
committed
Merge branch 'main' of github.com:wpilibsuite/systemcore-blocks-interface into pr_module_id
2 parents 9ee1246 + 06687eb commit 86ce70e

File tree

5 files changed

+32
-24
lines changed

5 files changed

+32
-24
lines changed

src/reactComponents/Menu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ export function Component(props: MenuProps): React.JSX.Element {
336336
}
337337

338338
try {
339-
const blobUrl = await storageProject.downloadProject(props.storage, props.project.projectName);
339+
const blobUrl = await storageProject.downloadProject(props.storage, props.project);
340340
const filename = props.project.projectName + storageNames.UPLOAD_DOWNLOAD_FILE_EXTENSION;
341341

342342
// Create a temporary link to download the file

src/storage/client_side_storage.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,12 @@ class ClientSideStorage implements commonStorage.Storage {
126126
}
127127

128128
async listModules(
129-
opt_modulePathFilter?: commonStorage.ModulePathFilter):
130-
Promise<{[path: string]: storageModuleContent.ModuleContent}> {
129+
opt_modulePathRegexPattern?: string
130+
): Promise<{[path: string]: storageModuleContent.ModuleContent}> {
131+
132+
const regExp = opt_modulePathRegexPattern
133+
? new RegExp(opt_modulePathRegexPattern)
134+
: null;
131135
return new Promise((resolve, reject) => {
132136
const pathToModuleContent: {[path: string]: storageModuleContent.ModuleContent} = {};
133137
const openCursorRequest = this.db.transaction([MODULES_STORE_NAME], 'readonly')
@@ -144,7 +148,7 @@ class ClientSideStorage implements commonStorage.Storage {
144148
const value = cursor.value;
145149
// TODO(lizlooney): do we need value.path? Is there another way to get the path?
146150
const modulePath = value.path;
147-
if (!opt_modulePathFilter || opt_modulePathFilter(modulePath)) {
151+
if (!regExp || regExp.test(modulePath)) {
148152
const moduleContent = storageModuleContent.parseModuleContentText(value.content);
149153
pathToModuleContent[modulePath] = moduleContent;
150154
}

src/storage/common_storage.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@
2121

2222
import * as storageModuleContent from './module_content';
2323

24-
export type ModulePathFilter = (modulePath: string) => boolean;
25-
2624
export interface Storage {
2725
saveEntry(entryKey: string, entryValue: string): Promise<void>;
26+
2827
fetchEntry(entryKey: string, defaultValue: string): Promise<string>;
29-
listModules(opt_modulePathFilter?: ModulePathFilter): Promise<{[path: string]: storageModuleContent.ModuleContent}>;
28+
29+
listModules(
30+
opt_modulePathRegexPattern?: string
31+
): Promise<{[path: string]: storageModuleContent.ModuleContent}>;
32+
3033
fetchModuleDateModifiedMillis(modulePath: string): Promise<number>;
34+
3135
fetchModuleContentText(modulePath: string): Promise<string>;
36+
3237
saveModule(modulePath: string, moduleContentText: string): Promise<void>;
38+
3339
deleteModule(modulePath: string): Promise<void>;
3440
}

src/storage/names.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,16 @@ export function snakeCaseToPascalCase(snakeCaseName: string): string {
7676
}
7777

7878
/**
79-
* Returns the module path prefix for the given project name.
79+
* Returns the module path regex pattern for modules in the given project.
8080
*/
81-
export function makeModulePathPrefix(projectName: string): string {
82-
return projectName + '/';
81+
export function makeModulePathRegexPattern(projectName: string): string {
82+
const prefix = projectName + '/';
83+
const suffix = JSON_FILE_EXTENSION;
84+
return '^' + escapeRegExp(prefix) + '.*' + escapeRegExp(suffix) + '$';
85+
}
86+
87+
function escapeRegExp(text: string): string {
88+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
8389
}
8490

8591
/**

src/storage/project.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,8 @@ export async function copyProject(
160160
async function renameOrCopyProject(
161161
storage: commonStorage.Storage, project: Project, newProjectName: string,
162162
rename: boolean): Promise<void> {
163-
const modulePathPrefix = storageNames.makeModulePathPrefix(project.projectName);
164163
const pathToModuleContent = await storage.listModules(
165-
(modulePath: string) => modulePath.startsWith(modulePathPrefix));
164+
storageNames.makeModulePathRegexPattern(project.projectName));
166165

167166
for (const modulePath in pathToModuleContent) {
168167
const className = storageNames.getClassName(modulePath);
@@ -183,9 +182,9 @@ async function renameOrCopyProject(
183182
*/
184183
export async function deleteProject(
185184
storage: commonStorage.Storage, project: Project): Promise<void> {
186-
const modulePathPrefix = storageNames.makeModulePathPrefix(project.projectName);
187185
const pathToModuleContent = await storage.listModules(
188-
(modulePath: string) => modulePath.startsWith(modulePathPrefix));
186+
storageNames.makeModulePathRegexPattern(project.projectName));
187+
189188
for (const modulePath in pathToModuleContent) {
190189
await storage.deleteModule(modulePath);
191190
}
@@ -287,14 +286,8 @@ export async function copyModuleInProject(
287286
async function renameOrCopyModule(
288287
storage: commonStorage.Storage, project: Project, newClassName: string,
289288
oldModule: storageModule.Module, rename: boolean): Promise<string> {
290-
const pathToModuleContent = await storage.listModules(
291-
(modulePath: string) => modulePath === oldModule.modulePath);
292-
if (! (oldModule.modulePath in pathToModuleContent)) {
293-
throw new Error('Failed to find module with path ' + oldModule.modulePath);
294-
}
295-
296289
const newModulePath = storageNames.makeModulePath(project.projectName, newClassName);
297-
const moduleContentText = pathToModuleContent[oldModule.modulePath].getModuleContentText();
290+
const moduleContentText = await storage.fetchModuleContentText(oldModule.modulePath);
298291
await storage.saveModule(newModulePath, moduleContentText);
299292
if (rename) {
300293
// For rename, delete the old module.
@@ -404,10 +397,9 @@ export function findModuleByModulePath(project: Project, modulePath: string): st
404397
* Produce the blob for downloading a project.
405398
*/
406399
export async function downloadProject(
407-
storage: commonStorage.Storage, projectName: string): Promise<string> {
408-
const modulePathPrefix = storageNames.makeModulePathPrefix(projectName);
400+
storage: commonStorage.Storage, project: Project): Promise<string> {
409401
const pathToModuleContent = await storage.listModules(
410-
(modulePath: string) => modulePath.startsWith(modulePathPrefix));
402+
storageNames.makeModulePathRegexPattern(project.projectName));
411403

412404
const classNameToModuleContentText: {[className: string]: string} = {}; // value is module content text
413405
for (const modulePath in pathToModuleContent) {

0 commit comments

Comments
 (0)