From a7c5157437608a335fc67d5abdbe9bb740c6494b Mon Sep 17 00:00:00 2001 From: Jonathan Hedstrom Date: Sat, 4 Feb 2017 13:21:35 -0800 Subject: [PATCH] Skip modules that already have version info. - Fixes #3. --- src/Writer/Drupal.php | 29 ++++++++++++++++++++++++++--- src/Writer/Drupal7.php | 4 ++++ tests/DrupalInfoTest.php | 5 +++++ tests/InfoFileTrait.php | 6 ++++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/Writer/Drupal.php b/src/Writer/Drupal.php index f594afc..7caf7c8 100644 --- a/src/Writer/Drupal.php +++ b/src/Writer/Drupal.php @@ -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. * @@ -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); + } } } @@ -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); + } } diff --git a/src/Writer/Drupal7.php b/src/Writer/Drupal7.php index 9861e5c..d4c305e 100644 --- a/src/Writer/Drupal7.php +++ b/src/Writer/Drupal7.php @@ -7,6 +7,10 @@ */ class Drupal7 extends Drupal { + /** + * {@inheritdoc} + */ + const VERSION_EXISTS_PATTERN = '#version=.*[\d+].*#'; /** * Format version and timestamp into INI format. diff --git a/tests/DrupalInfoTest.php b/tests/DrupalInfoTest.php index 1e66f35..ea8bbff 100644 --- a/tests/DrupalInfoTest.php +++ b/tests/DrupalInfoTest.php @@ -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)); } /** diff --git a/tests/InfoFileTrait.php b/tests/InfoFileTrait.php index e7b8854..eb8e177 100644 --- a/tests/InfoFileTrait.php +++ b/tests/InfoFileTrait.php @@ -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' => [ @@ -48,6 +51,9 @@ public function generateDirectories() ], ], ], + 'module_with_version' => [ + 'module_with_version.info' => "name = module with version\nversion = 8.x-1.0-alpha4", + ], ], ]);