/src/main/php/org/stubbles/phing/StarWriterTask.php

https://github.com/stubbles/star · PHP · 321 lines · 137 code · 26 blank · 158 comment · 15 complexity · 1b69b63f707ace31141614e372f3e873 MD5 · raw file

  1. <?php
  2. /**
  3. * Task to create star files.
  4. *
  5. * @author Frank Kleine <mikey@stubbles.net>
  6. * @package star
  7. * @subpackage phing
  8. */
  9. /**
  10. * Uses the Phing Task
  11. */
  12. require_once 'phing/Task.php';
  13. /**
  14. * Uses star writer classes
  15. */
  16. require_once 'star/StarArchive.php';
  17. /**
  18. * Uses star writer classes
  19. */
  20. require_once 'star/StarFile.php';
  21. /**
  22. * Uses star writer classes
  23. */
  24. require_once 'star/StarCreator.php';
  25. /**
  26. * Uses star decorator
  27. */
  28. require_once dirname(__FILE__) . '/StarDecorator.php';
  29. /**
  30. * Task to create star files.
  31. *
  32. * @package star
  33. * @subpackage phing
  34. */
  35. class StarWriterTask extends Task
  36. {
  37. /**
  38. * the title meta information
  39. *
  40. * @var string
  41. */
  42. protected $title = '';
  43. /**
  44. * version to create
  45. *
  46. * @var string
  47. */
  48. protected $version = '';
  49. /**
  50. * the package meta information
  51. *
  52. * @var string
  53. */
  54. protected $package = '';
  55. /**
  56. * the author meta information
  57. *
  58. * @var string
  59. */
  60. protected $author = '';
  61. /**
  62. * the copyright meta information
  63. *
  64. * @var string
  65. */
  66. protected $copyright = '';
  67. /**
  68. * the copyright meta information
  69. *
  70. * @var string
  71. */
  72. protected $copyrightStartYear = 2007;
  73. /**
  74. * the preface to add to the star file
  75. *
  76. * @var string
  77. */
  78. protected $preface = null;
  79. /**
  80. * the version of the starfile to build
  81. *
  82. * @var int
  83. */
  84. protected $starVersion = 2;
  85. /**
  86. * the path to put the star archive into
  87. *
  88. * @var string
  89. */
  90. protected $buildPath;
  91. /**
  92. * the base source path
  93. *
  94. * @var string
  95. */
  96. protected $baseSrcPath;
  97. /**
  98. * the source files
  99. *
  100. * @var FileSet
  101. */
  102. protected $filesets = array();
  103. /**
  104. * list of decorators for single files
  105. *
  106. * @var array<string,StarDecorator>
  107. */
  108. protected $decorators = array();
  109. /**
  110. * sets the title
  111. *
  112. * @param string $title
  113. */
  114. public function setTitle($title)
  115. {
  116. $this->title = $title;
  117. }
  118. /**
  119. * sets the version number
  120. *
  121. * @param string $version
  122. */
  123. public function setVersion($version)
  124. {
  125. $this->version = $version;
  126. }
  127. /**
  128. * sets the package
  129. *
  130. * @param string $package
  131. */
  132. public function setPackage($package)
  133. {
  134. $this->package = $package;
  135. }
  136. /**
  137. * sets the author
  138. *
  139. * @param string $author
  140. */
  141. public function setAuthor($author)
  142. {
  143. $this->author = $author;
  144. }
  145. /**
  146. * sets the copyright
  147. *
  148. * @param string $copyright
  149. */
  150. public function setCopyright($copyright)
  151. {
  152. $this->copyright = $copyright;
  153. }
  154. /**
  155. * sets the copyright start year
  156. *
  157. * @param string $copyrightStartYear
  158. */
  159. public function setCopyrightStartYear($copyrightStartYear)
  160. {
  161. $this->copyrightStartYear = $copyrightStartYear;
  162. }
  163. /**
  164. * sets the preface
  165. *
  166. * @param string $preface
  167. */
  168. public function setPreface($preface)
  169. {
  170. $this->preface = $preface;
  171. }
  172. /**
  173. * sets the star version number
  174. *
  175. * @param string $starVersion
  176. */
  177. public function setStarVersion($starVersion)
  178. {
  179. $this->starVersion = $starVersion;
  180. }
  181. /**
  182. * the the path to put the star archive into
  183. *
  184. * @param string $buildPath
  185. */
  186. public function setBuildPath($buildPath)
  187. {
  188. $this->buildPath = $buildPath;
  189. }
  190. /**
  191. * sets the base source path
  192. *
  193. * @param string $baseSrcPath
  194. */
  195. public function setBaseSrcPath($baseSrcPath)
  196. {
  197. $this->baseSrcPath = realpath($baseSrcPath);
  198. }
  199. /**
  200. * adds a decorator
  201. *
  202. * @param StarDecorator $decorator
  203. */
  204. public function addStarDecorator(StarDecorator $decorator)
  205. {
  206. $this->decorators[] = $decorator;
  207. }
  208. /**
  209. * Nested creator, adds a set of files (nested fileset attribute).
  210. */
  211. public function createFileSet()
  212. {
  213. $num = array_push($this->filesets, new FileSet());
  214. return $this->filesets[$num - 1];
  215. }
  216. /**
  217. * The init method: Do init steps.
  218. */
  219. public function init()
  220. {
  221. // nothing to do here
  222. }
  223. /**
  224. * The main entry point method.
  225. */
  226. public function main()
  227. {
  228. // need to rewrap the decorator array because due to some unknown
  229. // reasons the properties of the decorator are not set when it is
  230. // added to the task
  231. // works now with refactored buildfile
  232. $decorators = array();
  233. foreach ($this->decorators as $decorator) {
  234. $decorators[$decorator->getStarId()] = $decorator;
  235. }
  236. $starArchive = new StarArchive(new StarCreator($this->buildPath), $this->starVersion);
  237. foreach($this->filesets as $fs) {
  238. try {
  239. $files = $fs->getDirectoryScanner($this->project)->getIncludedFiles();
  240. $realPath = str_replace($this->baseSrcPath, '', realpath($fs->getDir($this->project)));
  241. if (DIRECTORY_SEPARATOR === $realPath{0}) {
  242. $realPath = substr($realPath, 1);
  243. }
  244. foreach ($files as $file) {
  245. if (strlen($realPath) > 0) {
  246. $fullFileName = $this->baseSrcPath . DIRECTORY_SEPARATOR . $realPath . DIRECTORY_SEPARATOR . $file;
  247. $file = $realPath . DIRECTORY_SEPARATOR . $file;
  248. } else {
  249. $fullFileName = $this->baseSrcPath . DIRECTORY_SEPARATOR . $file;
  250. }
  251. if (substr($file, 0, 3) == 'php') {
  252. $srcDir = 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'php' . DIRECTORY_SEPARATOR;
  253. $pos = strpos($fullFileName, $srcDir) + strlen($srcDir);
  254. $id = str_replace(DIRECTORY_SEPARATOR, '::', str_replace('.php', '', substr($fullFileName, $pos)));
  255. } else {
  256. $srcDir = 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR;
  257. $pos = strpos($fullFileName, $srcDir) + strlen($srcDir);
  258. $id = str_replace(DIRECTORY_SEPARATOR, '/', substr($fullFileName, $pos));
  259. }
  260. $this->log('Adding ' . $fullFileName . ' as ' . $id);
  261. if (isset($decorators[$id]) === false) {
  262. $starFile = new StarFile($fullFileName, $this->baseSrcPath);
  263. } else {
  264. require_once $decorators[$id]->getFile();
  265. $class = $decorators[$id]->getClass();
  266. $starFile = new $class(new StarFile($fullFileName, $this->baseSrcPath));
  267. $this->log('Decorating ' . $id . ' with ' . $class);
  268. }
  269. $starArchive->add($starFile, $id);
  270. }
  271. } catch (BuildException $be) {
  272. // directory doesn't exist or is not readable
  273. if ($this->failonerror) {
  274. throw $be;
  275. } else {
  276. $this->log($be->getMessage(), $this->quiet ? PROJECT_MSG_VERBOSE : PROJECT_MSG_WARN);
  277. }
  278. }
  279. }
  280. if (null != $this->preface) {
  281. $starArchive->setPreface($this->preface);
  282. }
  283. $starArchive->addMetaData('title', $this->title);
  284. $starArchive->addMetaData('package', $this->package);
  285. $starArchive->addMetaData('version', $this->version);
  286. $starArchive->addMetaData('author', $this->author);
  287. if (date('Y') == $this->copyrightStartYear) {
  288. $starArchive->addMetaData('copyright', '(c) ' . $this->copyrightStartYear . ' ' . $this->copyright);
  289. } else {
  290. $starArchive->addMetaData('copyright', '(c) ' . $this->copyrightStartYear . '-' . date('Y') . ' ' . $this->copyright);
  291. }
  292. $starArchive->create();
  293. }
  294. }
  295. ?>