-
Notifications
You must be signed in to change notification settings - Fork 13
[FEATURE] Determine the version to publish from the extension #104
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
Open
bmack
wants to merge
1
commit into
main
Choose a base branch
from
feature/resolve-version-for-publish
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of the TYPO3 project - inspiring people to share! | ||
| * (c) 2020 Oliver Bartsch & Benni Mack | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace TYPO3\Tailor\Dto; | ||
|
|
||
| /** | ||
| * A version together with the source it was taken from. | ||
| * | ||
| * Since the version does not have to be stated on the command line, | ||
| * commands can tell the user where the version they work with comes from. | ||
| */ | ||
| class ResolvedVersion | ||
| { | ||
| public const SOURCE_ARGUMENT = 'argument'; | ||
| public const SOURCE_GIT_TAG = 'the tag of the checked out commit'; | ||
| public const SOURCE_EMCONF = 'ext_emconf.php'; | ||
| public const SOURCE_COMPOSER = 'composer.json'; | ||
|
|
||
| /** @var string */ | ||
| protected $version; | ||
|
|
||
| /** @var string */ | ||
| protected $source; | ||
|
|
||
| public function __construct(string $version, string $source) | ||
| { | ||
| $this->version = $version; | ||
| $this->source = $source; | ||
| } | ||
|
|
||
| public function getVersion(): string | ||
| { | ||
| return $this->version; | ||
| } | ||
|
|
||
| public function getSource(): string | ||
| { | ||
| return $this->source; | ||
| } | ||
|
|
||
| public function isFromArgument(): bool | ||
| { | ||
| return $this->source === self::SOURCE_ARGUMENT; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of the TYPO3 project - inspiring people to share! | ||
| * (c) 2020 Oliver Bartsch & Benni Mack | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace TYPO3\Tailor\Exception; | ||
|
|
||
| class VersionMissingException extends \InvalidArgumentException {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of the TYPO3 project - inspiring people to share! | ||
| * (c) 2020 Oliver Bartsch & Benni Mack | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace TYPO3\Tailor\Filesystem; | ||
|
|
||
| /** | ||
| * Reading information from ext_emconf.php | ||
| */ | ||
| class EmConfReader | ||
| { | ||
| /** @var array */ | ||
| protected $configuration = []; | ||
|
|
||
| public function __construct(string $path = '') | ||
| { | ||
| $filename = rtrim($path ?: (string)(getcwd() ?: '.'), '/') . '/ext_emconf.php'; | ||
| if (!file_exists($filename)) { | ||
| return; | ||
| } | ||
|
|
||
| $_EXTKEY = 'dummy'; | ||
| @include $filename; | ||
|
|
||
| if (!isset($EM_CONF) || !is_array($EM_CONF)) { | ||
| return; | ||
| } | ||
|
|
||
| $configuration = reset($EM_CONF); | ||
| if (is_array($configuration)) { | ||
| $this->configuration = $configuration; | ||
| } | ||
| } | ||
|
|
||
| public function getVersion(): string | ||
| { | ||
| return trim((string)($this->configuration['version'] ?? '')); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,9 +13,14 @@ | |
| namespace TYPO3\Tailor\Helper; | ||
|
|
||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use TYPO3\Tailor\Dto\ResolvedVersion; | ||
| use TYPO3\Tailor\Environment\Variables; | ||
| use TYPO3\Tailor\Exception\ExtensionKeyMissingException; | ||
| use TYPO3\Tailor\Exception\VersionMissingException; | ||
| use TYPO3\Tailor\Filesystem\ComposerReader; | ||
| use TYPO3\Tailor\Filesystem\EmConfReader; | ||
| use TYPO3\Tailor\Service\GitService; | ||
| use TYPO3\Tailor\Validation\VersionValidator; | ||
|
|
||
| /** | ||
| * Helper class for console commands. | ||
|
|
@@ -47,6 +52,91 @@ final class CommandHelper | |
| 'typo3extension', | ||
| ]; | ||
|
|
||
| /** | ||
| * The version is looked up in the extension itself if not given as argument, | ||
| * so a release does not have to repeat its version on the command line. | ||
| */ | ||
| public static function getVersionFromInput(InputInterface $input): ResolvedVersion | ||
| { | ||
| // 1. CLI argument has highest priority | ||
| $version = (string)($input->getArgument('version') ?? ''); | ||
| if ($version !== '') { | ||
| return new ResolvedVersion($version, ResolvedVersion::SOURCE_ARGUMENT); | ||
| } | ||
|
|
||
| $path = self::getPathFromInput($input); | ||
|
|
||
| // 2. The tag of the checked out commit marks the released version | ||
| $versions = (new GitService())->getVersionsFromTagsOfHead($path); | ||
|
|
||
| if (count($versions) > 1) { | ||
| throw new VersionMissingException( | ||
| sprintf( | ||
| 'The checked out commit is tagged with more than one version (%s). Please state the version to use as argument.', | ||
| implode(', ', $versions) | ||
| ), | ||
| 1786492801 | ||
| ); | ||
| } | ||
|
|
||
| if ($versions !== []) { | ||
| return new ResolvedVersion($versions[0], ResolvedVersion::SOURCE_GIT_TAG); | ||
| } | ||
|
|
||
| // 3. The version files, as written by the `set-version` command | ||
| $versionValidator = new VersionValidator(); | ||
|
|
||
| $version = (new EmConfReader($path))->getVersion(); | ||
| if ($versionValidator->isValid($version)) { | ||
| return new ResolvedVersion($version, ResolvedVersion::SOURCE_EMCONF); | ||
| } | ||
|
|
||
| $version = (new ComposerReader($path))->getVersion(); | ||
| if ($versionValidator->isValid($version)) { | ||
| return new ResolvedVersion($version, ResolvedVersion::SOURCE_COMPOSER); | ||
| } | ||
|
Comment on lines
+89
to
+97
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd give |
||
|
|
||
| throw new VersionMissingException( | ||
| 'The version must either be set as argument, or be available in the tag of the checked out commit, ' | ||
| . 'in `ext_emconf.php` or in `composer.json`.', | ||
| 1786492802 | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Move the extension key to its argument if it was passed as only argument. | ||
| * | ||
| * Since the version argument comes first and is optional, a single argument | ||
| * is ambiguous. Extension keys never look like a version though, so | ||
| * `ter:publish my_extension` can safely be told apart from `ter:publish 1.2.3`. | ||
| */ | ||
| public static function normalizeVersionAndExtensionKeyArguments(InputInterface $input): void | ||
| { | ||
| if (!$input->hasArgument('version') || !$input->hasArgument('extensionkey')) { | ||
| return; | ||
| } | ||
|
|
||
| $version = (string)($input->getArgument('version') ?? ''); | ||
| $extensionKey = (string)($input->getArgument('extensionkey') ?? ''); | ||
|
|
||
| if ($extensionKey !== '' || !preg_match('/^[a-z][a-z0-9_]+$/', $version)) { | ||
| return; | ||
| } | ||
|
|
||
| $input->setArgument('extensionkey', $version); | ||
| $input->setArgument('version', null); | ||
| } | ||
|
|
||
| /** | ||
| * The path of the extension to work with. Defaults to the current working directory. | ||
| */ | ||
| public static function getPathFromInput(InputInterface $input): string | ||
| { | ||
| $path = $input->hasOption('path') ? (string)($input->getOption('path') ?? '') : ''; | ||
|
|
||
| return $path !== '' ? $path : (string)(getcwd() ?: '.'); | ||
| } | ||
|
|
||
| public static function getExtensionKeyFromInput(InputInterface $input): string | ||
| { | ||
| // 1. CLI argument has highest priority | ||
|
|
@@ -56,8 +146,8 @@ public static function getExtensionKeyFromInput(InputInterface $input): string | |
| return $key; | ||
| } | ||
|
|
||
| // 2. composer.json is the recommended source | ||
| $extensionKeyFromComposer = (new ComposerReader())->getExtensionKey(); | ||
| // 2. composer.json of the extension is the recommended source | ||
| $extensionKeyFromComposer = (new ComposerReader(self::getPathFromInput($input)))->getExtensionKey(); | ||
| if ($extensionKeyFromComposer !== '') { | ||
| return $extensionKeyFromComposer; | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure if we should really fail here if more than one version tag points to HEAD (which is actually a quite uncommon circumstance), since we probably still read it from
composer.jsonorext_emconf.php.