Reputation: 6154
There are some similar questions but none of them helped my case. All the other questions were talking about versioning number but I needed something that will only increase the build number.
I needed a script that would check if there are any files changed/created/deleted and increase the build number by 1.
I couldn't find an answer to this problem online and prepared a script myself. I am asking this question, so I can share my script as an answer.
Here's the script I came up with. Feel free to improve it further and modify my answer or post your own answer:
// Opening the json file that holds the file paths and file modification dates
$jsonArray = json_decode(file_get_contents('files.json'), true);
$jsonFileArray = array();
// Putting the values into a local array
foreach ($jsonArray as $filePath => $modifiedDate) {
$jsonFileArray[$filePath] = $modifiedDate;
}
// Iterating through the directories and putting the file paths and modification dates into a local array
$filesArray = array();
$dir_iterator = new RecursiveDirectoryIterator(".");
$recursive_iterator = new RecursiveIteratorIterator($dir_iterator);
foreach ($recursive_iterator as $file) {
if ($file->isDir()) {
continue;
}
if (substr($file, -9) != 'error_log') {
$fileName = $file->getPathname();
$fileModifiedDate = date('m/d/y H:i:s', $file->getMTime());
$filesArray[$fileName] = $fileModifiedDate;
}
}
// Checking if there are any files that are modified/created/deleted
if ($jsonFileArray != $filesArray) {
// If there are any changes, the build number is increased by 1 and saved into 'build' file
$buildFile = "build";
file_put_contents($buildFile, file_get_contents($buildFile) + 1);
}
// Updating the json file with the latest modifiedDates
$jsonFile = fopen('files.json', 'w');
fwrite($jsonFile, json_encode($filesArray, JSON_UNESCAPED_SLASHES));
fclose($jsonFile);
Upvotes: 0
Views: 426
Reputation: 6154
The code below fetches all the files in a directory, puts them in an array and saves it as a JSON file. When the script is run again, it fetches all the files and the modification dates again and compares it to the JSON file. If there are any changes (e.g. files were modified/created/deleted), it increases the build number by 1.
// Opening the json file that holds the file paths and file modification dates
$jsonArray = json_decode(file_get_contents('files.json'), true);
$jsonFileArray = array();
// Putting the values into a local array
foreach ($jsonArray as $filePath => $modifiedDate) {
$jsonFileArray[$filePath] = $modifiedDate;
}
// Iterating through the directories and putting the file paths and modification dates into a local array
$filesArray = array();
$dir_iterator = new RecursiveDirectoryIterator(".");
$recursive_iterator = new RecursiveIteratorIterator($dir_iterator);
foreach ($recursive_iterator as $file) {
if ($file->isDir()) {
continue;
}
if (substr($file, -9) != 'error_log') {
$fileName = $file->getPathname();
$fileModifiedDate = date('m/d/y H:i:s', $file->getMTime());
$filesArray[$fileName] = $fileModifiedDate;
}
}
// Checking if there are any files that are modified/created/deleted
if ($jsonFileArray != $filesArray) {
// If there are any changes, the build number is increased by 1 and saved into 'build' file
$buildFile = "build";
file_put_contents($buildFile, file_get_contents($buildFile) + 1);
}
// Updating the json file with the latest modifiedDates
$jsonFile = fopen('files.json', 'w');
fwrite($jsonFile, json_encode($filesArray, JSON_UNESCAPED_SLASHES));
fclose($jsonFile);
Upvotes: 1