-
Notifications
You must be signed in to change notification settings - Fork 0
Add replace-version command #54
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ea732c1
Add replace-version command
jonwaldstein 73a5c6d
Address review feedback on replace-version
jonwaldstein a381894
refactor: move command into dedicated traits namespace
jonwaldstein 71dad43
refactor: update package command to call replace-version command
jonwaldstein d57eced
refactor: call command class directly
jonwaldstein 9957581
refactor: move trait to command directory and keep commands DRY
jonwaldstein a0026c8
Merge branch 'main' into add-replace-version-command
jonwaldstein 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
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,84 @@ | ||
| <?php | ||
|
|
||
| namespace StellarWP\Pup\Commands; | ||
|
|
||
| use StellarWP\Pup\App; | ||
| use StellarWP\Pup\Exceptions\BaseException; | ||
| use StellarWP\Pup\Command\Command; | ||
| use StellarWP\Pup\Commands\Traits\DevSuffix; | ||
| use StellarWP\Pup\Utils\Directory as DirectoryUtils; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| class ReplaceVersion extends Command { | ||
| use DevSuffix; | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| * | ||
| * @return void | ||
| */ | ||
| protected function configure() { | ||
| $this->setName( 'replace-version' ) | ||
| ->addArgument( 'version', InputArgument::REQUIRED, 'The version to write into the version files.' ) | ||
| ->addOption( 'dev', null, InputOption::VALUE_NONE, 'Append the dev suffix to the version.' ) | ||
| ->addOption( 'root', null, InputOption::VALUE_REQUIRED, 'Set the root directory for running commands.' ) | ||
| ->setDescription( 'Replaces the version in the files defined in .puprc paths.versions.' ) | ||
| ->setHelp( 'Replaces the version in the files defined in .puprc paths.versions with the provided version.' ); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| protected function execute( InputInterface $input, OutputInterface $output ) { | ||
| parent::execute( $input, $output ); | ||
|
|
||
| $config = App::getConfig(); | ||
| $version = $input->getArgument( 'version' ); | ||
| $version_files = $config->getVersionFiles(); | ||
|
|
||
| if ( empty( $version_files ) ) { | ||
| $output->writeln( '<fg=yellow>No version files found in .puprc paths.versions.</>' ); | ||
| return 1; | ||
| } | ||
|
|
||
| if ( $input->getOption( 'dev' ) ) { | ||
| $version .= $this->getDevSuffix(); | ||
| } | ||
|
|
||
| $root = $input->getOption( 'root' ); | ||
| $root = $root ? DirectoryUtils::trailingSlashIt( $root ) : ''; | ||
|
|
||
| foreach ( $version_files as $file ) { | ||
| $contents = file_get_contents( $root . $file->getPath() ); | ||
|
|
||
| if ( ! $contents ) { | ||
| throw new BaseException( 'Could not read file: ' . $file->getPath() ); | ||
| } | ||
|
|
||
| $count = 0; | ||
| $replaced = preg_replace( '/' . $file->getRegex() . '/', '${1}' . $version, $contents, 1, $count ); | ||
|
|
||
| if ( null === $replaced ) { | ||
| throw new BaseException( 'Could not replace version in file (check the regex): ' . $file->getPath() ); | ||
| } | ||
|
|
||
| if ( $count === 0 ) { | ||
| $output->writeln( "<fg=yellow>!</> No version found in <fg=cyan>{$file->getPath()}</> matching its regex. Skipping." ); | ||
| continue; | ||
| } | ||
|
|
||
| $results = file_put_contents( $root . $file->getPath(), $replaced ); | ||
|
|
||
| if ( false === $results ) { | ||
| throw new BaseException( 'Could not write to file: ' . $file->getPath() ); | ||
| } | ||
|
|
||
| $output->writeln( "<fg=green>✓</> Updated version in <fg=cyan>{$file->getPath()}</> to <fg=cyan>{$version}</>." ); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } |
|
d4mation marked this conversation as resolved.
|
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,20 @@ | ||
| <?php | ||
|
|
||
| namespace StellarWP\Pup\Commands\Traits; | ||
|
|
||
| /** | ||
| * Provides the dev version suffix used by version-related commands. | ||
| */ | ||
| trait DevSuffix { | ||
| /** | ||
| * Builds the dev suffix (e.g. -dev-<timestamp>-<hash>) from the current git HEAD. | ||
| * | ||
| * @return string | ||
| */ | ||
| protected function getDevSuffix(): string { | ||
| $timestamp = exec( 'git show -s --format=%ct HEAD' ); | ||
| $hash = exec( 'git rev-parse --short=8 HEAD' ); | ||
|
|
||
| return "-dev-{$timestamp}-{$hash}"; | ||
| } | ||
| } |
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,93 @@ | ||
| <?php | ||
|
|
||
| namespace StellarWP\Pup\Tests\Cli\Commands; | ||
|
|
||
| use StellarWP\Pup\Tests\Cli\AbstractBase; | ||
| use StellarWP\Pup\Tests\CliTester; | ||
|
|
||
| class ReplaceVersionCest extends AbstractBase { | ||
|
|
||
| /** | ||
| * The version files modified by the replace-version command. These are git-tracked | ||
| * fixtures, so they must be restored after each test. | ||
| * | ||
| * @var string[] | ||
| */ | ||
| protected $version_files = [ | ||
| 'bootstrap.php', | ||
| 'package.json', | ||
| 'src/Plugin.php', | ||
| ]; | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function _after( CliTester $I ) { | ||
| $this->restore_version_files(); | ||
| parent::_after( $I ); | ||
| } | ||
|
|
||
| /** | ||
| * Restores the git-tracked version fixture files modified during a test. | ||
| * | ||
| * @return void | ||
| */ | ||
| protected function restore_version_files(): void { | ||
| foreach ( $this->version_files as $file ) { | ||
| $path = $this->tests_root . '/_data/fake-project/' . $file; | ||
| system( 'git checkout -- ' . escapeshellarg( $path ) ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function it_should_replace_the_version_in_version_files( CliTester $I ) { | ||
| $this->write_default_puprc(); | ||
|
|
||
| chdir( $this->tests_root . '/_data/fake-project' ); | ||
|
|
||
| $I->runShellCommand( "php {$this->pup} replace-version 2.5.0" ); | ||
| $I->seeResultCodeIs( 0 ); | ||
| $I->seeInShellOutput( 'bootstrap.php' ); | ||
| $I->seeInShellOutput( '2.5.0' ); | ||
|
|
||
| $project = $this->tests_root . '/_data/fake-project'; | ||
| $I->assertStringContainsString( "define( 'FAKE_PROJECT_VERSION', '2.5.0' );", (string) file_get_contents( $project . '/bootstrap.php' ) ); | ||
| $I->assertStringContainsString( '"version": "2.5.0"', (string) file_get_contents( $project . '/package.json' ) ); | ||
| $I->assertStringContainsString( "const VERSION = '2.5.0';", (string) file_get_contents( $project . '/src/Plugin.php' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function it_should_append_the_dev_suffix_with_the_dev_option( CliTester $I ) { | ||
| $this->write_default_puprc(); | ||
|
|
||
| chdir( $this->tests_root . '/_data/fake-project' ); | ||
|
|
||
| $I->runShellCommand( "php {$this->pup} replace-version 2.5.0 --dev" ); | ||
| $I->seeResultCodeIs( 0 ); | ||
|
|
||
| $project = $this->tests_root . '/_data/fake-project'; | ||
| $contents = (string) file_get_contents( $project . '/bootstrap.php' ); | ||
|
|
||
| // The suffix is -dev-<timestamp>-<hash>, so assert the prefix landed in the file. | ||
| $I->assertMatchesRegularExpression( "/FAKE_PROJECT_VERSION', '2\.5\.0-dev-\d+-[0-9a-f]+'/", $contents ); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function it_should_fail_when_no_version_files_are_configured( CliTester $I ) { | ||
| $puprc = $this->get_puprc(); | ||
| $puprc['paths']['versions'] = []; | ||
| $this->write_puprc( $puprc ); | ||
|
|
||
| chdir( $this->tests_root . '/_data/fake-project' ); | ||
|
|
||
| $I->runShellCommand( "php {$this->pup} replace-version 2.5.0", false ); | ||
| $I->seeResultCodeIs( 1 ); | ||
| $I->seeInShellOutput( 'No version files found' ); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.