PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/v1.4.5/Build/build.php

#
PHP | 336 lines | 178 code | 43 blank | 115 comment | 52 complexity | 1ca033488c71c17a8d8e21dc18ed83a1 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2007 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  22. * @license http://www.gnu.org/licenses/lgpl.txt LGPL
  23. * @version ##VERSION##, ##DATE##
  24. */
  25. /**
  26. * This file creates a build of PHPExcel
  27. */
  28. // Build parameters
  29. $sVersion = "";
  30. $sDate = "";
  31. // Read build parameters from STDIN
  32. $stdin = fopen("php://stdin", 'r');
  33. echo "PHPExcel build script\n";
  34. echo "---------------------\n";
  35. echo "Enter the version number you want to add to the build:\t\t\t";
  36. $sVersion = rtrim(fread($stdin, 1024));
  37. echo "Enter the date number you want to add to the build: (YYYY-MM-DD)\t";
  38. $sDate = rtrim(fread($stdin, 1024));
  39. echo "\n\n";
  40. // Specify paths and files to include
  41. $aFilesToInclude = array('../changelog.txt', '../install.txt', '../license.txt');
  42. $aPathsToInclude = array('../Classes', '../Tests', '../Documentation');
  43. $aIgnorePatterns = array('.svn', '.settings', '.project', '.projectOptions', '.cache', 'assets');
  44. $sClassPath = '../Classes';
  45. $sPEARPath = 'C:\lamp\php5\pear';
  46. $sAPIDocumentation = '../Documentation/API/';
  47. // Create API documentation folder and tell to create documentation
  48. @mkdir($sAPIDocumentation);
  49. echo "Please, generate API documentation using phpDocumentor.\r\n";
  50. $finished = '';
  51. while (strtolower($finished) != 'y') {
  52. $finished = '';
  53. echo "Has documentation generation finished? (y/n)\t";
  54. $finished = rtrim(fread($stdin, 1024));
  55. }
  56. echo "\n\n\n";
  57. // Resulting file
  58. $strResultingFile = $sVersion . '.zip';
  59. // Starting build
  60. echo date('H:i:s') . " Starting build...\n";
  61. // Create new ZIP file and open it for writing
  62. echo date('H:i:s') . " Creating ZIP archive...\n";
  63. $objZip = new ZipArchive();
  64. // Try opening the ZIP file
  65. if ($objZip->open($strResultingFile, ZIPARCHIVE::OVERWRITE) !== true) {
  66. throw new Exeption("Could not open " . $strResultingFile . " for writing!");
  67. }
  68. // Add files to include
  69. foreach ($aFilesToInclude as $strFile) {
  70. echo date('H:i:s') . " Adding file $strFile\n";
  71. addFileToZIP($strFile, $objZip, $sVersion, $sDate);
  72. }
  73. // Add paths to include
  74. foreach ($aPathsToInclude as $strPath) {
  75. addPathToZIP($strPath, $objZip, $sVersion, $sDate);
  76. }
  77. // Set archive comment...
  78. echo date('H:i:s') . " Set archive comment...\n";
  79. $objZip->setArchiveComment('PHPExcel - http://www.codeplex.com/PHPExcel');
  80. // Close file
  81. echo date('H:i:s') . " Saving ZIP archive...\n";
  82. $objZip->close();
  83. // Copy classes directory
  84. echo date('H:i:s') . " Copying class directory...\n";
  85. mkdir('./tmp');
  86. dircopy($sClassPath, './tmp');
  87. // Create PEAR package.xml
  88. echo date('H:i:s') . " Creating PEAR package.xml...\n";
  89. $packageFile = file_get_contents('package.xml');
  90. $packageFile = replaceMetaData($packageFile, $sVersion, $sDate);
  91. $packageFile = str_replace('##PEAR_DIR##', addPathToPEAR('./tmp', '', $sVersion, $sDate), $packageFile);
  92. $fh = fopen('./tmp/package.xml', 'w');
  93. fwrite($fh, $packageFile);
  94. fclose($fh);
  95. // Create PEAR package
  96. echo date('H:i:s') . " Creating PEAR package...\n";
  97. echo shell_exec("$sPEARPath package ./tmp/package.xml");
  98. // Wait a minute (TortoiseSVN on USB stick is slow!)
  99. echo date('H:i:s') . " Waiting...\n";
  100. sleep(120);
  101. // Clean temporary files
  102. echo date('H:i:s') . " Cleaning temporary files...\n";
  103. unlink('./tmp/package.xml');
  104. rm('./tmp');
  105. // Finished build
  106. echo date('H:i:s') . " Finished build!\n";
  107. fclose($stdin);
  108. /**
  109. * Add a specific path's files and folders to a ZIP object
  110. *
  111. * @param string $strPath Path to add
  112. * @param ZipArchive $objZip ZipArchive object
  113. * @param string $strVersion Version string
  114. * @param string $strDate Date string
  115. */
  116. function addPathToZIP($strPath, $objZip, $strVersion, $strDate) {
  117. global $aIgnorePatterns;
  118. echo date('H:i:s') . " Adding path $strPath...\n";
  119. $currentDir = opendir($strPath);
  120. while ($strFile = readdir($currentDir)) {
  121. if ($strFile != '.' && $strFile != '..') {
  122. if (is_file($strPath . '/' . $strFile)) {
  123. addFileToZIP($strPath . '/' . $strFile, $objZip, $strVersion, $strDate);
  124. } else if (is_dir($strPath . '/' . $strFile)) {
  125. if (!shouldIgnore($strFile)) {
  126. addPathToZIP( ($strPath . '/' . $strFile), $objZip, $strVersion, $strDate );
  127. }
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * Add a specific file to ZIP
  134. *
  135. * @param string $strFile File to add
  136. * @param ZipArchive $objZip ZipArchive object
  137. * @param string $strVersion Version string
  138. * @param string $strDate Date string
  139. */
  140. function addFileToZIP($strFile, $objZip, $strVersion, $strDate) {
  141. if (!shouldIgnore($strFile)) {
  142. $fileContents = file_get_contents($strFile);
  143. $fileContents = replaceMetaData($fileContents, $strVersion, $strDate);
  144. //$objZip->addFile($strFile, cleanFileName($strFile));
  145. $objZip->addFromString( cleanFileName($strFile), $fileContents );
  146. }
  147. }
  148. /**
  149. * Cleanup a filename
  150. *
  151. * @param string $strFile Filename
  152. * @return string Filename
  153. */
  154. function cleanFileName($strFile) {
  155. $strFile = str_replace('../', '', $strFile);
  156. $strFile = str_replace('WINDOWS', '', $strFile);
  157. while (eregi('//', $strFile)) {
  158. $strFile = str_replace('//', '/', $strFile);
  159. }
  160. return $strFile;
  161. }
  162. /**
  163. * Replace metadata in string
  164. *
  165. * @param string $strString String contents
  166. * @param string $strVersion Version string
  167. * @param string $strDate Date string
  168. * @return string String contents
  169. */
  170. function replaceMetaData($strString, $strVersion, $strDate) {
  171. $strString = str_replace('##VERSION##', $strVersion, $strString);
  172. $strString = str_replace('##DATE##', $strDate, $strString);
  173. return $strString;
  174. }
  175. /**
  176. * Add a specific path's files and folders to a PEAR dir list
  177. *
  178. * @param string $strPath Path to add
  179. * @param string $strPEAR String containing PEAR dir definitions
  180. * @param string $strVersion Version string
  181. * @param string $strDate Date string
  182. * @return string String containing PEAR dir definitions
  183. */
  184. function addPathToPEAR($strPath, $strPEAR, $strVersion, $strDate) {
  185. global $aIgnorePatterns;
  186. $currentDir = opendir($strPath);
  187. while ($strFile = readdir($currentDir)) {
  188. if ($strFile != '.' && $strFile != '..') {
  189. if (is_file($strPath . '/' . $strFile) && !eregi('package.xml', $strFile)) {
  190. $strPEAR .= addFileToPEAR($strPath . '/' . $strFile, '', $strVersion, $strDate);
  191. } else if (is_dir($strPath . '/' . $strFile)) {
  192. if (!shouldIgnore($strFile)) {
  193. $strPEAR .= '<dir name="' . $strFile . '">';
  194. $strPEAR .= addPathToPEAR( ($strPath . '/' . $strFile), '', $strVersion, $strDate );
  195. $strPEAR .= '</dir>';
  196. }
  197. }
  198. }
  199. }
  200. return $strPEAR;
  201. }
  202. /**
  203. * Add a specific file to a PEAR dir list
  204. *
  205. * @param string $strFile File to add
  206. * @param string $strPEAR String containing PEAR dir definitions
  207. * @param string $strVersion Version string
  208. * @param string $strDate Date string
  209. * @return string String containing PEAR dir definitions
  210. */
  211. function addFileToPEAR($strFile, $strPEAR, $strVersion, $strDate) {
  212. if (!shouldIgnore($strFile)) {
  213. $fileContents = file_get_contents($strFile);
  214. $fileContents = replaceMetaData($fileContents, $strVersion, $strDate);
  215. $fh = fopen($strFile, 'w');
  216. fwrite($fh, $fileContents);
  217. fclose($fh);
  218. $strPEAR .= '<file name="' . basename($strFile) . '" role="php" />';
  219. return $strPEAR;
  220. } else {
  221. return '';
  222. }
  223. }
  224. /**
  225. * Copy a complete directory
  226. *
  227. * @param string $srcdir Source directory
  228. * @param string $dstdir Destination directory
  229. * @return int Number of copied files
  230. */
  231. function dircopy($srcdir, $dstdir, $verbose = false) {
  232. $num = 0;
  233. if(!is_dir($dstdir) && !shouldIgnore($dstdir)) mkdir($dstdir);
  234. if($curdir = opendir($srcdir)) {
  235. while($file = readdir($curdir)) {
  236. if($file != '.' && $file != '..') {
  237. $srcfile = $srcdir . '\\' . $file;
  238. $dstfile = $dstdir . '\\' . $file;
  239. if(is_file($srcfile) && !shouldIgnore($srcfile)) {
  240. if(is_file($dstfile)) $ow = filemtime($srcfile) - filemtime($dstfile); else $ow = 1;
  241. if($ow > 0) {
  242. if($verbose) echo "Copying '$srcfile' to '$dstfile'...";
  243. if(copy($srcfile, $dstfile)) {
  244. touch($dstfile, filemtime($srcfile)); $num++;
  245. if($verbose) echo "OK\n";
  246. }
  247. else echo "Error: File '$srcfile' could not be copied!\n";
  248. }
  249. }
  250. else if(is_dir($srcfile) && !shouldIgnore($srcfile)) {
  251. $num += dircopy($srcfile, $dstfile, $verbose);
  252. }
  253. }
  254. }
  255. closedir($curdir);
  256. }
  257. return $num;
  258. }
  259. /**
  260. * rm() -- Very Vigorously erase files and directories. Also hidden files !!!!
  261. *
  262. * @param $dir string
  263. * be carefull to:
  264. * if($obj=='.' || $obj=='..') continue;
  265. * if not it will erase all the server...it happened to me ;)
  266. * the function is permission dependent.
  267. */
  268. function rm($dir) {
  269. if(!$dh = @opendir($dir)) return;
  270. while (($obj = readdir($dh))) {
  271. if($obj=='.' || $obj=='..') continue;
  272. @chmod($dir.'/'.$obj, 0777);
  273. if (!@unlink($dir.'/'.$obj)) rm($dir.'/'.$obj);
  274. }
  275. @rmdir($dir);
  276. @shell_exec('rmdir /S /Q "' . $dir . '"');
  277. }
  278. /**
  279. * Should a file/folder be ignored?
  280. *
  281. * @param string $pName
  282. * @return boolean
  283. */
  284. function shouldIgnore($pName = '') {
  285. global $aIgnorePatterns;
  286. $ignore = false;
  287. foreach ($aIgnorePatterns as $ignorePattern) {
  288. if (eregi($ignorePattern, $pName)) {
  289. $ignore = true;
  290. }
  291. }
  292. return $ignore;
  293. }