问题
I have a script that can lookup and output or write my current release # to a text file. Now the only problem is how do I get this version number into a PHING property.
Right now my PHING target builds build.zip and built.tar, I would like it to build build-1.0.0.zip or whatever the version script decides the current version is. How can I do this? Will I have to create my own "task"?
回答1:
You might need to create your own task for this. The task may look something like...
<?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);
}
}
Then you would define the task in your build xml
<taskdef classname="VersionNumberTask" name="versiontask" />
Then call the task
<target name="dist">
<versiontask versionprop="version.number"/>
</target>
At this point, you should be able to access the version number using ${version.number} throughout your build xml.
Hope this helps!
回答2:
An alternative approach is to use the outputProperty
attribute on the ExecTask to provide a property in your build file.
<target name="version">
<exec command="cat version.txt" outputProperty="version.number" />
<echo msg="Version: ${version.number}" />
</target>
More information
回答3:
An alternative approach that works on both Windows and 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}"/>
Assumes the last line of the file is simply the version number, and if you would like to update the version number in the file. Try this.
<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" />
回答4:
Also alternative and best way (my opinion) that works on both Windows and Linux it is use native task LoadFileTask
<loadfile property="myVersion" file="version.txt" />
<echo msg="Current version is: ${myVersion}"/>
also you can use filterchain
<loadfile property="myVersion" file="version.txt">
<filterchain><striplinebreaks /></filterchain>
</loadfile>
<echo msg="Current version is: ${myVersion}"/>
More information
来源:https://stackoverflow.com/questions/1836665/phing-call-a-command-get-its-output-into-a-property