Skip to content

Commit a7140af

Browse files
committed
More refactoring and bug fixes
1 parent 0b28c00 commit a7140af

File tree

6 files changed

+43
-29
lines changed

6 files changed

+43
-29
lines changed

vscode-wpilib/src/cpp/commands.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
import * as fs from 'fs';
3-
import { cp } from 'fs/promises';
3+
import { copyFile } from 'fs/promises';
44
import * as jsonc from 'jsonc-parser';
55
import * as path from 'path';
66
import * as vscode from 'vscode';
@@ -33,14 +33,14 @@ async function performCopy(
3333
const renamedHeader = path.join(folderHeader.fsPath, `${replaceName}.h`);
3434
const renamedSource = path.join(folderSrc.fsPath, `${replaceName}.cpp`);
3535
// The source and header arrays are always one item long
36-
await cp(path.join(commandFolder, command.source[0]), renamedHeader);
37-
await cp(path.join(commandFolder, command.headers[0]), renamedSource);
36+
await copyFile(path.join(commandFolder, command.headers[0]), renamedHeader);
37+
await copyFile(path.join(commandFolder, command.source[0]), renamedSource);
3838

3939
// Process header files
4040
const headerReplacements = new Map<RegExp, string>();
4141
headerReplacements.set(new RegExp(command.replacename, 'g'), replaceName);
4242

43-
await fileUtils.processFile(renamedHeader, folderHeader.fsPath, headerReplacements);
43+
await fileUtils.processFile(renamedHeader, headerReplacements);
4444
// Process source files with more complex replacements
4545
const sourceReplacements = new Map<RegExp, string>();
4646
const joinedName = path
@@ -53,7 +53,7 @@ async function performCopy(
5353
);
5454
sourceReplacements.set(new RegExp(command.replacename, 'g'), replaceName);
5555

56-
await fileUtils.processFile(renamedSource, folderSrc.fsPath, sourceReplacements);
56+
await fileUtils.processFile(renamedSource, sourceReplacements);
5757
return true;
5858
} catch (error) {
5959
logger.error('Error performing copy operation:', error);

vscode-wpilib/src/java/commands.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
2-
import * as fs from 'fs';
3-
import { cp } from 'fs/promises';
2+
import { readFile } from 'fs';
3+
import { copyFile } from 'fs/promises';
44
import * as jsonc from 'jsonc-parser';
55
import * as path from 'path';
66
import * as vscode from 'vscode';
77
import { ICommandAPI, ICommandCreator, IPreferencesAPI } from '../api';
88
import { logger } from '../logger';
9-
import { getClassName, getPackageName } from '../utilities';
109
import * as fileUtils from '../shared/fileUtils';
10+
import { getClassName, getPackageName } from '../utilities';
1111

1212
export interface IJavaJsonLayout {
1313
name: string;
@@ -27,7 +27,7 @@ async function performCopy(
2727
try {
2828
// Copy files and track them
2929
const renamedCommand = path.join(folder.fsPath, `${replaceName}.java`);
30-
await cp(
30+
await copyFile(
3131
path.join(commandRoot, command.foldername, `${command.replacename}.java`),
3232
renamedCommand
3333
);
@@ -42,7 +42,7 @@ async function performCopy(
4242
replacements.set(new RegExp(command.replacename, 'g'), replaceName);
4343

4444
// Process files with replacements
45-
await fileUtils.processFile(renamedCommand, folder.fsPath, replacements);
45+
await fileUtils.processFile(renamedCommand, replacements);
4646

4747
const document = await vscode.workspace.openTextDocument(vscode.Uri.file(renamedCommand));
4848
await vscode.window.showTextDocument(document);
@@ -59,7 +59,7 @@ export class Commands {
5959
constructor(resourceRoot: string, core: ICommandAPI, preferences: IPreferencesAPI) {
6060
const commandFolder = path.join(resourceRoot, 'src', 'commands');
6161
const resourceFile = path.join(commandFolder, this.commandResourceName);
62-
fs.readFile(resourceFile, 'utf8', (err, data) => {
62+
readFile(resourceFile, 'utf8', (err, data) => {
6363
if (err) {
6464
logger.error('Command file error: ', err);
6565
return;

vscode-wpilib/src/shared/fileUtils.ts

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

33
import { readFile, writeFile } from 'fs/promises';
4-
import * as path from 'path';
54
import { logger } from '../logger';
65

7-
export async function processFile(
8-
file: string,
9-
basePath: string,
10-
replacements: Map<RegExp, string>
11-
) {
12-
return updateFileContents(path.join(basePath, file), (content) => {
6+
/**
7+
* Updates a file using a set of regex replacements.
8+
* @param filePath The absolute path to a file.
9+
* @param replacements The regexes and what they are to be replaced with.
10+
*/
11+
export async function processFile(filePath: string, replacements: Map<RegExp, string>) {
12+
return updateFileContents(filePath, (content) => {
1313
// Apply all replacements
1414
for (const [pattern, replacement] of replacements) {
1515
content = content.replace(pattern, replacement);
@@ -19,7 +19,8 @@ export async function processFile(
1919
}
2020

2121
/**
22-
* Safely updates file contents by reading and writing atomically
22+
* Safely updates file contents by reading and writing atomically.
23+
* @param filePath The absolute path to a file.
2324
*/
2425
export async function updateFileContents(
2526
filePath: string,

vscode-wpilib/src/shared/generator.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ export async function generateCopyCpp(
5555
}
5656
}
5757

58+
/**
59+
* Generates a Java project.
60+
* @param resourcesFolder The folder where the extension resources for a language are.
61+
* @param fromTemplateFolder The folder where the template/project source files are.
62+
* @param fromTemplateTestFolder The folder where the template/project test source files are.
63+
* @param fromGradleFolder The folder where build.gradle is located.
64+
* @param toFolder The folder to copy everything to. This is the root of the project.
65+
* @param robotClassTo The main robot class.
66+
* @param copyRoot The base package in folder form to copy to.
67+
* @param directGradleImport Whether or not the file in fromTemplateFolder are in the Gradle project structure already.
68+
* @param extraVendordeps List of extra WPILib vendordeps to add to the project.
69+
* @param packageReplaceString The base package to replace with frc.robot.
70+
* @returns True if the project successfully generated, false otherwise.
71+
*/
5872
export async function generateCopyJava(
5973
resourcesFolder: string,
6074
fromTemplateFolder: string,
@@ -105,16 +119,12 @@ export async function generateCopyJava(
105119
}
106120

107121
// Process template files
108-
await Promise.all(
109-
files.map((testFile) => fileUtils.processFile(testFile, codePath, replacements))
110-
);
122+
await Promise.all(files.map((file) => fileUtils.processFile(file, replacements)));
111123

112124
// Process test files if they exist
113125
if (fromTemplateTestFolder !== undefined) {
114126
const testFiles = await genUtils.findMatchingFiles(testPath);
115-
await Promise.all(
116-
testFiles.map((testFile) => fileUtils.processFile(testFile, testPath, replacements))
117-
);
127+
await Promise.all(testFiles.map((testFile) => fileUtils.processFile(testFile, replacements)));
118128
}
119129

120130
// Setup project structure

vscode-wpilib/src/shared/pathUtils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use strict';
22

3-
import { readdir } from 'fs/promises';
3+
import { copyFile, readdir } from 'fs/promises';
44
import * as path from 'path';
55
import { logger } from '../logger';
6-
import { copyFileAsync } from '../utilities';
76

87
/**
98
* Creates source and test paths based on project type and import mode
@@ -48,7 +47,7 @@ export async function copyVendorDep(
4847
try {
4948
const sourcePath = path.join(path.dirname(resourcesFolder), 'vendordeps', vendorDepName);
5049
const targetPath = path.join(targetDir, vendorDepName);
51-
await copyFileAsync(sourcePath, targetPath);
50+
await copyFile(sourcePath, targetPath);
5251
return true;
5352
} catch (err) {
5453
logger.error(`Failed to copy vendor dependency: ${vendorDepName}`, err);

vscode-wpilib/src/shared/projectGeneratorUtils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ export async function findMatchingFiles(
6565
}
6666

6767
/**
68-
* Setup project structure and copy Gradle files
68+
* Setup project structure and copy Gradle files.
69+
* @param fromGradleFolder The folder where the files, like build.gradle, for a specific project type are located.
70+
* @param toFolder The folder to copy files to.
71+
* @param grRoot The folder where the extension's Gradle files are.
6972
*/
7073
export async function setupProjectStructure(
7174
fromGradleFolder: string,
@@ -119,6 +122,7 @@ export async function setupDeployDirectory(
119122
isJava: boolean
120123
): Promise<boolean> {
121124
try {
125+
// Already done when files were copied to the code path
122126
if (directGradleImport) {
123127
return true;
124128
}

0 commit comments

Comments
 (0)