Last active
August 29, 2015 14:06
-
-
Save nojacko/9070673dcd47543a7947 to your computer and use it in GitHub Desktop.
Composer: Copy one project to vendor folder of another
This file contains 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 copyToVendor.php project_one project_two | |
Will copy project_one's source and put it into project_two's vendor folder under the namespace/package from composer.json |
This file contains 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 | |
echo PHP_EOL; | |
if (count($argv) == 1) { | |
echo 'Usage: <source_folder> <destination_folder>' . PHP_EOL . PHP_EOL; | |
exit; | |
} | |
$ds = DIRECTORY_SEPARATOR; | |
$base = dirname(__FILE__) . $ds; | |
$from = isset($argv[1]) ? $argv[1] : null; | |
$to = isset($argv[2]) ? $argv[2] : null; | |
if ($from && $to) { | |
if ($json = @file_get_contents($from . '/composer.json')) { | |
$config = json_decode($json, true); | |
$name = isset($config['name']) ? $config['name'] : null; | |
if ($name) { | |
list($namespace, $package) = explode('/', $name); | |
if ($namespace && $package) { | |
echo 'Package: ' . $namespace . '/' . $package . PHP_EOL; | |
$source = realpath($base . $ds . $from . $ds); | |
$destionationProject = trim(realpath($base . $ds . $to) . $ds); | |
$dest = trim($destionationProject . 'vendor' . $ds . $namespace . $ds . $package); | |
if ($source && $dest) { | |
echo ' - ' . $source . PHP_EOL; | |
echo ' > ' . $dest . PHP_EOL; | |
if (is_file($destionationProject . 'composer.json')) { | |
// Dest cannot have spaces and must be /Users/*/*/*/.... | |
if (preg_match('/^\/Users\/([^ ]*\/){3,}/', $dest)) { | |
exec('rm -rf ' . $dest ); | |
mkdir($dest); | |
$iterator = new RecursiveIteratorIterator( | |
new RecursiveDirectoryIterator( | |
$source, | |
RecursiveDirectoryIterator::SKIP_DOTS | |
), | |
RecursiveIteratorIterator::SELF_FIRST | |
); | |
foreach ($iterator as $item) { | |
// Skip vendor | |
if ($item->isDir() && $item->getFilename() == 'vendor') { | |
continue; | |
} else if (substr($iterator->getSubPathName(), 0, 7) == 'vendor/'){ | |
continue; | |
} else if (substr($iterator->getSubPathName(), 0, 4) == '.git'){ | |
continue; | |
} | |
if ($item->isDir()) { | |
mkdir($dest . $ds . $iterator->getSubPathName()); | |
} else { | |
copy($item, $dest . $ds . $iterator->getSubPathName());; | |
} | |
} | |
} else { | |
echo 'Error: Destination looks too risky to rm -rf on'; | |
} | |
} else { | |
echo 'Error: Could not find composer.json in destination project. ' . $destionationProject; | |
} | |
} else { | |
echo 'Error: Could not locate source or destination'; | |
} | |
} else { | |
echo 'Error: Could not get namespace / package'; | |
} | |
} else { | |
echo 'Error: Could not get name form composer.json'; | |
} | |
} else { | |
echo 'Error: Could not load or parse ' . $from . '/composer.json'; | |
} | |
} else { | |
echo 'Error: Source and destination must be given.'; | |
} | |
echo PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment