Skip to content

Commit

Permalink
DirSet implementation for various tasks. (phingofficial#678)
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 authored and mrook committed Mar 24, 2017
1 parent 9933c07 commit 21bbaec
Show file tree
Hide file tree
Showing 19 changed files with 290 additions and 53 deletions.
11 changes: 9 additions & 2 deletions classes/phing/tasks/system/ChmodTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

require_once 'phing/Task.php';
include_once 'phing/types/FileSet.php';
include_once 'phing/types/DirSetAware.php';

/**
* Task that changes the permissions on a file/directory.
Expand All @@ -32,6 +33,8 @@
*/
class ChmodTask extends Task
{
use DirSetAware;

private $file;

private $mode;
Expand Down Expand Up @@ -123,8 +126,10 @@ public function main()
*/
private function checkParams()
{
if ($this->file === null && empty($this->filesets)) {
throw new BuildException("Specify at least one source - a file or a fileset.");
if ($this->file === null && empty($this->filesets) && empty($this->dirsets)) {
throw new BuildException(
"Specify at least one source - a file, dirset or a fileset."
);
}

if ($this->mode === null) {
Expand Down Expand Up @@ -160,6 +165,8 @@ private function chmod()
$this->chmodFile($this->file, $mode);
}

$this->filesets = array_merge($this->filesets, $this->dirsets);

// filesets
foreach ($this->filesets as $fs) {
$ds = $fs->getDirectoryScanner($this->project);
Expand Down
7 changes: 6 additions & 1 deletion classes/phing/tasks/system/ChownTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

require_once 'phing/Task.php';
include_once 'phing/types/FileSet.php';
include_once 'phing/types/DirSetAware.php';

/**
* Task that changes the permissions on a file/directory.
Expand All @@ -31,6 +32,8 @@
*/
class ChownTask extends Task
{
use DirSetAware;

private $file;

private $user;
Expand Down Expand Up @@ -131,7 +134,7 @@ public function main()
*/
private function checkParams()
{
if ($this->file === null && empty($this->filesets)) {
if ($this->file === null && empty($this->filesets) && empty($this->dirsets)) {
throw new BuildException("Specify at least one source - a file or a fileset.");
}

Expand Down Expand Up @@ -166,6 +169,8 @@ private function chown()
$this->chownFile($this->file, $user, $group);
}

$this->filesets = array_merge($this->filesets, $this->dirsets);

// filesets
foreach ($this->filesets as $fs) {
$ds = $fs->getDirectoryScanner($this->project);
Expand Down
37 changes: 33 additions & 4 deletions classes/phing/tasks/system/CopyTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
include_once 'phing/util/SourceFileScanner.php';
include_once 'phing/mappers/IdentityMapper.php';
include_once 'phing/mappers/FlattenMapper.php';
include_once 'phing/types/DirSetAware.php';

/**
* A phing copy task. Copies a file or directory to a new file
Expand All @@ -36,6 +37,8 @@
*/
class CopyTask extends Task
{
use DirSetAware;

/** @var PhingFile */
protected $file = null; // the source file (from xml attribute)

Expand Down Expand Up @@ -344,6 +347,32 @@ public function main()
$this->_scan($fromDir, $this->destDir, $srcFiles, $srcDirs);
}

foreach ($this->dirsets as $dirset) {
try {
$ds = $dirset->getDirectoryScanner($project);
$fromDir = $dirset->getDir($project);
$srcDirs = $ds->getIncludedDirectories();

$srcFiles = [];
foreach ($srcDirs as $srcDir) {
$srcFiles[] = $srcDir;
}

if (!$this->flatten && $this->mapperElement === null && $ds->isEverythingIncluded()
) {
$this->completeDirMap[$fromDir->getAbsolutePath()] = $this->destDir->getAbsolutePath();
}

$this->_scan($fromDir, $this->destDir, $srcFiles, $srcDirs);
} catch (BuildException $e) {
if ($this->haltonerror === true) {
throw $e;
}

$this->logError($e->getMessage());
}
}

// process filesets
foreach ($this->filesets as $fs) {
try {
Expand Down Expand Up @@ -384,7 +413,7 @@ public function main()
*/
protected function validateAttributes()
{
if ($this->file === null && count($this->filesets) === 0 && count($this->filelists) === 0) {
if ($this->file === null && count($this->dirsets) === 0 && count($this->filesets) === 0 && count($this->filelists) === 0) {
throw new BuildException("CopyTask. Specify at least one source - a file, fileset or filelist.");
}

Expand All @@ -400,7 +429,7 @@ protected function validateAttributes()
throw new BuildException("Use a fileset to copy directories.");
}

if ($this->destFile !== null && count($this->filesets) > 0) {
if ($this->destFile !== null && (count($this->filesets) > 0 || count($this->dirsets) > 0)) {
throw new BuildException("Cannot concatenate multiple files into a single file.");
}

Expand All @@ -425,9 +454,9 @@ private function _scan(&$fromDir, &$toDir, &$files, &$dirs)
/* mappers should be generic, so we get the mappers here and
pass them on to builMap. This method is not redundan like it seems */
$mapper = $this->getMapper();

$this->buildMap($fromDir, $toDir, $files, $mapper, $this->fileCopyMap);

if ($this->includeEmpty) {
$this->buildMap($fromDir, $toDir, $dirs, $mapper, $this->dirCopyMap);
}
Expand Down
48 changes: 33 additions & 15 deletions classes/phing/tasks/system/DeleteTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

require_once 'phing/Task.php';
include_once 'phing/types/DirSetAware.php';

/**
* Deletes a file or directory, or set of files defined by a fileset.
Expand All @@ -29,6 +30,8 @@
*/
class DeleteTask extends Task
{
use DirSetAware;

protected $file;
protected $dir;
protected $filesets = [];
Expand Down Expand Up @@ -136,11 +139,12 @@ public function createFileList()
*/
public function main()
{
if ($this->file === null && $this->dir === null && count($this->filesets) === 0 && count(
$this->filelists
) === 0
if ($this->file === null && $this->dir === null && count($this->dirsets) === 0
&& count($this->filesets) === 0 && count($this->filelists) === 0
) {
throw new BuildException("At least one of the file or dir attributes, or a fileset element, or a filelist element must be set.");
throw new BuildException(
"At least one of the file or dir attributes, or a fileset, filelist or dirset element must be set."
);
}

if ($this->quiet && $this->failonerror) {
Expand Down Expand Up @@ -179,20 +183,34 @@ public function main()
}
}

// delete the directory
if ($this->dir !== null) {
if ($this->dir->exists() && $this->dir->isDirectory()) {
if ($this->verbosity === Project::MSG_VERBOSE) {
$this->log("Deleting directory " . $this->dir->__toString());
}
$this->removeDir($this->dir);
$this->dirsets[] = $this->dir;
}
foreach ($this->dirsets as $dirset) {
if (!$dirset instanceof PhingFile) {
$ds = $dirset->getDirectoryScanner($this->getProject());
$dirs = $ds->getIncludedDirectories();
$baseDir = $ds->getBasedir();
} else {
$message = "Directory " . $this->dir->getAbsolutePath() . " does not exist or is not a directory.";

if ($this->failonerror) {
throw new BuildException($message);
$dirs[0] = $dirset;
}
foreach ($dirs as $dir) {
if (!$dir instanceof PhingFile) {
$dir = new PhingFile($baseDir, $dir);
}
if ($dir->exists() && $dir->isDirectory()) {
if ($this->verbosity === Project::MSG_VERBOSE) {
$this->log("Deleting directory " . $dir->__toString());
}
$this->removeDir($dir);
} else {
$this->log($message, ($this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN));
$message = "Directory " . $dir->getAbsolutePath() . " does not exist or is not a directory.";

if ($this->failonerror) {
throw new BuildException($message);
} else {
$this->log($message, ($this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN));
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions classes/phing/tasks/system/EchoTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

include_once 'phing/Task.php';
include_once 'phing/types/DirSetAware.php';

/**
* Echos a message to the logging system or to a file
Expand All @@ -31,6 +32,8 @@
*/
class EchoTask extends Task
{
use DirSetAware;

protected $msg = "";

protected $file = "";
Expand Down Expand Up @@ -63,6 +66,8 @@ public function main()
break;
}

$this->filesets = array_merge($this->filesets, $this->dirsets);

if (count($this->filesets)) {
if (trim(substr($this->msg, -1)) != '') {
$this->msg .= "\n";
Expand Down
13 changes: 11 additions & 2 deletions classes/phing/tasks/system/ForeachTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require_once 'phing/system/io/FileSystem.php';
include_once 'phing/mappers/FileNameMapper.php';
include_once 'phing/tasks/system/PhingTask.php';
include_once 'phing/types/DirSetAware.php';

/**
* <foreach> task
Expand Down Expand Up @@ -50,6 +51,7 @@
*/
class ForeachTask extends Task
{
use DirSetAware;

/** Delimter-separated list of values to process. */
private $list;
Expand Down Expand Up @@ -124,8 +126,8 @@ class ForeachTask extends Task
*/
public function main()
{
if ($this->list === null && $this->currPath === null && count($this->filesets) == 0 && count($this->filelists) == 0) {
throw new BuildException("Need either list, path, nested fileset, nested dirset or nested filelist to iterate through");
if ($this->list === null && $this->currPath === null && count($this->dirsets) === 0 && count($this->filesets) == 0 && count($this->filelists) == 0) {
throw new BuildException("Need either list, path, nested dirset, nested fileset or nested filelist to iterate through");
}
if ($this->param === null) {
throw new BuildException("You must supply a property name to set on each iteration in param");
Expand Down Expand Up @@ -197,6 +199,13 @@ public function main()
$this->process($callee, $fs->getDir($this->project), $srcFiles, $srcDirs);
}

foreach ($this->dirsets as $dirset) {
$ds = $dirset->getDirectoryScanner($this->project);
$srcDirs = $ds->getIncludedDirectories();

$this->process($callee, $dirset->getDir($this->project), [], $srcDirs);
}

if ($this->list === null) {
$this->log(
"Processed {$this->total_dirs} directories and {$this->total_files} files",
Expand Down
11 changes: 11 additions & 0 deletions classes/phing/tasks/system/MoveTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ protected function doWork()
}
}
}

$dirsets = $this->getDirSets();
if (count($dirsets) > 0) {
// process dirsets
foreach ($dirsets as $ds) {
$dir = $ds->getDir($this->project);
if ($this->okToDelete($dir)) {
$this->deleteDir($dir);
}
}
}
}

/**
Expand Down
39 changes: 39 additions & 0 deletions classes/phing/types/DirSetAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

include_once 'phing/types/DirSet.php';

trait DirSetAware
{
/** @var DirSet[] $dirsets */
private $dirsets = [];

public function addDirSet(DirSet $dirSet)
{
$this->dirsets[] = $dirSet;
}

/**
* @return DirSet[]
*/
public function getDirSets()
{
return $this->dirsets;
}
}
18 changes: 14 additions & 4 deletions test/classes/phing/tasks/system/ChmodTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,30 @@ public function tearDown()
public function testChangeModeFile()
{
$this->executeTarget(__FUNCTION__);

clearstatcache();
$mode = fileperms(PHING_TEST_BASE . '/etc/tasks/system/tmp/chmodtest');

$this->assertEquals(octdec('0400'), $mode & 0777, 'chmodtest mode should have changed to 0400');
}

public function testChangeModeFileSet()
{
$this->executeTarget(__FUNCTION__);

clearstatcache();
$mode = fileperms(PHING_TEST_BASE . '/etc/tasks/system/tmp/chmodtest');


$this->assertEquals(octdec('0400'), $mode & 0777, 'chmodtest mode should have changed to 0400');
}

public function testChangeModeDirSet()
{
$this->executeTarget(__FUNCTION__);

clearstatcache();
$mode = fileperms(PHING_TEST_BASE . '/etc/tasks/system/tmp/A');

$this->assertEquals(octdec('0400'), $mode & 0777, 'chmodtest mode should have changed to 0400');
}
}
Loading

0 comments on commit 21bbaec

Please sign in to comment.