Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config/nativephp-internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@
'vendor/bin',
],

/**
* A list of files and folders that should be forcibly included
* even if they match exclusion patterns.
*
* Internal use only for now
*/
'cleanup_include_files' => [
'vendor/nativephp/desktop/resources/electron/electron-plugin/src/preload/livewire-dispatcher.js',
],

/**
* The binary path of PHP for NativePHP to use at build.
*/
Expand Down
44 changes: 36 additions & 8 deletions src/Builder/Concerns/CopiesToBuildDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function copyToBuildDirectory(): bool
}

$this->keepRequiredDirectories();
$this->keepLivewireDispatcher();
$this->copyIncludedFiles();

return true;
}
Expand All @@ -108,16 +108,44 @@ private function keepRequiredDirectories()
$filesystem->dumpFile("{$buildPath}/storage/logs/_native.json", '{}');
}

private function keepLivewireDispatcher()
private function copyIncludedFiles(): void
{
// This is a bit leaky, since we only need this for electron, not potential other drivers
// We'll find a better place for it when we add more drivers.
$dispatcherPath = 'vendor/nativephp/desktop/resources/electron/electron-plugin/src/preload/livewire-dispatcher.js';

$sourcePath = $this->sourcePath();
$buildPath = $this->buildPath('app');
$filesystem = new Filesystem;

$filesystem->copy(
$this->sourcePath($dispatcherPath),
$this->buildPath("app/{$dispatcherPath}")
$patterns = array_merge(
config('nativephp-internal.cleanup_include_files', []),
config('nativephp.cleanup_include_files', []),
);

foreach ($patterns as $pattern) {
$matchingFiles = glob($sourcePath.'/'.$pattern, GLOB_BRACE);

foreach ($matchingFiles as $sourceFile) {
$relativePath = substr($sourceFile, strlen($sourcePath) + 1);
$targetFile = $buildPath.'/'.$relativePath;

// Create target directory if it doesn't exist
$targetDir = dirname($targetFile);
if (! is_dir($targetDir)) {
$filesystem->mkdir($targetDir, 0755);
}

// Copy the file
if (is_file($sourceFile)) {
copy($sourceFile, $targetFile);

// Preserve permissions on non-Windows systems
if (PHP_OS_FAMILY !== 'Windows') {
$perms = fileperms($sourceFile);
if ($perms !== false) {
chmod($targetFile, $perms);
}
}
}
}
}
}
}
21 changes: 21 additions & 0 deletions tests/Build/CopyToBuildDirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,24 @@ public function buildPath(string $path = ''): string

expect(fileperms("$buildPath/app/file-under-test.txt"))->toBe($originalPermissions);
});

it('keeps cleanup_include_files if their parent directory is excluded', function () use ($sourcePath, $buildPath, $command) {

createFiles([
"$sourcePath/excluded/foo/remove.txt",
"$sourcePath/excluded/foo/keep.txt",
]);

config()->set('nativephp.cleanup_exclude_files', [
'excluded/*',
]);

config()->set('nativephp-internal.cleanup_include_files', [
'excluded/foo/keep.txt',
]);

$command->copyToBuildDirectory();

expect("$buildPath/app/excluded/foo/remove.txt")->not->toBeFile();
expect("$buildPath/app/excluded/foo/keep.txt")->toBeFile();
});