PageRenderTime 33ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Intl/Resources/bin/update-data.php

http://github.com/symfony/symfony
PHP | 232 lines | 140 code | 77 blank | 15 comment | 10 complexity | 9b9abec3a250b94c7ac291314d69223a MD5 | raw file
  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\Filesystem\Filesystem;
  11. use Symfony\Component\Intl\Data\Bundle\Compiler\GenrbCompiler;
  12. use Symfony\Component\Intl\Data\Bundle\Writer\JsonBundleWriter;
  13. use Symfony\Component\Intl\Data\Generator\CurrencyDataGenerator;
  14. use Symfony\Component\Intl\Data\Generator\GeneratorConfig;
  15. use Symfony\Component\Intl\Data\Generator\LanguageDataGenerator;
  16. use Symfony\Component\Intl\Data\Generator\LocaleDataGenerator;
  17. use Symfony\Component\Intl\Data\Generator\RegionDataGenerator;
  18. use Symfony\Component\Intl\Data\Generator\ScriptDataGenerator;
  19. use Symfony\Component\Intl\Data\Generator\TimezoneDataGenerator;
  20. use Symfony\Component\Intl\Intl;
  21. use Symfony\Component\Intl\Locale;
  22. use Symfony\Component\Intl\Util\GitRepository;
  23. require_once __DIR__.'/common.php';
  24. require_once __DIR__.'/autoload.php';
  25. $argc = $_SERVER['argc'];
  26. $argv = $_SERVER['argv'];
  27. if ($argc > 3 || 2 === $argc && '-h' === $argv[1]) {
  28. bailout(<<<'MESSAGE'
  29. Usage: php update-data.php <path/to/icu/source> <path/to/icu/build>
  30. Updates the ICU data for Symfony to the latest version of ICU.
  31. If you downloaded the git repository before, you can pass the path to the
  32. repository source in the first optional argument.
  33. If you also built the repository before, you can pass the directory where that
  34. build is stored in the second parameter. The build directory needs to contain
  35. the subdirectories bin/ and lib/.
  36. For running this script, the intl extension must be loaded and all vendors
  37. must have been installed through composer:
  38. composer install
  39. MESSAGE
  40. );
  41. }
  42. echo LINE;
  43. echo centered('ICU Resource Bundle Compilation')."\n";
  44. echo LINE;
  45. if (!Intl::isExtensionLoaded()) {
  46. bailout('The intl extension for PHP is not installed.');
  47. }
  48. if ($argc >= 2) {
  49. $repoDir = $argv[1];
  50. $git = new GitRepository($repoDir);
  51. echo "Using the existing git repository at {$repoDir}.\n";
  52. } else {
  53. echo "Starting git clone. This may take a while...\n";
  54. $repoDir = sys_get_temp_dir().'/icu-data';
  55. $git = GitRepository::download('https://github.com/unicode-org/icu.git', $repoDir);
  56. echo "Git clone to {$repoDir} complete.\n";
  57. }
  58. $gitTag = $git->getLastTag(function ($tag) {
  59. return preg_match('#^release-[0-9]{1,}-[0-9]{1}$#', $tag);
  60. });
  61. $shortIcuVersion = strip_minor_versions(preg_replace('#release-([0-9]{1,})-([0-9]{1,})#', '$1.$2', $gitTag));
  62. echo "Checking out `{$gitTag}` for version `{$shortIcuVersion}`...\n";
  63. $git->checkout('tags/'.$gitTag);
  64. $filesystem = new Filesystem();
  65. $sourceDir = $repoDir.'/icu4c/source';
  66. if ($argc >= 3) {
  67. $buildDir = $argv[2];
  68. } else {
  69. // Always build genrb so that we can determine the ICU version of the
  70. // download by running genrb --version
  71. echo "Building genrb.\n";
  72. cd($sourceDir);
  73. echo "Running configure...\n";
  74. $buildDir = sys_get_temp_dir().'/icu-data/'.$shortIcuVersion.'/build';
  75. $filesystem->remove($buildDir);
  76. $filesystem->mkdir($buildDir);
  77. run('./configure --prefix='.$buildDir.' 2>&1');
  78. echo "Running make...\n";
  79. // If the directory "lib" does not exist in the download, create it or we
  80. // will run into problems when building libicuuc.so.
  81. $filesystem->mkdir($sourceDir.'/lib');
  82. // If the directory "bin" does not exist in the download, create it or we
  83. // will run into problems when building genrb.
  84. $filesystem->mkdir($sourceDir.'/bin');
  85. echo '[1/6] libicudata.so...';
  86. cd($sourceDir.'/stubdata');
  87. run('make 2>&1 && make install 2>&1');
  88. echo " ok.\n";
  89. echo '[2/6] libicuuc.so...';
  90. cd($sourceDir.'/common');
  91. run('make 2>&1 && make install 2>&1');
  92. echo " ok.\n";
  93. echo '[3/6] libicui18n.so...';
  94. cd($sourceDir.'/i18n');
  95. run('make 2>&1 && make install 2>&1');
  96. echo " ok.\n";
  97. echo '[4/6] libicutu.so...';
  98. cd($sourceDir.'/tools/toolutil');
  99. run('make 2>&1 && make install 2>&1');
  100. echo " ok.\n";
  101. echo '[5/6] libicuio.so...';
  102. cd($sourceDir.'/io');
  103. run('make 2>&1 && make install 2>&1');
  104. echo " ok.\n";
  105. echo '[6/6] genrb...';
  106. cd($sourceDir.'/tools/genrb');
  107. run('make 2>&1 && make install 2>&1');
  108. echo " ok.\n";
  109. }
  110. $genrb = $buildDir.'/bin/genrb';
  111. $genrbEnv = 'LD_LIBRARY_PATH='.$buildDir.'/lib ';
  112. echo "Using $genrb.\n";
  113. $icuVersionInDownload = get_icu_version_from_genrb($genrbEnv.' '.$genrb);
  114. echo "Preparing resource bundle compilation (version $icuVersionInDownload)...\n";
  115. $compiler = new GenrbCompiler($genrb, $genrbEnv);
  116. $config = new GeneratorConfig($sourceDir.'/data', $icuVersionInDownload);
  117. $jsonDir = dirname(__DIR__).'/data';
  118. $config->addBundleWriter($jsonDir, new JsonBundleWriter());
  119. echo "Starting resource bundle compilation. This may take a while...\n";
  120. // We don't want to use fallback to English during generation
  121. Locale::setDefaultFallback('root');
  122. echo "Generating language data...\n";
  123. $generator = new LanguageDataGenerator($compiler, Intl::LANGUAGE_DIR);
  124. $generator->generateData($config);
  125. echo "Generating script data...\n";
  126. $generator = new ScriptDataGenerator($compiler, Intl::SCRIPT_DIR);
  127. $generator->generateData($config);
  128. echo "Generating region data...\n";
  129. $generator = new RegionDataGenerator($compiler, Intl::REGION_DIR);
  130. $generator->generateData($config);
  131. echo "Generating currency data...\n";
  132. $generator = new CurrencyDataGenerator($compiler, Intl::CURRENCY_DIR);
  133. $generator->generateData($config);
  134. echo "Generating locale data...\n";
  135. $generator = new LocaleDataGenerator($compiler, Intl::LOCALE_DIR);
  136. $generator->generateData($config);
  137. echo "Generating timezone data...\n";
  138. $generator = new TimezoneDataGenerator($compiler, Intl::TIMEZONE_DIR);
  139. $generator->generateData($config);
  140. echo "Resource bundle compilation complete.\n";
  141. $gitInfo = <<<GIT_INFO
  142. Git information
  143. ===============
  144. URL: {$git->getUrl()}
  145. Revision: {$git->getLastCommitHash()}
  146. Author: {$git->getLastAuthor()}
  147. Date: {$git->getLastAuthoredDate()->format('c')}
  148. GIT_INFO;
  149. $gitInfoFile = $jsonDir.'/git-info.txt';
  150. file_put_contents($gitInfoFile, $gitInfo);
  151. echo "Wrote $gitInfoFile.\n";
  152. $versionFile = $jsonDir.'/version.txt';
  153. file_put_contents($versionFile, "$icuVersionInDownload\n");
  154. echo "Wrote $versionFile.\n";
  155. echo "Done.\n";