woocommerce/tools/changelogger/Formatter.php

285 lines
7.8 KiB
PHP
Raw Normal View History

2021-11-11 03:28:10 +00:00
<?php
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
*
* 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
*/
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';
/**
* Get Release link given a version number.
*
* @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 ) {
2021-11-11 04:23:47 +00:00
// Catpure anything past /woocommerce in the current working directory.
preg_match( '/\/woocommerce\/(.+)/', getcwd(), $path );
2021-11-11 03:28:10 +00:00
if ( ! count( $path ) ) {
throw new \InvalidArgumentException( 'Invalid directory.' );
2021-11-11 03:28:10 +00:00
}
2022-06-15 22:06:31 +00:00
$release_url = '';
if ( strpos( $path[1], 'packages/js/' ) !== false ) {
2022-06-15 22:06:31 +00:00
$package = substr( $path[1], 12 );
$release_url ='https://www.npmjs.com/package/@woocommerce/' . $package . '/v/';
} else if ( 'plugins/woocommerce' === $path[1] ) {
$release_url = 'https://github.com/woocommerce/woocommerce/releases/tag/';
} else if ( 'plugins/woocommerce-beta-tester' === $path[1] ) {
$release_url = 'https://github.com/woocommerce/woocommerce/releases/tag/';
} else {
throw new \InvalidArgumentException( 'Release URL not found.' );
2021-11-11 03:28:10 +00:00
}
2022-06-15 22:06:31 +00:00
2021-11-11 03:28:10 +00:00
return $release_url . $version;
}
/**
* Modified version of parse() from KeepAChangelogParser.
*
* @param string $changelog Changelog contents.
* @return Changelog
* @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 ) ) {
throw new \InvalidArgumentException( 'Invalid heading' );
2021-11-11 03:28:10 +00:00
}
$version = '';
$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 {
$timestamp = new \DateTime( $timestamp, new \DateTimeZone( 'UTC' ) );
2021-11-11 03:28:10 +00:00
} catch ( \Exception $ex ) {
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 ) ) {
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 );
$significance = trim( strtolower( $row_segments[0] ) );
2021-11-11 03:28:10 +00:00
array_push(
$changes,
array(
'subheading' => $is_subentry ? '' : trim( $row_segments[0] ),
'content' => $is_subentry ? trim( $row ) : trim( isset($row_segments[1]) ? $row_segments[1] : '' ),
'significance' => in_array( $significance, array( 'patch', 'minor', 'major' ) ) ? $significance : null,
2021-11-11 03:28:10 +00:00
)
);
}
foreach ( $changes as $change ) {
$entry->appendChange(
$this->newChangeEntry(
array(
'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 );
$ret->setEpilogue( $this->epilogue );
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;
}
}