/_build/tasks/yuiCompressTask.php

https://github.com/gbds/revolution · PHP · 255 lines · 108 code · 33 blank · 114 comment · 20 complexity · 75b09556f02efa8110efc9493a0188c6 MD5 · raw file

  1. <?php
  2. require_once 'phing/Task.php';
  3. /**
  4. * Phing Task class to compress files using the YUICompressor library.
  5. */
  6. class yuiCompressTask extends Task {
  7. /**
  8. * Path to the YUICompressor library.
  9. *
  10. * @var string
  11. */
  12. protected $lib;
  13. /**
  14. * The source files to compress.
  15. *
  16. * @var FileSet
  17. */
  18. protected $filesets = array();
  19. /**
  20. * Indicates if build should stop if an error is encountered.
  21. *
  22. * @var boolean
  23. */
  24. protected $stopOnError = false;
  25. /**
  26. * Indicates if YUICompressor should be used with --nomunge flag.
  27. *
  28. * @var bool
  29. */
  30. protected $nomunge = false;
  31. /**
  32. * Indicates if YUICompressor should be used with --preserve-semicolons flag.
  33. *
  34. * @var bool
  35. */
  36. protected $preservesemicolons = false;
  37. /**
  38. * Indicates if YUICompressor should be used with --disable-optimizations flag.
  39. *
  40. * @var bool
  41. */
  42. protected $disableoptimizations = false;
  43. /**
  44. * An optional charset to force on the compressed file output.
  45. *
  46. * @var string
  47. */
  48. protected $charset = '';
  49. /**
  50. * An optional file type, js or css; empty by default means determine by file extension.
  51. *
  52. * @var string
  53. */
  54. protected $type = '';
  55. /**
  56. * The line-break setting; -1 means not set.
  57. *
  58. * @var int
  59. */
  60. protected $linebreak = -1;
  61. /**
  62. * A suffix for the compressed files.
  63. *
  64. * @var string
  65. */
  66. protected $outputsuffix = '-min';
  67. /**
  68. * Where to output the compressed files.
  69. *
  70. * @var string
  71. */
  72. protected $dest;
  73. /**
  74. * Indicate if YUICompressor should use nomunge mode on js files.
  75. *
  76. * @param boolean $bool True if the nomunge option should be set; false by default.
  77. */
  78. public function setNomunge($bool) {
  79. $this->nomunge = $bool;
  80. }
  81. /**
  82. * Indicate if YUICompressor should preserve semicolons in js files.
  83. *
  84. * @param boolean $bool True if semicolons should be preserved; false by default.
  85. */
  86. public function setPreservesemicolons($bool) {
  87. $this->preservesemicolons = $bool;
  88. }
  89. /**
  90. * Indicate if YUICompressor should disable micro-optimizations for js files.
  91. *
  92. * @param boolean $bool True to disable optimizations; false by default.
  93. */
  94. public function setDisableoptimizations($bool) {
  95. $this->disableoptimizations = $bool;
  96. }
  97. /**
  98. * An optional charset to force when outputting the compressed file.
  99. *
  100. * @param string $string A valid charset identifier.
  101. */
  102. public function setCharset($string) {
  103. $this->charset = strtolower($string);
  104. }
  105. /**
  106. * The type of file to process; leave blank to determine by file extension.
  107. *
  108. * @param string $string The type argument: empty, 'js', or 'css'.
  109. */
  110. public function setType($string) {
  111. if (in_array(strtolower($string), array('', 'js', 'css'))) {
  112. $this->type = strtolower($string);
  113. }
  114. }
  115. /**
  116. * An optional line-break argument for YUICompressor.
  117. *
  118. * @param string $string The column to set the line-break at.
  119. */
  120. public function setLinebreak($string) {
  121. if (!empty($string) || $string === '0') {
  122. $this->type = (integer) $string;
  123. } else {
  124. $this->type = -1;
  125. }
  126. }
  127. /**
  128. * An output suffix added to the end of the file name, before the file extension.
  129. *
  130. * @param string $string An output suffix to be inserted before the file extension.
  131. */
  132. public function setOutputsuffix($string) {
  133. $this->outputsuffix = $string;
  134. }
  135. /**
  136. * Set the absolute path to the yuicompressor-x.y.z.jar.
  137. *
  138. * @param string $path The absolute path to the YUICompressor.
  139. */
  140. public function setLib($path) {
  141. $this->lib = $path;
  142. }
  143. /**
  144. * Add a FileSet to the stack and return it.
  145. *
  146. * @return FileSet The new FileSet that was added to the stack.
  147. */
  148. public function createFileSet() {
  149. $num = array_push($this->filesets, new FileSet());
  150. return $this->filesets[$num - 1];
  151. }
  152. /**
  153. * Set a flag to indicate if the build should stop when an error occurs.
  154. *
  155. * @param boolean $value True if the build should stop on error.
  156. */
  157. public function setStopOnError($value) {
  158. $this->stopOnError = (boolean) $value;
  159. }
  160. /**
  161. * Set the location to output the compressed files.
  162. *
  163. * @param string $path The location to output the compressed files.
  164. */
  165. public function setDest($path) {
  166. $this->dest = $path;
  167. }
  168. public function init() {}
  169. public function main() {
  170. $options = array();
  171. if (empty($this->type) || $this->type !== 'css') {
  172. if ($this->nomunge) $options[] = "--nomunge";
  173. if ($this->preservesemicolons) $options[] = "--preserve-semi";
  174. if ($this->disableoptimizations) $options[] = "--disable-optimizations";
  175. }
  176. if (!empty($this->type)) $options[] = "--type {$this->type}";
  177. if (!empty($this->charset)) $options[] = "--charset {$this->charset}";
  178. if ($this->linebreak > -1) $options[] = "--line-break {$this->linebreak}";
  179. foreach ($this->filesets as $fs) {
  180. try {
  181. $files = $fs->getDirectoryScanner($this->project)->getIncludedFiles();
  182. $fullPath = realpath($fs->getDir($this->project));
  183. foreach ($files as $file) {
  184. $filename = $file;
  185. if (!empty($this->outputsuffix)) {
  186. $extPos = strrpos($filename, '.');
  187. if ($extPos > 0) {
  188. $ext = substr($filename, $extPos);
  189. $filename = substr($filename, 0, $extPos);
  190. $filename .= $this->outputsuffix . $ext;
  191. }
  192. }
  193. $outfile = $this->dest . '/' . $filename;
  194. if (file_exists(dirname($outfile)) == false) {
  195. mkdir(dirname($outfile), 0700, true);
  196. }
  197. $lib = realpath($this->lib);
  198. $args = implode(' ', $options);
  199. $src = $fullPath . DIRECTORY_SEPARATOR . $file;
  200. $command = "java -jar {$lib} -o {$outfile} {$args} {$src}";
  201. $output = array();
  202. $return = null;
  203. $this->log("Compressing src {$src} to {$outfile} using args {$args}", Project::MSG_VERBOSE);
  204. exec($command, $output, $return);
  205. foreach ($output as $line) {
  206. $this->log($line);
  207. }
  208. if ($return != 0) {
  209. throw new BuildException("Task exited with code {$return}");
  210. }
  211. }
  212. } catch (BuildException $be) {
  213. // directory doesn't exist or is not readable
  214. if ($this->stopOnError) {
  215. throw $be;
  216. } else {
  217. $this->log($be->getMessage(), Project::MSG_WARN);
  218. }
  219. }
  220. }
  221. }
  222. }