Skip to content

Commit 6ce815b

Browse files
committed
refatored extensions.js
1 parent 1fadbda commit 6ce815b

File tree

1 file changed

+76
-191
lines changed

1 file changed

+76
-191
lines changed

extension.js

Lines changed: 76 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -419,56 +419,19 @@ async function createHelloWorld() {
419419

420420
// Update setup.mk with correct paths
421421
const setupMkPath = path.join(projectPath, 'setup.mk');
422-
if (fs.existsSync(setupMkPath)) {
423-
// Read the setup.mk file
424-
let setupMkContent = fs.readFileSync(setupMkPath, 'utf8');
425-
426-
// Replace placeholders in setup.mk with actual paths
427-
setupMkContent = setupMkContent
428-
.replace(/\$\(GCC_PATH\)/g, gccPath.replace(/\\/g, '/'))
429-
.replace(/\$\(PS1SDK_PATH\)/g, ps1SdkPath.replace(/\\/g, '/'))
430-
.replace(/\$\(PLUGIN_SDK_PATH\)/g, ps1SdkPath.replace(/\\/g, '/'))
431-
.replace(/\$\(EMULATOR_PATH\)/g, emulatorPath.replace(/\\/g, '/'))
432-
.replace(/\$\(GDB_PATH\)/g, gdbPath.replace(/\\/g, '/'));
433-
434-
// Write the updated setup.mk
435-
fs.writeFileSync(setupMkPath, setupMkContent);
436-
}
422+
await updatePathsInFile(setupMkPath);
437423

438-
// Update launch.json with correct paths if it exists
439-
const vscodeFolderPath = path.join(projectPath, '.vscode');
440-
const launchJsonPath = path.join(vscodeFolderPath, 'launch.json');
441-
if (fs.existsSync(launchJsonPath)) {
442-
// Read the launch.json file
443-
let launchJsonContent = fs.readFileSync(launchJsonPath, 'utf8');
444-
445-
// Get the target name from the Makefile
446-
const makefilePath = path.join(projectPath, 'Makefile');
447-
let targetName = 'hello_world'; // Default target name
448-
449-
if (fs.existsSync(makefilePath)) {
450-
const makefileContent = fs.readFileSync(makefilePath, 'utf8');
451-
const targetMatch = makefileContent.match(/TARGET\s*=\s*([\w_-]+)/);
452-
if (targetMatch && targetMatch[1]) {
453-
targetName = targetMatch[1];
454-
}
455-
}
456-
457-
// Replace placeholders in launch.json with actual paths
458-
launchJsonContent = launchJsonContent
459-
.replace(/\$\(GDB_PATH\)/g, gdbPath.replace(/\\/g, '/'))
460-
.replace(/\$\{workspaceFolder\}\/bin\/hello_world/g, `\${workspaceFolder}/bin/${targetName}`);
461-
462-
// Write the updated launch.json
463-
fs.writeFileSync(launchJsonPath, launchJsonContent);
464-
}
424+
// Update launch.json with correct paths and target
425+
await updateLaunchJson(projectPath);
465426

466427
vscode.window.showInformationMessage('Hello World project created and configured with correct SDK paths.');
467428

468429
// Open the main.c file
469430
const mainCPath = path.join(projectPath, 'main.c');
470-
const document = await vscode.workspace.openTextDocument(mainCPath);
471-
await vscode.window.showTextDocument(document);
431+
if (fs.existsSync(mainCPath)) {
432+
const document = await vscode.workspace.openTextDocument(mainCPath);
433+
await vscode.window.showTextDocument(document);
434+
}
472435
}
473436

