-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefactor_pkg.php
107 lines (97 loc) · 2.65 KB
/
refactor_pkg.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
102
103
104
105
106
107
<?php
do {
echo "Что ищем: ";
$find = trim(fgets(STDIN));
} while ($find == '');
do {
echo "На что меняем: ";
$replace = trim(fgets(STDIN));
} while ($replace == '');
echo PHP_EOL;
echo sprintf('Скрипт заменит во всех файлах "%s" на "%s"', $find, $replace).PHP_EOL;
echo sprintf("Все файлы в папке будут заменены").PHP_EOL;
echo PHP_EOL;
echo getcwd().PHP_EOL;
echo PHP_EOL;
echo "Запускаем?".PHP_EOL;
echo "yes для запуска, иное для отмены: ";
$answer = strtolower(trim(fgets(STDIN)));
if ($answer != "yes")
die('Отмена'.PHP_EOL);
define("FIND", $find);
define("REPLACE", $replace);
scanDirectory(getcwd());
function refactorFile(string $filePath)
{
file_put_contents(
$filePath,
str_replace(
FIND,
REPLACE,
file_get_contents($filePath),
$count
)
);
if ($count > 0)
echo sprintf('replaced %d times in %s', $count, $filePath).PHP_EOL;
}
function scanDirectory(string $path)
{
logg(sprintf('scanning %s', $path), true);
$files = scandir($path);
foreach ($files as $fileName) {
logg($fileName);
$p = $path.DIRECTORY_SEPARATOR.$fileName;
if (in_array($fileName, ['.', '..', basename(__FILE__)])) {
logg(sprintf('skip %s', $p));
}
elseif (is_dir($p)) {
logg(sprintf('dir %s', $p));
if (in_array($fileName, ['.gradle', '.idea', 'build', '.git']))
removeDir($p);
else
scanDirectory($p);
}
elseif (is_file($p)) {
logg(sprintf('file %s', $p));
if (in_array($fileName, ['google-services.json']))
removeFile($p);
else
refactorFile($p);
}
logg();
}
}
function removeFile(string $filePath)
{
unlink($filePath);
echo sprintf('removed file %s', $filePath).PHP_EOL;
}
function removeDir(string $dirPath)
{
rrmdir($dirPath);
echo sprintf('removed dir %s', $dirPath).PHP_EOL;
}
function rrmdir($dirPath) {
$dir = opendir($dirPath);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
$full = $dirPath . DIRECTORY_SEPARATOR . $file;
if ( is_dir($full) ) {
rrmdir($full);
}
else {
unlink($full);
}
}
}
closedir($dir);
rmdir($dirPath);
}
function logg(string $msg = '', bool $addEmptyLine = false)
{
return;
echo $msg.PHP_EOL;
if ($addEmptyLine)
echo PHP_EOL;
}