diff --git a/src/Bump/ComposerJson.php b/src/Bump/ComposerJson.php new file mode 100644 index 0000000..6ede7c3 --- /dev/null +++ b/src/Bump/ComposerJson.php @@ -0,0 +1,35 @@ +content->version; + } + + /** + * {@inheritdoc} + */ + public function setVersion(string $version): self + { + $this->content->version = $version; + + return $this; + } +} diff --git a/src/Bump/PackageJson.php b/src/Bump/PackageJson.php new file mode 100644 index 0000000..7dec1da --- /dev/null +++ b/src/Bump/PackageJson.php @@ -0,0 +1,35 @@ +content->version; + } + + /** + * {@inheritdoc} + */ + public function setVersion(string $version): self + { + $this->content->version = $version; + + return $this; + } +} diff --git a/src/Changelog.php b/src/Changelog.php index 4b68ef9..44ac41c 100644 --- a/src/Changelog.php +++ b/src/Changelog.php @@ -2,11 +2,15 @@ namespace ConventionalChangelog; +use ConventionalChangelog\Bump\ComposerJson; +use ConventionalChangelog\Bump\PackageJson; use ConventionalChangelog\Git\ConventionalCommit; use ConventionalChangelog\Git\Repository; use ConventionalChangelog\Helper\Formatter; use ConventionalChangelog\Helper\SemanticVersion; +use ConventionalChangelog\Type\Bump; use DateTime; +use Exception; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -72,6 +76,13 @@ public function generate(InputInterface $input, SymfonyStyle $output): int $autoCommit = $autoCommit || $autoCommitAll; $autoBump = false; + /** + * @var Bump[] + */ + $packageBumps = [ + ComposerJson::class, + PackageJson::class, + ]; if (empty($root) || !is_dir($root)) { $root = $this->config->getRoot(); @@ -361,6 +372,22 @@ public function generate(InputInterface $input, SymfonyStyle $output): int // Add all changes list to new changelog $changelogNew .= $this->getMarkdownChanges($changes); } + $filesToCommit = [$file]; + foreach ($packageBumps as $packageBump) { + try { + /** + * @var Bump + */ + $bumper = new $packageBump($root); + if ($bumper->exists()) { + $bumper->setVersion($newVersion); + $bumper->save(); + $filesToCommit[] = $bumper->getFilePath(); + } + } catch (Exception $e) { + $output->error('An error occurred bumping package version: ' . $e->getMessage()); + } + } // Print summary if (!empty($summary)) { @@ -385,7 +412,7 @@ public function generate(InputInterface $input, SymfonyStyle $output): int Repository::addAll(); } $message = $this->getReleaseCommitMessage($newVersion); - $result = Repository::commit($message, [$file], $amend, $hooks); + $result = Repository::commit($message, $filesToCommit, $amend, $hooks); if ($result !== false) { $output->success('Release committed!'); // Create tag diff --git a/src/Type/Bump.php b/src/Type/Bump.php new file mode 100644 index 0000000..405b3ae --- /dev/null +++ b/src/Type/Bump.php @@ -0,0 +1,157 @@ +path = $path; + if ($this->exists()) { + $raw = file_get_contents($this->getFilePath()); + switch ($this->fileType) { + case 'json': + $this->originContent = json_decode($raw); + if ($this->originContent === null && json_last_error() !== JSON_ERROR_NONE) { + throw new Exception(json_last_error_msg(), json_last_error()); + } + $this->content = clone $this->originContent; + break; + default: + $this->originContent = $raw; + $this->content = $raw; + } + } + } + + /** + * Get version. + */ + public function getVersion(): ?string + { + return null; + } + + /** + * Set version. + * + * @return $this + */ + public function setVersion(string $version) + { + return $this; + } + + /** + * Get file path. + */ + public function getFilePath(): string + { + if (empty($this->path)) { + $this->path = getcwd(); + } + $path = $this->path . DIRECTORY_SEPARATOR . $this->fileName; + + return preg_replace('/' . preg_quote(DIRECTORY_SEPARATOR, '/') . '+/', DIRECTORY_SEPARATOR, $path); + } + + /** + * Get filename. + */ + public function getFileName(): string + { + return $this->fileName; + } + + /** + * Package file exists. + */ + public function exists(): bool + { + return is_file($this->getFilePath()); + } + + /** + * Save content. + * + * @return $this + */ + public function save() + { + if ($this->exists()) { + switch ($this->fileType) { + case 'json': + $content = json_encode($this->content, JSON_PRETTY_PRINT); + if ($content === null && json_last_error() !== JSON_ERROR_NONE) { + throw new Exception(json_last_error_msg(), json_last_error()); + } + break; + default: + $content = $this->content; + } + file_put_contents($this->getFilePath(), $content); + } + + return $this; + } + + /** + * Set content. + * + * @param mixed $content + * + * @return $this + */ + protected function setContent($content) + { + $this->content = $content; + + return $this; + } + + /** + * Get content and back backup. + * + * @return mixed + */ + protected function getContent() + { + return $this->content; + } +}