Skip to content

Commit

Permalink
JS string generation, git control,
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Mooyman committed Aug 19, 2015
1 parent 120fd5e commit 5219cf1
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 18 deletions.
32 changes: 27 additions & 5 deletions src/Model/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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();
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/Model/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
100 changes: 93 additions & 7 deletions src/Steps/Release/UpdateTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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(
Expand All @@ -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 = <<<TMPL
// This file was generated by silverstripe/cow from %FILE%.
// See https://github.com/tractorcow/cow for details
if(typeof(ss) == 'undefined' || typeof(ss.i18n) == 'undefined') {
if(typeof(console) != 'undefined') console.error('Class ss.i18n not defined');
} else {
ss.i18n.addDictionary('%LOCALE%', %TRANSLATIONS%);
}
TMPL;
// Update each source file
foreach(glob("{$path}/src/*.js") as $sourceFile) {
$count++;
// Get contents and location
$sourceContents = file_get_contents($sourceFile);
$locale = preg_replace('/\.js$/','', basename($sourceFile));
$targetFile = dirname(dirname($sourceFile)) . '/' . $locale . '.js';

if(OutputInterface::VERBOSITY_VERY_VERBOSE <= $output->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;
}

/**
Expand All @@ -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"));
}
}

/**
Expand Down

0 comments on commit 5219cf1

Please sign in to comment.