Skip to content

Commit 6dc5ab2

Browse files
authored
First release (#7)
* Create README.md * Feature/git (#3) * Initial commit * Added file1 and file2 * Initial commit * Added two init files * Add 'modules/' from commit '9cf59fb669d37e196d06bd223dfaeba79feab3fc' git-subtree-dir: modules git-subtree-mainline: 9cf59fb git-subtree-split: 9cf59fb * Error subtrees fixed * Changed files module, from parent * Changed files module, from parent * Changing files in parent and module project * Changing files in parent and module project * Adding another file to module folder * Adding another file to module folder * Removed some files * added gitignore * Deleted idea folder * Initial commit * Composer & TestClass * Fix files * gitignore * Added subtree for testing * Added option (add subtree), to menu * Add subtree working, in console environment * Delete subtree content * Delete subtree content * Adding menu subtrees * Git add subtree refactoring * Fix variable names * Add Trait for write composer file * Refactor Add Subtree * Subtree checker added * Fix error add subtree * Testing subtrees * Testing subtrees * Testing subtrees * Testing subtrees * Testing subtrees * Testing subtrees * Remove subtree * Fixing checking package name * Remove subtree * Fixing add subtree * Push subtree done * test * test * test comment * test comment * Add Pull functionality. * Refactor same behaviour select questions. * Git subtree add, added remote * Testing subtrees * Git remove subtree added. * Removing Articstudio/git-test subtree * Removing Maurisss94/test-subtrees subtree * Add ask to remove subtree of composer.json * Delete remote when remove subtree * Removing Articstudio/git-test subtree * Fixed option exit, interrupt execution done. * Remove subtree tag. * Release/1.x (#5) * Initial commit * Added file1 and file2 * Initial commit * Added two init files * Add 'modules/' from commit '9cf59fb669d37e196d06bd223dfaeba79feab3fc' git-subtree-dir: modules git-subtree-mainline: 9cf59fb git-subtree-split: 9cf59fb * Error subtrees fixed * Changed files module, from parent * Changed files module, from parent * Changing files in parent and module project * Changing files in parent and module project * Adding another file to module folder * Adding another file to module folder * Removed some files * added gitignore * Deleted idea folder * Initial commit * Composer & TestClass * Fix files * gitignore * Added subtree for testing * Added option (add subtree), to menu * Add subtree working, in console environment * Delete subtree content * Delete subtree content * Adding menu subtrees * Git add subtree refactoring * Fix variable names * Add Trait for write composer file * Refactor Add Subtree * Subtree checker added * Fix error add subtree * Testing subtrees * Testing subtrees * Testing subtrees * Testing subtrees * Testing subtrees * Testing subtrees * Remove subtree * Fixing checking package name * Remove subtree * Fixing add subtree * Push subtree done * test * test * test comment * test comment * Add Pull functionality. * Refactor same behaviour select questions. * Git subtree add, added remote * Testing subtrees * Git remove subtree added. * Removing Articstudio/git-test subtree * Removing Maurisss94/test-subtrees subtree * Add ask to remove subtree of composer.json * Delete remote when remove subtree * Removing Articstudio/git-test subtree * Init Composer install * Fixed option exit, interrupt execution done. * Remove subtree tag. * Fixing error commands when add and remove subtree. * Adding module for testing. * Fixing add subtree, commit before add subtree * Fixing add subtree, commit before add subtree * Add subtree Articstudio/git-test * Testing add and remove subtree * Add subtree Articstudio/git-test * Testing add and remove subtree * Add subtree Articstudio/git-test * Testing add and remove subtree * Testing add and remove subtree * Add subtree Articstudio/git-test * Testing add and remove subtree * Add subtree Articstudio/git-test * Removing Articstudio/git-test subtree * Testing add and remove subtree * Removing Articstudio/git-test subtree * Add and remove works fine * Add subtree Articstudio/git-test * Installing dev packages. * Composer install packages, done. * Get dev packages, works, added to menu. * Add subtree Maurisss94/test-subtrees * testing subtree and composer dev packages * testing subtree and composer dev packages * Removing Articstudio/git-test subtree * Removing Maurisss94/test-subtrees subtree * Menu functionality added, works fine. * Update packages versions in progress. * Update packages versions done. * Removing Articstudio/git-test subtree * Removing Maurisss94/test-subtrees subtree * Composer normalize, testing and refactoring. * Composer normalize, testing and refactoring. * Composer normalize, testing and refactoring. * Delete error subtree * Composer normalize done. * Refactoring main methods, execute. * Removing Articstudio/git-test subtree * Feature/refactor (#6) * Initial commit * Added file1 and file2 * Initial commit * Added two init files * Add 'modules/' from commit '9cf59fb669d37e196d06bd223dfaeba79feab3fc' git-subtree-dir: modules git-subtree-mainline: 9cf59fb git-subtree-split: 9cf59fb * Error subtrees fixed * Changed files module, from parent * Changed files module, from parent * Changing files in parent and module project * Changing files in parent and module project * Adding another file to module folder * Adding another file to module folder * Removed some files * added gitignore * Deleted idea folder * Initial commit * Composer & TestClass * Fix files * gitignore * test * test comment * testing subtree and composer dev packages * Delete subtree "0". * Refactoring Add and remove subtree. * Remove unused option of remove subtree. * Removing Articstudio/git-test subtree * Fix error adding subtree. * Removing Articstudio/git-test subtree * Removing Maurisss94/test-subtrees subtree * remove unused function * Removing Articstudio/git-test subtree * Refactoring pull subtrees. * Refactor pull subtrees, function. * Refactor rename function more abstract. * Removing Articstudio/git-test subtree
1 parent 8342898 commit 6dc5ab2

21 files changed

+1119
-107
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# php-bin
2+
PHP bin helpers

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929
"config": {
3030
"optimize-autoloader": true,
3131
"preferred-install": "dist",
32-
"sort-packages": true
32+
"sort-packages": true,
33+
"subtree": {
34+
"Articstudio/git-test": "[email protected]:articstudio/git-test.git",
35+
"Maurisss94/test-subtrees": "[email protected]:Maurisss94/test-subtrees.git"
36+
}
3337
},
3438
"extra": {},
3539
"autoload": {
@@ -59,4 +63,4 @@
5963
"issues": "https://github.com/articstudio/php-bin/issues",
6064
"source": "https://github.com/articstudio/php-bin"
6165
}
62-
}
66+
}

