forked from slowprog/CopyFile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScriptHandler.php
111 lines (90 loc) · 3.82 KB
/
ScriptHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace SlowProg\CopyFile;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException;
use Composer\Script\Event;
class ScriptHandler
{
/**
* @param Event $event
*/
public static function copy(Event $event)
{
$extras = $event->getComposer()->getPackage()->getExtra();
$extraField = $event->isDevMode() && isset($extras['copy-file-dev']) ? 'copy-file-dev' : 'copy-file';
$io = $event->getIO();
if (!isset($extras[$extraField])) {
$io->write("No dirs or files are configured through the extra.{$extraField} setting.");
return;
}
$files = $extras[$extraField];
if (!is_array($files) || $files === array_values($files)) {
$io->write("The extra.{$extraField} must be hash like \"\{<dir_or_file_from>: <dir_to>\}\".");
return;
}
$fs = new Filesystem;
foreach ($files as $from => $to) {
// check pattern
$pattern = null;
if (strpos($from, '#') > 0) {
list($from, $pattern) = explode('#', $from, 2);
}
// check the overwrite newer files disable flag (? in end of path)
$overwriteNewerFiles = substr($to, -1) != '?';
if (!$overwriteNewerFiles) {
$to = substr($to, 0, -1);
}
$overwriteExistingFiles = substr($to, -1) != '!';
$to = rtrim($to, '!');
if (!$overwriteExistingFiles) {
if($fs->exists($to)) {
continue;
}
}
// 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 {
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), $e->getCode(), $e);
}
if (false === file_exists($from)) {
throw new \InvalidArgumentException(sprintf('<error>Source directory or file "%s" does not exist.</error>', $from));
}
if (is_dir($from)) {
$finder = new Finder;
$finder->files()->ignoreDotFiles(false)->in($from);
if ($pattern) {
$finder->path("#{$pattern}#");
}
foreach ($finder as $file) {
$dest = sprintf('%s/%s', $to, $file->getRelativePathname());
try {
$fs->copy($file, $dest, $overwriteNewerFiles);
} catch (IOException $e) {
throw new \InvalidArgumentException(sprintf('<error>Could not copy %s</error>', $file->getBaseName()), $e->getCode(), $e);
}
}
} else {
try {
if ($isRenameFile) {
$fs->copy($from, $to, $overwriteNewerFiles);
} else {
$fs->copy($from, $to.'/'.basename($from), $overwriteNewerFiles);
}
} catch (IOException $e) {
throw new \InvalidArgumentException(sprintf('<error>Could not copy %s</error>', $from), $e->getCode(), $e);
}
}
$io->write(sprintf('Copied file(s) from <comment>%s</comment> to <comment>%s</comment>.', $from, $to));
}
}
}