Merge pull request #22 from opdavies/minified-xml-21

Minify the generated XML by default
This commit is contained in:
Oliver Davies 2019-04-19 01:34:19 +01:00 committed by GitHub
commit d6f63dcc57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 19 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@
composer.lock
filters.php
*.xml
!/tests/fixtures/*/*.xml

5
CHANGELOG.md Normal file
View file

@ -0,0 +1,5 @@
# CHANGELOG
## 2.x
* [#21](https://github.com/opdavies/gmail-filter-builder/issues/21): Minify the generated XML by default

View file

@ -24,7 +24,8 @@ class GenerateCommand extends Command
->setName(self::NAME)
->setDefinition([
new InputOption('input-file', 'i', InputOption::VALUE_OPTIONAL, 'The name of the PHP file containing your filters.', 'filters.php'),
new InputOption('output-file', 'o', InputOption::VALUE_OPTIONAL, 'The name of the XML file to generate.', 'filters.xml')
new InputOption('output-file', 'o', InputOption::VALUE_OPTIONAL, 'The name of the XML file to generate.', 'filters.xml'),
new InputOption('expanded', 'e', InputOption::VALUE_NONE, 'Whether to generate expanded XML.')
])
->setDescription('Generates XML for Gmail filters.')
;
@ -39,7 +40,7 @@ class GenerateCommand extends Command
try {
// TODO: Inject this.
new Builder($this->filters($input), $outputFile = $this->outputFile($input));
new Builder($this->filters($input), $outputFile = $this->outputFile($input), true, $input->getOption('expanded'));
$io->success(sprintf('%s file generated.', $outputFile));
} catch (IOException $e) {
@ -60,6 +61,6 @@ class GenerateCommand extends Command
throw new \RuntimeException('No input file found.');
}
return require_once $inputFile;
return require $inputFile;
}
}

View file

@ -29,12 +29,16 @@ class Builder
*/
private $xml;
public function __construct(array $filters, $outputFile = 'filters.xml', $writeFile = true)
/** @var bool */
private $expanded;
public function __construct(array $filters, $outputFile = 'filters.xml', $writeFile = true, $expanded = false)
{
$this->filesystem = new Filesystem();
$this->filters = $filters;
$this->outputFile = $outputFile;
$this->writeFile = $writeFile;
$this->expanded = $expanded;
$this->build();
}
@ -61,14 +65,14 @@ class Builder
*/
private function build(): void
{
$prefix = "<?xml version='1.0' encoding='UTF-8'?>" . PHP_EOL . "<feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>";
$prefix = "<?xml version='1.0' encoding='UTF-8'?>" . $this->glue() . "<feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>";
$suffix = '</feed>';
$xml = collect($this->filters)->map(function ($items) {
return $this->buildEntry($items);
})->implode(PHP_EOL);
})->implode($this->glue());
$this->xml = collect([$prefix, $xml, $suffix])->implode(PHP_EOL);
$this->xml = collect([$prefix, $xml, $suffix])->implode($this->glue());
if ($this->writeFile) {
$this->filesystem->dumpFile($this->outputFile, $this->xml);
@ -88,9 +92,9 @@ class Builder
->map(function ($value, $key): string {
return $this->buildProperty($value, $key);
})
->implode(PHP_EOL);
->implode($this->glue());
return collect(['<entry>', $entry, '</entry>'])->implode(PHP_EOL);
return collect(['<entry>', $entry, '</entry>'])->implode($this->glue());
}
/**
@ -128,4 +132,9 @@ class Builder
return sprintf('(%s)', collect($value)->implode($separator));
}
private function glue(): ?string
{
return $this->expanded ? PHP_EOL : null;
}
}

View file

@ -34,7 +34,7 @@ class GenerateFiltersTest extends TestCase
}
/** @test */
public function it_converts_filters_from_php_to_xml()
public function it_converts_filters_from_php_to_minified_xml()
{
$this->commandTester->execute([
'--input-file' => self::INPUT_FILENAME,
@ -48,4 +48,21 @@ class GenerateFiltersTest extends TestCase
$this->assertEquals(trim($expected), $result);
}
/** @test */
public function it_converts_filters_from_php_to_expanded_xml()
{
$this->commandTester->execute([
'--input-file' => self::INPUT_FILENAME,
'--output-file' => self::OUTPUT_FILENAME,
'--expanded' => true,
]);
$this->assertTrue($this->fs->exists(self::OUTPUT_FILENAME));
$expected = file_get_contents(__DIR__ . '/../../../fixtures/simple/output-expanded.xml');
$result = file_get_contents(self::OUTPUT_FILENAME);
$this->assertEquals(trim($expected), $result);
}
}

View file

@ -20,7 +20,7 @@ class BuilderTest extends TestCase
->star()
->important();
$result = new Builder([$filterA, $filterB], '', false);
$result = new Builder([$filterA, $filterB], '', false, true);
$expected = <<<EOF
<?xml version='1.0' encoding='UTF-8'?>

View file

@ -0,0 +1,8 @@
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
<entry>
<apps:property name='from' value='example.com'/>
<apps:property name='label' value='Test'/>
<apps:property name='shouldArchive' value='true'/>
</entry>
</feed>

View file

@ -1,8 +1 @@
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
<entry>
<apps:property name='from' value='example.com'/>
<apps:property name='label' value='Test'/>
<apps:property name='shouldArchive' value='true'/>
</entry>
</feed>
<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'><entry><apps:property name='from' value='example.com'/><apps:property name='label' value='Test'/><apps:property name='shouldArchive' value='true'/></entry></feed>