generated from espocrm/ext-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.php
77 lines (55 loc) · 2.37 KB
/
init.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
<?php
fwrite(\STDOUT, "Enter an extension name:\n");
$fh = fopen('php://stdin', 'r');
$name = trim(fgets($fh));
fclose($fh);
$nameLabel = $name;
$name = ucfirst($name);
$name = str_replace(' ', '', ucwords(preg_replace('/^a-z0-9]+/', ' ', $name)));
$nameHyphen = strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $name));
fwrite(\STDOUT, "Enter a description text:\n");
$fh = fopen('php://stdin', 'r');
$description = trim(fgets($fh));
fclose($fh);
if (substr($description, -1) !== '.') $description .= '.';
fwrite(\STDOUT, "Enter an author name:\n");
$fh = fopen('php://stdin', 'r');
$author = trim(fgets($fh));
fclose($fh);
$replacePlaceholders = function (string $file) use ($name, $nameHyphen, $nameLabel, $description, $author)
{
if (!file_exists($file)) {
return;
}
$content = file_get_contents($file);
$content = str_replace('{@name}', $name, $content);
$content = str_replace('{@nameHyphen}', $nameHyphen, $content);
$content = str_replace('{@nameLabel}', $nameLabel, $content);
$content = str_replace('{@description}', $description, $content);
$content = str_replace('{@author}', $author, $content);
file_put_contents($file, $content);
};
$replaceNamespace = function (string $directory) use ($name)
{
$list = new \RecursiveDirectoryIterator($directory);
foreach (new RecursiveIteratorIterator($list) as $file) {
$filePath = $file->getPathname();
$fileExtension = pathinfo($filePath, \PATHINFO_EXTENSION);
if (!in_array(strtolower($fileExtension), ['php'])) {
continue;
}
$content = file_get_contents($filePath);
$content = str_replace('MyModuleName', $name, $content);
file_put_contents($filePath, $content);
}
};
$replacePlaceholders('package.json');
$replacePlaceholders('extension.json');
$replacePlaceholders('config-default.json');
$replacePlaceholders('README.md');
$replaceNamespace('src/files/application/Espo/Modules/MyModuleName');
rename('src/files/application/Espo/Modules/MyModuleName', 'src/files/application/Espo/Modules/'. $name);
rename('src/files/client/modules/my-module-name', 'src/files/client/modules/'. $nameHyphen);
rename('tests/unit/Espo/Modules/MyModuleName', 'tests/unit/Espo/Modules/'. $name);
rename('tests/integration/Espo/Modules/MyModuleName', 'tests/integration/Espo/Modules/'. $name);
echo "Ready. Now you need to run 'npm install'.\n";