Skip to content

Commit

Permalink
Merge pull request #13 from rask/feature/file-copy-flag
Browse files Browse the repository at this point in the history
Add option for copying files instead of moving them
  • Loading branch information
onnimonni authored Feb 4, 2018
2 parents b09dd53 + 6c25880 commit bb0016b
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 15 deletions.
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
composer.lock

# Stuff from tests
tests/htdocs
tests/vendor
tests/composer.lock
tests/move/htdocs
tests/copy/htdocs
tests/move/vendor
tests/copy/vendor
tests/move/composer.lock
tests/copy/composer.lock
9 changes: 6 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ before_script:

# We use the same repo straight from github because it required lesser amount of hacks
# Here we force the same current git commit id for composer
- sed -i -e "s|%%TRAVIS_COMMIT%%|$TRAVIS_COMMIT|g" tests/composer.json
- sed -i -e "s|%%TRAVIS_COMMIT%%|$TRAVIS_COMMIT|g" tests/move/composer.json
- sed -i -e "s|%%TRAVIS_COMMIT%%|$TRAVIS_COMMIT|g" tests/copy/composer.json

# Install test run into tests/ folder using tests/composer.json
- composer install --working-dir=./tests/
- composer install --working-dir=./tests/move/
- composer install --working-dir=./tests/copy/

script:
# Sanity php syntax check
Expand All @@ -31,4 +33,5 @@ script:
- composer validate composer.json

# Run the real tests finally
- "phpunit tests/*.php"
- "vendor/bin/phpunit tests/move/*.php"
- "vendor/bin/phpunit tests/copy/*.php"
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@
},
"extra": {
"class": "Koodimonni\\Composer\\Dropin"
},
"scripts": {
"test": [
"composer install --working-dir=./tests/move/ && vendor/bin/phpunit ./tests/move/*.php",
"composer install --working-dir=./tests/copy/ && vendor/bin/phpunit ./tests/copy/*.php"
]
}
}
26 changes: 25 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://travis-ci.org/Koodimonni/Composer-Dropin-Installer.svg?branch=master)](https://travis-ci.org/Koodimonni/Composer-Dropin-Installer) [![Latest Stable Version](https://poser.pugx.org/koodimonni/composer-dropin-installer/v/stable)](https://packagist.org/packages/koodimonni/composer-dropin-installer) [![Total Downloads](https://poser.pugx.org/koodimonni/composer-dropin-installer/downloads)](https://packagist.org/packages/koodimonni/composer-dropin-installer) [![Latest Unstable Version](https://poser.pugx.org/koodimonni/composer-dropin-installer/v/unstable)](https://packagist.org/packages/koodimonni/composer-dropin-installer) [![License](https://poser.pugx.org/koodimonni/composer-dropin-installer/license)](https://packagist.org/packages/koodimonni/composer-dropin-installer)

This composer plugin helps you to move your composer packaged files where you want them to be.
This composer plugin helps you to move or copy your composer packaged files where you want them to be.

Composer only allows you to install full directories into their own directories. There's really useful [composer/installers](https://github.com/composer/installers) for custom installation paths but it overwrites everything in folder and doesn't allow coexist of two or more projects. We just let composer install things and take it from there.

Expand Down Expand Up @@ -96,6 +96,21 @@ I created this originally for installing multiple languages for WordPress with c
}
```

## Moving v. copying files

By default this dropin installer moves files from the source to the destination,
which means the files disappear from the source.

If you would prefer copying instead (which keeps the files at the source after
installation) insert the following configuration to your `composer.json` `config`
declarations:

```json
"config": {
"dropin-installer": "copy"
}
```

##But how about the impossible looking syntax?
Dropin syntax consists from four parts: ```"{path}": "{directive}:{target}:{files}"```

Expand Down Expand Up @@ -126,6 +141,15 @@ phpunit.xml
```
* Script requires unix filesystem (OS X,Linux)

## Testing

Run PHPUnit tests with

$ composer test

Tests are run inside the `tests/` directory where two dummy Composer projects are used to test dropin
installation methods.

##Todo
* Handle deletions on removal and on update. This could be easily done with json-database in [vendor-dir]

Expand Down
80 changes: 75 additions & 5 deletions src/Dropin.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,27 @@ public function dropNewFiles(PackageInterface $package){
$src = "{$vendorDir}/{$info['package']}";
}

$config = $this->composer->getPackage()->getConfig();
$shouldCopy = isset($config['dropin-installer']) && $config['dropin-installer'] === 'copy';

