Как автоматически увеличить номер сборки в Stack Overflow

Есть несколько похожих вопросов, но ни один из них не помог мне. Все остальные вопросы говорили о номере версии, но мне нужно что-то, что только увеличит номер сборки.

Мне нужен был скрипт, который бы проверял, были ли какие-либо файлы изменены / созданы / удалены и увеличил номер сборки на 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

Решение

Приведенный ниже код выбирает все файлы в каталоге, помещает их в массив и сохраняет как файл 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);
1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector