Skip to content

Commit 2382f69

Browse files
committed
Initial commit
0 parents  commit 2382f69

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
composer.lock

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# File Copy Composer Plugin
2+
3+
Allows files to be copied on dependency installation, default composer behaviour only runs scripts for the root package.
4+
5+
Will only work on modules with `skywire` in the package name, will only work with paths relative to the working directory, e.g. `dev` instead of `./dev` or `../../dev`
6+
7+
Uses https://github.com/slowprog/CopyFile to copy files, see their documentation for configuration, only the `extras.copy-files` section is rquired
8+
9+
# Installation
10+
11+
From your module repository run
12+
13+
`composer require skywire/file-copy-plugin`

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "skywire/file-copy-plugin",
3+
"type": "composer-plugin",
4+
"description": "A plugin to allow for dependencies to copy files into the project on install / update",
5+
"require": {
6+
"php": ">=7.0",
7+
"composer-plugin-api": "^1.1",
8+
"slowprog/composer-copy-file": "*"
9+
},
10+
"minimum-stability": "dev",
11+
"autoload": {
12+
"psr-4": {
13+
"Skywire\\FileCopyPlugin\\": "src/"
14+
}
15+
},
16+
"extra": {
17+
"class": "Skywire\\FileCopyPlugin\\Plugin"
18+
}
19+
}

src/Plugin.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Skywire\FileCopyPlugin;
4+
5+
use Composer\Composer;
6+
use Composer\EventDispatcher\EventSubscriberInterface;
7+
use Composer\IO\IOInterface;
8+
use Composer\Package\CompletePackage;
9+
use Composer\Plugin\PluginInterface;
10+
use Composer\Script\ScriptEvents;
11+
use SlowProg\CopyFile\ScriptHandler;
12+
13+
class Plugin implements PluginInterface, EventSubscriberInterface
14+
{
15+
/** @var \Composer\Composer */
16+
protected $composer;
17+
18+
protected $io;
19+
20+
public function activate(Composer $composer, IOInterface $io)
21+
{
22+
$this->composer = $composer;
23+
$this->io = $io;
24+
}
25+
26+
public static function getSubscribedEvents()
27+
{
28+
return array(
29+
ScriptEvents::POST_INSTALL_CMD => [
30+
array('copyPackageFiles', 0),
31+
],
32+
ScriptEvents::POST_UPDATE_CMD => [
33+
array('copyPackageFiles', 0),
34+
]
35+
);
36+
}
37+
38+
public function copyPackageFiles($event)
39+
{
40+
$packages = $event->getComposer()->getRepositoryManager()->getLocalRepository()->getPackages();
41+
// find skywire only packages
42+
$packages = array_filter($packages, function (CompletePackage $package) {
43+
return strpos($package->getName(), 'skywire') !== false;
44+
});
45+
46+
$toCopy = [];
47+
48+
foreach ($packages as $package) {
49+
/** @var CompletePackage $package */
50+
if ($package->getExtra() && isset($package->getExtra()['copy-file'])) {
51+
$toCopy += $package->getExtra()['copy-file'];
52+
}
53+
}
54+
55+
$this->validatePaths($toCopy);
56+
57+
// add our pacakge copy config to the root package config, so that slowprog can find it
58+
$rootPackage = $event->getComposer()->getPackage();
59+
$rootPackageExtra = $rootPackage->getExtra();
60+
61+
$currentToCopy = $rootPackageExtra['copy-file'] ?? [];
62+
63+
$mergedCopy = $currentToCopy + $toCopy;
64+
65+
$rootPackageExtra['copy-file'] = $mergedCopy;
66+
67+
$rootPackage->setExtra($rootPackageExtra);
68+
69+
// do the copy via slowprog/composer-copy-file
70+
ScriptHandler::copy($event);
71+
}
72+
73+
protected function validatePaths(array $paths)
74+
{
75+
foreach ($paths as $path) {
76+
if (strpos($path, './') !== false || $path[0] == '/' || strpos($path, '~/') !== false) {
77+
throw new \InvalidArgumentException("path $path is not valid, all paths must be relative to the current working directory, and cannot go above it");
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)