Skip to content

Instantly share code, notes, and snippets.

@nojacko nojacko/Composer Copy
Last active Aug 29, 2015

Embed
What would you like to do?
Composer: Copy one project to vendor folder of another
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
<?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
You can’t perform that action at this time.