-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomate_fetch_github_remove_non_php.php
101 lines (92 loc) · 2.83 KB
/
automate_fetch_github_remove_non_php.php
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
#!/usr/bin/env php
<?php
// future proving on styling change
// retrieve from github, copied to target(s) folder, execute removeNonePHP, then work on differences manually
/*
* the main function to call, fetch()
*/
fetch();
function fetch() {
$targets = [
"https://github.com/laravel/laravel/archive/master.zip",
"https://github.com/laravel/framework/archive/4.2.zip",
];
foreach ($targets as $target) {
preg_match("/\/([^\/]+)\/archive\/([^\/]+)\.zip$/", $target, $matches); // print_r($matches);
array_shift($matches);
$folder = implode('-', $matches);
$filename = $folder . '.zip'; // echo $filename;
try {
file_put_contents($filename, @file($target)); //swap for fast execution
// file_put_contents($filename, @file($filename)); //swap for fast execution
$zip = new ZipArchive;
$res = $zip->open($filename);
if ($res === TRUE) {
$zip->extractTo('./');
$zip->close();
}
} catch (Exception $e) {
echo $e;
}
echo "Downloaded $target, extracted to $folder", PHP_EOL;
echo removeNonePHP($folder, '');
echo recursiveCopy($folder, $folder . "_phpfmt");
// echo recursiveCopy($folder, $folder . "_kdiff3");
}
return true;
}
function recursiveCopy($target, $destination) {
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($target));
$it->rewind(); // create directories part
while ($it->valid()) {
if ($it->isDir()) {
$path = str_replace($target, $destination, $it->getPath());
if (!file_exists($path)) {
mkdir($path);
}
// echo "Created directory: ", $path, PHP_EOL;
}
$it->next();
}
$it->rewind(); // copy files part
while ($it->valid()) {
if (!$it->isDot()) {
$src = $it->getPathName();
$dest = str_replace($target, $destination, $src);
copy($src, $dest);
// echo "Copied file: " . $src . " => " . $dest . "\n"; // die;
}
$it->next();
}
return "Duplicated $target to $destination\n";
}
function removeNonePHP($target, $appended) {
$directory = $target . $appended . DIRECTORY_SEPARATOR; // echo $directory;
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
$it->rewind(); // only php part
while ($it->valid()) {
if (!$it->isDot()) {
if (!preg_match('/\.php$/', $it->key())) {
unlink($it->key());
// echo "deleted file: ", $it->key(), PHP_EOL;
} else {
// see if can normalized.
$convertEOL = file_get_contents($it->key());
$convertEOL = str_replace("\r\n", "\n", $convertEOL);
file_put_contents($it->key(), $convertEOL);
}
}
$it->next();
}
$it->rewind(); // remove empty folder part
while ($it->valid()) {
if ($it->isDir() and preg_match('/\.\.$/', $it->key())) {
if (count(glob($it->getPath() . DIRECTORY_SEPARATOR . "*")) === 0) {
rmdir($it->getPath());
// echo "removed directory: ", $it->key(), PHP_EOL;
}
}
$it->next();
}
return "Cleaned $directory\n";
}