diff --git a/setup/cli/modules/deploy.php b/setup/cli/modules/deploy.php index 8ba0c0fafbf9740957072d12ea803efb9e2c6a16..97d3e1abb7b02f4bd3c51284f0d128d88aacd7bb 100644 --- a/setup/cli/modules/deploy.php +++ b/setup/cli/modules/deploy.php @@ -20,6 +20,10 @@ class Deployment extends Unpacker { 'action'=>'store_true', 'help'=>'Deploy the setup folder. Useful for deploying for new installations.'); + $this->options['clean'] = array('-C','--clean', + 'action'=>'store_true', + 'help'=>'Remove files from the destination that are no longer + included in this repository'); # super(*args); call_user_func_array(array('parent', '__construct'), func_get_args()); } @@ -34,6 +38,55 @@ class Deployment extends Unpacker { return realpath($start); } + /** + * Removes files from the deployment location that no longer exist in + * the local repository + */ + function clean($local, $destination, $recurse=0, $exclude=false) { + $dryrun = $this->getOption('dry-run', false); + $verbose = $dryrun || $this->getOption('verbose'); + $destination = rtrim($destination, '/') . '/'; + $contents = glob($destination.'{,.}*', GLOB_BRACE|GLOB_NOSORT); + foreach ($contents as $i=>$file) { + if ($this->exclude($exclude, $file)) + continue; + if (is_file($file)) { + $ltarget = $local . '/' . basename($file); + if (is_file($ltarget)) + continue; + if ($verbose) + $this->stdout->write("(delete): $file\n"); + if (!$dryrun) + unlink($file); + unset($contents[$i]); + } + elseif (in_array(basename($file), array('.','..'))) { + // Doesn't indicate that the folder has contents + unset($contents[$i]); + } + } + if ($recurse) { + $folders = glob(dirname($destination).'/'.basename($destination).'/*', + GLOB_BRACE|GLOB_ONLYDIR|GLOB_NOSORT); + foreach ($folders as $dir) { + if (in_array(basename($dir), array('.','..'))) + continue; + elseif ($this->exclude($exclude, $dir)) + continue; + $this->clean( + $local.'/'.basename($dir), + $destination.basename($dir), + $recurse - 1, $exclude); + } + } + if (!$contents || !glob($destination.'{,.}*', GLOB_BRACE|GLOB_NOSORT)) { + if ($verbose) + $this->stdout->write("(delete-folder): $destination\n"); + if (!$dryrun) + rmdir($destination); + } + } + function run($args, $options) { $this->destination = $args['install-path']; if (!is_dir($this->destination)) @@ -69,6 +122,13 @@ class Deployment extends Unpacker { if (!$options['dry-run'] && $include != "{$this->destination}/include") $this->change_include_dir($include); + + if ($options['clean']) { + // Clean everything but include folder first + $this->clean($root, $this->destination, -1, + array($include, "setup/")); + $this->clean("$root/include", $include, -1); + } } }