Skip to content

Commit

Permalink
Add renaming of a files #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Tyshev committed Mar 26, 2017
1 parent 778b422 commit c76bcc6
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 6 deletions.
81 changes: 80 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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
```
17 changes: 14 additions & 3 deletions ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<error>Could not create directory %s.</error>', $to));
}
Expand All @@ -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('<error>Could not copy %s</error>', $from));
}
Expand Down
17 changes: 15 additions & 2 deletions tests/CopyFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testCopyDirToDir()
$this->assertTrue($root->hasChild('to/file2'));
}

public function testCopyToNotExistsDir()
public function testCopyDirToNotExistsDir()
{
$root = $this->getFilesystem();

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit c76bcc6

Please sign in to comment.