Skip to content

Commit 718357d

Browse files
committed
feat: replace fixme script
1 parent 975766b commit 718357d

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
"theme:dev": [
140140
"Composer\\Config::disableProcessTimeout",
141141
"cd web/wp-content/themes/fixme-app-name && yarn dev"
142-
]
142+
],
143+
"replace-fixme": "php scripts/composer/ReplaceFixme.php"
143144
}
144145
}

scripts/composer/ReplaceFixme.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
// Define the directory to search in and an array of replacements
4+
$directory = dirname(__DIR__, 2);
5+
$replacements = [
6+
'fixme-app-name' => 'FIXME', // ex: app-name
7+
'fixme-project-name' => 'FIXME', // ex: Client Name
8+
'fixme-domain' => 'FIXME' // ex: www.clientdomain.org (do not include the protocal)
9+
// Add more pairs as needed
10+
];
11+
12+
$exclusions = [
13+
'scripts/composer/ReplaceFixme.php', // Relative path from project root
14+
'.env.example',
15+
'.lando.example.yml'
16+
];
17+
18+
$excludedDirectories = [
19+
".git",
20+
".vscode",
21+
"scripts",
22+
"web/wp-content",
23+
"tests"
24+
];
25+
26+
// Function to recursively get all files in a directory, skipping excluded directories
27+
function getFiles($dir, $excludedDirectories) {
28+
$files = [];
29+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
30+
foreach ($iterator as $file) {
31+
$filePath = $file->getPathname();
32+
33+
// Check if file is in an excluded directory
34+
foreach ($excludedDirectories as $excludedDir) {
35+
$excludedPath = dirname(__DIR__, 2) . '/' . $excludedDir;
36+
if (strpos($filePath, $excludedPath) === 0) {
37+
continue 2; // Skip files in this directory
38+
}
39+
}
40+
41+
if ($file->isFile()) {
42+
$files[] = $filePath;
43+
}
44+
}
45+
return $files;
46+
}
47+
48+
// Function to perform replacements
49+
function replaceTextInFiles($files, $replacements, $exclusions) {
50+
foreach ($files as $file) {
51+
// Convert file path to a format relative to the project root for comparison
52+
$relativePath = str_replace(dirname(__DIR__, 2) . '/', '', $file);
53+
54+
// Skip files listed in exclusions
55+
if (in_array($relativePath, $exclusions)) {
56+
continue;
57+
}
58+
$content = file_get_contents($file);
59+
$updatedContent = str_replace(array_keys($replacements), array_values($replacements), $content);
60+
61+
// Only write if changes were made
62+
if ($content !== $updatedContent) {
63+
file_put_contents($file, $updatedContent);
64+
echo "Updated: $file\n";
65+
}
66+
}
67+
}
68+
69+
// Get all files and perform replacements
70+
$files = getFiles($directory, $excludedDirectories);
71+
echo implode("\n", $files) . "\n";
72+
replaceTextInFiles($files, $replacements, $exclusions);
73+
74+
echo "Replacement complete.\n";

0 commit comments

Comments
 (0)