Skip to content

Commit 738f1e5

Browse files
committed
replace hardcoded copy
1 parent 54b67f7 commit 738f1e5

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

config/nativephp-internal.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@
9898
'vendor/bin',
9999
],
100100

101+
/**
102+
* A list of files and folders that should be forcibly included
103+
* even if they match exclusion patterns.
104+
*
105+
* Internal use only for now
106+
*/
107+
'cleanup_include_files' => [
108+
'vendor/nativephp/desktop/resources/electron/electron-plugin/src/preload/livewire-dispatcher.js',
109+
],
110+
101111
/**
102112
* The binary path of PHP for NativePHP to use at build.
103113
*/

src/Builder/Concerns/CopiesToBuildDirectory.php

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function copyToBuildDirectory(): bool
8888
}
8989

9090
$this->keepRequiredDirectories();
91-
$this->keepLivewireDispatcher();
91+
$this->copyIncludedFiles();
9292

9393
return true;
9494
}
@@ -108,16 +108,45 @@ private function keepRequiredDirectories()
108108
$filesystem->dumpFile("{$buildPath}/storage/logs/_native.json", '{}');
109109
}
110110

111-
private function keepLivewireDispatcher()
111+
private function copyIncludedFiles(): void
112112
{
113-
// This is a bit leaky, since we only need this for electron, not potential other drivers
114-
// We'll find a better place for it when we add more drivers.
115-
$dispatcherPath = 'vendor/nativephp/desktop/resources/electron/electron-plugin/src/preload/livewire-dispatcher.js';
113+
114+
$sourcePath = $this->sourcePath();
115+
$buildPath = $this->buildPath('app');
116116
$filesystem = new Filesystem;
117117

118-
$filesystem->copy(
119-
$this->sourcePath($dispatcherPath),
120-
$this->buildPath("app/{$dispatcherPath}")
118+
$patterns = array_merge(
119+
config('nativephp-internal.cleanup_include_files', []),
120+
config('nativephp.cleanup_include_files', []),
121121
);
122+
123+
foreach ($patterns as $pattern) {
124+
$matchingFiles = glob($sourcePath . '/' . $pattern, GLOB_BRACE);
125+
126+
foreach ($matchingFiles as $sourceFile) {
127+
$relativePath = substr($sourceFile, strlen($sourcePath) + 1);
128+
$targetFile = $buildPath . '/' . $relativePath;
129+
130+
// Create target directory if it doesn't exist
131+
$targetDir = dirname($targetFile);
132+
if (!is_dir($targetDir)) {
133+
$filesystem->mkdir($targetDir, 0755);
134+
}
135+
136+
// Copy the file
137+
if (is_file($sourceFile)) {
138+
copy($sourceFile, $targetFile);
139+
140+
// Preserve permissions on non-Windows systems
141+
if (PHP_OS_FAMILY !== 'Windows') {
142+
$perms = fileperms($sourceFile);
143+
if ($perms !== false) {
144+
chmod($targetFile, $perms);
145+
}
146+
}
147+
}
148+
}
149+
}
122150
}
151+
123152
}

tests/Build/CopyToBuildDirectoryTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,24 @@ public function buildPath(string $path = ''): string
219219

220220
expect(fileperms("$buildPath/app/file-under-test.txt"))->toBe($originalPermissions);
221221
});
222+
223+
it('keeps cleanup_include_files if their parent directory is excluded', function () use ($sourcePath, $buildPath, $command) {
224+
225+
createFiles([
226+
"$sourcePath/excluded/foo/remove.txt",
227+
"$sourcePath/excluded/foo/keep.txt",
228+
]);
229+
230+
config()->set('nativephp.cleanup_exclude_files', [
231+
'excluded/*',
232+
]);
233+
234+
config()->set('nativephp-internal.cleanup_include_files', [
235+
'excluded/foo/keep.txt',
236+
]);
237+
238+
$command->copyToBuildDirectory();
239+
240+
expect("$buildPath/app/excluded/foo/remove.txt")->not->toBeFile();
241+
expect("$buildPath/app/excluded/foo/keep.txt")->toBeFile();
242+
});

0 commit comments

Comments
 (0)