RoboFile.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. use Symfony\Component\Finder\Finder;
  3. /**
  4. * This is project's console commands configuration for Robo task runner.
  5. *
  6. * @see http://robo.li/
  7. */
  8. class RoboFile extends \Robo\Tasks {
  9. protected $name = "singlesignon";
  10. protected $issues = "https://github.com/edgardmessias/glpi-singlesignon/issues";
  11. protected function getLocaleFiles() {
  12. $finder = new Finder();
  13. $finder
  14. ->files()
  15. ->name('*.po')
  16. ->in('locales');
  17. $files = [];
  18. foreach ($finder as $file) {
  19. $files[] = str_replace('\\', '/', $file->getRelativePathname());
  20. }
  21. return $files;
  22. }
  23. public function compile_locales() {
  24. $files = $this->getLocaleFiles();
  25. foreach ($files as $file) {
  26. $lang = basename($file, ".po");
  27. $this->taskExec('msgfmt')->args([
  28. "locales/$lang.po",
  29. "-o",
  30. "locales/$lang.mo",
  31. ])->run();
  32. }
  33. }
  34. public function update_locales() {
  35. $finder = new Finder();
  36. $finder
  37. ->files()
  38. ->name('*.php')
  39. ->in(__DIR__)
  40. ->exclude([
  41. 'vendor'
  42. ])
  43. ->sortByName();
  44. if (!$finder->hasResults()) {
  45. return false;
  46. }
  47. $args = [];
  48. foreach ($finder as $file) {
  49. $args[] = str_replace('\\', '/', $file->getRelativePathname());
  50. }
  51. $args[] = '-D';
  52. $args[] = '.';
  53. $args[] = '-o';
  54. $args[] = "locales/{$this->name}.pot";
  55. $args[] = '-L';
  56. $args[] = 'PHP';
  57. $args[] = '--add-comments=TRANS';
  58. $args[] = '--from-code=UTF-8';
  59. $args[] = '--force-po';
  60. $args[] = '--keyword=__sso';
  61. $args[] = "--package-name={$this->name}";
  62. if ($this->issues) {
  63. $args[] = "--msgid-bugs-address={$this->issues}";
  64. }
  65. try {
  66. $content = file_get_contents('setup.php');
  67. $name = 'PLUGIN_' . strtoupper($this->name) . '_VERSION';
  68. preg_match("/'$name',\s*'([\w\.]+)'/", $content, $matches);
  69. $args[] = '--package-version=' . $matches[1];
  70. } catch (\Exception $ex) {
  71. echo $ex->getMessage();
  72. }
  73. putenv("LANG=C");
  74. $this->taskExec('xgettext')->args($args)->run();
  75. $this->taskReplaceInFile("locales/{$this->name}.pot")
  76. ->from('CHARSET')
  77. ->to('UTF-8')
  78. ->run();
  79. $this->taskExec('msginit')->args([
  80. '--no-translator',
  81. '-i',
  82. "locales/{$this->name}.pot",
  83. '-l',
  84. 'en_GB.UTF8',
  85. '-o',
  86. 'locales/en_GB.po',
  87. ])->run();
  88. $files = $this->getLocaleFiles();
  89. foreach ($files as $file) {
  90. $lang = basename($file, ".po");
  91. if ($lang === "en_GB") {
  92. continue;
  93. }
  94. $this->taskExec('msgmerge')->args([
  95. "--update",
  96. "locales/$lang.po",
  97. "locales/{$this->name}.pot",
  98. "--lang=$lang",
  99. "--backup=off",
  100. ])->run();
  101. }
  102. $this->compile_locales();
  103. }
  104. public function build() {
  105. $this->_remove(["$this->name.zip", "$this->name.tgz"]);
  106. $this->compile_locales();
  107. $tmpPath = $this->_tmpDir();
  108. $exclude = glob(__DIR__ . '/.*');
  109. $exclude[] = 'plugin.xml';
  110. $exclude[] = 'screenshots';
  111. $exclude[] = 'tools';
  112. $exclude[] = 'vendor';
  113. $exclude[] = "$this->name.zip";
  114. $exclude[] = "$this->name.tgz";
  115. $this->taskCopyDir([__DIR__ => $tmpPath])
  116. ->exclude($exclude)
  117. ->run();
  118. $composer_file = "$tmpPath/composer.json";
  119. if (file_exists($composer_file)) {
  120. $hasDep = false;
  121. try {
  122. $data = json_decode(file_get_contents($composer_file), true);
  123. $hasDep = isset($data['require']) && count($data['require']) > 0;
  124. } catch (\Exception $ex) {
  125. $hasDep = true;
  126. }
  127. if ($hasDep) {
  128. $this->taskComposerInstall()
  129. ->workingDir($tmpPath)
  130. ->noDev()
  131. ->run();
  132. }
  133. }
  134. $this->_remove("$tmpPath/composer.lock");
  135. // Pack
  136. $this->taskPack("$this->name.zip")
  137. ->addDir($this->name, $tmpPath)
  138. ->run();
  139. $this->taskPack("$this->name.tgz")
  140. ->addDir($this->name, $tmpPath)
  141. ->run();
  142. }
  143. }