123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- <?php
-
- include_once 'phing/Task.php';
- include_once 'phing/BuildException.php';
- include_once 'phing/lib/Capsule.php';
- include_once 'phing/util/StringHelper.php';
- class CapsuleTask extends Task {
-
- protected $context;
-
-
- protected $assignedVars = array();
-
-
- protected $controlTemplate;
-
-
- protected $templatePath;
-
-
- protected $outputDirectory;
-
-
- protected $outputFile;
-
- protected $contextProperties;
-
-
-
-
-
-
-
- public function setControlTemplate ($controlTemplate) {
- $this->controlTemplate = $controlTemplate;
- }
-
- public function getControlTemplate() {
- return $this->controlTemplate;
- }
-
- public function setTemplatePath($templatePath) {
- $resolvedPath = "";
- $tok = strtok($templatePath, ",");
- while ( $tok ) {
-
-
- $fullPath = $this->project->resolveFile($tok);
- $cpath = $fullPath->getCanonicalPath();
- if ($cpath === false) {
- $this->log("Template directory does not exist: " . $fullPath->getAbsolutePath());
- } else {
- $resolvedPath .= $cpath;
- }
- $tok = strtok(",");
- if ( $tok ) {
- $resolvedPath .= ",";
- }
- }
- $this->templatePath = $resolvedPath;
- }
-
- public function getTemplatePath() {
- return $this->templatePath;
- }
-
- public function setOutputDirectory(PhingFile $outputDirectory) {
- try {
- if (!$outputDirectory->exists()) {
- $this->log("Output directory does not exist, creating: " . $outputDirectory->getPath(),Project::MSG_VERBOSE);
- if (!$outputDirectory->mkdirs()) {
- throw new IOException("Unable to create Ouptut directory: " . $outputDirectory->getAbsolutePath());
- }
- }
- $this->outputDirectory = $outputDirectory->getCanonicalPath();
- } catch (IOException $ioe) {
- throw new BuildException($ioe);
- }
- }
-
-
- public function getOutputDirectory() {
- return $this->outputDirectory;
- }
-
- public function setOutputFile($outputFile) {
- $this->outputFile = $outputFile;
- }
-
- public function getOutputFile() {
- return $this->outputFile;
- }
-
-
- public function setContextProperties($file) {
- $sources = explode(",", $file);
- $this->contextProperties = new Properties();
-
-
-
-
-
-
-
- for ($i=0, $sourcesLength=count($sources); $i < $sourcesLength; $i++) {
- $source = new Properties();
-
- try {
-
-
-
- $fullPath = $this->project->resolveFile($sources[$i]);
- $this->log("Using contextProperties file: " . $fullPath->toString());
- $source->load($fullPath);
-
- } catch (Exception $e) {
-
- throw new BuildException("Context properties file " . $sources[$i] .
- " could not be found in the file system!");
-
- }
-
- $keys = $source->keys();
-
- foreach ($keys as $key) {
- $name = $key;
- $value = $this->project->replaceProperties($source->getProperty($name));
- $this->contextProperties->setProperty($name, $value);
- }
- }
- }
-
- public function getContextProperties() {
- return $this->contextProperties;
- }
-
- public function createAssign() {
- $a = new AssignedVar();
- $this->assignedVars[] = $a;
- return $a;
- }
-
-
-
-
-
-
- public function initControlContext() {
- $this->context->clear();
- foreach($this->assignedVars as $var) {
- $this->context->put($var->getName(), $var->getValue());
- }
- return $this->context;
- }
-
-
- public function main() {
-
-
- if (empty($this->templatePath)) {
- throw new BuildException("The template path needs to be defined!");
- }
-
-
- if ($this->controlTemplate === null) {
- throw new BuildException("The control template needs to be defined!");
- }
-
- if ($this->outputDirectory === null) {
- throw new BuildException("The output directory needs to be defined!");
- }
-
-
- if ($this->outputFile === null) {
- throw new BuildException("The output file needs to be defined!");
- }
-
-
-
-
-
-
-
- $this->context = new Capsule();
-
- if ($this->templatePath !== null) {
- $this->log("Using templatePath: " . $this->templatePath);
- $this->context->setTemplatePath($this->templatePath);
- }
-
-
-
- $outputDir = new PhingFile($this->outputDirectory);
- if (!$outputDir->exists()) {
- $this->log("Output directory does not exist, creating: " . $outputDir->getAbsolutePath());
- $outputDir->mkdirs();
- }
-
- $this->context->setOutputDirectory($outputDir->getAbsolutePath());
-
- $path = $this->outputDirectory . DIRECTORY_SEPARATOR . $this->outputFile;
- $this->log("Generating to file " . $path);
-
-
-
-
-
-
- $c = $this->initControlContext();
-
-
-
- $this->populateInitialContext($c);
-
-
-
-
- if ($this->contextProperties !== null) {
-
- foreach($this->contextProperties->keys() as $property) {
-
- $value = $this->contextProperties->getProperty($property);
-
-
-
-
-
- if (preg_match('/file\.contents$/', $property)) {
-
-
- $property = substr($property, 0, strpos($property, "file.contents") - 1);
-
-
-
- $value = "";
- $f = new PhingFile($project->resolveFile($value)->getCanonicalPath());
- if ($f->exists()) {
- $fr = new FileReader($f);
- $fr->readInto($value);
- }
-
- }
-
- if (StringHelper::isBoolean($value)) {
- $value = StringHelper::booleanValue($value);
- }
-
- $c->put($property, $value);
-
- }
-
- }
-
- try {
- $this->log("Parsing control template: " . $this->controlTemplate);
- $c->parse($this->controlTemplate, $path);
- } catch (Exception $ioe) {
- throw new BuildException("Cannot write parsed template: ". $ioe->getMessage());
- }
-
- $this->cleanup();
- }
-
- protected function populateInitialContext(Capsule $context) {
- $this->context->put("now", strftime("%c", time()));
- $this->context->put("task", $this);
- }
-
- protected function cleanup() {
- }
- }
- class AssignedVar {
-
- private $name;
- private $value;
-
- function setName($v) {
- $this->name = $v;
- }
-
- function setValue($v) {
- $this->value = $v;
- }
-
- function getName() {
- return $this->name;
- }
-
- function getValue() {
- return $this->value;
- }
- }
|