Created
July 25, 2017 09:05
-
-
Save killpond/1ea6abb9b77a07875f778321e0d41d58 to your computer and use it in GitHub Desktop.
Create an upgrade patch (Including binary additions/deleations) for Magento
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function display_usage() { | |
echo <<< EOF | |
Usage : create_patch.php [-f|--from] [-t|--to] | |
-f : From Magento version (e.g. ce_1.9.0.1) | |
-t : To Magento version (e.g. ee_1.14.1.0) | |
--from : alias of -f | |
--to : alias of -t | |
EOF; | |
die(); | |
} | |
$shortOpts = ''; | |
$shortOpts .= 'f:'; // From Magento Version | |
$shortOpts .= 't:'; // To Magento Version | |
$longOpts = array( | |
'from:', // From Magento Version | |
'to:', // To Magento Version | |
); | |
$options = getopt($shortOpts, $longOpts); | |
if (!isset($options['from']) && !isset($options['f'])) { | |
display_usage(); | |
} | |
if (!isset($options['to']) && !isset($options['t'])) { | |
display_usage(); | |
} | |
$from = ((isset($options['from'])) ? $options['from'] : $options['f']); | |
$to = ((isset($options['to'])) ? $options['to'] : $options['t']); | |
if (!is_dir('../'.$from)) { | |
echo 'Fatal Error : "From" Magento directory does not exist.'.PHP_EOL; | |
display_usage(); | |
} | |
if (!is_dir('../'.$to)) { | |
echo 'Fatal Error : "To" Magento directory does not exist.'.PHP_EOL; | |
display_usage(); | |
} | |
$workingDir = realpath(__DIR__); | |
@mkdir($from.'_to_'.$to); | |
$upgradeDir = realpath($from.'_to_'.$to); | |
$fromPath = realpath('../'.$from); | |
$toPath = realpath('../'.$to); | |
$patchFilename = $from.'_to_'.$to.'.patch'; | |
// Create diff patch | |
chdir('../'); | |
$cmd = 'diff -rupN '.escapeshellarg($from).' '.escapeshellarg($to).' > '.escapeshellarg($upgradeDir.'/'.$patchFilename); | |
exec($cmd); | |
// Find all binary file changes | |
$binFiles = array( | |
'modified' => array(), | |
'created' => array(), | |
'deleted' => array(), | |
); | |
$fp = fopen($upgradeDir.'/'.$patchFilename, 'r'); | |
while ($line = fgets($fp)) { | |
if (strpos($line, 'Binary files ') === 0) { | |
$files = preg_replace('/^Binary files '.preg_quote($from).'\/(.*) and '.preg_quote($to).'\/(.*) differ\n$/si', '$1|$2', $line); | |
$files = explode('|', $files); | |
if (file_exists($from.'/'.$files[0]) && file_exists($to.'/'.$files[1])) { | |
// Modified | |
$binFiles['modified'][] = $files[0]; | |
} elseif (!file_exists($to.'/'.$files[0])) { | |
// Deleted | |
$binFiles['deleted'][] = $files[1]; | |
} else { | |
// Created | |
$binFiles['created'][] = $files[0]; | |
} | |
} | |
} | |
chdir($workingDir); | |
// Create tar of all binary files created or modified | |
$binaryFileAdditionsFileName = $from.'_to_'.$to.'_bin_additions.tgz'; | |
$additionalFiles = array_merge($binFiles['modified'], $binFiles['created']); | |
if (count($additionalFiles)) { | |
chdir($toPath); | |
$cmd = 'tar -czf '.escapeshellarg($upgradeDir.'/'.$binaryFileAdditionsFileName).' '.implode(' ', $additionalFiles); | |
exec($cmd); | |
} | |
// Create script for all files deleted | |
$binaryFileRemovalFileName = $from.'_to_'.$to.'_bin_removal.sh'; | |
if (count($binFiles['deleted'])) { | |
file_put_contents($upgradeDir.'/'.$binaryFileRemovalFileName, '#!/bin/bash'.PHP_EOL); | |
foreach ($binFiles['deleted'] as $file) { | |
file_put_contents($upgradeDir.'/'.$binaryFileRemovalFileName, 'rm -f '.escapeshellarg($file).PHP_EOL, FILE_APPEND); | |
} | |
chmod($upgradeDir.'/'.$binaryFileRemovalFileName, 0777); | |
} | |
// Create a archive of all the files. | |
chdir($upgradeDir); | |
$files = array( | |
'../'.$from.'_to_'.$to.'.tgz' // TAR filename | |
); | |
if (file_exists($patchFilename)) {$files[] = $patchFilename;} | |
if (file_exists($binaryFileRemovalFileName)) {$files[] = $binaryFileRemovalFileName;} | |
if (file_exists($binaryFileAdditionsFileName)) {$files[] = $binaryFileAdditionsFileName;} | |
$files = array_map('escapeshellarg', $files); | |
$cmd = 'tar -czf '.implode(' ', $files); | |
exec($cmd); | |
@unlink($binaryFileRemovalFileName); | |
@unlink($binaryFileAdditionsFileName); | |
@unlink($patchFilename); | |
chdir('../'); | |
rmdir($upgradeDir); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment