Skip to content

Commit 9ef8f2d

Browse files
authored
Merge pull request #8 from neronmoon/develop
Add short syntax to run scripts
2 parents 4286d20 + 794bbd0 commit 9ef8f2d

9 files changed

+303
-56
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ After installing you able to add extra.scripts-dev directive in your `composer.j
1212
"post-install-cmd": [
1313
"npm install --dev"
1414
],
15-
"post-update-cmd": "php ./someCoolCommand.php"
15+
"post-update-cmd": "php ./someCoolCommand.php",
16+
"test": "phpunit"
1617
},
1718
}
1819
...

src/DevScriptProxyCommand.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace ScriptsDev;
4+
5+
6+
use Composer\Command\BaseCommand;
7+
use Symfony\Component\Console\Input\ArrayInput;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Input\InputOption;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
13+
class DevScriptProxyCommand extends BaseCommand
14+
{
15+
protected function configure()
16+
{
17+
$this
18+
->setName('{name}')
19+
->setDescription('Run the {name} defined in composer.json.')
20+
->setDefinition(array(
21+
new InputArgument('script', InputArgument::OPTIONAL, ''),
22+
new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''),
23+
new InputOption('timeout', null, InputOption::VALUE_REQUIRED,
24+
'Sets script timeout in seconds, or 0 for never.'),
25+
new InputOption('dev', null, InputOption::VALUE_NONE, 'Sets the dev mode.'),
26+
new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables the dev mode.'),
27+
new InputOption('list', 'l', InputOption::VALUE_NONE, 'List scripts.'),
28+
));
29+
}
30+
31+
protected function execute(InputInterface $input, OutputInterface $output)
32+
{
33+
$args = array(
34+
'command' => 'run-script',
35+
'script' => $this->getName(),
36+
'args' => $input->getArgument('args'),
37+
'--timeout' => $input->getOption('timeout') ?: '0',
38+
'--dev' => $input->getOption('dev'),
39+
'--no-dev' => $input->getOption('no-dev'),
40+
'--list' => $input->getOption('list'),
41+
);
42+
43+
$command = $this->getApplication()->find('run-script');
44+
45+
$arrayInput = new ArrayInput($args);
46+
$command->run($arrayInput, $output);
47+
}
48+
}

src/DevScriptsCommandProvider.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace ScriptsDev;
4+
5+
use Composer\Composer;
6+
use Composer\IO\IOInterface;
7+
use Composer\Plugin\Capability\CommandProvider;
8+
9+
class DevScriptsCommandProvider implements CommandProvider
10+
{
11+
/**
12+
* @var Composer
13+
*/
14+
private $composer;
15+
/**
16+
* @var IOInterface
17+
*/
18+
private $io;
19+
20+
public function __construct(array $args)
21+
{
22+
if (!$args['composer'] instanceof Composer) {
23+
throw new \RuntimeException('Expected a "composer" key');
24+
}
25+
if (!$args['io'] instanceof IOInterface) {
26+
throw new \RuntimeException('Expected an "io" key');
27+
}
28+
$this->composer = $args['composer'];
29+
$this->io = $args['io'];
30+
}
31+
32+
public function getCommands()
33+
{
34+
$package = $this->composer->getPackage();
35+
$extractor = new PackageScriptsExtractor($this->io);
36+
$devScripts = $extractor->extract($package);
37+
$commands = array();
38+
foreach ($devScripts as $name => $cmd) {
39+
$commands[] = $this->buildCommand($name);
40+
}
41+
return $commands;
42+
}
43+
44+
/**
45+
* @param $name
46+
* @return DevScriptProxyCommand
47+
*/
48+
private function buildCommand($name)
49+
{
50+
$command = new DevScriptProxyCommand($name);
51+
$command->setName($this->replaceName($command->getName(), $name));
52+
$command->setDescription($this->replaceName($command->getDescription(), $name));
53+
return $command;
54+
}
55+
56+
private function replaceName($string, $replacement)
57+
{
58+
return str_replace('{name}', $replacement, $string);
59+
}
60+
}

src/PackageScriptsExtractor.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace ScriptsDev;
4+
5+
6+
use Composer\IO\IOInterface;
7+
use Composer\Package\AliasPackage;
8+
use Composer\Package\CompletePackage;
9+
use Composer\Package\Package;
10+
11+
class PackageScriptsExtractor
12+
{
13+
/**
14+
* @var IOInterface
15+
*/
16+
private $io;
17+
18+
protected static $warningPrinted = false;
19+
20+
public function __construct(IOInterface $io)
21+
{
22+
$this->io = $io;
23+
}
24+
25+
/**
26+
* @param Package $package
27+
* @return array
28+
*/
29+
public function extract(Package $package)
30+
{
31+
// If we have extra.branch-alias, package will be an instanceof RootAliasPackage instead of RootPackage
32+
if ($package instanceof AliasPackage) {
33+
$package = $package->getAliasOf();
34+
}
35+
36+
if ($package instanceof CompletePackage && in_array('--no-dev', $_SERVER['argv'], true)) {
37+
return array();
38+
}
39+
40+
$devScripts = array();
41+
$config = json_decode(file_get_contents(getcwd() . '/composer.json'), true);
42+
if (isset($config['scripts-dev']) && is_array($config['scripts-dev'])) {
43+
if (!static::$warningPrinted) {
44+
$this->io->writeError(
45+
"<warning>You're using deprecated way to define development-only scripts.
46+
Please, move commands under `scripts-dev` directive into `extra` field.
47+
See https://github.com/neronmoon/scriptsdev/blob/master/README.md for more details.</warning>"
48+
);
49+
static::$warningPrinted = true;
50+
}
51+
$devScripts = array_merge_recursive($devScripts, $config['scripts-dev']);
52+
}
53+
54+
$extra = $package->getExtra();
55+
if (isset($extra['scripts-dev']) && is_array($extra['scripts-dev'])) {
56+
$devScripts = array_merge_recursive($devScripts, $extra['scripts-dev']);
57+
}
58+
59+
foreach ($devScripts as $event => &$listeners) {
60+
$listeners = (array)$listeners;
61+
}
62+
63+
return $devScripts;
64+
}
65+
}

src/Plugin.php

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,24 @@
44

55
use Composer\Composer;
66
use Composer\IO\IOInterface;
7-
use Composer\Package\AliasPackage;
8-
use Composer\Package\CompletePackage;
7+
use Composer\Plugin\Capable;
98
use Composer\Plugin\PluginInterface;
109

11-
class Plugin implements PluginInterface
10+
class Plugin implements PluginInterface, Capable
1211
{
12+
public function getCapabilities()
13+
{
14+
return array(
15+
'Composer\Plugin\Capability\CommandProvider' => 'ScriptsDev\DevScriptsCommandProvider',
16+
);
17+
}
18+
1319
public function activate(Composer $composer, IOInterface $io)
1420
{
1521
$package = $composer->getPackage();
1622

17-
// If we have extra.branch-alias, package will be an instanceof RootAliasPackage instead of RootPackage
18-
if ($package instanceof AliasPackage) {
19-
$package = $package->getAliasOf();
20-
}
21-
22-
if ($package instanceof CompletePackage && in_array('--no-dev', $_SERVER['argv'], true)) {
23-
return;
24-
}
25-
26-
$devScripts = array();
27-
$config = json_decode(file_get_contents(getcwd().'/composer.json'), true);
28-
if (isset($config['scripts-dev']) && is_array($config['scripts-dev'])) {
29-
$io->writeError(
30-
"You're using deprecated way to define development-only scripts.
31-
Please, move commands under `scripts-dev` directive into `extra` field.
32-
See README.md for more details."
33-
);
34-
$devScripts = array_merge_recursive($devScripts, $config['scripts-dev']);
35-
}
36-
37-
$extra = $package->getExtra();
38-
if (isset($extra['scripts-dev']) && is_array($extra['scripts-dev'])) {
39-
$devScripts = array_merge_recursive($devScripts, $extra['scripts-dev']);
40-
}
41-
42-
foreach ($devScripts as $event => &$listeners) {
43-
$listeners = (array) $listeners;
44-
}
23+
$extractor = new PackageScriptsExtractor($io);
24+
$devScripts = $extractor->extract($package);
4525

4626
$package->setScripts(array_merge_recursive($package->getScripts(), $devScripts));
4727
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "neronmoon/scriptsdev-test",
3+
"description": "Scripts-dev behaviour for Composer",
4+
"keywords": ["dev", "scripts", "composer", "commands", "commands execution"],
5+
"homepage": "https://github.com/neronmoon/scriptsdev",
6+
"type": "project",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Vitaliy Krasnoperov",
11+
"email": "alistar.neron@gmail.com"
12+
}
13+
],
14+
"repositories": [
15+
{
16+
"type": "path",
17+
"url": "<PLUGIN_PATH>"
18+
}
19+
],
20+
"require": {
21+
"neronmoon/scriptsdev": "*@dev"
22+
},
23+
"extra": {
24+
"scripts-dev": {
25+
"test": "echo SCRIPTSDEV RULEZ"
26+
}
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "neronmoon/scriptsdev-test",
3+
"description": "Scripts-dev behaviour for Composer",
4+
"keywords": ["dev", "scripts", "composer", "commands", "commands execution"],
5+
"homepage": "https://github.com/neronmoon/scriptsdev",
6+
"type": "project",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Vitaliy Krasnoperov",
11+
"email": "alistar.neron@gmail.com"
12+
}
13+
],
14+
"repositories": [
15+
{
16+
"type": "path",
17+
"url": "<PLUGIN_PATH>"
18+
}
19+
],
20+
"require": {
21+
"neronmoon/scriptsdev": "*@dev"
22+
},
23+
"scripts-dev": {
24+
"test": "echo SCRIPTSDEV RULEZ"
25+
}
26+
}

tests/legacy.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
"scripts-dev": {
2424
"post-update-cmd": "echo SCRIPTSDEV RULEZ"
2525
}
26-
}
26+
}

0 commit comments

Comments
 (0)