diff --git a/README.md b/README.md index 93cf1a3..41982bc 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ For example copy fonts: { "require":{ "twbs/bootstrap": "~3.3", - "slowprog/composer-copy-file": "~0.1" + "slowprog/composer-copy-file": "~0.2" }, "scripts": { "post-install-cmd": [ @@ -25,3 +25,82 @@ For example copy fonts: } } ``` + +## Use cases + +You need to be careful when using a last slash. The file-destination is different from the directory-destination with the slash. + +Source directory hierarchy: + +``` +dir/ + subdir/ + file1.txt + file2.txt +``` + +1. Dir-to-dir: + + ``` + { + "extra": { + "copy-file": { + "dir/subdir/": "web/other/" + } + } + } + ``` + + Result: + + ``` + web/ + other/ + file1.txt + file2.txt + ``` + +2. File-to-dir: + + ``` + { + "extra": { + "copy-file": { + "dir/subdir/file1.txt": "web/other/" + "dir/subdir/file2.txt": "web/other/file2.txt/" + } + } + } + ``` + + Result: + + ``` + web/ + other/ + file1.txt + file2.txt/ + file2.txt + ``` + +3. File-to-file: + + ``` + { + "extra": { + "copy-file": { + "dir/subdir/file1.txt": "web/other/file1.txt" + "dir/subdir/file2.txt": "web/other/file_rename.txt" + } + } + } + ``` + + Result: + + ``` + web/ + other/ + file1.txt + file_rename.txt + ``` diff --git a/ScriptHandler.php b/ScriptHandler.php index a612f9e..9323236 100644 --- a/ScriptHandler.php +++ b/ScriptHandler.php @@ -30,12 +30,19 @@ public static function copy(Event $event) $io = $event->getIO(); foreach ($files as $from => $to) { - if (file_exists($to) && !is_dir($to)) { + // Check the renaming of file for direct moving (file-to-file) + $isRenameFile = substr($to, -1) != '/' && !is_dir($from); + + if (file_exists($to) && !is_dir($to) && !$isRenameFile) { throw new \InvalidArgumentException('Destination directory is not a directory.'); } try { - $fs->mkdir($to); + if ($isRenameFile) { + $fs->mkdir(dirname($to)); + } else { + $fs->mkdir($to); + } } catch (IOException $e) { throw new \InvalidArgumentException(sprintf('Could not create directory %s.', $to)); } @@ -59,7 +66,11 @@ public static function copy(Event $event) } } else { try { - $fs->copy($from, $to.'/'.basename($from)); + if ($isRenameFile) { + $fs->copy($from, $to); + } else { + $fs->copy($from, $to.'/'.basename($from)); + } } catch (IOException $e) { throw new \InvalidArgumentException(sprintf('Could not copy %s', $from)); } diff --git a/tests/CopyFileTest.php b/tests/CopyFileTest.php index d937bae..1bca11b 100644 --- a/tests/CopyFileTest.php +++ b/tests/CopyFileTest.php @@ -40,7 +40,7 @@ public function testCopyDirToDir() $this->assertTrue($root->hasChild('to/file2')); } - public function testCopyToNotExistsDir() + public function testCopyDirToNotExistsDir() { $root = $this->getFilesystem(); @@ -84,12 +84,25 @@ public function testCopyFileToDir() $this->assertFalse($root->hasChild('to/file3')); ScriptHandler::copy($this->getEventMock([ - vfsStream::url('root/file3')=> vfsStream::url('root/to') + vfsStream::url('root/file3')=> vfsStream::url('root/to/') ])); $this->assertTrue($root->hasChild('to/file3')); } + public function testCopyFileToFile() + { + $root = $this->getFilesystem(); + + $this->assertFalse($root->hasChild('to/file_new')); + + ScriptHandler::copy($this->getEventMock([ + vfsStream::url('root/file3')=> vfsStream::url('root/to/file_new') + ])); + + $this->assertTrue($root->hasChild('to/file_new')); + } + public function testCopyFromComplexDir() { $root = $this->getFilesystem();