forked from NativePHP/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopiesToBuildDirectory.php
More file actions
143 lines (114 loc) · 4.67 KB
/
CopiesToBuildDirectory.php
File metadata and controls
143 lines (114 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* This trait is responsible for copying over the app to the build directory.
* It skips any ignored paths/globs during the copy step
*
* TODO: When more drivers/adapters are added, this should be relocated
*/
namespace Native\Electron\Traits;
use RecursiveCallbackFilterIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Symfony\Component\Filesystem\Filesystem;
use Throwable;
use function Laravel\Prompts\warning;
trait CopiesToBuildDirectory
{
abstract protected function buildPath(string $path = ''): string;
abstract protected function sourcePath(string $path = ''): string;
public array $cleanupExcludeFiles = [
// .git and dev directories
'.git',
'dist',
'build',
'temp',
'docker',
'packages',
'**/.github',
// Potentially containing sensitive info
'auth.json', // Composer auth file
'database/*.sqlite',
'database/*.sqlite-shm',
'database/*.sqlite-wal',
'storage/framework/sessions/*',
'storage/framework/testing/*',
'storage/framework/cache/*',
'storage/framework/views/*',
'storage/logs/*',
// Only needed for local testing
'vendor/nativephp/electron/resources',
'vendor/nativephp/electron/vendor',
'vendor/nativephp/electron/bin',
'vendor/nativephp/laravel/vendor',
'vendor/nativephp/php-bin',
// Also deleted in PrunesVendorDirectory after fresh composer install
'vendor/bin',
];
public function copyToBuildDirectory(): bool
{
$sourcePath = $this->sourcePath();
$buildPath = $this->buildPath();
$filesystem = new Filesystem;
$patterns = array_merge(
$this->cleanupExcludeFiles,
config('nativephp.cleanup_exclude_files', []),
);
// Clean and create build directory
$filesystem->remove($buildPath);
$filesystem->mkdir($buildPath);
// A filtered iterator that will exclude files matching our skip patterns
$directory = new RecursiveDirectoryIterator(
$sourcePath,
RecursiveDirectoryIterator::SKIP_DOTS | RecursiveDirectoryIterator::FOLLOW_SYMLINKS
);
$filter = new RecursiveCallbackFilterIterator($directory, function ($current) use ($patterns) {
$relativePath = substr($current->getPathname(), strlen($this->sourcePath()) + 1);
$relativePath = str_replace(DIRECTORY_SEPARATOR, '/', $relativePath); // Windows
// Check each skip pattern against the current file/directory
foreach ($patterns as $pattern) {
// fnmatch supports glob patterns like "*.txt" or "cache/*"
if (fnmatch($pattern, $relativePath)) {
return false;
}
}
return true;
});
// Now we walk all directories & files and copy them over accordingly
$iterator = new RecursiveIteratorIterator($filter, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $item) {
$target = $buildPath.DIRECTORY_SEPARATOR.substr($item->getPathname(), strlen($sourcePath) + 1);
if ($item->isDir()) {
if (! is_dir($target)) {
mkdir($target, 0755, true);
}
continue;
}
try {
copy($item->getPathname(), $target);
if (PHP_OS_FAMILY !== 'Windows') {
$perms = fileperms($item->getPathname());
if ($perms !== false) {
chmod($target, $perms);
}
}
} catch (Throwable $e) {
warning('[WARNING] '.$e->getMessage().', file: '.$item->getPathname());
}
}
$this->keepRequiredDirectories();
return true;
}
private function keepRequiredDirectories()
{
// Electron build removes empty folders, so we have to create dummy files
// dotfiles unfortunately don't work.
$filesystem = new Filesystem;
$buildPath = $this->buildPath();
$filesystem->dumpFile("{$buildPath}/storage/framework/cache/_native.json", '{}');
$filesystem->dumpFile("{$buildPath}/storage/framework/sessions/_native.json", '{}');
$filesystem->dumpFile("{$buildPath}/storage/framework/testing/_native.json", '{}');
$filesystem->dumpFile("{$buildPath}/storage/framework/views/_native.json", '{}');
$filesystem->dumpFile("{$buildPath}/storage/app/public/_native.json", '{}');
$filesystem->dumpFile("{$buildPath}/storage/logs/_native.json", '{}');
}
}