Skip to content

Commit

Permalink
Functions for running tty processes
Browse files Browse the repository at this point in the history
  • Loading branch information
vladitot committed Sep 3, 2018
1 parent 3439bcb commit ec0a2dd
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 38 deletions.
39 changes: 1 addition & 38 deletions src/ExtraGetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,49 +28,12 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$settings = $this->getAllExtra('composer.json', $input->getArgument('param'));
$settings = StaticHelper::getAllExtra('composer.json', $input->getArgument('param'));
foreach ($settings as $key=>$prop) {
$output->write($key.'='.$prop.''." ");
}
$output->writeln('');
}

/**
* Getter params from extra
* Say them, which key do you want to get. Another time you will able to export them, for example.
* @param $file
* @param $searchForString
* @return array
*/
protected function getAllExtra($file, $searchForString) {
$searchFor = explode('-', $searchForString);
$searchable = [];

$content = json_decode(file_get_contents($file), true);
if (isset($content['extra'])) {
$currentEl = $content['extra'];

foreach ($searchFor as $item) {
if (isset($currentEl[$item])) {
$currentEl = $currentEl[$item];
} else {
break;
}
}
if ($currentEl != $content['extra']) {
$searchable = $currentEl;
}
}

if (isset($content['extra']['merge-plugin']['include'])) {
$extraIncludes = $content['extra']['merge-plugin']['include'];
$includedFoundSearchables = [];
foreach ($extraIncludes as $file) {
$includedFoundSearchables = array_replace($includedFoundSearchables, $this->getAllExtra($file, $searchForString));
}
} else {
$includedFoundSearchables = [];
}
return array_replace($searchable, $includedFoundSearchables);
}
}
42 changes: 42 additions & 0 deletions src/RunScriptCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Created by PhpStorm.
* User: vladitot
* Date: 29.08.18
* Time: 12:00
*/

namespace ExtraPlugin;


use Composer\Command\BaseCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ExtraGetCommand extends BaseCommand
{
/**
* First-time set up
*/
protected function configure()
{
$this->setDescription('Can be used to to start scripts via tty');
$this->setName('runt');
$this->addArgument('param', InputArgument::REQUIRED, 'Name of script which need to run from "extra" block');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$commands = StaticHelper::getAllExtra('composer.json', 'extra-commands');
if (!isset($commands[$input->getArgument('param')])) {
echo $input->getArgument('param').' - not found in extra -> extra-commands block';
exit(1);
}
$process = new \Symfony\Component\Process\Process($commands[$input->getArgument('param')]);
$process->setTty(true);
$process->run();
}


}
52 changes: 52 additions & 0 deletions src/StaticHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Created by PhpStorm.
* User: vladitot
* Date: 03.09.18
* Time: 16:28
*/

namespace ExtraPlugin;


class StaticHelper
{
/**
* Getter params from extra
* Say them, which key do you want to get. Another time you will able to export them, for example.
* @param $file
* @param $searchForString
* @return array
*/
public static function getAllExtra($file, $searchForString) {
$searchFor = explode('-', $searchForString);
$searchable = [];

$content = json_decode(file_get_contents($file), true);
if (isset($content['extra'])) {
$currentEl = $content['extra'];

foreach ($searchFor as $item) {
if (isset($currentEl[$item])) {
$currentEl = $currentEl[$item];
} else {
break;
}
}
if ($currentEl != $content['extra']) {
$searchable = $currentEl;
}
}

if (isset($content['extra']['merge-plugin']['include'])) {
$extraIncludes = $content['extra']['merge-plugin']['include'];
$includedFoundSearchables = [];
foreach ($extraIncludes as $file) {
$includedFoundSearchables = array_replace($includedFoundSearchables, self::getAllExtra($file, $searchForString));
}
} else {
$includedFoundSearchables = [];
}
return array_replace($searchable, $includedFoundSearchables);
}
}

0 comments on commit ec0a2dd

Please sign in to comment.