Created
July 4, 2011 23:19
-
-
Save hiromi2424/1064049 to your computer and use it in GitHub Desktop.
quick upgrade plugins or app
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 | |
// Usage | |
// php upgrade.php on app dir or plugin root | |
class App { | |
public static function __callStatic($method, $params) { | |
} | |
} | |
$base = '/path/to/cake/'; | |
require_once Upgrade::denormalizePath($base . 'lib/Cake/Utility/Inflector.php'); | |
require_once Upgrade::denormalizePath($base . 'lib/Cake/Utility/String.php'); | |
require_once Upgrade::denormalizePath($base . 'lib/Cake/Utility/Set.php'); | |
Upgrade::init(dirname(__FILE__) . DIRECTORY_SEPARATOR); | |
Upgrade::remove('tests/files'); | |
Upgrade::remove('tests/groups'); | |
Upgrade::convert(array( | |
'tests' => array( | |
'Test', | |
'cases' => 'Case', | |
'fixtures' => 'Fixture', | |
), | |
)); | |
Upgrade::convert(array( | |
'controllers' => array( | |
'Controller', | |
'components' => 'Component', | |
), | |
'models' => array( | |
'Model', | |
'behaviors' => 'Behavior', | |
), | |
'views' => array( | |
'View', | |
'helpers' => 'Helper', | |
), | |
'libs' => array( | |
'Lib', | |
'route' => 'Routing/Route', | |
'test' => 'TestSuite', | |
), | |
), array( | |
'test' => true, | |
)); | |
Upgrade::ucfirst('locale'); | |
Upgrade::move('vendors', 'Console'); | |
Upgrade::move('Console/shells/templates', 'Console/Templates'); | |
Upgrade::inflect('Test/Fixture'); | |
Upgrade::inflect('Model'); | |
Upgrade::inflect('Controller/Component', 'Component'); | |
Upgrade::inflectTest('Test/Case/Controller/Component', 'Component'); | |
Upgrade::inflect('Model/Behavior', 'Behavior'); | |
Upgrade::inflectTest('Test/Case/Model/Behavior', 'Behavior'); | |
Upgrade::inflect('View/Helper', 'Helper'); | |
Upgrade::inflectTest('Test/Case/View/Helper', 'Helper'); | |
Upgrade::inflectRecursive('Lib'); | |
Upgrade::inflectTestRecursive('Test/Case/Lib'); | |
class Upgrade { | |
public static $baseDir; | |
public static function init($baseDir) { | |
self::$baseDir = dirname(__FILE__) . DIRECTORY_SEPARATOR; | |
} | |
public static function convert($paths, $options = array()) { | |
$options += array( | |
'test' => false, | |
'inflect' => false, | |
); | |
$map = array(); | |
foreach (Set::normalize($paths) as $from => $to) { | |
if (empty($to)) { | |
self::ucFirst($from); | |
} else { | |
$destination = false; | |
$subDirs = array(); | |
foreach ((array)$to as $key => $value) { | |
if (is_numeric($key)) { | |
$destination = $value; | |
} else { | |
$subDirs[$key] = $value; | |
} | |
} | |
if (!empty($destination)) { | |
$map[$from] = $destination; | |
$from = $destination; | |
} | |
if (!empty($subDirs)) { | |
foreach ($subDirs as $subFrom => $subTo) { | |
$subFrom = $from . '/' . $subFrom; | |
$subTo = $from . '/' . $subTo; | |
$map[$subFrom] = $subTo; | |
} | |
} | |
} | |
} | |
foreach ($map as $from => $to) { | |
self::move($from, $to); | |
if ($options['test']) { | |
self::move('Test/Case/' . $from, 'Test/Case/' . $to); | |
} | |
} | |
} | |
public static function ucFirst($path, $lastPath = true) { | |
if ($lastPath) { | |
$parts = explode('/', $path); | |
$last = array_pop($parts); | |
array_push($parts, ucfirst($last)); | |
$to = implode('/', $parts); | |
} else { | |
$to = ucfirst($path); | |
} | |
$tmp = '___' . $to; | |
self::move($path, $tmp); | |
self::move($tmp, $to); | |
} | |
public static function inflectRecursive($path, $suffix = '') { | |
$files = self::_recursiveFiles($path, '*'); | |
if (empty($files)) { | |
return false; | |
} | |
self::_inflect($files, $suffix); | |
} | |
public static function inflect($path, $suffix = '') { | |
$files = self::_files($path); | |
if (empty($files)) { | |
return false; | |
} | |
self::_inflect($files, $suffix); | |
} | |
protected static function _inflect($files, $suffix, $callback = 'self::_inflectCommon') { | |
foreach ($files as $file) { | |
if (is_dir($file)) { | |
continue; | |
} | |
$from = self::normalizePath(str_replace(self::$baseDir, '', self::denormalizePath($file))); | |
$to = str_replace(self::$baseDir, '', call_user_func($callback, $file, $suffix)); | |
self::move($from, $to); | |
} | |
} | |
protected static function _inflectCommon($file, $suffix) { | |
$dirName = dirname($file); | |
$fileName = basename($file); | |
list($fileName, $ext) = explode('.', $fileName, 2); | |
$fileName = Inflector::camelize($fileName) . $suffix; | |
$file = $dirName . DIRECTORY_SEPARATOR . $fileName . '.' . $ext; | |
return $file; | |
} | |
protected static function _inflectTest($file, $suffix) { | |
$dirName = dirname($file); | |
$fileName = basename($file); | |
list($fileName, $testExt, $ext) = explode('.', $fileName, 3); | |
$fileName = Inflector::camelize($fileName) . $suffix; | |
$file = $dirName . DIRECTORY_SEPARATOR . $fileName . '.' . $ext; | |
return $file; | |
} | |
public static function inflectTestRecursive($path, $suffix = '') { | |
$files = self::_recursiveFiles($path, '*'); | |
if (empty($files)) { | |
return false; | |
} | |
self::_inflect($files, $suffix . 'Test', 'self::_inflectTest'); | |
} | |
public static function inflectTest($path, $suffix = '') { | |
$files = self::_files($path); | |
if (empty($files)) { | |
return false; | |
} | |
self::_inflect($files, $suffix . 'Test', 'self::_inflectTest'); | |
} | |
protected static function _files($path) { | |
$fullPath = rtrim(self::denormalizePath(self::$baseDir . $path), '/'); | |
$dh = @opendir($fullPath); | |
if (!$dh) { | |
return false; | |
} | |
$files = array(); | |
while (($file = readdir($dh)) !== false) { | |
$fullFileName = $fullPath . DIRECTORY_SEPARATOR . $file; | |
if (is_dir($fullFileName) || !preg_match('/\.(php)$/', $file)) { | |
continue; | |
} | |
$files[] = $fullFileName; | |
} | |
closedir($dh); | |
return $files; | |
} | |
protected static function _recursiveFiles($path, $pattern = '*') { | |
$path = str_replace(array('//', DIRECTORY_SEPARATOR . '/'), '/', $path . '/'); | |
$path = str_replace(self::$baseDir, '', self::denormalizePath($path)); | |
$files = glob(self::$baseDir . $path . $pattern); | |
$subDirFiles = array(); | |
foreach ($files as $i => $file) { | |
if (is_dir($file)) { | |
$subDirFiles = array_merge($subDirFiles, self::_recursiveFiles($file, $pattern)); | |
unset($files[$i]); | |
} | |
} | |
$files = array_merge($files, $subDirFiles); | |
return $files; | |
} | |
public static function move($from, $to) { | |
if (!file_exists(self::denormalizePath(self::$baseDir . $from))) { | |
return false; | |
} | |
$realPath = self::denormalizePath(self::$baseDir . $to); | |
if (!file_exists($realPath)) { | |
$parentDir = dirname($realPath); | |
if (!file_exists($parentDir)) { | |
mkdir($parentDir); | |
} | |
} | |
$from = rtrim(self::normalizePath($from), '/'); | |
$to = rtrim(self::normalizePath($to), '/'); | |
return self::exec('git mv', array($from, $to)); | |
} | |
public static function remove($path) { | |
if (!file_exists(self::denormalizePath(self::$baseDir . $path))) { | |
return false; | |
} | |
return self::exec('git rm -r', $path); | |
} | |
public static function exec($command, $arguments) { | |
$option = new Option; | |
foreach ((array)$arguments as $key => $argument) { | |
if (is_numeric($key)) { | |
$option->add($argument); | |
} else { | |
$option->addParam($key, $argument); | |
} | |
} | |
$command = $command . $option->get(); | |
echo $command, PHP_EOL; | |
return exec($command); | |
} | |
public static function normalizePath($path) { | |
$path = str_replace(DIRECTORY_SEPARATOR, '/', $path); | |
return $path; | |
} | |
public static function denormalizePath($path) { | |
$path = str_replace('/', DIRECTORY_SEPARATOR, $path); | |
return $path; | |
} | |
} | |
class Option { | |
protected $_option = ''; | |
public function add($arg) { | |
$this->_option .= " $arg"; | |
} | |
public function addParam($key, $value) { | |
$this->_option .= " $key"; | |
$this->_option .= preg_match('/ /', $value) ? '"' . $value . '"' : $value; | |
} | |
public function get() { | |
return $this->_option; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yes, for helping migration app or plugin. this is a junk code though, your suggestion would be right. I will create a repository. thanks :)