Skip to content

Commit

Permalink
Merge pull request #4 from drupal-composer/03-version-info
Browse files Browse the repository at this point in the history
Skip modules that already have version info.
  • Loading branch information
jhedstrom authored Feb 4, 2017
2 parents 5340d94 + a7c5157 commit b566d66
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/Writer/Drupal.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit b566d66

Please sign in to comment.