$installFiles = self::getFilesToInstall($info);
$this->io->write(" Moving dropin files...\n");
if ($installFiles == "*") {
self::rmove($src,$dest);
if ($shouldCopy) {
$this->io->write(" Copying dropin files...\n");
if ($installFiles == "*") {
self::rcopy($src, $dest);
} else {
foreach ($installFiles as $file) {
self::copy("{$src}/{$file}", $dest);
}
}
} else {
foreach($installFiles as $file) {
self::move("{$src}/{$file}",$dest);
$this->io->write(" Moving dropin files...\n");
if ($installFiles == "*") {
self::rmove($src, $dest);
} else {
foreach ($installFiles as $file) {
self::move("{$src}/{$file}", $dest);
}
}
}
}
Expand Down Expand Up @@ -300,6 +314,62 @@ private static function move($src, $dest){
}
rename($src, "$dest/" . basename($src));
}
/**
* Recursively copy files from one directory to another
*
* @param string $src - Source of files being copied
* @param string $dest - Destination of files being copied
*/
private static function rcopy($src, $dest){
// If source is not a directory stop processing
if(!is_dir($src)) {
echo "Source is not a directory";
return false;
}

// If the destination directory does not exist create it
if(!is_dir($dest)) {
if(!mkdir($dest,0777,true)) {
// If the destination directory could not be created stop processing
echo "Can't create destination path: {$dest}\n";
return false;
}
}

// Open the source directory to read in files
$i = new \DirectoryIterator($src);
foreach($i as $f) {
#Skip useless files&folders
if (self::isFileIgnored($f->getFilename())) continue;

if($f->isFile()) {
copy($f->getRealPath(), "$dest/" . $f->getFilename());
} else if(!$f->isDot() && $f->isDir()) {
self::rcopy($f->getRealPath(), "$dest/$f");
#unlink($f->getRealPath());
}
}
#We could Remove original directories but don't do it
#unlink($src);
}
/**
* Copy a file from one location to another.
*
* @param $src - File being copied
* @param $dest - Destinatin directory
*/
private static function copy($src, $dest)
{
// If the destination directory does not exist create it
if(!is_dir($dest)) {
if(!mkdir($dest,0777,true)) {
// If the destination directory could not be created stop processing
echo "Can't create destination path: {$dest}\n";
return false;
}
}
copy($src, "$dest/" . basename($src));
}
/**
* Returns type and information of dropin directive
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/copy/KoodimonniComposerCopiedFilesExistsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

class KoodimonniComposerCopiedFilesExistsTest extends PHPUnit_Framework_TestCase
{

public function testPackageWasInstalled()
{
$this->assertFileExists(dirname( __FILE__ ) . '/htdocs/dropin-test.php');
}
public function testPackageFileExistsInVendor()
{
$this->assertFileExists(dirname( __FILE__ ) . '/vendor/dropininternal/dropin-test-package/dropin-test.php');
}

}
38 changes: 38 additions & 0 deletions tests/copy/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "koodimonni/composer-dropin-installer-tests",
"description": "composer.json for testing composer-dropin-installer",
"license": "WTFPL",
"type": "project",
"authors": [
{
"name": "Onni Hakala",
"email": "[email protected]"
}
],
"repositories": [
{
"type": "vcs",
"url": "https://github.com/Koodimonni/Composer-Dropin-Installer"
},
{
"type": "path",
"url": "../testpkg"
},
{
"packagist": false
}
],
"require": {
"koodimonni/composer-dropin-installer": "dev-master#%%TRAVIS_COMMIT%%",
"dropininternal/dropin-test-package": "dev-master"
},
"extra": {
"dropin-paths": {
"htdocs/": ["package:dropininternal/dropin-test-package"]
}
},
"config": {
"dropin-installer": "copy"
},
"minimum-stability": "dev"
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
class KoodimonniComposerFilesExistsTest extends PHPUnit_Framework_TestCase
class KoodimonniComposerMovedFilesExistsTest extends PHPUnit_Framework_TestCase
{

public function testFinnishLanguageWasInstalled()
{
$this->assertFileExists(dirname( __FILE__ ) . '/htdocs/wp-content/languages/fi.po');
Expand All @@ -11,4 +11,4 @@ public function testFinnishLanguageNotExistsInVendor()
$this->assertFileNotExists(dirname( __FILE__ ) . '/vendor/koodimonni-language/fi/fi.po');
}

}
}
File renamed without changes.
6 changes: 6 additions & 0 deletions tests/move/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit>
<testsuite name='Dropin Installer tests'>
<directory suffix='.php'>./</directory>
</testsuite>
</phpunit>
5 changes: 5 additions & 0 deletions tests/testpkg/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "dropininternal/dropin-test-package",
"type": "library",
"version": "dev-master"
}
3 changes: 3 additions & 0 deletions tests/testpkg/dropin-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo 'hello world';

0 comments on commit bb0016b

Please sign in to comment.