PageRenderTime 59ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Tasks/ProjectTask.php

http://github.com/phpundercontrol/phpUnderControl
PHP | 241 lines | 127 code | 26 blank | 88 comment | 11 complexity | 872da30e216132a09ddac8d5eb06a577 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of phpUnderControl.
  4. *
  5. * PHP Version 5.2.0
  6. *
  7. * Copyright (c) 2007-2011, Manuel Pichler <mapi@phpundercontrol.org>.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Manuel Pichler nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @category QualityAssurance
  40. * @package Tasks
  41. * @author Manuel Pichler <mapi@phpundercontrol.org>
  42. * @copyright 2007-2011 Manuel Pichler. All rights reserved.
  43. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  44. * @version SVN: $Id$
  45. * @link http://www.phpundercontrol.org/
  46. */
  47. /**
  48. * This task creates the base directory structure for a new project.
  49. *
  50. * @category QualityAssurance
  51. * @package Tasks
  52. * @author Manuel Pichler <mapi@phpundercontrol.org>
  53. * @copyright 2007-2011 Manuel Pichler. All rights reserved.
  54. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  55. * @version Release: @package_version@
  56. * @link http://www.phpundercontrol.org/
  57. */
  58. class phpucProjectTask extends phpucAbstractTask implements phpucConsoleExtensionI
  59. {
  60. /**
  61. * Validates that the required <cc-install-dir>/projects directory exists.
  62. *
  63. * @return void
  64. * @throws phpucValidateException If the directory doesn't exist or the
  65. * a project for the given name already exists.
  66. */
  67. public function validate()
  68. {
  69. $installDir = $this->args->getArgument( 'cc-install-dir' );
  70. $projectName = $this->args->getOption( 'project-name' );
  71. if ( !is_dir( $installDir . '/projects' ) )
  72. {
  73. throw new phpucValidateException(
  74. 'Missing projects directory <cc-install-dir>/projects.'
  75. );
  76. }
  77. if ( is_dir( $installDir . '/projects/' . $projectName ) )
  78. {
  79. throw new phpucValidateException( 'Project directory already exists.' );
  80. }
  81. if ( $this->args->hasOption( 'ant-script', 'project' ) )
  82. {
  83. if ( !( $path = realpath( $this->args->getOption( 'ant-script' ) ) ) )
  84. {
  85. throw new phpucValidateException('Not a valid ant-script location.');
  86. }
  87. }
  88. }
  89. /**
  90. * Adds a new project to CruiseControl.
  91. *
  92. * This method creates the base directory structure for a CruiseControl
  93. * project and adds this project to the CruiseControl config.xml file.
  94. *
  95. * @return void
  96. */
  97. public function execute()
  98. {
  99. $out = phpucConsoleOutput::get();
  100. $out->writeLine( 'Performing project task.' );
  101. $installDir = $this->args->getArgument( 'cc-install-dir' );
  102. $projectName = $this->args->getOption( 'project-name' );
  103. $projectPath = sprintf( '%s/projects/%s', $installDir, $projectName );
  104. $out->startList();
  105. $out->writeListItem(
  106. 'Creating project directory: projects/{1}', $projectName
  107. );
  108. mkdir( $projectPath );
  109. $out->writeListItem(
  110. 'Creating source directory: projects/{1}/source', $projectName
  111. );
  112. mkdir( $projectPath . '/source' );
  113. $out->writeListItem(
  114. 'Creating build directory: projects/{1}/build', $projectName
  115. );
  116. mkdir( $projectPath . '/build' );
  117. $out->writeListItem(
  118. 'Creating log directory: projects/{1}/build/logs', $projectName
  119. );
  120. mkdir( $projectPath . '/build/logs' );
  121. $out->writeListItem(
  122. 'Creating build file: projects/{1}/build.xml', $projectName
  123. );
  124. $buildFile = new phpucBuildFile( $projectPath . '/build.xml', $projectName );
  125. $buildFile->store();
  126. $out->writeListItem( 'Creating backup of file: config.xml.orig' );
  127. @unlink( $installDir . '/config.xml.orig' );
  128. copy( $installDir . '/config.xml', $installDir . '/config.xml.orig' );
  129. $out->writeListItem( 'Searching ant directory' );
  130. if ( !( $anthome = $this->getAntHome( $installDir ) ) )
  131. {
  132. throw new phpucExecuteException( 'ERROR: Cannot locate ant directory.' );
  133. }
  134. $out->writeListItem( 'Modifying project file: config.xml' );
  135. $config = new phpucConfigFile( $installDir . '/config.xml' );
  136. $project = $config->createProject( $projectName );
  137. $project->interval = $this->args->getOption( 'schedule-interval' );
  138. $project->anthome = $anthome;
  139. if ( $this->args->hasOption( 'ant-script' ) )
  140. {
  141. $project->antscript = $this->args->getOption( 'ant-script' );
  142. }
  143. $config->store();
  144. $out->writeLine();
  145. }
  146. /**
  147. * Tries to find the ant home directory.
  148. *
  149. * @param string $installDir The CruiseControl installation directory.
  150. *
  151. * @return string
  152. */
  153. public function getAntHome( $installDir )
  154. {
  155. if ( count( $ant = glob( sprintf( '%s/apache-ant*', $installDir ) ) ) === 0 )
  156. {
  157. if ( file_exists( $installDir . '/bin/ant' ) )
  158. {
  159. return $installDir;
  160. }
  161. $os = phpucFileUtil::getOS();
  162. if ( $os !== phpucFileUtil::OS_WINDOWS )
  163. {
  164. $ant = shell_exec( 'which ant' );
  165. }
  166. if ( strstr( trim( $ant ), 'bin/ant' ) )
  167. {
  168. return substr( $ant, 0, ( strlen( $ant ) - 7 ) );
  169. }
  170. return false;
  171. } else {
  172. return array_pop( $ant );
  173. }
  174. }
  175. /**
  176. * Callback method that registers a command extension.
  177. *
  178. * @param phpucConsoleInputDefinition $def
  179. * The input definition container.
  180. * @param phpucConsoleCommandI $command
  181. * The context cli command instance.
  182. *
  183. * @return void
  184. */
  185. public function registerCommandExtension(
  186. phpucConsoleInputDefinition $def,
  187. phpucConsoleCommandI $command
  188. ) {
  189. $def->addOption(
  190. $command->getCommandId(),
  191. 'A',
  192. 'ant-script',
  193. 'The full path to a custom ant launcher script.',
  194. true
  195. );
  196. $def->addOption(
  197. $command->getCommandId(),
  198. 'j',
  199. 'project-name',
  200. 'The name of the generated project.',
  201. true,
  202. 'php-under-control',
  203. true
  204. );
  205. $def->addOption(
  206. $command->getCommandId(),
  207. 'i',
  208. 'schedule-interval',
  209. 'Schedule interval.',
  210. true,
  211. 300,
  212. true
  213. );
  214. }
  215. }