Skip to content

Commit

Permalink
Merge pull request #63 from bedita/v3/ci-mode
Browse files Browse the repository at this point in the history
[v3] Add ci flag for update command
  • Loading branch information
didoda authored Jun 16, 2022
2 parents 166801f + f509623 commit b4cc0e2
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 14 deletions.
59 changes: 47 additions & 12 deletions src/Shell/GettextShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
*/
class GettextShell extends Shell
{
/**
* @var int
*/
public const CODE_SUCCESS = 0;

/**
* @var int
*/
public const CODE_CHANGES = 2;

/**
* Get the option parser for this shell.
*
Expand Down Expand Up @@ -61,6 +71,11 @@ public function getOptionParser(): ConsoleOptionParser
'short' => 'p',
'required' => false,
],
'ci' => [
'help' => 'Run in CI mode. Exit with error if PO files are changed.',
'required' => false,
'boolean' => true,
],
],
],
]);
Expand Down Expand Up @@ -123,16 +138,14 @@ public function getLocalePath(): string
/**
* Update gettext po files
*
* @return void
* @return int
*/
public function update(): void
public function update(): int
{
$resCmd = [];
exec('which msgmerge 2>&1', $resCmd);
if (empty($resCmd[0])) {
$this->out('ERROR: msgmerge not available. Please install gettext utilities.');

return;
$this->abort('ERROR: msgmerge not available. Please install gettext utilities.');
}

$this->out('Updating .pot and .po files...');
Expand All @@ -144,7 +157,7 @@ public function update(): void
}

$this->out('Creating master .pot file');
$this->writeMasterPot();
$hasChanges = $this->writeMasterPot();
$this->ttagExtract();

$this->hr();
Expand All @@ -154,6 +167,12 @@ public function update(): void
$this->writePoFiles();

$this->out('Done');

if ($this->param('ci') && $hasChanges) {
return GettextShell::CODE_CHANGES;
}

return GettextShell::CODE_SUCCESS;
}

/**
Expand Down Expand Up @@ -196,30 +215,46 @@ private function setupPaths(): void
/**
* Write `master.pot` file
*
* @return void
* @return bool True if file was updated, false otherwise
*/
private function writeMasterPot(): void
private function writeMasterPot(): bool
{
$updated = false;

foreach ($this->poResult as $domain => $poResult) {
$potFilename = sprintf('%s/%s.pot', $this->localePath, $domain);
$this->out(sprintf('Writing new .pot file: %s', $potFilename));
$pot = new File($potFilename, true);
$pot->write($this->header('pot'));
ksort($poResult);

$contents = $pot->read();

// remove headers from pot file
$contents = preg_replace('/^msgid ""\nmsgstr ""/', '', $contents);
$contents = trim(preg_replace('/^"([^"]*?)"$/m', '', $contents));

$lines = [];
ksort($poResult);
foreach ($poResult as $res => $contexts) {
sort($contexts);
foreach ($contexts as $ctx) {
if (!empty($ctx)) {
$pot->write(sprintf('%smsgctxt "%s"%smsgid "%s"%smsgstr ""%s', "\n", $ctx, "\n", $res, "\n", "\n"));
$lines[] = sprintf('msgctxt "%s"%smsgid "%s"%smsgstr ""', $ctx, "\n", $res, "\n");
} else {
$pot->write(sprintf('%smsgid "%s"%smsgstr ""%s', "\n", $res, "\n", "\n"));
$lines[] = sprintf('msgid "%s"%smsgstr ""', $res, "\n");
}
}
}

$result = implode("\n\n", $lines);
if ($contents !== $result) {
$pot->write(sprintf("%s\n%s\n", $this->header('pot'), $result));
$updated = true;
}

$pot->close();
}

return $updated;
}

/**
Expand Down
48 changes: 46 additions & 2 deletions tests/TestCase/Shell/GettextShellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function testUpdate(): void
$reflection->setValue($this->shell, $localePath);

// call the method
$this->shell->update();
static::assertSame(GettextShell::CODE_SUCCESS, $this->shell->update());

// check po files are not empty
foreach (['en_US', 'it_IT'] as $locale) {
Expand All @@ -91,6 +91,39 @@ public function testUpdate(): void
}
}

/**
* Test execute method with ci flag
*
* @return void
* @covers ::update()
* @covers ::getPoResult()
* @covers ::getTemplatePaths()
* @covers ::getLocalePath()
*/
public function testUpdateWithCi(): void
{
$this->shell->params['app'] = sprintf('%s/tests/test_app/TestApp', getcwd());
$this->shell->params['ci'] = true;

// set localePath using reflection class
$localePath = sprintf('%s/tests/test_app/TestApp/Locale', getcwd());
$reflection = new \ReflectionProperty(get_class($this->shell), 'localePath');
$reflection->setAccessible(true);
$reflection->setValue($this->shell, $localePath);

// call the method
static::assertSame(GettextShell::CODE_CHANGES, $this->shell->update());

// check po files are not empty
foreach (['en_US', 'it_IT'] as $locale) {
$content = file_get_contents(sprintf('%s/%s/default.po', $localePath, $locale));
static::assertNotEmpty($content);
}

// call method again
static::assertSame(GettextShell::CODE_SUCCESS, $this->shell->update());
}

/**
* Provider for 'setupPaths'
*
Expand Down Expand Up @@ -261,12 +294,23 @@ public function testWriteMasterPot($expected, $values): void
$class = new \ReflectionClass('BEdita\I18n\Shell\GettextShell');
$method = $class->getMethod('writeMasterPot');
$method->setAccessible(true);
$method->invokeArgs($this->shell, []);
$result = $method->invokeArgs($this->shell, []);

// file default.pot have been override, check again content (it should be unchanged), except for POT-Creation-Date
$content = file_get_contents(sprintf('%s/default.pot', $localePath));

static::assertSame($expected, $content);
static::assertTrue($result);

$time = new FrozenTime('2022-01-02 00:00:00');

$result = $method->invokeArgs($this->shell, []);

// file default.pot have been override, check again content (it should be unchanged), except for POT-Creation-Date
$content = file_get_contents(sprintf('%s/default.pot', $localePath));

static::assertSame($expected, $content);
static::assertFalse($result);
}

/**
Expand Down

0 comments on commit b4cc0e2

Please sign in to comment.