update-data.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use Symfony\Component\Intl\Data\Bundle\Reader\JsonBundleReader;
  11. use Symfony\Component\Intl\Data\Bundle\Writer\JsonBundleWriter;
  12. use Symfony\Component\Intl\Data\Generator\CurrencyDataGenerator;
  13. use Symfony\Component\Intl\Data\Generator\GeneratorConfig;
  14. use Symfony\Component\Intl\Data\Generator\LanguageDataGenerator;
  15. use Symfony\Component\Intl\Data\Generator\LocaleDataGenerator;
  16. use Symfony\Component\Intl\Data\Generator\RegionDataGenerator;
  17. use Symfony\Component\Intl\Data\Generator\ScriptDataGenerator;
  18. use Symfony\Component\Intl\Data\Provider\LanguageDataProvider;
  19. use Symfony\Component\Intl\Data\Provider\RegionDataProvider;
  20. use Symfony\Component\Intl\Data\Provider\ScriptDataProvider;
  21. use Symfony\Component\Intl\Intl;
  22. use Symfony\Component\Intl\Data\Bundle\Compiler\GenrbCompiler;
  23. use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReader;
  24. use Symfony\Component\Intl\Locale;
  25. use Symfony\Component\Intl\Util\IcuVersion;
  26. use Symfony\Component\Intl\Util\SvnRepository;
  27. use Symfony\Component\Filesystem\Filesystem;
  28. require_once __DIR__.'/common.php';
  29. require_once __DIR__.'/autoload.php';
  30. $argc = $_SERVER['argc'];
  31. $argv = $_SERVER['argv'];
  32. if ($argc > 3 || 2 === $argc && '-h' === $argv[1]) {
  33. bailout(<<<'MESSAGE'
  34. Usage: php update-data.php <path/to/icu/source> <path/to/icu/build>
  35. Updates the ICU data for Symfony to the latest version of ICU.
  36. If you downloaded the SVN repository before, you can pass the path to the
  37. repository source in the first optional argument.
  38. If you also built the repository before, you can pass the directory where that
  39. build is stored in the second parameter. The build directory needs to contain
  40. the subdirectories bin/ and lib/.
  41. For running this script, the intl extension must be loaded and all vendors
  42. must have been installed through composer:
  43. composer install
  44. MESSAGE
  45. );
  46. }
  47. echo LINE;
  48. echo centered('ICU Resource Bundle Compilation')."\n";
  49. echo LINE;
  50. if (!Intl::isExtensionLoaded()) {
  51. bailout('The intl extension for PHP is not installed.');
  52. }
  53. $filesystem = new Filesystem();
  54. $urls = parse_ini_file(__DIR__.'/icu.ini');
  55. echo "icu.ini parsed. Available versions:\n";
  56. $maxVersion = 0;
  57. foreach ($urls as $urlVersion => $url) {
  58. $maxVersion = IcuVersion::compare($maxVersion, $urlVersion, '<')
  59. ? $urlVersion
  60. : $maxVersion;
  61. echo " $urlVersion\n";
  62. }
  63. $shortIcuVersion = strip_minor_versions($maxVersion);
  64. if ($argc >= 2) {
  65. $sourceDir = $argv[1];
  66. $svn = new SvnRepository($sourceDir);
  67. echo "Using existing SVN repository at {$sourceDir}.\n";
  68. } else {
  69. echo "Starting SVN checkout for version $shortIcuVersion. This may take a while...\n";
  70. $sourceDir = sys_get_temp_dir().'/icu-data/'.$shortIcuVersion.'/source';
  71. $svn = SvnRepository::download($urls[$shortIcuVersion], $sourceDir);
  72. echo "SVN checkout to {$sourceDir} complete.\n";
  73. }
  74. if ($argc >= 3) {
  75. $buildDir = $argv[2];
  76. } else {
  77. // Always build genrb so that we can determine the ICU version of the
  78. // download by running genrb --version
  79. echo "Building genrb.\n";
  80. cd($sourceDir);
  81. echo "Running configure...\n";
  82. $buildDir = sys_get_temp_dir().'/icu-data/'.$shortIcuVersion.'/build';
  83. $filesystem->remove($buildDir);
  84. $filesystem->mkdir($buildDir);
  85. run('./configure --prefix='.$buildDir.' 2>&1');
  86. echo "Running make...\n";
  87. // If the directory "lib" does not exist in the download, create it or we
  88. // will run into problems when building libicuuc.so.
  89. $filesystem->mkdir($sourceDir.'/lib');
  90. // If the directory "bin" does not exist in the download, create it or we
  91. // will run into problems when building genrb.
  92. $filesystem->mkdir($sourceDir.'/bin');
  93. echo '[1/6] libicudata.so...';
  94. cd($sourceDir.'/stubdata');
  95. run('make 2>&1 && make install 2>&1');
  96. echo " ok.\n";
  97. echo '[2/6] libicuuc.so...';
  98. cd($sourceDir.'/common');
  99. run('make 2>&1 && make install 2>&1');
  100. echo " ok.\n";
  101. echo '[3/6] libicui18n.so...';
  102. cd($sourceDir.'/i18n');
  103. run('make 2>&1 && make install 2>&1');
  104. echo " ok.\n";
  105. echo '[4/6] libicutu.so...';
  106. cd($sourceDir.'/tools/toolutil');
  107. run('make 2>&1 && make install 2>&1');
  108. echo " ok.\n";
  109. echo '[5/6] libicuio.so...';
  110. cd($sourceDir.'/io');
  111. run('make 2>&1 && make install 2>&1');
  112. echo " ok.\n";
  113. echo '[6/6] genrb...';
  114. cd($sourceDir.'/tools/genrb');
  115. run('make 2>&1 && make install 2>&1');
  116. echo " ok.\n";
  117. }
  118. $genrb = $buildDir.'/bin/genrb';
  119. $genrbEnv = 'LD_LIBRARY_PATH='.$buildDir.'/lib ';
  120. echo "Using $genrb.\n";
  121. $icuVersionInDownload = get_icu_version_from_genrb($genrbEnv.' '.$genrb);
  122. echo "Preparing resource bundle compilation (version $icuVersionInDownload)...\n";
  123. $compiler = new GenrbCompiler($genrb, $genrbEnv);
  124. $config = new GeneratorConfig($sourceDir.'/data', $icuVersionInDownload);
  125. $baseDir = dirname(__DIR__).'/data';
  126. //$txtDir = $baseDir.'/txt';
  127. $jsonDir = $baseDir;
  128. //$phpDir = $baseDir.'/'.Intl::PHP;
  129. //$resDir = $baseDir.'/'.Intl::RB_V2;
  130. $targetDirs = array($jsonDir/*, $resDir*/);
  131. $workingDirs = array($jsonDir/*, $txtDir, $resDir*/);
  132. //$config->addBundleWriter($txtDir, new TextBundleWriter());
  133. $config->addBundleWriter($jsonDir, new JsonBundleWriter());
  134. echo "Starting resource bundle compilation. This may take a while...\n";
  135. $filesystem->remove($workingDirs);
  136. foreach ($workingDirs as $targetDir) {
  137. $filesystem->mkdir(array(
  138. $targetDir.'/'.Intl::CURRENCY_DIR,
  139. $targetDir.'/'.Intl::LANGUAGE_DIR,
  140. $targetDir.'/'.Intl::LOCALE_DIR,
  141. $targetDir.'/'.Intl::REGION_DIR,
  142. $targetDir.'/'.Intl::SCRIPT_DIR,
  143. ));
  144. }
  145. // We don't want to use fallback to English during generation
  146. Locale::setDefaultFallback(null);
  147. echo "Generating language data...\n";
  148. $generator = new LanguageDataGenerator($compiler, Intl::LANGUAGE_DIR);
  149. $generator->generateData($config);
  150. //echo "Compiling...\n";
  151. //
  152. //$compiler->compile($txtDir.'/'.Intl::LANGUAGE_DIR, $resDir.'/'.Intl::LANGUAGE_DIR);
  153. echo "Generating script data...\n";
  154. $generator = new ScriptDataGenerator($compiler, Intl::SCRIPT_DIR);
  155. $generator->generateData($config);
  156. //echo "Compiling...\n";
  157. //
  158. //$compiler->compile($txtDir.'/'.Intl::SCRIPT_DIR, $resDir.'/'.Intl::SCRIPT_DIR);
  159. echo "Generating region data...\n";
  160. $generator = new RegionDataGenerator($compiler, Intl::REGION_DIR);
  161. $generator->generateData($config);
  162. //echo "Compiling...\n";
  163. //
  164. //$compiler->compile($txtDir.'/'.Intl::REGION_DIR, $resDir.'/'.Intl::REGION_DIR);
  165. echo "Generating currency data...\n";
  166. $generator = new CurrencyDataGenerator($compiler, Intl::CURRENCY_DIR);
  167. $generator->generateData($config);
  168. //echo "Compiling...\n";
  169. //
  170. //$compiler->compile($txtDir.'/'.Intl::CURRENCY_DIR, $resDir.'/'.Intl::CURRENCY_DIR);
  171. echo "Generating locale data...\n";
  172. $reader = new BundleEntryReader(new JsonBundleReader());
  173. $generator = new LocaleDataGenerator(
  174. Intl::LOCALE_DIR,
  175. new LanguageDataProvider($jsonDir.'/'.Intl::LANGUAGE_DIR, $reader),
  176. new ScriptDataProvider($jsonDir.'/'.Intl::SCRIPT_DIR, $reader),
  177. new RegionDataProvider($jsonDir.'/'.Intl::REGION_DIR, $reader)
  178. );
  179. $generator->generateData($config);
  180. //echo "Compiling...\n";
  181. //
  182. //$compiler->compile($txtDir.'/'.Intl::LOCALE_DIR, $resDir.'/'.Intl::LOCALE_DIR);
  183. //
  184. //$filesystem->remove($txtDir);
  185. echo "Resource bundle compilation complete.\n";
  186. $svnInfo = <<<SVN_INFO
  187. SVN information
  188. ===============
  189. URL: {$svn->getUrl()}
  190. Revision: {$svn->getLastCommit()->getRevision()}
  191. Author: {$svn->getLastCommit()->getAuthor()}
  192. Date: {$svn->getLastCommit()->getDate()}
  193. SVN_INFO;
  194. foreach ($targetDirs as $targetDir) {
  195. $svnInfoFile = $targetDir.'/svn-info.txt';
  196. file_put_contents($svnInfoFile, $svnInfo);
  197. echo "Wrote $svnInfoFile.\n";
  198. $versionFile = $targetDir.'/version.txt';
  199. file_put_contents($versionFile, "$icuVersionInDownload\n");
  200. echo "Wrote $versionFile.\n";
  201. }
  202. echo "Done.\n";