如何在PHP中自动增加版本号


How to increase build number automatically in PHP

有一些类似的问题,但它们对我的案例都没有帮助。所有其他问题都在讨论版本号,但我需要一些只会增加构建号的东西。

我需要一个脚本来检查是否有任何文件被更改/创建/删除,并将内部版本号增加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);

下面的代码获取目录中的所有文件,将它们放在数组中,并将其保存为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);