Repository navigation
Expand file tree
/
Copy pathcomposer-setup.php
More file actions
215 lines (189 loc) · 6.89 KB
/
Copy pathcomposer-setup.php
File metadata and controls
215 lines (189 loc) · 6.89 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
<?php
// composer-setup.php
// Benutzer nach den Ersatzwerten für die Konfiguration fragen
$pleaseEnter = "Please enter your value for ";
$pleaseEnterModuleNameCamelCase = "Please enter your value for the module name in CamelCase to replace the default value ";
$pleaseEnterCompanyPrefix = "Please enter your value for the creator prefix to replace the default ('NXD') value for namespace";
$replacements = array();
$value = readline($pleaseEnter . "[CREATOR-COMPANY]: ");
$replacements[] = new Replacement("[CREATOR-COMPANY]", $value);
$value = trim(readline($pleaseEnterCompanyPrefix . ": "));
if (!strlen($value))
{
$value = 'NXD';
}
$replacements[] = new Replacement("NXD", $value);
$value = readline($pleaseEnter . "[CREATOR-FULLNAME]: ");
$replacements[] = new Replacement("[CREATOR-FULLNAME]", $value);
$value = readline($pleaseEnter . "[CREATOR-URL]: ");
$replacements[] = new Replacement("[CREATOR-URL]", $value);
$value = readline($pleaseEnter . "[CREATOR-EMAIL]: ");
$replacements[] = new Replacement("[CREATOR-EMAIL]", $value);
$value = readline($pleaseEnter . "[EXTENSION-VERSION]: ");
$replacements[] = new Replacement("[EXTENSION-VERSION]", $value);
$value = readline($pleaseEnter . "[CREATED-DATE]: ");
$replacements[] = new Replacement("[CREATED-DATE]", $value);
$value = readline($pleaseEnter . "[CREATED-YEAR]: ");
$replacements[] = new Replacement("[CREATED-YEAR]", $value);
$value = ucfirst(trim(readline($pleaseEnterModuleNameCamelCase . "'BluePrint': ")));
if (!strlen($value))
{
$value = 'BluePrint';
}
$replacements['moduleNameCamelCase'] = new Replacement("BluePrint", $value);
$replacements['moduleNameCapitalized'] = new Replacement("Blueprint", ucfirst($value));
$replacements[] = new Replacement("blueprint", strtolower($value));
$replacements['moduleNameReplacement'] = new Replacement("mod_blueprint", 'mod_' . strtolower($value));
$replacements[] = new Replacement("BLUEPRINT", strtoupper($value));
// First, copy the root-module folder to the ‘customised’ folder
$sourceDir = __DIR__ . '/root-module';
$moduleDir = __DIR__ . '/customized/' . $replacements['moduleNameReplacement']->value;
if (!is_dir($sourceDir))
{
echo "Error: root-module folder not found!\n";
exit(1);
}
// Create the destination directory if it does not exist
if (!is_dir($moduleDir))
{
mkdir($moduleDir, 0755, true);
}
// Recursive copying
copyDirectory($sourceDir, $moduleDir);
echo "Copied root-module to customized/" . $replacements['moduleNameReplacement']->value . " folder.\n";
// Walk recursively through the folders and replace the content of the files
// Directory in which the recursive traversal is to begin
$startDirectory = $moduleDir . '/';
$ignoredFolders = array('.', '..', 'vendor', 'node_modules', '.git', '.idea');
// Create a recursive iterator for the starting directory
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($startDirectory, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
// Check all files in the root directory
replaceContentOfFolderFiles($startDirectory, $replacements);
// Iterate through the iterator and call the callback function for each folder found
foreach ($iterator as $fileInfo)
{
if (is_dir($fileInfo->getPathname())
&& !in_array(basename($fileInfo->getPathname()), $ignoredFolders)
&& !contains($fileInfo->getPathname(), $ignoredFolders)
)
{
echo "Processing directory: " . $fileInfo->getPathname() . "\n";
replaceContentOfFolderFiles($fileInfo->getPathname(), $replacements);
}
}
echo "Replacement completed!\n";
// Rename relevant filenames where mod_blueprint is used
rename($moduleDir . '/mod_blueprint.xml', $moduleDir . "/" . $replacements['moduleNameReplacement']->value . '.xml');
rename($moduleDir . '/language/en-GB/mod_blueprint.ini', $moduleDir . '/language/en-GB/' . $replacements['moduleNameReplacement']->value . '.ini');
rename($moduleDir . '/language/en-GB/mod_blueprint.sys.ini', $moduleDir . '/language/en-GB/' . $replacements['moduleNameReplacement']->value . '.sys.ini');
rename($moduleDir . '/src/Helper/BlueprintHelper.php', $moduleDir . '/src/Helper/' . $replacements['moduleNameCamelCase']->value . 'Helper.php');
// Rename the media folder
rename($moduleDir . '/media/mod_blueprint', $moduleDir . '/media/' . $replacements['moduleNameReplacement']->value);
echo "Renaming completed!\n";
echo " Your customized module is ready in the 'customized/" . $replacements['moduleNameReplacement']->value . "' folder.\n";
echo " Please remember to remove the files 'composer-setup.php', 'composer.json', 'composer.lock' and the 'vendor' folder from your project before deployment.\n";
/**
* Recursively copy a directory
*
* @param string $source Source directory path
* @param string $dest Destination directory path
*
* @return void
*
* @since 1.0.0
*/
function copyDirectory($source, $dest)
{
// Check for any symlinks in the source directory
if (is_link($source))
{
return symlink(readlink($source), $dest);
}
// If the source is not a directory, return false
if (!is_dir($source))
{
return false;
}
// Create the destination directory if it doesn't exist
if (!is_dir($dest))
{
mkdir($dest, 0755, true);
}
// Loop through the files in the source directory
$files = scandir($source);
foreach ($files as $file)
{
if ($file === '.' || $file === '..')
{
continue;
}
$sourcePath = $source . '/' . $file;
$destPath = $dest . '/' . $file;
if (is_dir($sourcePath))
{
copyDirectory($sourcePath, $destPath);
}
else
{
copy($sourcePath, $destPath);
}
}
}
/**
* Check if the array of strings contains a string, case-insensitive search returns true if found
*
* @param string $str The string to search in
* @param array $arr The array of strings to search for
*
* @return bool
*
* @since version
*/
function contains($str, array $arr): bool
{
foreach ($arr as $a)
{
if (stripos($str, $a) !== false) return true;
}
return false;
}
/**
* Replace the content of the files in the folder
*
* @param string $pathElement The path string to process (full path string)
* @param array $replacements The array of Replacement objects
*
* @since 1.0.0
*/
function replaceContentOfFolderFiles(string $pathElement, array $replacements): void
{
$ignoreFiles = array('composer-setup.php');
$files = glob($pathElement . '/*.*');
foreach ($files as $file)
{
if (in_array(basename($file), $ignoreFiles))
{
continue;
}
echo 'Processing file: ' . $file . "\n";
$content = file_get_contents($file);
foreach ($replacements as $replacement)
{
$content = str_replace($replacement->key, $replacement->value, $content);
}
file_put_contents($file, $content);
}
}
class Replacement
{
public $key;
public $value;
public function __construct($key, $value)
{
$this->key = $key;
$this->value = $value;
}
}