From b31f0cdc0e0243e2d4cf48e466b7239b78483916 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Sun, 18 May 2025 15:08:00 +0200 Subject: [PATCH] feat: add electron-builder command to publish electron-builder.js --- src/Commands/ElectronBuilderCommand.php | 26 +++++ src/ElectronServiceProvider.php | 2 + src/Traits/PublishesElectronBuilderJs.php | 98 +++++++++++++++++++ .../Traits/PublishesElectronBuilderJsTest.php | 95 ++++++++++++++++++ 4 files changed, 221 insertions(+) create mode 100644 src/Commands/ElectronBuilderCommand.php create mode 100644 src/Traits/PublishesElectronBuilderJs.php create mode 100644 tests/Unit/Traits/PublishesElectronBuilderJsTest.php diff --git a/src/Commands/ElectronBuilderCommand.php b/src/Commands/ElectronBuilderCommand.php new file mode 100644 index 00000000..8c52b153 --- /dev/null +++ b/src/Commands/ElectronBuilderCommand.php @@ -0,0 +1,26 @@ +option('restore')) { + // Restore the electron-builder.js file + $this->restoreElectronBuilderJs(); + + return; + } + + // Publish the electron-builder.js file + $this->publishElectronBuilderJs($this->option('force')); + } +} diff --git a/src/ElectronServiceProvider.php b/src/ElectronServiceProvider.php index 568bdf2a..479419e3 100644 --- a/src/ElectronServiceProvider.php +++ b/src/ElectronServiceProvider.php @@ -6,6 +6,7 @@ use Native\Electron\Commands\BuildCommand; use Native\Electron\Commands\BundleCommand; use Native\Electron\Commands\DevelopCommand; +use Native\Electron\Commands\ElectronBuilderCommand; use Native\Electron\Commands\InstallCommand; use Native\Electron\Commands\PublishCommand; use Native\Electron\Commands\ResetCommand; @@ -27,6 +28,7 @@ public function configurePackage(Package $package): void PublishCommand::class, BundleCommand::class, ResetCommand::class, + ElectronBuilderCommand::class, ]); } diff --git a/src/Traits/PublishesElectronBuilderJs.php b/src/Traits/PublishesElectronBuilderJs.php new file mode 100644 index 00000000..02da2db0 --- /dev/null +++ b/src/Traits/PublishesElectronBuilderJs.php @@ -0,0 +1,98 @@ + $value) { + if (str_contains($value, '--config')) { + // Add the publish path to the script + $packageJson['scripts'][$key] = str_replace( + '--config', + "--config $destinationPath ", + $value + ); + } + } + + // Create a backup of the original package.json if not exists + $originalPackageJsonPath = __DIR__.'/../../resources/js/package.json.bck'; + if (! file_exists($originalPackageJsonPath)) { + copy($packageJsonPath, $originalPackageJsonPath); + } + + // Save the updated package.json + file_put_contents($packageJsonPath, json_encode($packageJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); + } + + /** + * Restore the build configuration to the original state. + * + * @see /resources/js/electron-builder.js + */ + public function restoreElectronBuilderJs(): void + { + intro('Restoring default electron build configuration...'); + + // Get the backup path + $originalPackageJsonPath = __DIR__.'/../../resources/js/package.json.bck'; + + if (! file_exists($originalPackageJsonPath)) { + throw new \Exception('The original package.json file does not exist.'); + } + + // Restore the original package.json file + $packageJsonPath = __DIR__.'/../../resources/js/package.json'; + if (! copy($originalPackageJsonPath, $packageJsonPath)) { + throw new \Exception('Failed to restore the original package.json file.'); + } + + // Remove the backup file + if (file_exists($originalPackageJsonPath)) { + unlink($originalPackageJsonPath); + } + } +} diff --git a/tests/Unit/Traits/PublishesElectronBuilderJsTest.php b/tests/Unit/Traits/PublishesElectronBuilderJsTest.php new file mode 100644 index 00000000..6ac96b0b --- /dev/null +++ b/tests/Unit/Traits/PublishesElectronBuilderJsTest.php @@ -0,0 +1,95 @@ +setBasePath(realpath(__DIR__.'/../../../')); + + // Copy the original package.json file to a backup + $packageJsonPath = base_path('resources/js/package.json'); + $originalPackageJsonPath = base_path('resources/js/package.json.original'); + + // Create a backup of the file + copy($packageJsonPath, $originalPackageJsonPath); +}); + +afterEach(function () { + // Clean up after test + $publishPath = base_path('resources/vendor/nativephp/js/electron-builder.js'); + + // Remove the backup file + if (file_exists($publishPath)) { + unlink($publishPath); + } + + // Restore the original package.json file + $packageJsonPath = base_path('resources/js/package.json'); + $originalPackageJsonPath = base_path('resources/js/package.json.original'); + + // Restore the original file + if (file_exists($originalPackageJsonPath)) { + copy($originalPackageJsonPath, $packageJsonPath); + + // Remove the original copy + unlink($originalPackageJsonPath); + } + + // Remove package.json.bck + $packageJsonBckPath = base_path('resources/js/package.json.bck'); + if (file_exists($packageJsonBckPath)) { + unlink($packageJsonBckPath); + } +}); + +it('can publish a copy of the electron-builder.js to the destination folder and update the package.json', function (object $mock) { + $mock->publishElectronBuilderJs(); + + $publishPath = base_path('resources/vendor/nativephp/js/electron-builder.js'); + + expect(file_exists($publishPath))->toBeTrue() + ->and(file_get_contents($publishPath))->not->toBe('') + ->and(file_get_contents($publishPath))->toBe(file_get_contents(base_path('resources/js/electron-builder.js'))); + + // Check if the package.json file has been updated + $packageJsonPath = base_path('resources/js/package.json'); + $packageJson = json_decode(file_get_contents($packageJsonPath), true); + $publishPath = base_path('resources/vendor/nativephp/js/electron-builder.js'); + + // Check if the publish path is in the package.json + expect($packageJson['scripts']['build:win-x64'])->toContain('--config '.$publishPath) + ->and($packageJson['scripts']['build:mac-arm64'])->toContain('--config '.$publishPath) + ->and($packageJson['scripts']['build:mac-x86'])->toContain('--config '.$publishPath) + ->and($packageJson['scripts']['build:linux-x64'])->toContain('--config '.$publishPath); +}) + ->with([ + // Empty class with the MergesElectronConfig trait + new class + { + use PublishesElectronBuilderJs; + }, + ]); + +it('can restore the original package state', function (object $mock) { + // Create a backup of the original package.json file + $packageJsonPath = base_path('resources/js/package.json'); + $packageJsonContents = file_get_contents($packageJsonPath); + + // Publish the electron-builder.js file + $mock->publishElectronBuilderJs(true); + + // Restore the original package.json file + $mock->restoreElectronBuilderJs(); + + // Check if the package.json file has been restored + expect($packageJsonContents)->toBe(file_get_contents($packageJsonPath)); +}) + ->with([ + // Empty class with the MergesElectronConfig trait + new class + { + use PublishesElectronBuilderJs; + }, + ]);