Skip to content

Commit

Permalink
Re-order methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Oct 25, 2017
1 parent 04a4b5b commit 1fddbd6
Showing 1 changed file with 116 additions and 110 deletions.
226 changes: 116 additions & 110 deletions lib/Components/Helper/ChangeLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public function __construct(
$this->_directory = $config->getPath();
}

/* changelog.yml methods */

/**
* Update changelog.yml file.
*
Expand Down Expand Up @@ -104,6 +106,49 @@ public function changelogYml($log, $options)
}
}

/**
* Indicates if there is a changelog.yml file for this component.
*
* @return string|boolean The path to the changelog.yml file if it exists,
* false otherwise.
*/
public function changelogFileExists()
{
foreach (array(self::CHANGELOG, self::CHANGELOG_H5) as $path) {
$changes = $this->_directory . $path;
if (file_exists($changes)) {
return $changes;
}
}
return false;
}

/**
* Add a change log entry to changelog.yml
*
* @param string $entry Change log entry to add.
*
* @returns string The updated version.
*/
public function addChangelog($entry)
{
$hordeInfo = $this->_getHordeInfo($this->_directory);
$changelog = Horde_Yaml::loadFile($this->_directory . self::CHANGELOG);
$version = $hordeInfo['version']['release'];
$info = $changelog[$version];
$notes = explode("\n", trim($info['notes']));
array_unshift($notes, $entry);
$info['notes'] = implode("\n", $notes) . "\n";
$changelog[$version] = $info;
file_put_contents(
$this->_directory . self::CHANGELOG,
Horde_Yaml::dump($changelog, array('wordwrap' => 0))
);
return $version;
}

/* package.xml methods */

/**
* Update package.xml file.
*
Expand Down Expand Up @@ -135,6 +180,36 @@ public function packageXml($log, $xml, $file, $options)
}
}

/**
* Updates package.xml from changelog.yml.
*
* @param Horde_Pear_Package_Xml $xml The package xml handler.
* @param string $file Path to the package.xml.
* @param array $options Additional options.
*
* @return string Path to the updated package.xml file.
*/
public function updatePackage($xml, $file, $options)
{
$changelog = $this->changelogFileExists($this->_directory);
if (!$changelog || !file_exists($file)) {
return;
}

if (empty($options['pretend'])) {
$allchanges = Horde_Yaml::loadFile($changelog);
$xml->setNotes($allchanges);
file_put_contents($file, (string)$xml);
$this->_output->ok(sprintf('Updated %s.', $file));
} else {
$this->_output->info(sprintf('Would update %s now.', $file));
}

return $file;
}

/* CHANGES methods */

/**
* Update CHANGES file.
*
Expand Down Expand Up @@ -167,17 +242,17 @@ public function changes($log, $options)
}

/**
* Returns the link to the change log.
* Returns the link to the CHANGES file on GitHub.
*
* @param string $root The root of the component in the repository.
* @param string $root The root of the component in the repository.
*
* @return string|null The link to the change log.
* @return string The link to the change log.
*/
public function getChangelog($root)
{
if ($changes = $this->changesFileExists($this->_directory)) {
$blob = trim(
$this->systemInDirectory(
$this->_systemInDirectory(
'git log --format="%H" HEAD^..HEAD',
$this->_directory,
array()
Expand All @@ -189,59 +264,6 @@ public function getChangelog($root)
return '';
}

/**
* Run a system call.
*
* @param string $call The system call to execute.
* @param string $target_dir Run the command in the provided target path.
* @param array $options Additional options.
*
* @return string The command output.
*/
protected function systemInDirectory($call, $target_dir, $options)
{
$old_dir = getcwd();
chdir($target_dir);
$result = $this->system($call, $options);
chdir($old_dir);
return $result;
}

/**
* Run a system call.
*
* @param string $call The system call to execute.
* @param array $options Additional options.
*
* @return string The command output.
*/
protected function system($call, $options)
{
if (empty($options['pretend'])) {
//@todo Error handling
return exec($call);
} else {
$this->_output->info(sprintf('Would run "%s" now.', $call));
}
}

/**
* Indicates if there is a changelog.yml file for this component.
*
* @return string|boolean The path to the changelog.yml file if it exists,
* false otherwise.
*/
public function changelogFileExists()
{
foreach (array(self::CHANGES, self::CHANGES_H5) as $path) {
$changes = $this->_directory . $path;
if (file_exists($changes)) {
return $changes;
}
}
return false;
}

/**
* Indicates if there is a CHANGES file for this component.
*
Expand All @@ -259,30 +281,6 @@ public function changesFileExists()
return false;
}

/**
* Add a change log entry to changelog.yml
*
* @param string $entry Change log entry to add.
*
* @returns string The updated version.
*/
public function addChangelog($entry)
{
$hordeInfo = $this->_getHordeInfo($this->_directory);
$changelog = Horde_Yaml::loadFile($this->_directory . self::CHANGELOG);
$version = $hordeInfo['version']['release'];
$info = $changelog[$version];
$notes = explode("\n", trim($info['notes']));
array_unshift($notes, $entry);
$info['notes'] = implode("\n", $notes) . "\n";
$changelog[$version] = $info;
file_put_contents(
$this->_directory . self::CHANGELOG,
Horde_Yaml::dump($changelog, array('wordwrap' => 0))
);
return $version;
}

/**
* Add a change log entry to CHANGES
*
Expand All @@ -309,38 +307,10 @@ public function addChange($entry, $changes)
system("mv -f $tmp $changes");
}

/**
* Updates package.xml from changelog.yml.
*
* @param Horde_Pear_Package_Xml $xml The package xml handler.
* @param string $file Path to the package.xml.
* @param array $options Additional options.
*
* @return string Path to the updated package.xml file.
*/
public function updatePackage($xml, $file, $options)
{
$changelog = $this->changelogFileExists($this->_directory);
if (!$changelog || !file_exists($file)) {
return;
}

if (empty($options['pretend'])) {
$allchanges = Horde_Yaml::loadFile($changelog);
$xml->setNotes($allchanges);
file_put_contents($file, (string)$xml);
$this->_output->ok(sprintf('Updated %s.', $file));
} else {
$this->_output->info(sprintf('Would update %s now.', $file));
}

return $file;
}

/**
* Updates CHANGES from changelog.yml.
*
* @param array $options Additional options.
* @param array $options Additional options.
*
* @return string Path to the updated CHANGES file.
*/
Expand Down Expand Up @@ -411,4 +381,40 @@ protected function _getHordeInfo()
}
return Horde_Yaml::loadFile($path);
}

/**
* Run a system call.
*
* @param string $call The system call to execute.
* @param string $target_dir Run the command in the provided target path.
* @param array $options Additional options.
*
* @return string The command output.
*/
protected function _systemInDirectory($call, $target_dir, $options)
{
$old_dir = getcwd();
chdir($target_dir);
$result = $this->_system($call, $options);
chdir($old_dir);
return $result;
}

/**
* Run a system call.
*
* @param string $call The system call to execute.
* @param array $options Additional options.
*
* @return string The command output.
*/
protected function _system($call, $options)
{
if (empty($options['pretend'])) {
//@todo Error handling
return exec($call);
} else {
$this->_output->info(sprintf('Would run "%s" now.', $call));
}
}
}

0 comments on commit 1fddbd6

Please sign in to comment.