Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Support guides.xml in documentation version replacement #81

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ be done using the `set-version` command.
./vendor/bin/tailor set-version 1.2.0
```

If your extension also contains a `Documentation/Settings.cfg`
file, the command will also update the `release` and `version`
information in it. You can disable this feature by either
using `--no-docs` or by setting the environment variable
`TYPO3_DISABLE_DOCS_VERSION_UPDATE=1`.
If your extension also contains a `Documentation/guides.xml`
or `Documentation/Settings.cfg` file, the command will also
update the `release` and `version` information in it. You
can disable this feature by either using `--no-docs` or by
setting the environment variable `TYPO3_DISABLE_DOCS_VERSION_UPDATE=1`.

> [!TIP]
> It's also possible to use the `--path` option to
Expand Down
71 changes: 48 additions & 23 deletions src/Command/Extension/SetExtensionVersionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@

/**
* Command for updating the extension version in ext_emconf.php and
* the extension documentation configuration file Settings.cfg.
* the extension documentation configuration files guides.xml and Settings.cfg.
*/
class SetExtensionVersionCommand extends Command
{
private const EMCONF_PATTERN = '["\']version["\']\s=>\s["\']((?:[0-9]+)\.[0-9]+\.[0-9]+\s*)["\']';
private const DOCUMENTATION_VERSION_PATTERN = 'version\s*=\s*([0-9]+\.[0-9]+)';
private const DOCUMENTATION_RELEASE_PATTERN = 'release\s*=\s*([0-9]+\.[0-9]+\.[0-9]+)';

// Documentation/guides.xml
private const DOCUMENTATION_RELEASE_PATTERN = 'release="([0-9]+\.[0-9]+\.[0-9]+)"';
private const DOCUMENTATION_VERSION_PATTERN = 'version="([0-9]+\.[0-9]+)"';

// Documentation/Settings.cfg
private const DOCUMENTATION_RELEASE_LEGACY_PATTERN = 'release\s*=\s*([0-9]+\.[0-9]+\.[0-9]+)';
private const DOCUMENTATION_VERSION_LEGACY_PATTERN = 'version\s*=\s*([0-9]+\.[0-9]+)';

protected function configure(): void
{
Expand Down Expand Up @@ -80,28 +86,47 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

$documentationSettingsFile = rtrim($path, '/') . '/Documentation/Settings.cfg';
if (!file_exists($documentationSettingsFile)) {
$io->note(
'Documentation version update is enabled but was not performed because the file '
. $documentationSettingsFile . ' does not exist. To disable this operation use the \'--no-docs\' '
. 'option or set the \'TYPO3_DISABLE_DOCS_VERSION_UPDATE\' environment variable.'
);
return 0;
$documentationVersionReplaced = false;
$documentationSettingsFiles = [
rtrim($path, '/') . '/Documentation/guides.xml' => [
self::DOCUMENTATION_RELEASE_PATTERN,
self::DOCUMENTATION_VERSION_PATTERN,
],
rtrim($path, '/') . '/Documentation/Settings.cfg' => [
self::DOCUMENTATION_RELEASE_LEGACY_PATTERN,
self::DOCUMENTATION_VERSION_LEGACY_PATTERN,
],
];

foreach ($documentationSettingsFiles as $documentationSettingsFile => [$documentationReleasePattern, $documentationVersionPattern]) {
if (!file_exists($documentationSettingsFile)) {
continue;
}

try {
$versionReplacer->setVersion($documentationSettingsFile, $documentationReleasePattern);
} catch (\InvalidArgumentException $e) {
$io->error(sprintf('An error occurred while updating the release number in %s', $documentationSettingsFile));
return 1;
}

try {
$versionReplacer->setVersion($documentationSettingsFile, $documentationVersionPattern, 2);
} catch (\InvalidArgumentException $e) {
$io->error(sprintf('An error occurred while updating the version number in %s', $documentationSettingsFile));
return 1;
}

$documentationVersionReplaced = true;
}

try {
$versionReplacer->setVersion($documentationSettingsFile, self::DOCUMENTATION_RELEASE_PATTERN);
} catch (\InvalidArgumentException $e) {
$io->error(sprintf('An error occurred while updating the release number in %s', $documentationSettingsFile));
return 1;
}

try {
$versionReplacer->setVersion($documentationSettingsFile, self::DOCUMENTATION_VERSION_PATTERN, 2);
} catch (\InvalidArgumentException $e) {
$io->error(sprintf('An error occurred while updating the version number in %s', $documentationSettingsFile));
return 1;
if (!$documentationVersionReplaced) {
$io->note(
'Documentation version update is enabled but was not performed because the files '
. implode(' and ', array_keys($documentationSettingsFiles)) . ' do not exist. '
. 'To disable this operation use the \'--no-docs\' option or set the '
. '\'TYPO3_DISABLE_DOCS_VERSION_UPDATE\' environment variable.'
);
}

return 0;
Expand Down
50 changes: 38 additions & 12 deletions tests/Unit/Filesystem/VersionReplacerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,58 @@ public function replaceVersionReplacesProperVersionOfEmConf(): void
}

/**
* @test
* @return \Generator<string, array{string, string, string}>
*/
public function replaceVersionReplacesProperReleaseOfDocumentationConfiguration(): void
public static function replaceVersionReplacesProperReleaseOfDocumentationConfigurationDataProvider(): \Generator
{
$docSettings = file_get_contents(__DIR__ . '/../Fixtures/Documentation/Settings.cfg');
$tempFile = tempnam('/tmp/', 'tailor_settings.cfg');
yield 'guides.xml' => ['guides.xml', 'release="([0-9]+\.[0-9]+\.[0-9]+)"', 'release="6.9.0"'];
yield 'Settings.cfg' => ['Settings.cfg', 'release\s*=\s*([0-9]+\.[0-9]+\.[0-9]+)', 'release=6.9.0'];
}

/**
* @test
* @dataProvider replaceVersionReplacesProperReleaseOfDocumentationConfigurationDataProvider
*/
public function replaceVersionReplacesProperReleaseOfDocumentationConfiguration(
string $docSettingsFile,
string $docReleasePattern,
string $expected
): void {
$docSettings = file_get_contents(__DIR__ . '/../Fixtures/Documentation/' . $docSettingsFile);
$tempFile = tempnam(sys_get_temp_dir(), 'tailor_' . $docSettingsFile);
file_put_contents($tempFile, $docSettings);
$subject = new VersionReplacer('6.9.0');
$subject->setVersion($tempFile, 'release\s*=\s*([0-9]+\.[0-9]+\.[0-9]+)');
$subject->setVersion($tempFile, $docReleasePattern);
$contents = file_get_contents($tempFile);
self::assertStringContainsString('release=6.9.0', preg_replace('/\s+/', '', $contents));
self::assertStringContainsString($expected, preg_replace('/\s+/', '', $contents));
unlink($tempFile);
}

/**
* @test
* @return \Generator<string, array{string, string, string}>
*/
public function replaceVersionReplacesProperVersionOfDocumentationConfiguration(): void
public static function replaceVersionReplacesProperVersionOfDocumentationConfigurationDataProvider(): \Generator
{
$docSettings = file_get_contents(__DIR__ . '/../Fixtures/Documentation/Settings.cfg');
$tempFile = tempnam('/tmp/', 'tailor_settings.cfg');
yield 'guides.xml' => ['guides.xml', 'version="([0-9]+\.[0-9]+)"', 'version="6.9"'];
yield 'Settings.cfg' => ['Settings.cfg', 'version\s*=\s*([0-9]+\.[0-9]+)', 'version=6.9'];
}

/**
* @test
* @dataProvider replaceVersionReplacesProperVersionOfDocumentationConfigurationDataProvider
*/
public function replaceVersionReplacesProperVersionOfDocumentationConfiguration(
string $docSettingsFile,
string $docVersionPattern,
string $expected
): void {
$docSettings = file_get_contents(__DIR__ . '/../Fixtures/Documentation/' . $docSettingsFile);
$tempFile = tempnam(sys_get_temp_dir(), 'tailor_' . $docSettingsFile);
file_put_contents($tempFile, $docSettings);
$subject = new VersionReplacer('6.9.0');
$subject->setVersion($tempFile, 'version\s*=\s*([0-9]+\.[0-9]+)', 2);
$subject->setVersion($tempFile, $docVersionPattern, 2);
$contents = file_get_contents($tempFile);
self::assertStringContainsString('version=6.9', preg_replace('/\s+/', '', $contents));
self::assertStringContainsString($expected, preg_replace('/\s+/', '', $contents));
unlink($tempFile);
}

Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/Fixtures/Documentation/guides.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides ../vendor/phpdocumentor/guides-cli/resources/schema/guides.xsd"
links-are-relative="true"
>
<extension class="\T3Docs\Typo3DocsTheme\DependencyInjection\Typo3DocsThemeExtension"/>
<project
title="My extension"
release="1.0.0"
version="1.0"
copyright="2021"/>
</guides>