PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/build/build_diff.php

https://github.com/naderman/phpbb-orchestra
PHP | 415 lines | 302 code | 66 blank | 47 comment | 33 complexity | 430f270a0a0338a3dadc9fb5175ab946 MD5 | raw file
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. *
  5. * @package build
  6. * @version $Id$
  7. * @copyright (c) 2010 phpBB Group
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. *
  10. */
  11. if ($_SERVER['argc'] != 3)
  12. {
  13. die("Please specify the previous and current version as arguments (e.g. build_diff.php '1.0.2' '1.0.3').");
  14. }
  15. $old_version = trim($_SERVER['argv'][1]);
  16. $new_version = trim($_SERVER['argv'][2]);
  17. $substitute_old = $old_version;
  18. $substitute_new = $new_version;
  19. $simple_name_old = 'release-' . $old_version;
  20. $simple_name_new = 'release-' . $new_version;
  21. $echo_changes = false;
  22. // DO NOT EVER USE THE FOLLOWING! Fix the script to generate proper changes,
  23. // do NOT manually create them.
  24. // Set this to true to just compress the changes and do not build them again
  25. // This should be used for building custom modified txt file. ;)
  26. $package_changed_files = false;
  27. //$debug_file = 'includes/functions_user.php'; //'styles/prosilver/style.cfg';
  28. $debug_file = false;
  29. if ($debug_file !== false)
  30. {
  31. $echo_changes = false;
  32. }
  33. $s_name = 'save_' . $substitute_old . '_to_' . $substitute_new;
  34. $location = dirname(__FILE__);
  35. if (!$package_changed_files)
  36. {
  37. if (!$echo_changes)
  38. {
  39. // Create directory...
  40. run_command("mkdir $location/save/{$s_name}");
  41. run_command("mkdir $location/save/{$s_name}/language");
  42. run_command("mkdir $location/save/{$s_name}/prosilver");
  43. run_command("mkdir $location/save/{$s_name}/subsilver2");
  44. }
  45. }
  46. // Build code changes and place them into 'save'
  47. if (!$package_changed_files)
  48. {
  49. build_code_changes('language');
  50. build_code_changes('prosilver');
  51. build_code_changes('subsilver2');
  52. }
  53. // Package code changes
  54. $code_changes_filename = 'phpBB-' . $substitute_old . '_to_' . $substitute_new . '-codechanges';
  55. if (!$echo_changes)
  56. {
  57. // Now compress the files...
  58. // Build Main phpBB Release
  59. $compress_programs = array(
  60. // 'tar.gz' => 'tar -czf',
  61. 'tar.bz2' => 'tar -cjf',
  62. 'zip' => 'zip -r'
  63. );
  64. chdir($location . '/save/' . $s_name);
  65. foreach ($compress_programs as $extension => $compress_command)
  66. {
  67. echo "Packaging code changes for $extension\n";
  68. run_command("rm ./../../new_version/release_files/{$code_changes_filename}.{$extension}");
  69. flush();
  70. // Build Package
  71. run_command("$compress_command ./../../new_version/release_files/{$code_changes_filename}.{$extension} *");
  72. // Build MD5 Sum
  73. run_command("md5sum ./../../new_version/release_files/{$code_changes_filename}.{$extension} > ./../../new_version/release_files/{$code_changes_filename}.{$extension}.md5");
  74. flush();
  75. }
  76. }
  77. /**
  78. * $output_format can be: language, prosilver and subsilver2
  79. */
  80. function build_code_changes($output_format)
  81. {
  82. global $substitute_new, $substitute_old, $simple_name_old, $simple_name_new, $echo_changes, $package_changed_files, $location, $debug_file, $s_name;
  83. // Global array holding the data entries
  84. $data = array(
  85. 'header' => array(),
  86. 'diff' => array(),
  87. );
  88. // Read diff file and prepare the output filedata...
  89. //$patch_filename = '../new_version/patches/phpBB-' . $substitute_old . '_to_' . $substitute_new . '.patch';
  90. $release_filename = 'phpbb-' . $substitute_old . '_to_' . $substitute_new . '_' . $output_format . '.txt';
  91. if (!$package_changed_files)
  92. {
  93. if (!$echo_changes)
  94. {
  95. $fp = fopen('save/' . $s_name . '/' . $output_format . '/' . $release_filename, 'wb');
  96. if (!$fp)
  97. {
  98. die('Unable to create ' . $release_filename);
  99. }
  100. }
  101. }
  102. include_once($location . '/build_helper.php');
  103. $package = new build_package(array($substitute_old, $substitute_new), false);
  104. $titles = array(
  105. 'language' => 'phpBB ' . $substitute_old . ' to phpBB ' . $substitute_new . ' Language Pack Changes',
  106. 'prosilver' => 'phpBB ' . $substitute_old . ' to phpBB ' . $substitute_new . ' prosilver Changes',
  107. 'subsilver2' => 'phpBB ' . $substitute_old . ' to phpBB ' . $substitute_new . ' subsilver2 Changes',
  108. );
  109. $data['header'] = array(
  110. 'title' => $titles[$output_format],
  111. 'intro' => '
  112. These are the ' . $titles[$output_format] . ' summed up into a little Mod. These changes are only partial and do not include any code changes, therefore not meant for updating phpBB.
  113. ',
  114. 'included_files' => array(),
  115. );
  116. // We collect the files we want to diff first (ironically we grab this from a diff file)
  117. if (!$echo_changes)
  118. {
  119. echo "\n\nCollecting Filenames:";
  120. }
  121. // We re-create the patch file
  122. foreach ($package->old_packages as $_package_name => $dest_package_filename)
  123. {
  124. chdir($package->locations['old_versions']);
  125. if (!$echo_changes)
  126. {
  127. echo "\n\n" . 'Creating patch/diff files for phpBB-' . $dest_package_filename . $package->get('new_version_number');
  128. }
  129. $dest_package_filename = $location . '/save/' . $s_name . '/phpBB-' . $dest_package_filename . $package->get('new_version_number') . '.patch';
  130. $package->run_command('diff ' . $package->diff_options . ' ' . $_package_name . ' ' . $package->get('simple_name') . ' > ' . $dest_package_filename);
  131. // Parse this diff to determine file changes from the checked versions and save them
  132. $result = $package->collect_diff_files($dest_package_filename, $_package_name);
  133. $package->run_command('rm ' . $dest_package_filename);
  134. }
  135. chdir($location);
  136. $filenames = array();
  137. foreach ($result['files'] as $filename)
  138. {
  139. if ($debug_file !== false && $filename != $debug_file)
  140. {
  141. continue;
  142. }
  143. // Decide which files to compare...
  144. switch ($output_format)
  145. {
  146. case 'language':
  147. if (strpos($filename, 'language/en/') !== 0)
  148. {
  149. continue 2;
  150. }
  151. break;
  152. case 'prosilver':
  153. if (strpos($filename, 'styles/prosilver/') !== 0)
  154. {
  155. continue 2;
  156. }
  157. break;
  158. case 'subsilver2':
  159. if (strpos($filename, 'styles/subsilver2/') !== 0)
  160. {
  161. continue 2;
  162. }
  163. break;
  164. }
  165. if (!file_exists($location . '/old_versions/' . $simple_name_old . '/' . $filename))
  166. {
  167. // New file... include it
  168. $data['header']['included_files'][] = array(
  169. 'old' => $location . '/old_versions/' . $simple_name_old . '/' . $filename,
  170. 'new' => $location . '/old_versions/' . $simple_name_new . '/' . $filename,
  171. 'phpbb_filename' => $filename,
  172. );
  173. continue;
  174. }
  175. $filenames[] = array(
  176. 'old' => $location . '/old_versions/' . $simple_name_old . '/' . $filename,
  177. 'new' => $location . '/old_versions/' . $simple_name_new . '/' . $filename,
  178. 'phpbb_filename' => $filename,
  179. );
  180. }
  181. // Now let us go through the filenames list and create a more comprehensive diff
  182. if (!$echo_changes)
  183. {
  184. fwrite($fp, build_header($output_format, $filenames, $data['header']));
  185. }
  186. else
  187. {
  188. //echo build_header('text', $filenames, $data['header']);
  189. }
  190. // Copy files...
  191. $files_to_copy = array();
  192. foreach ($data['header']['included_files'] as $filename)
  193. {
  194. $files_to_copy[] = $filename['phpbb_filename'];
  195. }
  196. // First step is to copy the new version over (clean structure)
  197. if (!$echo_changes && sizeof($files_to_copy))
  198. {
  199. foreach ($files_to_copy as $file)
  200. {
  201. // Create directory?
  202. $dirname = dirname($file);
  203. if ($dirname)
  204. {
  205. $dirname = explode('/', $dirname);
  206. $__dir = array();
  207. foreach ($dirname as $i => $dir)
  208. {
  209. $__dir[] = $dir;
  210. run_command("mkdir -p $location/save/" . $s_name . '/' . $output_format . '/' . implode('/', $__dir));
  211. }
  212. }
  213. $source_file = $location . '/new_version/phpBB3/' . $file;
  214. $dest_file = $location . '/save/' . $s_name . '/' . $output_format . '/';
  215. $dest_file .= $file;
  216. $command = "cp -p $source_file $dest_file";
  217. $result = trim(`$command`);
  218. echo "- Copied File: " . $source_file . " -> " . $dest_file . "\n";
  219. }
  220. }
  221. include_once('diff_class.php');
  222. if (!$echo_changes)
  223. {
  224. echo "\n\nDiffing Codebases:";
  225. }
  226. foreach ($filenames as $file_ary)
  227. {
  228. if (!file_exists($file_ary['old']))
  229. {
  230. $lines1 = array();
  231. }
  232. else
  233. {
  234. $lines1 = file($file_ary['old']);
  235. }
  236. $lines2 = file($file_ary['new']);
  237. if (!sizeof($lines1))
  238. {
  239. // New File
  240. }
  241. else
  242. {
  243. $diff = new Diff($lines1, $lines2);
  244. $fmt = new BBCodeDiffFormatter(false, 5, $debug_file);
  245. if (!$echo_changes)
  246. {
  247. fwrite($fp, $fmt->format_open($file_ary['phpbb_filename']));
  248. fwrite($fp, $fmt->format($diff, $lines1));
  249. fwrite($fp, $fmt->format_close($file_ary['phpbb_filename']));
  250. }
  251. else
  252. {
  253. echo $fmt->format_open($file_ary['phpbb_filename']);
  254. echo $fmt->format($diff, $lines1);
  255. echo $fmt->format_close($file_ary['phpbb_filename']);
  256. }
  257. if ($debug_file !== false)
  258. {
  259. echo $fmt->format_open($file_ary['phpbb_filename']);
  260. echo $fmt->format($diff, $lines1);
  261. echo $fmt->format_close($file_ary['phpbb_filename']);
  262. exit;
  263. }
  264. }
  265. }
  266. if (!$echo_changes)
  267. {
  268. fwrite($fp, build_footer($output_format));
  269. // Close file
  270. fclose($fp);
  271. chmod('save/' . $s_name . '/' . $output_format . '/' . $release_filename, 0666);
  272. }
  273. else
  274. {
  275. echo build_footer($output_format);
  276. }
  277. }
  278. /**
  279. * Build Footer
  280. */
  281. function build_footer($mode)
  282. {
  283. $html = '';
  284. $html .= "# \n";
  285. $html .= "#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------ \n";
  286. $html .= "# \n";
  287. $html .= "# EoM";
  288. return $html;
  289. }
  290. /**
  291. * Build Header
  292. */
  293. function build_header($mode, $filenames, $header)
  294. {
  295. global $substitute_old;
  296. $html = '';
  297. $html .= "############################################################## \n";
  298. $html .= "## Title: " . $header['title'] . "\n";
  299. $html .= "## Author: naderman < naderman@phpbb.com > (Nils Adermann) http://www.phpbb.com \n";
  300. $html .= "## Description: \n";
  301. $intr = explode("\n", $header['intro']);
  302. $introduction = '';
  303. foreach ($intr as $_line)
  304. {
  305. $introduction .= wordwrap($_line, 80) . "\n";
  306. }
  307. $intr = explode("\n", $introduction);
  308. foreach ($intr as $_line)
  309. {
  310. $html .= "## " . $_line . "\n";
  311. }
  312. $html .= "## \n";
  313. $html .= "## Files To Edit: \n";
  314. foreach ($filenames as $file_ary)
  315. {
  316. $html .= "## " . $file_ary['phpbb_filename'] . "\n";
  317. }
  318. $html .= "##\n";
  319. if (sizeof($header['included_files']))
  320. {
  321. $html .= "## Included Files: \n";
  322. foreach ($header['included_files'] as $filename)
  323. {
  324. $html .= "## {$filename['phpbb_filename']}\n";
  325. }
  326. }
  327. $html .= "## License: http://opensource.org/licenses/gpl-license.php GNU General Public License v2 \n";
  328. $html .= "############################################################## \n";
  329. $html .= "\n";
  330. // COPY Statement?
  331. if (sizeof($header['included_files']))
  332. {
  333. $html .= "#\n#-----[ COPY ]------------------------------------------\n#\n";
  334. foreach ($header['included_files'] as $filename)
  335. {
  336. $html .= "copy {$filename['phpbb_filename']} to {$filename['phpbb_filename']}\n";
  337. }
  338. $html .= "\n";
  339. }
  340. return $html;
  341. }
  342. function run_command($command)
  343. {
  344. $result = trim(`$command`);
  345. echo "\n- Command Run: " . $command . "\n";
  346. }
  347. ?>