2021-11-11 03:28:10 +00:00
|
|
|
<?php
|
2022-07-06 20:54:48 +00:00
|
|
|
/**
|
|
|
|
* Formatter class
|
|
|
|
*
|
|
|
|
* @package WooCommerce
|
|
|
|
*/
|
2021-11-16 02:01:50 +00:00
|
|
|
|
|
|
|
namespace Automattic\WooCommerce\MonorepoTools\Changelogger;
|
|
|
|
|
2021-11-11 03:28:10 +00:00
|
|
|
/**
|
|
|
|
* Base Jetpack Changelogger Formatter for WooCommerce
|
|
|
|
*/
|
|
|
|
|
|
|
|
use Automattic\Jetpack\Changelog\Changelog;
|
|
|
|
use Automattic\Jetpack\Changelog\KeepAChangelogParser;
|
|
|
|
use Automattic\Jetpack\Changelogger\PluginTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base Jetpack Changelogger Formatter for WooCommerce
|
|
|
|
*
|
2021-11-16 02:01:50 +00:00
|
|
|
* Note: Since the "filename" loading relies on a single class implementing the plugin interface,
|
|
|
|
* we have to implement it in the child class, even though the base class satisfies it.
|
|
|
|
*
|
2021-11-11 03:28:10 +00:00
|
|
|
* Class Formatter
|
|
|
|
*/
|
2021-11-16 02:01:50 +00:00
|
|
|
class Formatter extends KeepAChangelogParser {
|
2021-11-11 03:28:10 +00:00
|
|
|
use PluginTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bullet for changes.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $bullet = '- ';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prologue text.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $prologue = "# Changelog \n\n";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Epilogue text.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $epilogue = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entry pattern regex.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $entry_pattern = '/^##\s+([^\n=]+)\s+((?:(?!^##).)+)/ms';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Heading pattern regex.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $heading_pattern = '/^## \[+(\[?[^] ]+\]?)\]\(.+\) - (.+?)\n/s';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subheading pattern regex.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $subentry_pattern = '/^###(.+)\n/m';
|
|
|
|
|
2022-06-29 20:09:46 +00:00
|
|
|
/**
|
|
|
|
* Return the epiologue.
|
|
|
|
*/
|
|
|
|
public function getEpilogue() {
|
|
|
|
return $this->epilogue;
|
|
|
|
}
|
|
|
|
|
2021-11-11 03:28:10 +00:00
|
|
|
/**
|
|
|
|
* Get Release link given a version number.
|
|
|
|
*
|
2021-11-16 02:01:50 +00:00
|
|
|
* @throws \InvalidArgumentException When directory parsing fails.
|
2021-11-11 03:28:10 +00:00
|
|
|
* @param string $version Release version.
|
|
|
|
*
|
|
|
|
* @return string Link to the version's release.
|
|
|
|
*/
|
|
|
|
public function getReleaseLink( $version ) {
|
2022-07-13 03:04:14 +00:00
|
|
|
return 'https://github.com/woocommerce/woocommerce/releases/tag/' . $version;
|
2021-11-11 03:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modified version of parse() from KeepAChangelogParser.
|
|
|
|
*
|
|
|
|
* @param string $changelog Changelog contents.
|
|
|
|
* @return Changelog
|
2021-11-16 02:01:50 +00:00
|
|
|
* @throws \InvalidArgumentException If the changelog data cannot be parsed.
|
2021-11-11 03:28:10 +00:00
|
|
|
*/
|
|
|
|
public function parse( $changelog ) {
|
|
|
|
$ret = new Changelog();
|
|
|
|
|
|
|
|
// Fix newlines and expand tabs.
|
|
|
|
$changelog = strtr( $changelog, array( "\r\n" => "\n" ) );
|
|
|
|
$changelog = strtr( $changelog, array( "\r" => "\n" ) );
|
|
|
|
while ( strpos( $changelog, "\t" ) !== false ) {
|
|
|
|
$changelog = preg_replace_callback(
|
|
|
|
'/^([^\t\n]*)\t/m',
|
|
|
|
function ( $m ) {
|
|
|
|
return $m[1] . str_repeat( ' ', 4 - ( mb_strlen( $m[1] ) % 4 ) );
|
|
|
|
},
|
|
|
|
$changelog
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entries make up the rest of the document.
|
|
|
|
$entries = array();
|
|
|
|
$entry_pattern = $this->entry_pattern;
|
|
|
|
preg_match_all( $entry_pattern, $changelog, $matches );
|
|
|
|
|
|
|
|
foreach ( $matches[0] as $section ) {
|
|
|
|
// Remove the epilogue, if it exists.
|
|
|
|
$section = str_replace( $this->epilogue, '', $section );
|
|
|
|
|
|
|
|
$heading_pattern = $this->heading_pattern;
|
|
|
|
$subentry_pattern = $this->subentry_pattern;
|
|
|
|
|
|
|
|
// Parse the heading and create a ChangelogEntry for it.
|
|
|
|
preg_match( $heading_pattern, $section, $heading );
|
|
|
|
|
|
|
|
// Check if the heading may be a sub-heading.
|
|
|
|
preg_match( $subentry_pattern, $section, $subheading );
|
|
|
|
$is_subentry = count( $subheading ) > 0;
|
|
|
|
|
|
|
|
if ( ! count( $heading ) && ! count( $subheading ) ) {
|
2021-11-16 02:01:50 +00:00
|
|
|
throw new \InvalidArgumentException( 'Invalid heading' );
|
2021-11-11 03:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$version = '';
|
2021-11-26 16:25:43 +00:00
|
|
|
$timestamp = new \DateTime( 'now', new \DateTimeZone( 'UTC' ) );
|
|
|
|
$entry_timestamp = new \DateTime( 'now', new \DateTimeZone( 'UTC' ) );
|
2021-11-11 03:28:10 +00:00
|
|
|
|
|
|
|
if ( count( $heading ) ) {
|
|
|
|
$version = $heading[1];
|
|
|
|
$timestamp = $heading[2];
|
|
|
|
|
|
|
|
try {
|
2021-11-26 16:25:43 +00:00
|
|
|
$timestamp = new \DateTime( $timestamp, new \DateTimeZone( 'UTC' ) );
|
2021-11-11 03:28:10 +00:00
|
|
|
} catch ( \Exception $ex ) {
|
2021-11-16 02:01:50 +00:00
|
|
|
throw new \InvalidArgumentException( "Heading has an invalid timestamp: $heading", 0, $ex );
|
2021-11-11 03:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( strtotime( $heading[2], 0 ) !== strtotime( $heading[2], 1000000000 ) ) {
|
2021-11-16 02:01:50 +00:00
|
|
|
throw new \InvalidArgumentException( "Heading has a relative timestamp: $heading" );
|
2021-11-11 03:28:10 +00:00
|
|
|
}
|
|
|
|
$entry_timestamp = $timestamp;
|
|
|
|
|
|
|
|
$content = trim( preg_replace( $heading_pattern, '', $section ) );
|
|
|
|
} elseif ( $is_subentry ) {
|
|
|
|
// It must be a subheading.
|
|
|
|
$version = $subheading[0]; // For now.
|
|
|
|
$content = trim( preg_replace( $subentry_pattern, '', $section ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$entry = $this->newChangelogEntry(
|
|
|
|
$version,
|
|
|
|
array(
|
|
|
|
'timestamp' => $timestamp,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$entries[] = $entry;
|
|
|
|
|
|
|
|
if ( '' === $content ) {
|
|
|
|
// Huh, no changes.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now parse all the subheadings and changes.
|
|
|
|
while ( '' !== $content ) {
|
|
|
|
$changes = array();
|
|
|
|
$rows = explode( "\n", $content );
|
|
|
|
foreach ( $rows as $row ) {
|
|
|
|
$row = trim( $row );
|
|
|
|
$row = preg_replace( '/' . $this->bullet . '/', '', $row, 1 );
|
|
|
|
$row_segments = explode( ' - ', $row );
|
2022-06-21 19:25:08 +00:00
|
|
|
$significance = trim( strtolower( $row_segments[0] ) );
|
2022-07-06 20:54:48 +00:00
|
|
|
|
2021-11-11 03:28:10 +00:00
|
|
|
array_push(
|
|
|
|
$changes,
|
|
|
|
array(
|
2022-06-21 19:25:08 +00:00
|
|
|
'subheading' => $is_subentry ? '' : trim( $row_segments[0] ),
|
2022-07-06 20:54:48 +00:00
|
|
|
'content' => $is_subentry ? trim( $row ) : trim( isset( $row_segments[1] ) ? $row_segments[1] : '' ),
|
|
|
|
'significance' => in_array( $significance, array( 'patch', 'minor', 'major' ), true ) ? $significance : null,
|
2021-11-11 03:28:10 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $changes as $change ) {
|
|
|
|
$entry->appendChange(
|
|
|
|
$this->newChangeEntry(
|
|
|
|
array(
|
2022-06-21 19:25:08 +00:00
|
|
|
'subheading' => $change['subheading'],
|
|
|
|
'content' => $change['content'],
|
|
|
|
'significance' => $change['significance'],
|
|
|
|
'timestamp' => $entry_timestamp,
|
2021-11-11 03:28:10 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$content = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$ret->setEntries( $entries );
|
|
|
|
$ret->setPrologue( $this->prologue );
|
2022-06-29 20:09:46 +00:00
|
|
|
$ret->setEpilogue( $this->getEpilogue() );
|
2021-11-11 03:28:10 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a Changelog object to a string.
|
|
|
|
*
|
|
|
|
* @param Changelog $changelog Changelog object.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function format( Changelog $changelog ) {
|
|
|
|
$ret = '';
|
|
|
|
$date_format = 'Y-m-d';
|
|
|
|
$bullet = $this->bullet;
|
|
|
|
$indent = str_repeat( ' ', strlen( $bullet ) );
|
|
|
|
|
|
|
|
$prologue = trim( $changelog->getPrologue() );
|
|
|
|
if ( '' !== $prologue ) {
|
|
|
|
$ret .= "$prologue\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $changelog->getEntries() as $entry ) {
|
|
|
|
$version = $entry->getVersion();
|
|
|
|
$is_subentry = preg_match( $this->subentry_pattern, $version, $subentry );
|
|
|
|
$timestamp = $entry->getTimestamp();
|
|
|
|
$release_link = $this->getReleaseLink( $version );
|
|
|
|
|
|
|
|
if ( $is_subentry ) {
|
|
|
|
$ret .= '###' . $subentry[1] . " \n\n";
|
|
|
|
} else {
|
|
|
|
$ret .= '## [' . $version . '](' . $release_link . ') - ' . $timestamp->format( $date_format ) . " \n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$prologue = trim( $entry->getPrologue() );
|
|
|
|
|
|
|
|
if ( '' !== $prologue ) {
|
|
|
|
$ret .= "$prologue\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $entry->getChangesBySubheading() as $heading => $changes ) {
|
|
|
|
foreach ( $changes as $change ) {
|
2021-11-15 22:48:38 +00:00
|
|
|
$significance = $change->getSignificance();
|
|
|
|
$breaking_change = 'major' === $significance ? ' [ **BREAKING CHANGE** ]' : '';
|
2021-11-11 03:28:10 +00:00
|
|
|
$text = trim( $change->getContent() );
|
|
|
|
if ( '' !== $text ) {
|
2021-11-15 22:48:38 +00:00
|
|
|
$preamble = $is_subentry ? '' : $bullet . ucfirst( $significance ) . $breaking_change . ' - ';
|
2021-11-11 03:28:10 +00:00
|
|
|
$ret .= $preamble . str_replace( "\n", "\n$indent", $text ) . "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$ret = trim( $ret ) . "\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$epilogue = trim( $changelog->getEpilogue() );
|
|
|
|
if ( '' !== $epilogue ) {
|
|
|
|
$ret .= "$epilogue\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$ret = trim( $ret ) . "\n";
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
2021-11-16 02:01:50 +00:00
|
|
|
}
|