Skip to content

Commit

Permalink
Add sync command (#1)
Browse files Browse the repository at this point in the history
* Add sync command

* lots of fixes

* fix use of mixed quotes

* fixed same key in sub arrays

* add indentation test case

* update readme

* update readme

* update readme

* update readme

* phpcbf
  • Loading branch information
bytestream authored Jun 2, 2021
1 parent 5fee378 commit 2b22add
Show file tree
Hide file tree
Showing 24 changed files with 496 additions and 94 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,34 @@
# Usage

```bash
$ composer require --dev supportpal/language-tools
$ composer require --dev supportpal/language-tools
```

## Compare Command

Compare your translation against the English translation files.

This will produce a diff for each file which differs from its English equivalent.

```bash
$ php vendor/bin/language-tools compare resources/lang/en/ resources/lang/es/
# Comparing resources/lang/en/ against resources/lang/es/

```

## Sync Command

> :warning: **Experimental** :warning:
>
> Check the changes match what you expect.
Synchronise your translation with the English translation files.

This will add / remove translation strings, and also update the formatting of the file
to match the English equivalent.

```bash
$ php vendor/bin/language-tools sync resources/lang/en/ resources/lang/es/
# Synchronising resources/lang/es/ with resources/lang/en/ ...

```
2 changes: 2 additions & 0 deletions language-tools
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ if ((!$loader = includeIfExists(__DIR__.'/vendor/autoload.php')) && (!$loader =
}

use SupportPal\LanguageTools\Command\CompareCommand;
use SupportPal\LanguageTools\Command\SyncCommand;
use Symfony\Component\Console\Application;

$console = new Application('SupportPal Language Tools');
$console->add(new CompareCommand);
$console->add(new SyncCommand);
$console->run();
42 changes: 42 additions & 0 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

namespace SupportPal\LanguageTools\Command;

use InvalidArgumentException;
use SupportPal\LanguageTools\Command\Output\Formatting;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use function is_string;
use function sprintf;

abstract class Command extends \Symfony\Component\Console\Command\Command
{
use Formatting;

/** @var InputInterface */
protected $input;

/** @var OutputInterface */
protected $output;

abstract public function handle(): int;

public function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;

return $this->handle();
}

protected function singleArg(string $name): string
{
$value = $this->input->getArgument($name);
if (! is_string($value)) {
throw new InvalidArgumentException(sprintf('Argument %s should be a string.', $name));
}

return $value;
}
}
32 changes: 7 additions & 25 deletions src/Command/CompareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@

namespace SupportPal\LanguageTools\Command;

use InvalidArgumentException;
use SupportPal\LanguageTools\CompareDirectory;
use SupportPal\LanguageTools\Output\Formatting;
use Symfony\Component\Console\Command\Command;
use SupportPal\LanguageTools\IO\Compare\CompareDirectory;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use function count;
use function is_string;
use function sprintf;

use const PHP_EOL;

class CompareCommand extends Command
{
use Formatting;

/** @var string */
protected static $defaultName = 'compare';

Expand All @@ -42,30 +34,20 @@ protected function configure()
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
public function handle(): int
{
$dir1 = $this->singleArg($input, 'dir1');
$dir2 = $this->singleArg($input, 'dir2');
$dir1 = $this->singleArg('dir1');
$dir2 = $this->singleArg('dir2');

$this->info($output, sprintf('# Comparing %s against %s' . PHP_EOL, $dir1, $dir2));
$this->info(sprintf('# Comparing %s against %s' . PHP_EOL, $dir1, $dir2));

$comparison = new CompareDirectory($dir1, $dir2);
$differences = $comparison->diff();
foreach ($differences as $filename => $diff) {
$this->error($output, sprintf('Found differences when comparing %s ...', $filename));
$this->error($output, $diff);
$this->error(sprintf('Found differences when comparing %s ...', $filename));
$this->error($diff);
}

return (int) (count($differences) > 0);
}

private function singleArg(InputInterface $input, string $name): string
{
$value = $input->getArgument($name);
if (! is_string($value)) {
throw new InvalidArgumentException(sprintf('Argument %s should be a string.', $name));
}

return $value;
}
}
25 changes: 25 additions & 0 deletions src/Command/Output/Formatting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace SupportPal\LanguageTools\Command\Output;

use function sprintf;

trait Formatting
{
protected function info(string $string): void
{
$this->line($string, 'info');
}

protected function error(string $string): void
{
$this->line($string, 'error');
}

protected function line(string $string, ?string $style = null): void
{
$styled = $style ? sprintf('<%s>%s</%s>', $style, $string, $style) : $string;

$this->output->writeln($styled);
}
}
47 changes: 47 additions & 0 deletions src/Command/SyncCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types=1);

namespace SupportPal\LanguageTools\Command;

