Skip to content

Commit

Permalink
feat: add packages bumper
Browse files Browse the repository at this point in the history
Ref: #11
  • Loading branch information
marcocesarato committed Apr 14, 2021
1 parent 9165042 commit 0849aad
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/Bump/ComposerJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace ConventionalChangelog\Bump;

use ConventionalChangelog\Type\Bump;

class ComposerJson extends Bump
{
/**
* {@inheritdoc}
*/
protected $fileName = 'composer.json';
/**
* {@inheritdoc}
*/
protected $fileType = 'json';

/**
* {@inheritdoc}
*/
public function getVersion(): ?string
{
return $this->content->version;
}

/**
* {@inheritdoc}
*/
public function setVersion(string $version): self
{
$this->content->version = $version;

return $this;
}
}
35 changes: 35 additions & 0 deletions src/Bump/PackageJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace ConventionalChangelog\Bump;

use ConventionalChangelog\Type\Bump;

class PackageJson extends Bump
{
/**
* {@inheritdoc}
*/
protected $fileName = 'package.json';
/**
* {@inheritdoc}
*/
protected $fileType = 'json';

/**
* {@inheritdoc}
*/
public function getVersion(): ?string
{
return $this->content->version;
}

/**
* {@inheritdoc}
*/
public function setVersion(string $version): self
{
$this->content->version = $version;

return $this;
}
}
29 changes: 28 additions & 1 deletion src/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)) {
Expand All @@ -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
Expand Down
157 changes: 157 additions & 0 deletions src/Type/Bump.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace ConventionalChangelog\Type;

use Exception;

abstract class Bump
{
/**
* Package filename.
*
* @var string
*/
protected $fileName;
/**
* Package file type.
*
* @var string
*/
protected $fileType;
/**
* Base path.
*
* @var string
*/
protected $path;
/**
* Content.
*
* @var mixed
*/
protected $content;
/**
* Original content for backup usage.
*
* @var mixed
*/
protected $originContent;

/**
* Bump constructor.
*/
public function __construct(string $path)
{
$this->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;
}
}

0 comments on commit 0849aad

Please sign in to comment.