diff --git a/src/Model/Module.php b/src/Model/Module.php index 1de31ec..1f2f6a4 100644 --- a/src/Model/Module.php +++ b/src/Model/Module.php @@ -44,7 +44,7 @@ public function __construct($directory, $name, Project $parent = null) { } /** - * Get the directory this module is saved in + * Get the base directory this module is saved in * * @return string */ @@ -58,16 +58,38 @@ public function getDirectory() { * @return string */ public function getLangDirectory() { - return $this->directory . '/lang'; + return $this->getMainDirectory() . '/lang'; } /** - * Base name only of location of code + * Gets the directory(s) of the JS lang folder. + * + * Can be a string or an array result + * + * @return string|array + */ + public function getJSLangDirectory() { + $dir = $this->getMainDirectory() . '/javascript/lang'; + + // Special case for framework which has a nested 'admin' submodule + if($this->getName() === 'framework') { + return array( + $this->getMainDirectory() . '/admin/javascript/lang', + $dir + ); + } else { + return $dir; + } + + } + + /** + * Directory where module files exist; Usually the one that sits just below the top level project * * @return string */ - public function getCodeDirectory() { - return $this->getName(); + public function getMainDirectory() { + return $this->getDirectory(); } /** diff --git a/src/Model/Project.php b/src/Model/Project.php index bf38898..7dccbb1 100644 --- a/src/Model/Project.php +++ b/src/Model/Project.php @@ -68,11 +68,8 @@ protected function isModulePath($path) { return !in_array($name, $ignore); } - public function getLangDirectory() { - return $this->directory . '/mysite/lang'; - } - - public function getCodeDirectory() { - return 'mysite'; + public function getMainDirectory() { + // Look in mysite for main content + return $this->getDirectory() . '/mysite'; } } diff --git a/src/Steps/Release/UpdateTranslations.php b/src/Steps/Release/UpdateTranslations.php index 2959878..ba42ec1 100644 --- a/src/Steps/Release/UpdateTranslations.php +++ b/src/Steps/Release/UpdateTranslations.php @@ -24,6 +24,7 @@ * `phing -Dmodule=my-module translation-generate-javascript-for-module` * - Push up all source translations * `tx push -s` + * - Commit changes to source control (without push) */ class UpdateTranslations extends Step { @@ -153,7 +154,7 @@ protected function collectStrings(OutputInterface $output, $modules) { // Get code dirs for each module $dirs = array(); foreach($modules as $module) { - $dirs[] = $module->getCodeDirectory(); + $dirs[] = basename($module->getMainDirectory()); } $sakeCommand = sprintf( @@ -168,10 +169,73 @@ protected function collectStrings(OutputInterface $output, $modules) { * Generate javascript for all modules * * @param OutputInterface $output - * @param type $modules + * @param Module[] $modules + */ + protected function generateJavascript(OutputInterface $output, $modules) { + $this->log($output, "Generating javascript locale files"); + // Check which paths in each module require processing + $count = 0; + foreach($modules as $module) { + $base = $module->getMainDirectory(); + $jsPath = $module->getJSLangDirectory(); + foreach((array)$jsPath as $path) { + $count += $this->generateJavascriptInDirectory($output, $base, $path); + } + } + $this->log($output, "Finished generating {$count} files"); + } + + + /** + * Process all javascript in a given path + * + * @param OutputInterface $output + * @param string $base Base directory of the module + * @param string $path Path to the location of JS files + * @return int Number of files generated */ - public function generateJavascript(OutputInterface $output, $modules) { - // @todo + protected function generateJavascriptInDirectory(OutputInterface $output, $base, $path) { + // Iterate through each source file + $count = 0; + $template = <<getVerbosity()) { + $this->log($output, "Generating file {$targetFile}", "info"); + } + + file_put_contents( + $targetFile, + str_replace( + array( + '%TRANSLATIONS%', + '%FILE%', + '%LOCALE%' + ), + array( + $sourceContents, + substr($sourceFile, strlen($base) + 1), // Trim off base dir + $locale + ), + $template + ) + ); + } + return $count; } /** @@ -181,17 +245,39 @@ public function generateJavascript(OutputInterface $output, $modules) { * @param type $modules */ public function pushSource(OutputInterface $output, $modules) { - // @todo + $this->log($output, "Pushing updated sources to transifex"); + + foreach($modules as $module) { + // Run tx pull + $pushCommand = sprintf( + '(cd %s && tx push -s)', + $module->getDirectory() + ); + $this->runCommand($output, $pushCommand); + } } /** * Commit changes for all modules * * @param OutputInterface $output - * @param type $modules + * @param Module[] $modules */ public function commitChanges(OutputInterface $output, $modules) { - // @todo + $this->log($output, 'Committing translations to git'); + + foreach($modules as $module) { + $repo = $module->getRepository(); + + // Add all changes + $jsPath = $module->getJSLangDirectory(); + $langPath = $module->getLangDirectory(); + foreach(array_merge((array)$jsPath, (array)$langPath) as $path) { + $repo->run("add", array($path . "/*")); + } + + $repo->run("commit", array("-m", "Update translations")); + } } /**