Last active
October 1, 2016 19:32
-
-
Save bulton-fr/4b1930569aeb6d59dfbdf49366d1c5af to your computer and use it in GitHub Desktop.
Script to migrate a BFW application from 2.2 to 3.0
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
#!/bin/php | |
<?php | |
/** | |
* Convert directories structure from BFW 2.2 to BFW 3.0 | |
* | |
* @project BFW | |
* @author bulton-fr <[email protected]> | |
*/ | |
function confirmPath($path, $pathName, $argumentName) | |
{ | |
global $inputHandler; | |
echo $pathName.' path is '.$path.' ? [y/N] : '; | |
$pathConfirmed = false; | |
$pathUserConfirm = trim(fgets($inputHandler)); | |
if ($pathUserConfirm === 'y' || $pathUserConfirm === 'Y') { | |
$pathConfirmed = true; | |
} | |
if ($pathConfirmed === false) { | |
echo "\033[1;31m".$pathName." not confirmed. Script exit.\033[0m\n"; | |
echo 'You can declare the path with the argument '.$argumentName."\n"; | |
exit(1); | |
} | |
} | |
function execCmd($cmd) | |
{ | |
$execOutput = []; | |
$execStatus = 0; | |
exec($cmd, $execOutput, $execStatus); | |
if ($execStatus !== 0) { | |
echo "\033[1;31mFail\033[0m\n"; | |
echo "The command has been runned is : ".$cmd."\n"; | |
echo "The return is :\n".implode("\n", $execOutput)."\n"; | |
exit(1); | |
} | |
} | |
$inputHandler = fopen('php://stdin', 'r'); | |
$cliOpt = getopt('h::', ['root-path::', 'backup-path::', 'help::']); | |
$rootPath = __DIR__; | |
$backupPath = __DIR__.'/../'; | |
if (isset($cliOpt['h']) || isset($cliOpt['help'])) { | |
echo 'This script is for migrate directories structure from a bfw application in v2.2 to v3.0'."\n"; | |
echo '[HELP COMMAND]'."\n"; | |
echo ' -h --help : Display this message.'."\n"; | |
echo ' --root-path: Define the root path to the application'."\n"; | |
echo ' --backup-path: Path where is save the application backup.'."\n"; | |
exit; | |
} | |
if (isset($cliOpt['root-path'])) { | |
$rootPath = $cliOpt['root-path']; | |
} | |
if (isset($cliOpt['backup-path'])) { | |
$backupPath = $cliOpt['backup-path']; | |
} | |
confirmPath($rootPath, 'Application root', '--root-path'); | |
confirmPath($backupPath, 'Backup root', '--backup-path'); | |
//All path confirmed :) | |
echo "\033[1;33mAll path confirmed. Script continue.\033[0m\n"; | |
$rootPath = realpath($rootPath); | |
$backupPath = realpath($backupPath); | |
//Check paths exists | |
echo "Check root path exists... "; | |
if (!file_exists($rootPath)) { | |
echo "\033[1;31mFail\033[0m\n"; | |
exit(1); | |
} | |
echo "\033[1;32mOK\033[0m\n"; | |
echo "Check backup path exists... "; | |
if (!file_exists($backupPath)) { | |
echo "\033[1;31mFail\033[0m\n"; | |
exit(1); | |
} | |
echo "\033[1;32mOK\033[0m\n"; | |
//Continue with create a backup of all directories. | |
echo "Create backup..."; | |
$backupTime = date('Y-m-d_H-i-s'); | |
execCmd('cd '.$rootPath.' && tar zcvf '.$backupPath.'/app-'.$backupTime.'.tar.gz ./ 2>&1'); | |
echo "\033[1;32mOK\033[0m\n"; | |
//Create directories structure | |
echo "Create all directories..."; | |
execCmd('cd '.$rootPath.' && mkdir -p app/config/bfw 2>&1'); | |
execCmd('cd '.$rootPath.' && mkdir -p app/modules 2>&1'); | |
execCmd('cd '.$rootPath.' && mkdir -p src/cli 2>&1'); | |
execCmd('cd '.$rootPath.' && mkdir -p src/controllers 2>&1'); | |
execCmd('cd '.$rootPath.' && mkdir -p src/modeles 2>&1'); | |
execCmd('cd '.$rootPath.' && mkdir -p src/view 2>&1'); | |
echo "\033[1;32mOK\033[0m\n"; | |
//Move directories and files | |
echo "Move directories and files..."; | |
execCmd('cd '.$rootPath.' && mv configs/* app/config/ 2>&1'); | |
execCmd('cd '.$rootPath.' && mv app/config/bfw_config.php app/config/bfw/config.php 2>&1'); | |
execCmd('cd '.$rootPath.' && mv modules/* app/modules/ 2>&1'); | |
execCmd('cd '.$rootPath.' && mv cli/* src/cli/ 2>&1'); | |
execCmd('cd '.$rootPath.' && mv controllers/* src/controllers/ 2>&1'); | |
execCmd('cd '.$rootPath.' && mv modeles/* src/modeles/ 2>&1'); | |
execCmd('cd '.$rootPath.' && mv view/* src/view/ 2>&1'); | |
echo "\033[1;32mOK\033[0m\n"; | |
//Remove old directories | |
echo "Remove v2.2 directories..."; | |
execCmd('cd '.$rootPath.' && rm -r configs 2>&1'); | |
execCmd('cd '.$rootPath.' && rm -r modules 2>&1'); | |
execCmd('cd '.$rootPath.' && rm -r cli 2>&1'); | |
execCmd('cd '.$rootPath.' && rm -r controllers 2>&1'); | |
execCmd('cd '.$rootPath.' && rm -r modeles 2>&1'); | |
execCmd('cd '.$rootPath.' && rm -r view 2>&1'); | |
echo "\033[1;32mOK\033[0m\n"; | |
//Done :) | |
echo "\033[1;33mScript terminated.\033[0m\n"; | |
echo "\033[1;33mYou should to update your composer dependency to bfw v3.0 and upgrade the config file (in /app/config/bfw/config.php).\033[0m\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment