Skip to content

Commit

Permalink
Automatically create changelog.yml if it doesn't exist yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Oct 25, 2017
1 parent 2e583cf commit ab8c867
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
11 changes: 11 additions & 0 deletions lib/Components/Component/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,19 @@ public function changed(
$log, Components_Helper_ChangeLog $helper, $options
)
{
// Create changelog.yml
if (!$helper->changelogFileExists() &&
file_exists($this->getPackageXmlPath())) {
$helper->migrateToChangelogYml($this->getPackageXml());
}

// Update changelog.yml
$file = $helper->changelogYml($log, $options);
if ($file && !empty($options['commit'])) {
$options['commit']->add($file, $this->_directory);
}

// Update package.xml
if (empty($options['nopackage'])) {
if ($helper->changelogFileExists()) {
$file = $helper->updatePackage(
Expand All @@ -241,6 +250,8 @@ public function changed(
$options['commit2']->add($file, $this->_directory);
}
}

// Update CHANGES
if (empty($options['nochanges'])) {
if ($helper->changelogFileExists()) {
$file = $helper->updateChanges($options);
Expand Down
84 changes: 81 additions & 3 deletions lib/Components/Helper/ChangeLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public function changelogYml($log, $options)
return;
}

$changelog = $this->changelogFileExists();

if ($changelog) {
if ($changelog = $this->changelogFileExists()) {
if (empty($options['pretend'])) {
$version = $this->addChangelog($log);
$this->_output->ok(
Expand Down Expand Up @@ -152,6 +150,86 @@ public function addChangelog($entry)
return $version;
}

/**
* Builds a changelog.yml from an existing package.xml.
*
* @param Horde_Pear_Package_Xml $xml The package xml handler.
*/
public function migrateToChangelogYml($xml)
{
$changes = array();
foreach ($xml->findNodes('/p:package/p:changelog/p:release') as $release) {
$version = $xml->getNodeTextRelativeTo(
'p:version/p:release', $release
);
$license = $xml->findNodeRelativeTo('p:license', $release);
$changes[$version] = array(
'api' => $xml->getNodeTextRelativeTo(
'p:version/p:api', $release
),
'state' => array(
'release' => $xml->getNodeTextRelativeTo(
'p:stability/p:release', $release
),
'api' => $xml->getNodeTextRelativeTo(
'p:stability/p:api', $release
),
),
'date' => $xml->getNodeTextRelativeTo('p:date', $release),
'license' => array(
'identifier' => $license->textContent,
'uri' => $license->getAttribute('uri')
),
'notes' => preg_replace(
'/^\* /m',
'',
trim($xml->getNodeTextRelativeTo('p:notes', $release))
) . "\n"
);
}
$changes = array_reverse($changes);

if ($changesFile = $this->changesFileExists()) {
$fp = fopen($changesFile, 'r');
$inHeader = $version = false;
while ($line = fgets($fp)) {
if (!strcspn($line, '-')) {
$inHeader = !$inHeader;
continue;
}
if (!trim($line)) {
continue;
}
if ($inHeader) {
if ($version && !isset($changes[$version])) {
$changes[$version] = array('notes' => $notes);
}
$notes = '';
$version = preg_replace('/v(.*?)(-git)?\n/m', '$1', $line);
continue;
}
if (strpos($line, ' ') === 0) {
$line = ltrim($line);
$notes = substr($notes, 0, -1) . ' ';
}
$notes .= $line;
}
if ($version && !isset($changes[$version])) {
$changes[$version] = array('notes' => $notes);
}
fclose($fp);
}

$changelog = is_dir($this->_directory . '/doc')
? $this->_directory . self::CHANGELOG
: $this->_directory . self::CHANGELOG_H5;
file_put_contents(
$changelog,
Horde_Yaml::dump($changes, array('wordwrap' => 0))
);
$this->_output->ok(sprintf('Created %s.', $changelog));
}

/* package.xml methods */

/**
Expand Down

0 comments on commit ab8c867

Please sign in to comment.