Skip to content

Commit

Permalink
Merge pull request #11 from lar-dragon/master
Browse files Browse the repository at this point in the history
Allow override files and development mode settings
  • Loading branch information
slowprog authored Dec 26, 2018
2 parents 75d816e + f5d0e38 commit 4cc4017
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 145 deletions.
43 changes: 43 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
language: php

dist: trusty
root: false

env:
global:
- setup=stable

cache:
directories:
- $HOME/.composer/cache

matrix:
fast_finish: true
allow_failures:
- php: hhvm
include:
- php: 5.6
- php: 5.6
env: setup=lowest
- php: 7.0
- php: 7.0
env: setup=lowest
- php: 7.1
- php: 7.1
env: setup=lowest
- php: 7.2
- php: 7.2
env: setup=lowest
- php: hhvm
- php: hhvm
env: setup=lowest

before_install:
- travis_retry composer self-update
- travis_retry composer config -g github-oauth.github.com "$GITHUB_TOKEN"

install:
- if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-stable --no-suggest; fi
- if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable --no-suggest; fi

script: composer run test
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[![Build Status](https://travis-ci.org/slowprog/CopyFile.svg?branch=master)](https://travis-ci.org/slowprog/CopyFile)
[![Latest Stable Version](https://poser.pugx.org/slowprog/composer-copy-file/version)](https://packagist.org/packages/slowprog/composer-copy-file)
[![Total Downloads](https://poser.pugx.org/slowprog/composer-copy-file/downloads)](https://packagist.org/packages/slowprog/composer-copy-file)

# Composer copy file

Composer script copying your files after install. Supports copying of entire directories, individual files and complex nested directories.
Expand Down Expand Up @@ -26,10 +30,39 @@ For example copy fonts:
}
```

In a development you may use `-dev` suffix. For example copy non-minified in development and minified in production:

```
{
"require":{
"twbs/bootstrap": "~3.3",
"slowprog/composer-copy-file": "~0.2"
},
"scripts": {
"post-install-cmd": [
"SlowProg\\CopyFile\\ScriptHandler::copy"
],
"post-update-cmd": [
"SlowProg\\CopyFile\\ScriptHandler::copy"
]
},
"extra": {
"copy-file": {
"vendor/twbs/bootstrap/dist/js/bootstrap.min.js": "web/js/bootstrap.js"
},
"copy-file-dev": {
"vendor/twbs/bootstrap/dist/js/bootstrap.js": "web/js/bootstrap.js"
}
}
}
```

## Use cases

You need to be careful when using a last slash. The file-destination is different from the directory-destination with the slash.

If in destination directory already exists copy of file, then it will be override. To overwrite only older files append `?` in end of destination path.

Source directory hierarchy:

```
Expand Down Expand Up @@ -104,3 +137,33 @@ dir/
file1.txt
file_rename.txt
```
4. Override only older files:
```
{
"extra": {
"copy-file": {
"dir/subdir/": "web/other/?"
}
}
}
```
Preset:
```
web/
other/
file1.txt - Recently modified
file2.txt - Old rotten file
```
Result:
```
web/
other/
file1.txt - Not changed
file2.txt - Replaced
```
17 changes: 12 additions & 5 deletions ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ class ScriptHandler
public static function copy(Event $event)
{
$extras = $event->getComposer()->getPackage()->getExtra();
$extraField = $event->isDevMode() && isset($extras['copy-file-dev']) ? 'copy-file-dev' : 'copy-file';

if (!isset($extras['copy-file'])) {
if (!isset($extras[$extraField])) {
throw new \InvalidArgumentException('The dirs or files needs to be configured through the extra.copy-file setting.');
}

$files = $extras['copy-file'];
$files = $extras[$extraField];

if ($files === array_values($files)) {
throw new \InvalidArgumentException('The extra.copy-file must be hash like "{<dir_or_file_from>: <dir_to>}".');
Expand All @@ -30,6 +31,12 @@ public static function copy(Event $event)
$io = $event->getIO();

foreach ($files as $from => $to) {
// check the overwrite newer files disable flag (? in end of path)
$overwriteNewerFiles = substr($to, -1) != '?';
if (!$overwriteNewerFiles) {
$to = substr($to, 0, -1);
}

// Check the renaming of file for direct moving (file-to-file)
$isRenameFile = substr($to, -1) != '/' && !is_dir($from);

Expand Down Expand Up @@ -59,17 +66,17 @@ public static function copy(Event $event)
$dest = sprintf('%s/%s', $to, $file->getRelativePathname());

try {
$fs->copy($file, $dest);
$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);
$fs->copy($from, $to, $overwriteNewerFiles);
} else {
$fs->copy($from, $to.'/'.basename($from));
$fs->copy($from, $to.'/'.basename($from), $overwriteNewerFiles);
}
} catch (IOException $e) {
throw new \InvalidArgumentException(sprintf('<error>Could not copy %s</error>', $from), $e->getCode(), $e);
Expand Down
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@
}
],
"require": {
"php": ">=5.3.3"
"php": ">=5.6"
},
"require-dev": {
"composer/composer": "1.0.*@dev",
"symfony/filesystem": "~2.7",
"symfony/finder": "~2.7",
"phpunit/phpunit": "~5.0",
"mikey179/vfsStream": "~1"
"phpunit/phpunit": "5.7.27",
"mikey179/vfsStream": "~1",
"php-mock/php-mock-phpunit": "~1"
},
"autoload": {
"psr-4": { "SlowProg\\CopyFile\\": "" }
},
"autoload-dev": {
"classmap": [ "tests/" ]
},
"scripts": {
"test": "phpunit --verbose"
}
}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
syntaxCheck="false">
<testsuites>
<testsuite name="CopyFile Test Suite">
<directory suffix=".php">./tests/</directory>
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
Loading

0 comments on commit 4cc4017

Please sign in to comment.