src/Commands/AbstractCommand.php

Lines changed: 87 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,97 @@
11
<?php
2+
23
namespace Articstudio\PhpBin\Commands;
34

45
use Symfony\Component\Console\Command\Command as SymfonyCommand;
56
use Articstudio\PhpBin\Ui\Menu;
67
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Input\InputInterface;
79
use Symfony\Component\Console\Input\ArrayInput;
10+
use Symfony\Component\Console\Output\ConsoleOutput;
811
use Articstudio\PhpBin\PhpBinException;
12+
use Symfony\Component\Console\Question\Question;
13+
use Symfony\Component\Console\Question\ConfirmationQuestion;
14+
use Symfony\Component\Console\Question\ChoiceQuestion;
15+
16+
abstract class AbstractCommand extends SymfonyCommand {
17+
18+
use \Articstudio\PhpBin\Concerns\HasOutput;
19+
use \Articstudio\PhpBin\Concerns\HasShell;
20+
use \Articstudio\PhpBin\Concerns\HasPhpBin;
21+
22+
23+
/**
24+
* Create menu
25+
*
26+
* @param string $title
27+
* @param array $options
28+
*
29+
* @return Menu
30+
*/
31+
public function menu( string $title, array $options ): Menu {
32+
return new Menu( $title, $options );
33+
}
34+
35+
public function showMenu( string $title, array $menu_options ) {
36+
return $this->menu( $title, $menu_options )->open() ?? null;
37+
}
38+
39+
public function showChoices( string $message, array $packages ) {
40+
return $this->choiceQuestion( $message, $packages );
41+
}
42+
43+
public function question( string $txt, $default = null, ?OutputInterface $output = null, ?InputInterface $input = null ) {
44+
$question_helper = $this->getHelper( 'question' );
45+
$question = new Question( $txt, $default );
46+
47+
return $question_helper->ask(
48+
( $input ?? new ArrayInput( [] ) ),
49+
( $output ?? new ConsoleOutput ),
50+
$question
51+
);
52+
}
53+
54+
public function confirmation( string $txt, $default = false, ?OutputInterface $output = null, ?InputInterface $input = null ) {
55+
$question_helper = $this->getHelper( 'question' );
56+
$question = new ConfirmationQuestion( $txt, $default );
57+
58+
return ! ! $question_helper->ask(
59+
( $input ?? new ArrayInput( [] ) ),
60+
( $output ?? new ConsoleOutput ),
61+
$question
62+
);
63+
}
64+
65+
public function choiceQuestion( string $txt, array $options, ?OutputInterface $output = null, ?InputInterface $input = null ) {
66+
$question_helper = $this->getHelper( 'question' );
67+
$question = new ChoiceQuestion( $txt, $options );
68+
$question->setMultiselect( true );
69+
70+
return $question_helper->ask(
71+
( $input ?? new ArrayInput( [] ) ),
72+
( $output ?? new ConsoleOutput ),
73+
$question
74+
);
75+
76+
77+
}
78+
79+
/**
80+
* Call command by name
81+
*
82+
* @param string $name
83+
* @param array $arguments
84+
* @param OutputInterface $output
85+
*
86+
* @return int
87+
*/
88+
protected function callCommandByName( string $name, ?array $arguments, OutputInterface $output ) {
89+
if ( ! $this->getApplication()->has( $name ) ) {
90+
throw new PhpBinException( "Command `{$name}` not found." );
91+
}
92+
$command = $this->getApplication()->get( $name );
93+
$input = new ArrayInput( $arguments ?? [] );
994

10-
abstract class AbstractCommand extends SymfonyCommand
11-
{
12-
13-
use \Articstudio\PhpBin\Concerns\HasOutput;
14-
use \Articstudio\PhpBin\Concerns\HasShell;
15-
use \Articstudio\PhpBin\Concerns\HasPhpBin;
16-
17-
/**
18-
* Create menu
19-
*
20-
* @param string $title
21-
* @param array $options
22-
* @return Menu
23-
*/
24-
public function menu(string $title, array $options): Menu
25-
{
26-
return new Menu($title, $options);
27-
}
28-
29-
/**
30-
* Call command by name
31-
*
32-
* @param string $name
33-
* @param array $arguments
34-
* @param OutputInterface $output
35-
* @return int
36-
*/
37-
protected function callCommandByName(string $name, ?array $arguments, OutputInterface $output)
38-
{
39-
if (!$this->getApplication()->has($name)) {
40-
throw new PhpBinException("Command `{$name}` not found.");
41-
}
42-
$command = $this->getApplication()->get($name);
43-
$input = new ArrayInput($arguments ?? []);
44-
return $command->run($input, $output);
45-
}
95+
return $command->run( $input, $output );
96+
}
4697
}

src/Commands/Composer/ComposerProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ class ComposerProvider extends AbstractProvider
1616
'\Articstudio\PhpBin\Commands\Composer\Install',
1717
'\Articstudio\PhpBin\Commands\Composer\Update',
1818
'\Articstudio\PhpBin\Commands\Composer\Normalize',
19+
'\Articstudio\PhpBin\Commands\Composer\GetDevPackages',
1920
];
2021
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: mauro
5+
* Date: 24/01/19
6+
* Time: 13:22
7+
*/
8+
9+
namespace Articstudio\PhpBin\Commands\Composer\Concerns;
10+
11+
12+
trait HasComposerBehaviour {
13+
14+
protected function getComposerJson( $dirname ) {
15+
$command = 'find ' . $dirname . ' -type f -name "composer.json"';
16+
list( $exit_code, $output, $exit_code_txt, $error ) = $this->callShell( $command, false );
17+
$return = array_filter( explode( "\n", $output ), function ( $value ) {
18+
return $value !== '';
19+
} );
20+
21+
return ( $exit_code === 0 ) ? $return : [];
22+
}
23+
24+
protected function getModulesByOption( $option ) {
25+
$modules = [];
26+
if ( $option === 'select' ) {
27+
$modules = $this->showChoices( "Select a module to normalize composer: ", array_keys( $this->getSubtrees() ) );
28+
} else if ( $option === 'all' ) {
29+
$modules = array_keys( $this->getSubtrees() );
30+
} else if ( $option === 'root' ) {
31+
$modules[] = $this->getComposerFile();
32+
}
33+
34+
return $modules;
35+
}
36+
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: mauro
5+
* Date: 22/01/19
6+
* Time: 14:45
7+
*/
8+
9+
namespace Articstudio\PhpBin\Commands\Composer\Concerns;
10+
11+
use Articstudio\PhpBin\Application;
12+
13+
trait HasComposerConfig {
14+
15+
public function getComposerData() {
16+
return Application::getInstance()->getComposer()['data'] ?? [];
17+
}
18+
19+
public function getComposerFile() {
20+
return Application::getInstance()->getComposer()['file'] ?? '';
21+
}
22+
23+
public function getComposerPath() {
24+
return Application::getInstance()->getComposer()['directory'];
25+
}
26+
27+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: mauro
5+
* Date: 24/01/19
6+
* Time: 9:29
7+
*/
8+
9+
namespace Articstudio\PhpBin\Commands\Composer;
10+
11+
use Articstudio\PhpBin\Commands\AbstractCommand as PhpBinCommand;
12+
13+
use Symfony\Component\Console\Input\InputArgument;
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
17+
class GetDevPackages extends PhpBinCommand {
18+
19+
use \Articstudio\PhpBin\Concerns\HasWriteComposer;
20+
use Concerns\HasComposerConfig;
21+
use Concerns\HasComposerBehaviour;
22+
use \Articstudio\PhpBin\Commands\Git\Subtree\Concerns\HasSubtreesConfig;
23+
24+
protected $composer;
25+
26+
/**
27+
* Command name
28+
*
29+
* @var string
30+
*/
31+
protected static $defaultName = 'composer:dev-packages';
32+
33+
protected function configure() {
34+
$this->addArgument( 'module_name', InputArgument::OPTIONAL, 'Nom del mòdul:' );
35+
}
36+
37+
protected function execute( InputInterface $input, OutputInterface $output ) {
38+
$this->composer = $this->getComposerData();
39+
$module_dir = $input->getArgument( 'module_name' ) ?: null;
40+
$menu_options = array(
41+
'select' => 'Select a single module',
42+
'all' => 'All modules'
43+
);
44+
45+
if ( $module_dir === null ) {
46+
$option = $this->showMenu( "Update packages versions", $menu_options );
47+
$modules = $this->getModulesByOption( $option );
48+
} else {
49+
$modules[] = $module_dir;
50+
}
51+
52+
53+
$requires_dev = array(
54+
'require-dev' => array()
55+
);
56+
foreach ( $modules as $module_name) {
57+
$this->composer = array_merge( $this->composer, $requires_dev );
58+
59+
array_map( function ( $name ) {
60+
$this->mergeDependencies( $name );
61+
}, $this->getComposerJson( $module_name ) );
62+
}
63+
64+
65+
$this->writeComposer( $this->composer, $this->getComposerFile() );
66+
}
67+
68+
69+
protected function addDependencies( $dependencies, $fname ) {
70+
if ( ! $dependencies ) {
71+
return;
72+
}
73+
foreach ( $dependencies as $dependency => $version ) {
74+
if ( ! key_exists( $dependency, $this->composer['require'] ) && ! key_exists( $dependency, $this->composer['require-dev'] ) ) {
75+
$this->composer['require-dev'][ $dependency ] = $version;
76+
printf( " + %s@%s \n", $dependency, $version );
77+
} else {
78+
if ( key_exists( $dependency, $this->composer['require-dev'] ) && $this->composer['require-dev'][ $dependency ] === $version
79+
|| key_exists( $dependency, $this->composer['require'] ) && $this->composer['require'][ $dependency ] === $version ) {
80+
printf( " = %s@%s \n", $dependency, $version );
81+
} else {
82+
printf( " ! %s@%s \n", $dependency, $version );
83+
}
84+
}
85+
}
86+
}
87+
88+
private function mergeDependencies( $fname ) {
89+
printf( "%s: \n", $fname );
90+
$data = json_decode( file_get_contents( $fname ), true );
91+
if ( key_exists( 'require', $data ) ) {
92+
$this->addDependencies( $data['require'], $fname );
93+
}
94+
if ( key_exists( 'require-dev', $data ) ) {
95+
$this->addDependencies( $data['require-dev'], $fname );
96+
}
97+
}
98+
99+
protected function showNewPackageQuestions() {
100+
return $this->question( 'Please enter the name of the module where you want to get the require/require-dev packages: ' );
101+
}
102+
103+
}

0 commit comments

Comments
 (0)