RoboFile.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * ---------------------------------------------------------------------
  4. * SingleSignOn is a plugin which allows to use SSO for auth
  5. * ---------------------------------------------------------------------
  6. * Copyright (C) 2022 Edgard
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. * ---------------------------------------------------------------------
  21. * @copyright Copyright © 2021 - 2022 Edgard
  22. * @license http://www.gnu.org/licenses/gpl.txt GPLv3+
  23. * @link https://github.com/edgardmessias/glpi-singlesignon/
  24. * ---------------------------------------------------------------------
  25. */
  26. use Symfony\Component\Finder\Finder;
  27. /**
  28. * This is project's console commands configuration for Robo task runner.
  29. *
  30. * @see http://robo.li/
  31. */
  32. class RoboFile extends \Robo\Tasks {
  33. protected $name = "singlesignon";
  34. protected $issues = "https://github.com/edgardmessias/glpi-singlesignon/issues";
  35. protected function getLocaleFiles() {
  36. $finder = new Finder();
  37. $finder
  38. ->files()
  39. ->name('*.po')
  40. ->in('locales');
  41. $files = [];
  42. foreach ($finder as $file) {
  43. $files[] = str_replace('\\', '/', $file->getRelativePathname());
  44. }
  45. return $files;
  46. }
  47. public function compile_locales() {
  48. $files = $this->getLocaleFiles();
  49. foreach ($files as $file) {
  50. $lang = basename($file, ".po");
  51. $this->taskExec('msgfmt')->args([
  52. "locales/$lang.po",
  53. "-o",
  54. "locales/$lang.mo",
  55. ])->run();
  56. }
  57. }
  58. public function update_locales() {
  59. $finder = new Finder();
  60. $finder
  61. ->files()
  62. ->name('*.php')
  63. ->in(__DIR__)
  64. ->exclude([
  65. 'vendor'
  66. ])
  67. ->sortByName();
  68. if (!$finder->hasResults()) {
  69. return false;
  70. }
  71. $args = [];
  72. foreach ($finder as $file) {
  73. $args[] = str_replace('\\', '/', $file->getRelativePathname());
  74. }
  75. $args[] = '-D';
  76. $args[] = '.';
  77. $args[] = '-o';
  78. $args[] = "locales/{$this->name}.pot";
  79. $args[] = '-L';
  80. $args[] = 'PHP';
  81. $args[] = '--add-comments=TRANS';
  82. $args[] = '--from-code=UTF-8';
  83. $args[] = '--force-po';
  84. $args[] = '--keyword=__sso';
  85. $args[] = "--package-name={$this->name}";
  86. if ($this->issues) {
  87. $args[] = "--msgid-bugs-address={$this->issues}";
  88. }
  89. try {
  90. $content = file_get_contents('setup.php');
  91. $name = 'PLUGIN_' . strtoupper($this->name) . '_VERSION';
  92. preg_match("/'$name',\s*'([\w\.]+)'/", $content, $matches);
  93. $args[] = '--package-version=' . $matches[1];
  94. } catch (\Exception $ex) {
  95. echo $ex->getMessage();
  96. }
  97. putenv("LANG=C");
  98. $this->taskExec('xgettext')->args($args)->run();
  99. $this->taskReplaceInFile("locales/{$this->name}.pot")
  100. ->from('CHARSET')
  101. ->to('UTF-8')
  102. ->run();
  103. $this->taskExec('msginit')->args([
  104. '--no-translator',
  105. '-i',
  106. "locales/{$this->name}.pot",
  107. '-l',
  108. 'en_GB.UTF8',
  109. '-o',
  110. 'locales/en_GB.po',
  111. ])->run();
  112. $files = $this->getLocaleFiles();
  113. foreach ($files as $file) {
  114. $lang = basename($file, ".po");
  115. if ($lang === "en_GB") {
  116. continue;
  117. }
  118. $this->taskExec('msgmerge')->args([
  119. "--update",
  120. "locales/$lang.po",
  121. "locales/{$this->name}.pot",
  122. "--lang=$lang",
  123. "--backup=off",
  124. ])->run();
  125. }
  126. $this->compile_locales();
  127. }
  128. public function build() {
  129. $this->_remove(["$this->name.zip", "$this->name.tgz"]);
  130. $this->compile_locales();
  131. $tmpPath = $this->_tmpDir();
  132. $exclude = glob(__DIR__ . '/.*');
  133. $exclude[] = 'plugin.xml';
  134. $exclude[] = 'RoboFile.php';
  135. $exclude[] = 'screenshots';
  136. $exclude[] = 'tools';
  137. $exclude[] = 'vendor';
  138. $exclude[] = "$this->name.zip";
  139. $exclude[] = "$this->name.tgz";
  140. $this->taskCopyDir([__DIR__ => $tmpPath])
  141. ->exclude($exclude)
  142. ->run();
  143. $composer_file = "$tmpPath/composer.json";
  144. if (file_exists($composer_file)) {
  145. $hasDep = false;
  146. try {
  147. $data = json_decode(file_get_contents($composer_file), true);
  148. $hasDep = isset($data['require']) && count($data['require']) > 0;
  149. } catch (\Exception $ex) {
  150. $hasDep = true;
  151. }
  152. if ($hasDep) {
  153. $this->taskComposerInstall()
  154. ->workingDir($tmpPath)
  155. ->noDev()
  156. ->run();
  157. }
  158. }
  159. $this->_remove("$tmpPath/composer.lock");
  160. // Pack
  161. $this->taskPack("$this->name.zip")
  162. ->addDir($this->name, $tmpPath)
  163. ->run();
  164. $this->taskPack("$this->name.tgz")
  165. ->addDir($this->name, $tmpPath)
  166. ->run();
  167. }
  168. }