use SupportPal\LanguageTools\IO\Sync\SyncDirectory;
use Symfony\Component\Console\Input\InputArgument;

use function sprintf;

use const PHP_EOL;

class SyncCommand extends Command
{
/** @var string */
protected static $defaultName = 'sync';

/**
* @return void
*/
protected function configure()
{
$this
->setName(self::$defaultName)
->setDefinition([
new InputArgument('dir1', InputArgument::REQUIRED, 'Path to directory containing the base language files.'),
new InputArgument('dir2', InputArgument::REQUIRED, 'Path to directory containing the language files you want to synchronise.'),
])
->setDescription('Synchronise language files in two directories.')
->setHelp(<<<EOF
The <info>%command.name%</info> synchronises translation files with the English version:
<info>php %command.full_name% resources/lang/en/ resources/lang/es/</info>
EOF
);
}

public function handle(): int
{
$dir1 = $this->singleArg('dir1');
$dir2 = $this->singleArg('dir2');

$this->info(sprintf('# Synchronising %s with %s ...' . PHP_EOL, $dir2, $dir1));

(new SyncDirectory($dir1, $dir2))->sync();

return 0;
}
}
27 changes: 27 additions & 0 deletions src/IO/Compare/CompareDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace SupportPal\LanguageTools\IO\Compare;

use SplFileInfo;
use SupportPal\LanguageTools\IO\Directory;

class CompareDirectory extends Directory
{
/**
* @return string[]
*/
public function diff(): array
{
$differences = [];
$this->each(function (SplFileInfo $file, SplFileInfo $otherPath) use ($differences) {
$comparison = new CompareFile($file->getPathname(), $otherPath->getPathname());
if (! $comparison->hasDifferences()) {
return null;
}

$differences[$file->getFilename()] = $comparison->diff();
});

return $differences;
}
}
24 changes: 3 additions & 21 deletions src/CompareFile.php → src/IO/Compare/CompareFile.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,25 @@
<?php declare(strict_types=1);

namespace SupportPal\LanguageTools;
namespace SupportPal\LanguageTools\IO\Compare;

use InvalidArgumentException;
use RuntimeException;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
use SupportPal\LanguageTools\IO\File;

use function file_exists;
use function file_get_contents;
use function is_string;
use function preg_replace;
use function sprintf;
use function strlen;
use function trim;

class CompareFile
class CompareFile extends File
{
/** @var string */
private $file1;

/** @var string */
private $file2;

/** @var array<mixed> */
private $diff;

public function __construct(string $file1, string $file2)
{
foreach ([$file1, $file2] as $file) {
if (! file_exists($file)) {
throw new InvalidArgumentException(sprintf('%s does not exist.', $file));
}
}

$this->file1 = $file1;
$this->file2 = $file2;
}

/**
* @return mixed[]
*/
Expand Down
26 changes: 8 additions & 18 deletions src/CompareDirectory.php → src/IO/Directory.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php declare(strict_types=1);

namespace SupportPal\LanguageTools;
namespace SupportPal\LanguageTools\IO;

use Closure;
use InvalidArgumentException;
use SplFileInfo;
use Symfony\Component\Finder\Finder;

use function file_exists;
Expand All @@ -11,13 +13,13 @@

use const DIRECTORY_SEPARATOR;

class CompareDirectory
class Directory
{
/** @var string */
private $dir1;
protected $dir1;

/** @var string */
private $dir2;
protected $dir2;

public function __construct(string $dir1, string $dir2)
{
Expand All @@ -31,24 +33,12 @@ public function __construct(string $dir1, string $dir2)
$this->dir2 = $dir2;
}

/**
* @return string[]
*/
public function diff(): array
public function each(Closure $callback): void
{
$differences = [];
foreach (Finder::create()->files()->name('*.php')->in($this->dir1) as $file) {
$path = $file->getPathname();
$otherPath = realpath($this->dir2) . DIRECTORY_SEPARATOR . $file->getFilename();

$comparison = new CompareFile($path, $otherPath);
if (! $comparison->hasDifferences()) {
continue;
}

$differences[$file->getFilename()] = $comparison->diff();
$callback->call($this, $file, new SplFileInfo($otherPath));
}

return $differences;
}
}
29 changes: 29 additions & 0 deletions src/IO/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace SupportPal\LanguageTools\IO;

use InvalidArgumentException;

use function file_exists;
use function sprintf;

class File
{
/** @var string */
protected $file1;

/** @var string */
protected $file2;

public function __construct(string $file1, string $file2)
{
foreach ([$file1, $file2] as $file) {
if (! file_exists($file)) {
throw new InvalidArgumentException(sprintf('%s does not exist.', $file));
}
}

$this->file1 = $file1;
$this->file2 = $file2;
}
}
Loading

0 comments on commit 2b22add

Please sign in to comment.