474437
// Function to build the project
@@ -494,65 +457,11 @@ async function buildProject() {
494457

495458
// Check if setup.mk exists and update it
496459
const setupMkPath = path.join(projectPath, 'setup.mk');
497-
if (fs.existsSync(setupMkPath)) {
498-
// Read the setup.mk file
499-
let setupMkContent = fs.readFileSync(setupMkPath, 'utf8');
500-
501-
// Replace placeholders in setup.mk
502-
setupMkContent = setupMkContent
503-
.replace(/\$\(GCC_PATH\)/g, gccPath.replace(/\\/g, '/'))
504-
.replace(/\$\(PS1SDK_PATH\)/g, ps1SdkPath.replace(/\\/g, '/'))
505-
.replace(/\$\(PLUGIN_SDK_PATH\)/g, ps1SdkPath.replace(/\\/g, '/'))
506-
.replace(/\$\(EMULATOR_PATH\)/g, emulatorPath.replace(/\\/g, '/'))
507-
.replace(/\$\(GDB_PATH\)/g, gdbPath.replace(/\\/g, '/'));
508-
509-
// Write the updated setup.mk
510-
fs.writeFileSync(setupMkPath, setupMkContent);
511-
} else {
512-
// If setup.mk doesn't exist, update the Makefile directly
513-
// Read the Makefile
514-
let makefileContent = fs.readFileSync(makefilePath, 'utf8');
515-
516-
// Replace placeholders in the Makefile
517-
makefileContent = makefileContent
518-
.replace(/\$\(GCC_PATH\)/g, gccPath.replace(/\\/g, '/'))
519-
.replace(/\$\(PS1SDK_PATH\)/g, ps1SdkPath.replace(/\\/g, '/'))
520-
.replace(/\$\(PLUGIN_SDK_PATH\)/g, ps1SdkPath.replace(/\\/g, '/'))
521-
.replace(/\$\(EMULATOR_PATH\)/g, emulatorPath.replace(/\\/g, '/'))
522-
.replace(/\$\(GDB_PATH\)/g, gdbPath.replace(/\\/g, '/'));
523-
524-
// Write the updated Makefile
525-
fs.writeFileSync(makefilePath, makefileContent);
526-
}
527-
528-
// Update launch.json with correct paths if it exists
529-
const vscodeFolderPath = path.join(projectPath, '.vscode');
530-
const launchJsonPath = path.join(vscodeFolderPath, 'launch.json');
531-
if (fs.existsSync(launchJsonPath)) {
532-
// Read the launch.json file
533-
let launchJsonContent = fs.readFileSync(launchJsonPath, 'utf8');
534-
535-
// Get the target name from the Makefile
536-
const makefilePath = path.join(projectPath, 'Makefile');
537-
let targetName = 'hello_world'; // Default target name
538-
539-
if (fs.existsSync(makefilePath)) {
540-
const makefileContent = fs.readFileSync(makefilePath, 'utf8');
541-
const targetMatch = makefileContent.match(/TARGET\s*=\s*([\w_-]+)/);
542-
if (targetMatch && targetMatch[1]) {
543-
targetName = targetMatch[1];
544-
}
545-
}
546-
547-
// Replace placeholders in launch.json with actual paths
548-
launchJsonContent = launchJsonContent
549-
.replace(/\$\(GDB_PATH\)/g, gdbPath.replace(/\\/g, '/'))
550-
.replace(/\$\{workspaceFolder\}\/bin\/hello_world/g, `\${workspaceFolder}/bin/${targetName}`);
551-
552-
// Write the updated launch.json
553-
fs.writeFileSync(launchJsonPath, launchJsonContent);
554-
}
555-
460+
await updatePathsInFile(setupMkPath);
461+
462+
// Update launch.json with correct paths and target
463+
await updateLaunchJson(projectPath);
464+
556465
// Create a terminal and run make
557466
const terminal = vscode.window.createTerminal('PS1 Build');
558467
terminal.show();
@@ -596,50 +505,11 @@ async function runEmulator() {
596505

597506
// Check if setup.mk exists and update it
598507
const setupMkPath = path.join(projectPath, 'setup.mk');
599-
if (fs.existsSync(setupMkPath)) {
600-
// Read the setup.mk file
601-
let setupMkContent = fs.readFileSync(setupMkPath, 'utf8');
602-
603-
// Replace placeholders in setup.mk
604-
setupMkContent = setupMkContent
605-
.replace(/\$\(GCC_PATH\)/g, gccPath.replace(/\\/g, '/'))
606-
.replace(/\$\(PS1SDK_PATH\)/g, ps1SdkPath.replace(/\\/g, '/'))
607-
.replace(/\$\(PLUGIN_SDK_PATH\)/g, ps1SdkPath.replace(/\\/g, '/'))
608-
.replace(/\$\(EMULATOR_PATH\)/g, emulatorPath.replace(/\\/g, '/'))
609-
.replace(/\$\(GDB_PATH\)/g, gdbPath.replace(/\\/g, '/'));
610-
611-
// Write the updated setup.mk
612-
fs.writeFileSync(setupMkPath, setupMkContent);
613-
}
614-
615-
// Update launch.json with correct paths if it exists
616-
const vscodeFolderPath = path.join(projectPath, '.vscode');
617-
const launchJsonPath = path.join(vscodeFolderPath, 'launch.json');
618-
if (fs.existsSync(launchJsonPath)) {
619-
// Read the launch.json file
620-
let launchJsonContent = fs.readFileSync(launchJsonPath, 'utf8');
621-
622-
// Get the target name from the Makefile
623-
const makefilePath = path.join(projectPath, 'Makefile');
624-
let targetName = 'hello_world'; // Default target name
625-
626-
if (fs.existsSync(makefilePath)) {
627-
const makefileContent = fs.readFileSync(makefilePath, 'utf8');
628-
const targetMatch = makefileContent.match(/TARGET\s*=\s*([\w_-]+)/);
629-
if (targetMatch && targetMatch[1]) {
630-
targetName = targetMatch[1];
631-
}
632-
}
633-
634-
// Replace placeholders in launch.json with actual paths
635-
launchJsonContent = launchJsonContent
636-
.replace(/\$\(GDB_PATH\)/g, gdbPath.replace(/\\/g, '/'))
637-
.replace(/\$\{workspaceFolder\}\/bin\/hello_world/g, `\${workspaceFolder}/bin/${targetName}`);
638-
639-
// Write the updated launch.json
640-
fs.writeFileSync(launchJsonPath, launchJsonContent);
641-
}
642-
508+
await updatePathsInFile(setupMkPath);
509+
510+
// Update launch.json with correct paths and target
511+
await updateLaunchJson(projectPath);
512+
643513
// Create a terminal and run make
644514
const terminal = vscode.window.createTerminal('PS1 Emulator');
645515
terminal.show();
@@ -683,50 +553,11 @@ async function generateISO() {
683553

684554
// Check if setup.mk exists and update it
685555
const setupMkPath = path.join(projectPath, 'setup.mk');
686-
if (fs.existsSync(setupMkPath)) {
687-
// Read the setup.mk file
688-
let setupMkContent = fs.readFileSync(setupMkPath, 'utf8');
689-
690-
// Replace placeholders in setup.mk
691-
setupMkContent = setupMkContent
692-
.replace(/\$\(GCC_PATH\)/g, gccPath.replace(/\\/g, '/'))
693-
.replace(/\$\(PS1SDK_PATH\)/g, ps1SdkPath.replace(/\\/g, '/'))
694-
.replace(/\$\(PLUGIN_SDK_PATH\)/g, ps1SdkPath.replace(/\\/g, '/'))
695-
.replace(/\$\(EMULATOR_PATH\)/g, emulatorPath.replace(/\\/g, '/'))
696-
.replace(/\$\(GDB_PATH\)/g, gdbPath.replace(/\\/g, '/'));
697-
698-
// Write the updated setup.mk
699-
fs.writeFileSync(setupMkPath, setupMkContent);
700-
}
701-
702-
// Update launch.json with correct paths if it exists
703-
const vscodeFolderPath = path.join(projectPath, '.vscode');
704-
const launchJsonPath = path.join(vscodeFolderPath, 'launch.json');
705-
if (fs.existsSync(launchJsonPath)) {
706-
// Read the launch.json file
707-
let launchJsonContent = fs.readFileSync(launchJsonPath, 'utf8');
708-
709-
// Get the target name from the Makefile
710-
const makefilePath = path.join(projectPath, 'Makefile');
711-
let targetName = 'hello_world'; // Default target name
712-
713-
if (fs.existsSync(makefilePath)) {
714-
const makefileContent = fs.readFileSync(makefilePath, 'utf8');
715-
const targetMatch = makefileContent.match(/TARGET\s*=\s*([\w_-]+)/);
716-
if (targetMatch && targetMatch[1]) {
717-
targetName = targetMatch[1];
718-
}
719-
}
720-
721-
// Replace placeholders in launch.json with actual paths
722-
launchJsonContent = launchJsonContent
723-
.replace(/\$\(GDB_PATH\)/g, gdbPath.replace(/\\/g, '/'))
724-
.replace(/\$\{workspaceFolder\}\/bin\/hello_world/g, `\${workspaceFolder}/bin/${targetName}`);
725-
726-
// Write the updated launch.json
727-
fs.writeFileSync(launchJsonPath, launchJsonContent);
728-
}
729-
556+
await updatePathsInFile(setupMkPath);
557+
558+
// Update launch.json with correct paths and target
559+
await updateLaunchJson(projectPath);
560+
730561
// Create a terminal and run make
731562
const terminal = vscode.window.createTerminal('PS1 ISO Generation');
732563
terminal.show();
@@ -747,6 +578,60 @@ async function generateISO() {
747578
}
748579
}
749580

581+
// Function to update paths in a file
582+
async function updatePathsInFile(filePath, targetName = null) {
583+
if (!fs.existsSync(filePath)) {
584+
return false;
585+
}
586+
587+
let content = fs.readFileSync(filePath, 'utf8');
588+
589+
// Replace placeholders with actual paths
590+
content = content
591+
.replace(/\$\(GCC_PATH\)/g, gccPath.replace(/\\/g, '/'))
592+
.replace(/\$\(PS1SDK_PATH\)/g, ps1SdkPath.replace(/\\/g, '/'))
593+
.replace(/\$\(PLUGIN_SDK_PATH\)/g, ps1SdkPath.replace(/\\/g, '/'))
594+
.replace(/\$\(EMULATOR_PATH\)/g, emulatorPath.replace(/\\/g, '/'))
595+
.replace(/\$\(GDB_PATH\)/g, gdbPath.replace(/\\/g, '/'));
596+
597+
fs.writeFileSync(filePath, content);
598+
return true;
599+
}
600+
601+
// Function to update launch.json with correct paths and target
602+
async function updateLaunchJson(projectPath) {
603+
const vscodeFolderPath = path.join(projectPath, '.vscode');
604+
const launchJsonPath = path.join(vscodeFolderPath, 'launch.json');
605+
606+
if (!fs.existsSync(launchJsonPath)) {
607+
return false;
608+
}
609+
610+
// Get the target name from the Makefile
611+
const makefilePath = path.join(projectPath, 'Makefile');
612+
let targetName = 'hello_world'; // Default target name
613+
614+
if (fs.existsSync(makefilePath)) {
615+
const makefileContent = fs.readFileSync(makefilePath, 'utf8');
616+
const targetMatch = makefileContent.match(/TARGET\s*=\s*([\w_-]+)/);
617+
if (targetMatch && targetMatch[1]) {
618+
targetName = targetMatch[1];
619+
}
620+
}
621+
622+
// Read and update the launch.json file
623+
let launchJsonContent = fs.readFileSync(launchJsonPath, 'utf8');
624+
625+
// Replace placeholders in launch.json with actual paths
626+
launchJsonContent = launchJsonContent
627+
.replace(/\$\(GDB_PATH\)/g, gdbPath.replace(/\\/g, '/'))
628+
.replace(/\$\{workspaceFolder\}\/bin\/hello_world/g, `\${workspaceFolder}/bin/${targetName}`);
629+
630+
// Write the updated launch.json
631+
fs.writeFileSync(launchJsonPath, launchJsonContent);
632+
return true;
633+
}
634+
750635
// Function to download and extract a tool
751636
async function downloadAndExtractTool(toolName) {
752637
try {

0 commit comments

Comments
 (0)