【问题标题】:How to increase build number automatically in PHP如何在 PHP 中自动增加内部版本号
【发布时间】:2015-12-31 15:07:22
【问题描述】:

有一些类似的问题,但没有一个对我的案子有帮助。所有其他问题都在谈论版本号,但我需要一些只会增加内部版本号的东西。

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

【问题讨论】:

  • 哇,这是一个快速的投票(在 2 秒内)。我认为你甚至不可能在 2 秒内读完问题。
  • 我相信你被否决了,因为你的问题没有表现出任何努力,而且非常广泛。你也有几票接近,这两个都表明你的问题太宽泛了。
  • 拒绝投票很好,我不反对。但是,甚至没有阅读问题就投反对票是荒谬的。他/她应该添加评论,所以我可以改进问题。我在问题中添加了我的代码,并试图在文本中进一步澄清。我试图将其作为一个质量问题,但否决票不是很有帮助。如果您想改进问题,请添加评论或自行编辑问题。

标签: php build versioning build-numbers


【解决方案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);

【讨论】:

  • 请编辑您的原始帖子以在您的问题中添加任何信息,例如此代码。
  • 我修改了它。我相信这个问题很清楚。我问它是为了分享我的知识。据我所知,举办诸如 SO 之类的论坛的全部意义在于:这样我们就可以分享我们的知识。
  • 是的......而且没有。 SO 不是一个(讨论)论坛,它是一个问答网站。 好的答案将始终解释所做的事情以及这样做的原因,不仅适用于 OP,也适用于 SO 的未来访问者。不清楚您是在回答自己的问题。
  • 我添加了对脚本功能的解释。我认为代码中的 cmets 足够清晰,但我想对脚本的作用进行更广泛的解释也很有帮助。感谢您的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-27
  • 2023-03-14
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-02
相关资源
最近更新 更多