Browse Source

Merge pull request #14173 from cakephp/3.x-i18n-extract-skip-unchanged

[3.x] Skip unchanged files when extracting translation strings
Mark Story 6 years ago
parent
commit
7713915970
1 changed files with 29 additions and 0 deletions
  1. 29 0
      src/Shell/Task/ExtractTask.php

+ 29 - 0
src/Shell/Task/ExtractTask.php

@@ -601,12 +601,14 @@ class ExtractTask extends Shell
      */
     protected function _writeFiles()
     {
+        $this->out();
         $overwriteAll = false;
         if (!empty($this->params['overwrite'])) {
             $overwriteAll = true;
         }
         foreach ($this->_storage as $domain => $sentences) {
             $output = $this->_writeHeader();
+            $headerLength = strlen($output);
             foreach ($sentences as $sentence => $header) {
                 $output .= $header . $sentence;
             }
@@ -619,6 +621,13 @@ class ExtractTask extends Shell
 
             $filename = str_replace('/', '_', $domain) . '.pot';
             $File = new File($this->_output . $filename);
+
+            if ($File->exists() && $this->_checkUnchanged($File, $headerLength, $output) === true) {
+                $this->out($filename . ' is unchanged. Skipping.');
+                $File->close();
+                continue;
+            }
+
             $response = '';
             while ($overwriteAll === false && $File->exists() && strtoupper($response) !== 'Y') {
                 $this->out();
@@ -670,6 +679,26 @@ class ExtractTask extends Shell
     }
 
     /**
+     * Check whether the old and new output are the same, thus unchanged
+     *
+     * Compares the sha1 hashes of the old and new file without header.
+     *
+     * @param File $oldFile The existing file.
+     * @param int $headerLength The length of the file header in bytes.
+     * @param string $newFileContent The content of the new file.
+     * @return bool Whether or not the old and new file are unchanged.
+     */
+    protected function _checkUnchanged(File $oldFile, $headerLength, $newFileContent)
+    {
+        $oldFileContent = $oldFile->read();
+
+        $oldChecksum = sha1(substr($oldFileContent, $headerLength));
+        $newChecksum = sha1(substr($newFileContent, $headerLength));
+
+        return $oldChecksum === $newChecksum;
+    }
+
+    /**
      * Get the strings from the position forward
      *
      * @param int $position Actual position on tokens array