From 0f7be5a43a5c8b334f2d6118fadf46fb26e4ed0a Mon Sep 17 00:00:00 2001 From: Jeff Poirier Date: Wed, 21 Aug 2024 00:42:44 +0000 Subject: [PATCH] integrated Phing as build framework, moved translation export for SPA from artisan --- .gitignore | 1 + build.xml | 21 +++- build/lib/BuildTaskBase.php | 31 +++++ build/lib/Directories.php | 43 +++++++ build/lib/i18n.php | 108 ------------------ build/lib/i18n/LanguageCode.php | 31 +++++ build/lib/i18n/TranslationSet.php | 40 +++++++ .../tasks/pixelfed/i18n/GenerateTask.php | 24 ++++ build/phing/tasks/pixelfed/i18n/generate.php | 18 --- 9 files changed, 185 insertions(+), 132 deletions(-) create mode 100644 build/lib/BuildTaskBase.php create mode 100644 build/lib/Directories.php delete mode 100644 build/lib/i18n.php create mode 100644 build/lib/i18n/LanguageCode.php create mode 100644 build/lib/i18n/TranslationSet.php create mode 100644 build/phing/tasks/pixelfed/i18n/GenerateTask.php delete mode 100644 build/phing/tasks/pixelfed/i18n/generate.php diff --git a/.gitignore b/.gitignore index 7efd6da3d..979b30604 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ /.composer /.env /.env.dottie-backup +/.vscode #/.git /.git-credentials /.gitconfig diff --git a/build.xml b/build.xml index 7d49d435e..bc7741eec 100644 --- a/build.xml +++ b/build.xml @@ -7,18 +7,27 @@ - + - + - - - + + + - + + + + + + + + + + \ No newline at end of file diff --git a/build/lib/BuildTaskBase.php b/build/lib/BuildTaskBase.php new file mode 100644 index 000000000..082397f48 --- /dev/null +++ b/build/lib/BuildTaskBase.php @@ -0,0 +1,31 @@ +bootstrapLaravelApplication(); + } + + /** + * Couples the Laravel application lifetime with the task lifetime, + * ensuring build tasks can use the same paradigm as artisan commands + */ + private function bootstrapLaravelApplication() { + $this->app = require Application::inferBasePath() . '/bootstrap/app.php'; + $this->app->make(Kernel::class)->bootstrap(); + } +} diff --git a/build/lib/Directories.php b/build/lib/Directories.php new file mode 100644 index 000000000..12527d9a5 --- /dev/null +++ b/build/lib/Directories.php @@ -0,0 +1,43 @@ +TranslationsRoot = resource_path('lang/'); + $this->TranslationsExportSpa = resource_path('assets/js/i18n/'); + $this->TranslationsExportSpaAlt = public_path('_lang/'); + } + + protected function __clone() { } + public function __wakeup() + { throw new \Exception("Cannot unserialize a singleton."); } + + /** + * Get the unique instance of the singleton. + * + * Only available after completion of BuildTaskBase constructor + * due to dependency on Laravel application initilization. + */ + public static function get(): DIRECTORIES { + if(!isset(self::$instance)) { + self::$instance = new DIRECTORIES(); + } + + return self::$instance; + } +} diff --git a/build/lib/i18n.php b/build/lib/i18n.php deleted file mode 100644 index 339278dbe..000000000 --- a/build/lib/i18n.php +++ /dev/null @@ -1,108 +0,0 @@ -lang_code); - $json = json_encode($strings, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); - $path = "{DIRECTORIES::get().export}{$this->lang_code}.json"; - file_put_contents($path, $json); - $pathAlt = "{DIRECTORIES::get().exportAlt}{$this->lang_code}.json"; - file_put_contents($pathAlt, $json); - } - - private function __construct(string $code) { - $this->lang_code = $code; - $this->rootPath = base_path('resources/lang') . '/' . $code; - } -}; - -class LanguageCodes { - /** - * By scanning the translation resources folder, builds - * a list of all currently defined translation languages. - * - * @return array[] Language codes for all defined translations - */ - public static function GetAllDefined() { - foreach (new \DirectoryIterator($path) as $io) { - $name = $io->getFilename(); - $skip = ['vendor']; - if($io->isDot() || in_array($name, $skip)) { - continue; - } - - if($io->isDir()) { - array_push($langs, $name); - } - } - } -}; - -/** - * This singleton class efficiently stores and - * tracks directories used in various operations - * of i18n. - */ -class DIRECTORIES { - private static $instance; - - // enforce singleton pattern - protected function __construct() { - $this->export = resource_path('assets/js/i18n/'); - $this->exportAlt = public_path('_lang/'); - } - - protected function __clone() { } - public function __wakeup() - { throw new \Exception("Cannot unserialize a singleton."); } - - public static function get(): DIRECTORIES { - if(!isset(self::$instance)) { - self::$instance = new DIRECTORIES(); - } - - return self::$instance; - } - - public $export; - public $exportAlt; -} diff --git a/build/lib/i18n/LanguageCode.php b/build/lib/i18n/LanguageCode.php new file mode 100644 index 000000000..d5ff4742a --- /dev/null +++ b/build/lib/i18n/LanguageCode.php @@ -0,0 +1,31 @@ +TranslationsRoot) as $io) { + $name = $io->getFilename(); + $skip = ['vendor']; + if($io->isDot() || in_array($name, $skip)) { + continue; + } + + if($io->isDir()) { + array_push($langs, $name); + } + } + + return $langs; + } +}; \ No newline at end of file diff --git a/build/lib/i18n/TranslationSet.php b/build/lib/i18n/TranslationSet.php new file mode 100644 index 000000000..c2b3a823b --- /dev/null +++ b/build/lib/i18n/TranslationSet.php @@ -0,0 +1,40 @@ +lang_code); + $json = json_encode($strings, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + $path = "{$dirs->TranslationsExportSpa}{$this->lang_code}.json"; + file_put_contents($path, $json); + + $pathAlt = "{$dirs->TranslationsExportSpaAlt}{$this->lang_code}.json"; + file_put_contents($pathAlt, $json); + } + + private function __construct(private string $lang_code) {} +}; \ No newline at end of file diff --git a/build/phing/tasks/pixelfed/i18n/GenerateTask.php b/build/phing/tasks/pixelfed/i18n/GenerateTask.php new file mode 100644 index 000000000..bb991a21b --- /dev/null +++ b/build/phing/tasks/pixelfed/i18n/GenerateTask.php @@ -0,0 +1,24 @@ +ExportForSinglePageApp(); + } + } +}; diff --git a/build/phing/tasks/pixelfed/i18n/generate.php b/build/phing/tasks/pixelfed/i18n/generate.php deleted file mode 100644 index 7e51cc509..000000000 --- a/build/phing/tasks/pixelfed/i18n/generate.php +++ /dev/null @@ -1,18 +0,0 @@ -