1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000 |
- <?php
- include_once 'phing/system/io/PhingFile.php';
- include_once 'phing/util/FileUtils.php';
- include_once 'phing/TaskAdapter.php';
- include_once 'phing/util/StringHelper.php';
- include_once 'phing/BuildEvent.php';
- include_once 'phing/input/DefaultInputHandler.php';
- class Project {
-
- const MSG_DEBUG = 4;
- const MSG_VERBOSE = 3;
- const MSG_INFO = 2;
- const MSG_WARN = 1;
- const MSG_ERR = 0;
-
-
- private $targets = array();
-
- private $globalFilterSet = array();
-
- private $globalFilters = array();
-
-
- private $properties = array();
-
-
- private $userProperties = array();
-
-
- private $inheritedProperties = array();
-
-
- private $taskdefs = array();
-
-
- private $typedefs = array();
-
-
- private $references = array();
-
-
- private $inputHandler;
-
-
-
-
- private $basedir;
-
-
- private $defaultTarget = 'all';
-
-
- private $name;
-
-
- private $description;
-
- private $phingVersion;
-
- private $fileUtils;
-
-
- private $listeners = array();
-
- function __construct() {
- $this->fileUtils = new FileUtils();
- $this->inputHandler = new DefaultInputHandler();
- }
-
- public function setInputHandler(InputHandler $handler) {
- $this->inputHandler = $handler;
- }
-
- public function getInputHandler() {
- return $this->inputHandler;
- }
-
- function init() {
-
- $this->setSystemProperties();
-
-
- $taskdefs = Phing::getResourcePath("phing/tasks/defaults.properties");
-
- try {
- $props = new Properties();
- $in = new PhingFile((string)$taskdefs);
- if ($in === null) {
- throw new BuildException("Can't load default task list");
- }
- $props->load($in);
- $enum = $props->propertyNames();
- foreach($enum as $key) {
- $value = $props->getProperty($key);
- $this->addTaskDefinition($key, $value);
- }
- } catch (IOException $ioe) {
- throw new BuildException("Can't load default task list");
- }
-
- $typedefs = Phing::getResourcePath("phing/types/defaults.properties");
- try {
- $props = new Properties();
- $in = new PhingFile((string)$typedefs);
- if ($in === null) {
- throw new BuildException("Can't load default datatype list");
- }
- $props->load($in);
- $enum = $props->propertyNames();
- foreach($enum as $key) {
- $value = $props->getProperty($key);
- $this->addDataTypeDefinition($key, $value);
- }
- } catch(IOException $ioe) {
- throw new BuildException("Can't load default datatype list");
- }
- }
-
- function getGlobalFilterSet() {
- return $this->globalFilterSet;
- }
-
-
-
-
-
- public function setProperty($name, $value) {
-
-
- if (isset($this->userProperties[$name])) {
- $this->log("Override ignored for user property " . $name, Project::MSG_VERBOSE);
- return;
- }
- if (isset($this->properties[$name])) {
- $this->log("Overriding previous definition of property " . $name, Project::MSG_VERBOSE);
- }
- $this->log("Setting project property: " . $name . " -> " . $value, Project::MSG_DEBUG);
- $this->properties[$name] = $value;
- }
-
- public function setNewProperty($name, $value) {
- if (isset($this->properties[$name])) {
- $this->log("Override ignored for property " . $name, Project::MSG_DEBUG);
- return;
- }
- $this->log("Setting project property: " . $name . " -> " . $value, Project::MSG_DEBUG);
- $this->properties[$name] = $value;
- }
-
- public function setUserProperty($name, $value) {
- $this->log("Setting ro project property: " . $name . " -> " . $value, Project::MSG_DEBUG);
- $this->userProperties[$name] = $value;
- $this->properties[$name] = $value;
- }
-
- public function setInheritedProperty($name, $value) {
- $this->inheritedProperties[$name] = $value;
- $this->setUserProperty($name, $value);
- }
-
- private function setPropertyInternal($name, $value) {
- if (isset($this->userProperties[$name])) {
- $this->log("Override ignored for user property " . $name, Project::MSG_VERBOSE);
- return;
- }
- $this->properties[$name] = $value;
- }
-
- public function getProperty($name) {
- if (!isset($this->properties[$name])) {
- return null;
- }
- $found = $this->properties[$name];
-
- if (false !== strpos($found, '${')) {
-
- $found = $this->replaceProperties($found);
-
- $this->properties[$name] = $found;
- }
- return $found;
- }
-
- public function replaceProperties($value) {
- return ProjectConfigurator::replaceProperties($this, $value, $this->properties);
- }
-
- public function getUserProperty($name) {
- if (!isset($this->userProperties[$name])) {
- return null;
- }
- return $this->userProperties[$name];
- }
-
- public function getProperties() {
- return $this->properties;
- }
-
- public function getUserProperties() {
- return $this->userProperties;
- }
-
- public function copyUserProperties(Project $other) {
- foreach($this->userProperties as $arg => $value) {
- if (isset($this->inheritedProperties[$arg])) {
- continue;
- }
- $other->setUserProperty($arg, $value);
- }
- }
-
- public function copyInheritedProperties(Project $other) {
- foreach($this->userProperties as $arg => $value) {
- if ($other->getUserProperty($arg) !== null) {
- continue;
- }
- $other->setInheritedProperty($arg, $value);
- }
- }
-
-
-
-
- function setDefaultTarget($targetName) {
- $this->defaultTarget = (string) trim($targetName);
- }
- function getDefaultTarget() {
- return (string) $this->defaultTarget;
- }
-
- function setName($name) {
- $this->name = (string) trim($name);
- $this->setProperty("phing.project.name", $this->name);
- }
-
- function getName() {
- return (string) $this->name;
- }
-
- function setDescription($description) {
- $this->description = (string) trim($description);
- }
-
- function getDescription() {
- return $this->description;
- }
-
- function setPhingVersion($version) {
- $version = str_replace('phing', '', strtolower($version));
- $this->phingVersion = (string)trim($version);
- }
-
- function getPhingVersion() {
- if($this->phingVersion === null) {
- $this->setPhingVersion(Phing::getPhingVersion());
- }
- return $this->phingVersion;
- }
-
- function setBasedir($dir) {
- if ($dir instanceof PhingFile) {
- $dir = $dir->getAbsolutePath();
- }
- $dir = $this->fileUtils->normalize($dir);
- $dir = new PhingFile((string) $dir);
- if (!$dir->exists()) {
- throw new BuildException("Basedir ".$dir->getAbsolutePath()." does not exist");
- }
- if (!$dir->isDirectory()) {
- throw new BuildException("Basedir ".$dir->getAbsolutePath()." is not a directory");
- }
- $this->basedir = $dir;
- $this->setPropertyInternal("project.basedir", $this->basedir->getAbsolutePath());
- $this->log("Project base dir set to: " . $this->basedir->getPath(), Project::MSG_VERBOSE);
-
-
- chdir($dir->getAbsolutePath());
- }
-
- function getBasedir() {
- if ($this->basedir === null) {
- try {
- $this->setBasedir(".");
- } catch (BuildException $exc) {
- throw new BuildException("Can not set default basedir. ".$exc->getMessage());
- }
- }
- return $this->basedir;
- }
-
- function setSystemProperties() {
-
-
- $systemP = array_merge( self::getProperties(), Phing::getProperties() );
- foreach($systemP as $name => $value) {
- $this->setPropertyInternal($name, $value);
- }
-
-
- foreach($_SERVER as $name => $value) {
-
- if (is_array($value)) {
- continue;
- }
- $this->setPropertyInternal('env.' . $name, $value);
- }
- return true;
- }
-
- function addTaskDefinition($name, $class, $classpath = null) {
- $name = $name;
- $class = $class;
- if ($class === "") {
- $this->log("Task $name has no class defined.", Project::MSG_ERR);
- } elseif (!isset($this->taskdefs[$name])) {
- Phing::import($class, $classpath);
- $this->taskdefs[$name] = $class;
- $this->log(" +Task definiton: $name ($class)", Project::MSG_DEBUG);
- } else {
- $this->log("Task $name ($class) already registerd, skipping", Project::MSG_VERBOSE);
- }
- }
- function &getTaskDefinitions() {
- return $this->taskdefs;
- }
-
- function addDataTypeDefinition($typeName, $typeClass, $classpath = null) {
- if (!isset($this->typedefs[$typeName])) {
- Phing::import($typeClass, $classpath);
- $this->typedefs[$typeName] = $typeClass;
- $this->log(" +User datatype: $typeName ($typeClass)", Project::MSG_DEBUG);
- } else {
- $this->log("Type $typeName ($typeClass) already registerd, skipping", Project::MSG_VERBOSE);
- }
- }
- function getDataTypeDefinitions() {
- return $this->typedefs;
- }
-
- function addTarget($targetName, &$target) {
- if (isset($this->targets[$targetName])) {
- throw new BuildException("Duplicate target: $targetName");
- }
- $this->addOrReplaceTarget($targetName, $target);
- }
- function addOrReplaceTarget($targetName, &$target) {
- $this->log(" +Target: $targetName", Project::MSG_DEBUG);
- $target->setProject($this);
- $this->targets[$targetName] = $target;
- $ctx = $this->getReference("phing.parsing.context");
- $current = $ctx->getConfigurator()->getCurrentTargets();
- $current[$targetName] = $target;
- }
- function getTargets() {
- return $this->targets;
- }
-
- function createTask($taskType) {
- try {
- $classname = "";
- $tasklwr = strtolower($taskType);
- foreach ($this->taskdefs as $name => $class) {
- if (strtolower($name) === $tasklwr) {
- $classname = $class;
- break;
- }
- }
-
- if ($classname === "") {
- return null;
- }
-
- $cls = Phing::import($classname);
-
- if (!class_exists($cls)) {
- throw new BuildException("Could not instantiate class $cls, even though a class was specified. (Make sure that the specified class file contains a class with the correct name.)");
- }
-
- $o = new $cls();
-
- if ($o instanceof Task) {
- $task = $o;
- } else {
- $this->log (" (Using TaskAdapter for: $taskType)", Project::MSG_DEBUG);
-
- $taskA = new TaskAdapter();
- $taskA->setProxy($o);
- $task = $taskA;
- }
- $task->setProject($this);
- $task->setTaskType($taskType);
-
- $task->setTaskName($taskType);
- $this->log (" +Task: " . $taskType, Project::MSG_DEBUG);
- } catch (Exception $t) {
- throw new BuildException("Could not create task of type: " . $taskType, $t);
- }
-
- return $task;
- }
-
- function createDataType($typeName) {
- try {
- $cls = "";
- $typelwr = strtolower($typeName);
- foreach ($this->typedefs as $name => $class) {
- if (strtolower($name) === $typelwr) {
- $cls = StringHelper::unqualify($class);
- break;
- }
- }
-
- if ($cls === "") {
- return null;
- }
-
- if (!class_exists($cls)) {
- throw new BuildException("Could not instantiate class $cls, even though a class was specified. (Make sure that the specified class file contains a class with the correct name.)");
- }
-
- $type = new $cls();
- $this->log(" +Type: $typeName", Project::MSG_DEBUG);
- if (!($type instanceof DataType)) {
- throw new Exception("$class is not an instance of phing.types.DataType");
- }
- if ($type instanceof ProjectComponent) {
- $type->setProject($this);
- }
- } catch (Exception $t) {
- throw new BuildException("Could not create type: $typeName", $t);
- }
-
- return $type;
- }
-
- function executeTargets($targetNames) {
- foreach($targetNames as $tname) {
- $this->executeTarget($tname);
- }
- }
-
- function executeTarget($targetName) {
-
- if ($targetName === null) {
- throw new BuildException("No target specified");
- }
-
-
- $sortedTargets = $this->_topoSort($targetName, $this->targets);
- $curIndex = (int) 0;
- $curTarget = null;
- do {
- try {
- $curTarget = $sortedTargets[$curIndex++];
- $curTarget->performTasks();
- } catch (BuildException $exc) {
- $this->log("Execution of target \"".$curTarget->getName()."\" failed for the following reason: ".$exc->getMessage(), Project::MSG_ERR);
- throw $exc;
- }
- } while ($curTarget->getName() !== $targetName);
- }
- function resolveFile($fileName, $rootDir = null) {
- if ($rootDir === null) {
- return $this->fileUtils->resolveFile($this->basedir, $fileName);
- } else {
- return $this->fileUtils->resolveFile($rootDir, $fileName);
- }
- }
-
- function _topoSort($root, &$targets) {
- $root = (string) $root;
- $ret = array();
- $state = array();
- $visiting = array();
-
-
-
-
-
-
-
- $this->_tsort($root, $targets, $state, $visiting, $ret);
- $retHuman = "";
- for ($i=0, $_i=count($ret); $i < $_i; $i++) {
- $retHuman .= $ret[$i]->toString()." ";
- }
- $this->log("Build sequence for target '$root' is: $retHuman", Project::MSG_VERBOSE);
- $keys = array_keys($targets);
- while($keys) {
- $curTargetName = (string) array_shift($keys);
- if (!isset($state[$curTargetName])) {
- $st = null;
- } else {
- $st = (string) $state[$curTargetName];
- }
- if ($st === null) {
- $this->_tsort($curTargetName, $targets, $state, $visiting, $ret);
- } elseif ($st === "VISITING") {
- throw new Exception("Unexpected node in visiting state: $curTargetName");
- }
- }
- $retHuman = "";
- for ($i=0,$_i=count($ret); $i < $_i; $i++) {
- $retHuman .= $ret[$i]->toString()." ";
- }
- $this->log("Complete build sequence is: $retHuman", Project::MSG_VERBOSE);
- return $ret;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- function _tsort($root, &$targets, &$state, &$visiting, &$ret) {
- $state[$root] = "VISITING";
- $visiting[] = $root;
- if (!isset($targets[$root]) || !($targets[$root] instanceof Target)) {
- $target = null;
- } else {
- $target = $targets[$root];
- }
-
- if ($target === null) {
- $sb = "Target '$root' does not exist in this project.";
- array_pop($visiting);
- if (!empty($visiting)) {
- $parent = (string) $visiting[count($visiting)-1];
- $sb .= "It is used from target '$parent'.";
- }
- throw new BuildException($sb);
- }
- $deps = $target->getDependencies();
- while($deps) {
- $cur = (string) array_shift($deps);
- if (!isset($state[$cur])) {
- $m = null;
- } else {
- $m = (string) $state[$cur];
- }
- if ($m === null) {
-
- $this->_tsort($cur, $targets, $state, $visiting, $ret);
- } elseif ($m == "VISITING") {
-
- throw $this->_makeCircularException($cur, $visiting);
- }
- }
- $p = (string) array_pop($visiting);
- if ($root !== $p) {
- throw new Exception("Unexpected internal error: expected to pop $root but got $p");
- }
- $state[$root] = "VISITED";
- $ret[] = $target;
- }
- function _makeCircularException($end, $stk) {
- $sb = "Circular dependency: $end";
- do {
- $c = (string) array_pop($stk);
- $sb .= " <- ".$c;
- } while($c != $end);
- return new BuildException($sb);
- }
-
- function addReference($name, $object) {
- if (isset($this->references[$name])) {
- $this->log("Overriding previous definition of reference to $name", Project::MSG_WARN);
- }
- $this->log("Adding reference: $name -> ".get_class($object), Project::MSG_DEBUG);
- $this->references[$name] = $object;
- }
-
- function getReferences() {
- return $this->references;
- }
-
-
- function getReference($key)
- {
- if (isset($this->references[$key])) {
- return $this->references[$key];
- }
- return null;
- }
-
- function log($msg, $level = Project::MSG_INFO) {
- $this->logObject($this, $msg, $level);
- }
- function logObject($obj, $msg, $level) {
- $this->fireMessageLogged($obj, $msg, $level);
- }
- function addBuildListener(BuildListener $listener) {
- $this->listeners[] = $listener;
- }
- function removeBuildListener(BuildListener $listener) {
- $newarray = array();
- for ($i=0, $size=count($this->listeners); $i < $size; $i++) {
- if ($this->listeners[$i] !== $listener) {
- $newarray[] = $this->listeners[$i];
- }
- }
- $this->listeners = $newarray;
- }
- function getBuildListeners() {
- return $this->listeners;
- }
- function fireBuildStarted() {
- $event = new BuildEvent($this);
- foreach($this->listeners as $listener) {
- $listener->buildStarted($event);
- }
- }
- function fireBuildFinished($exception) {
- $event = new BuildEvent($this);
- $event->setException($exception);
- foreach($this->listeners as $listener) {
- $listener->buildFinished($event);
- }
- }
- function fireTargetStarted($target) {
- $event = new BuildEvent($target);
- foreach($this->listeners as $listener) {
- $listener->targetStarted($event);
- }
- }
- function fireTargetFinished($target, $exception) {
- $event = new BuildEvent($target);
- $event->setException($exception);
- foreach($this->listeners as $listener) {
- $listener->targetFinished($event);
- }
- }
- function fireTaskStarted($task) {
- $event = new BuildEvent($task);
- foreach($this->listeners as $listener) {
- $listener->taskStarted($event);
- }
- }
- function fireTaskFinished($task, $exception) {
- $event = new BuildEvent($task);
- $event->setException($exception);
- foreach($this->listeners as $listener) {
- $listener->taskFinished($event);
- }
- }
- function fireMessageLoggedEvent($event, $message, $priority) {
- $event->setMessage($message, $priority);
- foreach($this->listeners as $listener) {
- $listener->messageLogged($event);
- }
- }
- function fireMessageLogged($object, $message, $priority) {
- $this->fireMessageLoggedEvent(new BuildEvent($object), $message, $priority);
- }
- }
|