PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/build.php

https://github.com/Leon-/last.mu
PHP | 265 lines | 178 code | 39 blank | 48 comment | 27 complexity | bda3b3b30af7b69e2e5dc98f54b87b19 MD5 | raw file
Possible License(s): GPL-2.0
  1. #!/usr/bin/php
  2. <?php
  3. /**
  4. * build script for last.mu
  5. */
  6. if (strtolower(php_sapi_name()) != 'cli') die("The build script has to be invoked from cli.\n");
  7. echo "Welcome to build script for last.mu\n";
  8. $options = parseParams($argv);
  9. if ($options['version'] === '') {
  10. if (file_exists('builds/.lastversion')) {
  11. $options['version'] = file_get_contents('builds/.lastversion');
  12. }
  13. else {
  14. $options['version'] = 'Unknown';
  15. }
  16. }
  17. if ($argc === 1) {
  18. echo "Which version do you want to build (Last was ".$options['version'].")?\n";
  19. echo "Version number strings should follow the 'PHP-standarized' version \nnumber string guidline.\n";
  20. echo "Enter version string and press enter:\n";
  21. echo "> ";
  22. $options['version'] = trim(fread(STDIN, 1024));
  23. echo "I will use ".$options['version']." as version number\n";
  24. do {
  25. echo "Do you want to include all available modules? (Y/N)\n";
  26. echo "> ";
  27. $input = strtoupper(trim(fread(STDIN, 1024)));
  28. if ($input === 'Y') {
  29. $options['modules']['AddOn'] = array_map(function($item) {
  30. return basename($item, '.js');
  31. }, glob('modules/AddOn/*.js'));
  32. }
  33. } while ($input !== 'Y' && $input !== 'N');
  34. do {
  35. echo "Do you want a minified version? (Y/N)\n";
  36. echo "> ";
  37. $input = strtoupper(trim(fread(STDIN, 1024)));
  38. if ($input == 'Y') {
  39. $options['minify'] = true;
  40. echo "I will minify the script\n";
  41. }
  42. } while ($input !== 'Y' && $input !== 'N');
  43. echo "I have everything i need, starting build";
  44. for ($i = 0; $i < 3; $i++) {
  45. echo ".";
  46. usleep(1E6/2);
  47. }
  48. echo "\n\n";
  49. }
  50. //build
  51. //find namespaces
  52. $namespaces = glob('namespaces/*');
  53. // find ProtoBasic files
  54. $protoBasicFiles = array('Object', 'Function', 'Class', 'Enumerable', 'Array', 'Hash', 'String', 'RegExp', 'Date', 'Storage', 'Element', 'Event', 'Style', 'Animations', 'Dragging');
  55. // find media resources
  56. $mediaResources = glob('media/*');
  57. // fileinfo object
  58. $finfo = new finfo(FILEINFO_MIME_TYPE);
  59. // read in header
  60. $result = file_get_contents('header.js')."\n";
  61. // add namespaces
  62. foreach ($namespaces as $namespace) {
  63. echo "Adding namespace: ".$namespace."\n";
  64. $result .= file_get_contents($namespace)."\n";
  65. }
  66. // add ProtoBasic files
  67. foreach ($protoBasicFiles as $protoBasicFile) {
  68. echo "Adding ProtoBasic file: ProtoBasic/".$protoBasicFile.".js\n";
  69. $result .= file_get_contents('ProtoBasic/'.$protoBasicFile.'.js')."\n";
  70. }
  71. // add media resources
  72. foreach ($mediaResources as $mediaResource) {
  73. echo "Adding resource: ".$mediaResource."\n";
  74. $name = substr(basename($mediaResource), 0, strrpos(basename($mediaResource), '.'));
  75. $mimeType = $finfo->file($mediaResource);
  76. $base64Content = base64_encode(file_get_contents($mediaResource));
  77. $result .= <<<MEDIA
  78. Media.$name = {
  79. mimeType: '$mimeType',
  80. content: '$base64Content',
  81. get dataURI() {
  82. return 'data:'+this.mimeType+';base64,'+this.content;
  83. }
  84. };
  85. MEDIA;
  86. }
  87. // add modules
  88. foreach ($options['modules'] as $categoryName => $category) {
  89. foreach ($category as $module) {
  90. if (file_exists('./modules/'.$categoryName.'/'.$module.'.js')) {
  91. echo "Adding ".strtolower($categoryName)." module: ".$module."\n";
  92. $result .= file_get_contents('./modules/'.$categoryName.'/'.$module.'.js')."\n";
  93. }
  94. else {
  95. echo $categoryName." module ".$module." doesn't exist!\n";
  96. }
  97. }
  98. }
  99. $result .= file_get_contents('main.js');
  100. $result = str_replace('{version}', $options['version'].'-'.$options['build'], $result);
  101. if ($options['minify']) {
  102. echo "Minifying\n";
  103. $result = preg_replace_callback("~('.*?')~", 'removeStrings', $result);
  104. $result = preg_replace_callback("~(// ==UserScript==.*// ==/UserScript==)~s", 'removeHeader', $result);
  105. $result = preg_replace('~/\\*.*\\*/~Us', '', $result);
  106. $result = preg_replace('~//.*~', '', $result);
  107. $result = str_replace(array("\t","\r"), '', $result);
  108. $result = str_replace("\n\n", "\n", $result);
  109. $result = StringStack::reinsertStrings($result, 'string');
  110. $result = StringStack::reinsertStrings($result, 'header');
  111. }
  112. echo "Writing file builds/last.mu ".$options['version'].'-'.$options['build'].".user.js\n";
  113. // Write file
  114. file_put_contents('builds/last.mu '.$options['version'].'-'.$options['build'].'.user.js', $result);
  115. // save version
  116. file_put_contents('builds/.lastversion', $options['version']);
  117. // save build
  118. file_put_contents('builds/.lastbuild', $options['build']);
  119. echo "Finished\n";
  120. if ($argc == 1) {
  121. echo "Press Enter to exit...";
  122. fread(STDIN, 1024);
  123. }
  124. function parseParams($argv) {
  125. $options = array(
  126. 'version' => '',
  127. 'build' => 1,
  128. 'minify' => false,
  129. 'modules' => array(
  130. // TODO: get util module stuff dynamic
  131. 'Util' => array('AbstractModule', 'AbstractCoreModule'),
  132. 'Core' => array_map(function($entry) {
  133. return basename($entry, '.js');
  134. }, glob('modules/Core/*.js')),
  135. 'AddOn' => array()
  136. )
  137. );
  138. for ($i = 1, $length = count($argv); $i < $length; $i++) {
  139. $command = substr($argv[$i], 2, (((strrpos($argv[$i], '-')-1) > 2) ? (strrpos($argv[$i], '-')-1) : (strlen($argv[$i])-1)));
  140. if (strrpos($command, '-') === (strlen($command)-1)) {
  141. $command = substr($command, 0, strlen($command)-1);
  142. }
  143. switch ($command) {
  144. case 'version':
  145. $options['version'] = substr($argv[$i], strrpos($argv[$i], '-')+1);
  146. break;
  147. case 'minify':
  148. $options['minify'] = true;
  149. break;
  150. case 'with-module':
  151. $options['modules']['AddOn'][] = substr($argv[$i], strrpos($argv[$i], '-')+1);
  152. break;
  153. }
  154. }
  155. if (file_exists('builds/.lastbuild')) {
  156. $options['build'] = (string)(intval(file_get_contents('builds/.lastbuild')) + 1);
  157. }
  158. $options['modules']['AddOn'] = array_unique($options['modules']['AddOn']);
  159. return $options;
  160. }
  161. function removeStrings($matches) {
  162. return StringStack::pushToStringStack($matches[1], 'string');
  163. }
  164. function removeHeader($matches) {
  165. return StringStack::pushToStringStack($matches[1]."\n", 'header');
  166. }
  167. /**
  168. * Replaces quoted strings in a text.
  169. *
  170. * @author Marcel Werk
  171. * @copyright 2001-2009 WoltLab GmbH
  172. * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  173. * @package com.woltlab.wcf
  174. * @subpackage util
  175. * @category Community Framework
  176. */
  177. class StringStack {
  178. protected static $stringStack = array();
  179. /**
  180. * Replaces a string with an unique hash value.
  181. *
  182. * @param string $string
  183. * @param string $type
  184. * @return string $hash
  185. */
  186. public static function pushToStringStack($string, $type = 'default') {
  187. $hash = '@@'.sha1(uniqid(microtime()).$string).'@@';
  188. if (!isset(self::$stringStack[$type])) {
  189. self::$stringStack[$type] = array();
  190. }
  191. self::$stringStack[$type][$hash] = $string;
  192. return $hash;
  193. }
  194. /**
  195. * Reinserts Strings that have been replaced by unique hash values.
  196. *
  197. * @param string $string
  198. * @param string $type
  199. * @return string $string
  200. */
  201. public static function reinsertStrings($string, $type = 'default') {
  202. if (isset(self::$stringStack[$type])) {
  203. foreach (self::$stringStack[$type] as $hash => $value) {
  204. if (strpos($string, $hash) !== false) {
  205. $string = str_replace($hash, $value, $string);
  206. unset(self::$stringStack[$type][$hash]);
  207. }
  208. }
  209. }
  210. return $string;
  211. }
  212. /**
  213. * Returns the stack.
  214. *
  215. * @param string $type
  216. * @return array
  217. */
  218. public static function getStack($type = 'default') {
  219. if (isset(self::$stringStack[$type])) {
  220. return self::$stringStack[$type];
  221. }
  222. return array();
  223. }
  224. }