有一些类似的问题,但没有一个帮助我的情况。所有其他问题都在谈论版本号,但我需要一些只会增加版本号的东西。
我需要一个脚本来检查是否有任何文件被更改/创建/删除,并将内部版本号增加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);
答案 0 :(得分:1)
下面的代码获取目录中的所有文件,将它们放在一个数组中并将其保存为JSON文件。再次运行脚本时,它会再次获取所有文件和修改日期,并将其与JSON文件进行比较。如果有任何更改(例如,文件被修改/创建/删除),则会将内部版本号增加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);