From f817abb4d25bb41009f0f7233dc542c62bda801a Mon Sep 17 00:00:00 2001 From: Claus Due Date: Sat, 12 Apr 2014 13:48:26 +0200 Subject: [PATCH 1/2] Feature: Plugin.CopyBuild wipe instruction When set to `true`, this added `wipe` property will completely remove the target directory contents (except hidden files) before copying the build files into it. The instruction is only allowed if `directory` is **not** `/` as a tiny and very inadequate measure against unintentional destruction of the host system. --- PHPCI/Plugin/CopyBuild.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/PHPCI/Plugin/CopyBuild.php b/PHPCI/Plugin/CopyBuild.php index ca7dae76..68ec91d4 100644 --- a/PHPCI/Plugin/CopyBuild.php +++ b/PHPCI/Plugin/CopyBuild.php @@ -22,6 +22,7 @@ class CopyBuild implements \PHPCI\Plugin { protected $directory; protected $ignore; + protected $wipe; protected $phpci; protected $build; @@ -31,6 +32,7 @@ class CopyBuild implements \PHPCI\Plugin $this->phpci = $phpci; $this->build = $build; $this->directory = isset($options['directory']) ? $options['directory'] : $path; + $this->wipe = isset($options['wipe']) ? (bool)$options['wipe'] : false; $this->ignore = isset($options['respect_ignore']) ? (bool)$options['respect_ignore'] : false; } @@ -45,10 +47,19 @@ class CopyBuild implements \PHPCI\Plugin return false; } + if ($this->wipe == true && $this->directory != '/' && is_dir($this->directory)) { + $cmd = 'rm -Rf "%s*"'; + $success = $this->phpci->executeCommand($cmd, $this->directory); + if (!$success) { + throw new \Exception('Failed to wipe existing directory ' . $this->directory . ' before copy'); + } + } + $cmd = 'mkdir -p "%s" && cp -R "%s" "%s"'; if (IS_WIN) { $cmd = 'mkdir -p "%s" && xcopy /E "%s" "%s"'; } + $success = $this->phpci->executeCommand($cmd, $this->directory, $build, $this->directory); if ($this->ignore) { From 27e5dcc48037e2d9fe0b8bd2379b20445e4dfb83 Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Thu, 15 May 2014 13:39:54 +0100 Subject: [PATCH 2/2] Update for PR #346 to fix PHPMD error --- PHPCI/Plugin/CopyBuild.php | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/PHPCI/Plugin/CopyBuild.php b/PHPCI/Plugin/CopyBuild.php index 68ec91d4..9be32395 100644 --- a/PHPCI/Plugin/CopyBuild.php +++ b/PHPCI/Plugin/CopyBuild.php @@ -47,13 +47,7 @@ class CopyBuild implements \PHPCI\Plugin return false; } - if ($this->wipe == true && $this->directory != '/' && is_dir($this->directory)) { - $cmd = 'rm -Rf "%s*"'; - $success = $this->phpci->executeCommand($cmd, $this->directory); - if (!$success) { - throw new \Exception('Failed to wipe existing directory ' . $this->directory . ' before copy'); - } - } + $this->wipeExistingDirectory(); $cmd = 'mkdir -p "%s" && cp -R "%s" "%s"'; if (IS_WIN) { @@ -62,6 +56,24 @@ class CopyBuild implements \PHPCI\Plugin $success = $this->phpci->executeCommand($cmd, $this->directory, $build, $this->directory); + $this->deleteIgnoredFiles(); + + return $success; + } + + protected function wipeExistingDirectory() + { + if ($this->wipe == true && $this->directory != '/' && is_dir($this->directory)) { + $cmd = 'rm -Rf "%s*"'; + $success = $this->phpci->executeCommand($cmd, $this->directory); + if (!$success) { + throw new \Exception('Failed to wipe existing directory ' . $this->directory . ' before copy'); + } + } + } + + protected function deleteIgnoredFiles() + { if ($this->ignore) { foreach ($this->phpci->ignore as $file) { $cmd = 'rm -Rf "%s/%s"'; @@ -71,7 +83,5 @@ class CopyBuild implements \PHPCI\Plugin $this->phpci->executeCommand($cmd, $this->directory, $file); } } - - return $success; } }