-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor
More file actions
276 lines (239 loc) · 8.05 KB
/
Copy pathmonitor
File metadata and controls
276 lines (239 loc) · 8.05 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Cordova Browser Monitor
* Monitors www directory for changes and syncs to platforms/browser/www
*/
class CordovaBrowserMonitor {
private $sourceDir;
private $targetDirs = [];
private $fileHashes = [];
private $excludePatterns = [
'/\.git/',
'/node_modules/',
'/\.DS_Store/',
'/Thumbs\.db/',
'/\.tmp/',
'/\.temp/'
];
public function __construct() {
$this->sourceDir = __DIR__ . '/www';
$this->targetDirs = [
__DIR__ . '/platforms/browser/www',
__DIR__ . '/platforms/android/app/src/main/assets/www'
];
// Ensure source directory exists
if (!is_dir($this->sourceDir)) {
die("Source directory does not exist: {$this->sourceDir}\n");
}
// Check target directories and warn if missing
foreach ($this->targetDirs as $targetDir) {
if (!is_dir($targetDir)) {
$this->log("Warning: Target directory does not exist: {$targetDir}");
}
}
$this->log("Monitor initialized");
$this->log("Source: {$this->sourceDir}");
foreach ($this->targetDirs as $targetDir) {
$this->log("Target: {$targetDir}");
}
}
/**
* Start monitoring for file changes
*/
public function start() {
$this->log("Starting Cordova Browser Monitor...");
$this->log("Press Ctrl+C to stop monitoring");
// Initial sync
$this->syncAll();
$this->buildFileHashMap();
while (true) {
$this->checkForChanges();
usleep(500000); // Check every 0.5 seconds
}
}
/**
* Perform initial full sync
*/
private function syncAll() {
$this->log("Performing initial sync...");
foreach ($this->targetDirs as $targetDir) {
if (is_dir($targetDir)) {
$this->copyDirectory($this->sourceDir, $targetDir, true);
}
}
$this->log("Initial sync completed");
}
/**
* Build hash map of all files
*/
private function buildFileHashMap() {
$this->fileHashes = [];
$this->scanDirectory($this->sourceDir, function($file) {
$relativePath = $this->getRelativePath($file, $this->sourceDir);
if (!$this->shouldExclude($relativePath)) {
$this->fileHashes[$relativePath] = md5_file($file);
}
});
$this->log("Built hash map for " . count($this->fileHashes) . " files");
}
/**
* Check for file changes
*/
private function checkForChanges() {
$currentHashes = [];
$changes = [];
// Scan for current files and detect changes
$this->scanDirectory($this->sourceDir, function($file) use (&$currentHashes, &$changes) {
$relativePath = $this->getRelativePath($file, $this->sourceDir);
if ($this->shouldExclude($relativePath)) {
return;
}
$currentHash = md5_file($file);
$currentHashes[$relativePath] = $currentHash;
// Check if file is new or modified
if (!isset($this->fileHashes[$relativePath])) {
$changes[] = ['type' => 'added', 'file' => $relativePath];
} elseif ($this->fileHashes[$relativePath] !== $currentHash) {
$changes[] = ['type' => 'modified', 'file' => $relativePath];
}
});
// Check for deleted files
foreach ($this->fileHashes as $relativePath => $hash) {
if (!isset($currentHashes[$relativePath])) {
$changes[] = ['type' => 'deleted', 'file' => $relativePath];
}
}
// Process changes
if (!empty($changes)) {
$this->processChanges($changes);
$this->fileHashes = $currentHashes;
}
}
/**
* Process detected changes
*/
private function processChanges($changes) {
foreach ($changes as $change) {
$relativePath = $change['file'];
$sourcePath = $this->sourceDir . '/' . $relativePath;
switch ($change['type']) {
case 'added':
case 'modified':
foreach ($this->targetDirs as $targetDir) {
if (is_dir($targetDir)) {
$targetPath = $targetDir . '/' . $relativePath;
$this->copyFile($sourcePath, $targetPath);
}
}
$this->log("🔄 {$change['type']}: {$relativePath}");
break;
case 'deleted':
foreach ($this->targetDirs as $targetDir) {
$targetPath = $targetDir . '/' . $relativePath;
if (file_exists($targetPath)) {
unlink($targetPath);
}
}
$this->log("🗑️ deleted: {$relativePath}");
break;
}
}
}
/**
* Copy a single file
*/
private function copyFile($source, $target) {
$targetDir = dirname($target);
if (!is_dir($targetDir)) {
mkdir($targetDir, 0755, true);
}
copy($source, $target);
}
/**
* Recursively copy directory
*/
private function copyDirectory($source, $target, $overwrite = false) {
if (!is_dir($target)) {
mkdir($target, 0755, true);
}
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $item) {
$relativePath = $this->getRelativePath($item->getPathname(), $source);
if ($this->shouldExclude($relativePath)) {
continue;
}
$targetPath = $target . DIRECTORY_SEPARATOR . $relativePath;
if ($item->isDir()) {
if (!is_dir($targetPath)) {
mkdir($targetPath, 0755, true);
}
} else {
if ($overwrite || !file_exists($targetPath)) {
$this->copyFile($item->getPathname(), $targetPath);
}
}
}
}
/**
* Scan directory with callback
*/
private function scanDirectory($dir, $callback) {
if (!is_dir($dir)) return;
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $item) {
if ($item->isFile()) {
$callback($item->getPathname());
}
}
}
/**
* Get relative path
*/
private function getRelativePath($file, $base) {
$file = str_replace('\\', '/', realpath($file));
$base = str_replace('\\', '/', realpath($base));
return ltrim(substr($file, strlen($base)), '/');
}
/**
* Check if file should be excluded
*/
private function shouldExclude($path) {
foreach ($this->excludePatterns as $pattern) {
if (preg_match($pattern, $path)) {
return true;
}
}
return false;
}
/**
* Log message with timestamp
*/
private function log($message) {
$timestamp = date('Y-m-d H:i:s');
echo "[{$timestamp}] {$message}\n";
}
}
// Handle command line arguments
if (php_sapi_name() !== 'cli') {
die("This script must be run from command line.\n");
}
// Handle Ctrl+C gracefully
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGINT, function() {
echo "\n\n🛑 Monitor stopped by user\n";
exit(0);
});
}
try {
$monitor = new CordovaBrowserMonitor();
$monitor->start();
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
exit(1);
}