Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip modules that already have version info. #4

Merged
merged 1 commit into from Feb 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/Writer/Drupal.php
Expand Up @@ -7,6 +7,11 @@
*/
class Drupal implements WriterInterface
{
/**
* Pattern to indicate a file already has version info.
*/
const VERSION_EXISTS_PATTERN = '#version:.*[\d+].*#';

/**
* File paths to rewrite.
*
Expand All @@ -28,9 +33,12 @@ public function set(array $paths)
public function rewrite($version, $timestamp)
{
foreach ($this->paths as $info_file) {
$file = fopen($info_file, 'a+');
fwrite($file, $this->formatInfo($version, $timestamp));
fclose($file);
// Don't write to files that already contain version information.
if (!$this->hasVersionInfo($info_file)) {
$file = fopen($info_file, 'a+');
fwrite($file, $this->formatInfo($version, $timestamp));
fclose($file);
}
}
}

Expand Down Expand Up @@ -61,4 +69,19 @@ protected function formatInfo($version, $timestamp)
EOL;
return $info;
}

/**
* Determine if a given file already contains version info.
*
* @param string $file_path
* Path to the info file.
*
* @return bool
* Returns true if file already has version info.
*/
protected function hasVersionInfo($file_path)
{
$contents = file_get_contents($file_path);
return preg_match(static::VERSION_EXISTS_PATTERN, $contents);
}
}
4 changes: 4 additions & 0 deletions src/Writer/Drupal7.php
Expand Up @@ -7,6 +7,10 @@
*/
class Drupal7 extends Drupal
{
/**
* {@inheritdoc}
*/
const VERSION_EXISTS_PATTERN = '#version=.*[\d+].*#';

/**
* Format version and timestamp into INI format.
Expand Down
5 changes: 5 additions & 0 deletions tests/DrupalInfoTest.php
Expand Up @@ -119,6 +119,11 @@ public function testInstallOperationWriteInfoFiles()
$this->assertFileExists($file);
$this->assertContains($info, file_get_contents($file));
}

// Verify that module with existing version information is not updated.
$file = $this->getDirectory() . '/module_with_version/module_with_version.info.yml';
$this->assertFileExists($file);
$this->assertNotContains($info, file_get_contents($file));
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/InfoFileTrait.php
Expand Up @@ -31,6 +31,9 @@ public function generateDirectories()
],
],
],
'module_with_version' => [
'module_with_version.info.yml' => "name: module with version\nversion:8.x-1.0-alpha4",
],
],
// Drupal 7.
'drupal7' => [
Expand All @@ -48,6 +51,9 @@ public function generateDirectories()
],
],
],
'module_with_version' => [
'module_with_version.info' => "name = module with version\nversion = 8.x-1.0-alpha4",
],
],

]);
Expand Down