Skip to content

Instantly share code, notes, and snippets.

@thekid
Created November 10, 2013 17:13
Show Gist options
  • Select an option

  • Save thekid/7400980 to your computer and use it in GitHub Desktop.

Select an option

Save thekid/7400980 to your computer and use it in GitHub Desktop.
Execute using `xp -cp ../_tools/ MoveTests src/test/php/ net.xp_framework.unittest.remote remote.unittest`
<?php
use util\cmd\Console;
use io\streams\Streams;
use io\collections\FileCollection;
use io\collections\iterate\FilteredIOCollectionIterator;
use io\collections\iterate\ExtensionEqualsFilter;
class MoveTests extends \lang\Object {
protected static function collectionForPackage($base, $package, $create= false) {
$r= $base;
foreach (explode('.', $package) as $path) {
if (null === ($child= $r->findCollection($path))) {
if ($create) {
$child= $r->newCollection($path);
} else {
throw new \lang\IllegalArgumentException('No such collection '.$path.' in '.$r->toString());
}
}
$r= $child;
}
return $r;
}
protected static function elementForClass($base, $class) {
$p= strrpos($class, '.');
$r= self::collectionForPackage($base, substr($class, 0, $p), true);
$name= substr($class, $p + 1).\xp::CLASS_FILE_EXT;
if (null === ($child= $r->findElement($name))) {
$child= $r->newElement($name);
}
return $child;
}
public static function main(array $args) {
$dir= new FileCollection($args[0]);
$origin= $args[1];
$target= $args[2];
Console::writeLine('Converting ', $origin, ' -> ', $target, ' in ', $dir);
$in= self::collectionForPackage($dir, $origin);
$out= self::collectionForPackage($dir, $target, true);
// Work on files
foreach (new FilteredIOCollectionIterator($in, new ExtensionEqualsFilter(\xp::CLASS_FILE_EXT), true) as $source) {
$old= str_replace(DIRECTORY_SEPARATOR, '.', substr(
$source->getURI(),
strlen($dir->getURI()),
-strlen(\xp::CLASS_FILE_EXT)
));
$new= str_replace($origin, $target, $old);
Console::writeLine($old, ' -> ', $new);
$file= self::elementForClass($dir, $new);
$stream= $file->getOutputStream();
$stream->write(preg_replace(
[
'/namespace '.str_replace('.', '\\\\', $origin).';/',
'/\\\\'.str_replace('.', '\\\\', $origin).'/',
'/([\'"])'.$origin.'/'
],
[
'namespace '.str_replace('.', '\\', $target).';',
'\\\\'.str_replace('.', '\\\\', $target),
'$1'.$target
],
Streams::readAll($source->getInputStream())
));
$stream->close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment