我有一个脚本,可以查找和输出或将我当前的版本# 写入文本文件。现在唯一的问题是如何将此版本号放入 PHING 属性中。
现在我的 PHING 目标构建 build.zip 和 built.tar,我希望它构建 build-1.0.0.zip 或任何版本脚本决定的当前版本。我怎样才能做到这一点?我必须创建自己的“任务”吗?
另一种方法是使用outputProperty
ExecTask 上的属性在构建文件中提供属性。
<target name="version">
<exec command="cat version.txt" outputProperty="version.number" />
<echo msg="Version: ${version.number}" />
</target>
您可能需要为此创建自己的任务。该任务可能看起来像...
<?php
require_once "phing/Task.php";
class VersionNumberTask extends Task
{
private $versionprop;
public function setVersionProp($versionprop)
{
$this->versionprop = $versionprop;
}
public function init()
{
}
public function main()
{
// read the version text file in to a variable
$version = file_get_contents("version.txt");
$this->project->setProperty($this->versionprop, $version);
}
}
然后您将在构建 xml 中定义任务
<taskdef classname="VersionNumberTask" name="versiontask" />
然后调用任务
<target name="dist">
<versiontask versionprop="version.number"/>
</target>
此时,您应该能够在整个构建 xml 中使用 ${version.number} 访问版本号。
希望这可以帮助!
一种适用于 Windows 和 Linux 的替代方法。
<exec executable="php" outputProperty="version.number">
<arg value="-r" />
<arg value="$fh=file('version.txt'); echo trim(array_pop($fh));" />
</exec>
<echo msg="Current version is: ${version.number}"/>
假设文件的最后一行只是版本号,如果您想更新文件中的版本号。试试这个。
<propertyprompt propertyName="release_version" defaultValue="${version.numver}" promptText="Enter version to be released."/>
<exec executable="php">
<arg value="-r" />
<arg value="$file=file_get_contents('version.txt'); $file = str_replace('${version.number}', '${release_version}', $file); file_put_contents('version.txt', $file);" />
</exec>
<echo msg="Version number updated." />
<property name="version.number" value="${release_version}" override="true" />
在 Windows 和 Linux 上都可以使用的替代和最佳方式(我认为)是使用本机任务LoadFileTask
<loadfile property="myVersion" file="version.txt" />
<echo msg="Current version is: ${myVersion}"/>
你也可以使用filterchain
<loadfile property="myVersion" file="version.txt">
<filterchain><striplinebreaks /></filterchain>
</loadfile>
<echo msg="Current version is: ${myVersion}"/>