Skip to content

Commit

Permalink
Merge pull request #2 from mleko/register
Browse files Browse the repository at this point in the history
Register
  • Loading branch information
mleko authored Aug 22, 2017
2 parents 3f3017d + 747d828 commit 7cae5d5
Show file tree
Hide file tree
Showing 19 changed files with 279 additions and 34 deletions.
21 changes: 7 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,18 @@ Format `composer.json` in current directory
```
$ cd /project-dir
$ wingman
Formatting file: /project-dir/composer.json
Formatting file: ./composer.json
```

## Composer script
Install `mleko/wingman` as dependency
```
$ composer require --dev mleko/wingman
```

add `post-update-cmd` script `Mleko\\Wingman\\Composer\\EventHandler::format`
$ vendor/bin/wingman register
Register wingman in file: ./composer.json
Wingman registered
Formatting file: ./composer.json
example
```
{
...
"scripts": {
"post-update-cmd": [
"Mleko\\Wingman\\Composer\\EventHandler::format"
]
}
}
```

`composer.json` will be reformatted after every package update.
1 change: 1 addition & 0 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct()
parent::__construct("Wingman");

$this->add(new FormatCommand());
$this->add(new RegisterCommand());

$this->setDefaultCommand("format");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Console/FormatCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function configure()
parent::configure();
$this->setName("format")
->setDescription("Format composer.json file")
->addArgument("path", InputArgument::OPTIONAL, "Path to composer.json file", getcwd() . DIRECTORY_SEPARATOR . "composer.json");
->addArgument("path", InputArgument::OPTIONAL, "Path to composer.json file", "./composer.json");
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand Down
36 changes: 36 additions & 0 deletions src/Console/RegisterCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Wingman
*
* @link http://github.com/mleko/wingman
* @copyright Copyright (c) 2017 Daniel Król
* @license MIT
*/

namespace Mleko\Wingman\Console;


use Mleko\Wingman\Wingman;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class RegisterCommand extends Command
{
protected function configure()
{
parent::configure();
$this->setName("register")
->setDescription("Register wingman as post-update script and format composer.json file")
->addArgument("path", InputArgument::OPTIONAL, "Path to composer.json file", "./composer.json");
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$path = $input->getArgument("path");
$wingman = new Wingman();
$wingman->registerInFile($path, new ConsoleOutputAdapter($output));
}

}
65 changes: 61 additions & 4 deletions src/Wingman.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,47 @@ public function format($composerJson)

public function formatFile($path, Output $output)
{
if (!is_readable($path) || !is_writable($path)) {
$output->write("composer.json is not readable/writable\n");
return $this->processFile($path, $output);
}

public function registerInFile($path, Output $output)
{
return $this->processFile($path, $output, true);
}

public function register($composerJson)
{
if (!isset($composerJson->scripts)) {
$composerJson->scripts = new \stdClass();
}
if (!isset($composerJson->scripts->{"post-update-cmd"})) {
$composerJson->scripts->{"post-update-cmd"} = [];
}
$puc = &$composerJson->scripts->{"post-update-cmd"};
if (is_array($puc)) {
if (!in_array("Mleko\\Wingman\\Composer\\EventHandler::format", $puc)) {
$puc[] = "Mleko\\Wingman\\Composer\\EventHandler::format";
}
} elseif ($puc !== "Mleko\\Wingman\\Composer\\EventHandler::format") {
$puc = [$puc, "Mleko\\Wingman\\Composer\\EventHandler::format"];
}
return $composerJson;
}

private function processFile($path, Output $output, $register = false)
{
$composerJson = $this->readFile($path, $output);
if (null === $composerJson) {
return false;
}
if ($register) {
$output->write(sprintf("Register wingman in file: %s\n", $path));
$composerJson = $this->register($composerJson);
$output->write("Wingman registered\n");
}
$output->write(sprintf("Formatting file: %s\n", $path));
$composerJson = json_decode(file_get_contents($path));
$composerJson = $this->format($composerJson);
return false !== file_put_contents($path, json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n");
return $this->writeFile($path, $composerJson);
}

/**
Expand All @@ -93,4 +126,28 @@ private function normalizeConfig(array $config): Rules
}
return new Rules($rules);
}

/**
* @param $path
* @param Output $output
* @return string|null
*/
private function readFile($path, Output $output)
{
if (!is_readable($path) || !is_writable($path)) {
$output->write("composer.json is not readable/writable\n");
return null;
}
return json_decode(file_get_contents($path));
}

/**
* @param $path
* @param $composerJson
* @return bool
*/
private function writeFile($path, $composerJson): bool
{
return false !== file_put_contents($path, json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n");
}
}
52 changes: 52 additions & 0 deletions tests/Console/RegisterCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Wingman
*
* @link http://github.com/mleko/wingman
* @copyright Copyright (c) 2017 Daniel Król
* @license MIT
*/

namespace Mleko\Wingman\Console;

use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;

class RegisterCommandTest extends TestCase
{
/**
* @var vfsStreamDirectory
*/
private $root;

/**
* set up test environment
*/
public function setUp()
{
$this->root = vfsStream::setup('testRoot');
}

public function testRegisterCommand()
{
$virtualFile = vfsStream::newFile("composer.json");
$this->root->addChild($virtualFile);
$virtualFile->setContent(json_encode(["name" => "acme/test"]));

$application = new Application();
$registerCommand = $application->find("register");
$commandTester = new CommandTester($registerCommand);

$resultCode = $commandTester->execute([
"command" => $registerCommand->getName(),
"path" => $virtualFile->url()
]);

$this->assertEquals(0, $resultCode);
$contents = json_decode($virtualFile->getContent());
$this->assertContains("Mleko\\Wingman\\Composer\\EventHandler::format", $contents->scripts->{"post-update-cmd"});
$this->assertEquals("Register wingman in file: vfs://testRoot/composer.json\nWingman registered\nFormatting file: vfs://testRoot/composer.json\n", $commandTester->getDisplay());
}
}
58 changes: 43 additions & 15 deletions tests/WingmanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,26 @@ public function testFormat($expected, $input)

public function caseProvider()
{
$cases = [];
$iterator = new \DirectoryIterator(__DIR__ . "/fixtures");
$pattern = "!^composer\.(\d+)\.input\.json$!";
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
if (!preg_match($pattern, $item->getFilename(), $matches)) {
continue;
}
$cases[] = [
json_decode(file_get_contents(__DIR__ . "/fixtures/composer." . $matches[1] . ".expected.json")),
json_decode(file_get_contents(__DIR__ . "/fixtures/composer." . $matches[1] . ".input.json"))
];
}
return $cases;
return $this->fixtureReader(__DIR__ . "/fixtures", $pattern);
}

/**
* @dataProvider registerCaseProvider
*/
public function testRegister($expected, $input)
{
$wingman = new Wingman();
$actual = $wingman->register($input);
$this->assertEquals($expected, $actual);
$jsonOptions = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
$this->assertEquals(json_encode($expected, $jsonOptions), json_encode($actual, $jsonOptions));
}

public function registerCaseProvider()
{
$pattern = "!^composer\.(\d+)\.input\.json$!";
return $this->fixtureReader(__DIR__ . "/fixtures/register", $pattern);
}

public function testMissingFile()
Expand Down Expand Up @@ -117,4 +121,28 @@ public function testFormattingFile()

$this->assertEquals(file_get_contents(__DIR__ . "/fixtures/composer.1.expected.json"), $virtualFile->getContent());
}

/**
* @param $path
* @param $pattern
* @return array
*/
private function fixtureReader($path, $pattern): array
{
$iterator = new \DirectoryIterator($path);
$cases = [];
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
if (!preg_match($pattern, $item->getFilename(), $matches)) {
continue;
}
$cases[] = [
json_decode(file_get_contents("$path/composer." . $matches[1] . ".expected.json")),
json_decode(file_get_contents("$path/composer." . $matches[1] . ".input.json"))
];
}
return $cases;
}
}
9 changes: 9 additions & 0 deletions tests/fixtures/register/composer.1.expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"license": "MIT",
"name": "mleko/wingman",
"scripts": {
"post-update-cmd": [
"Mleko\\Wingman\\Composer\\EventHandler::format"
]
}
}
4 changes: 4 additions & 0 deletions tests/fixtures/register/composer.1.input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"license": "MIT",
"name": "mleko/wingman"
}
7 changes: 7 additions & 0 deletions tests/fixtures/register/composer.2.expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"scripts": {
"post-update-cmd": [
"Mleko\\Wingman\\Composer\\EventHandler::format"
]
}
}
7 changes: 7 additions & 0 deletions tests/fixtures/register/composer.2.input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"scripts": {
"post-update-cmd": [
"Mleko\\Wingman\\Composer\\EventHandler::format"
]
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/register/composer.3.expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"scripts": {
"test": "phpunit",
"post-update-cmd": [
"Mleko\\Wingman\\Composer\\EventHandler::format"
]
}
}
5 changes: 5 additions & 0 deletions tests/fixtures/register/composer.3.input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scripts": {
"test": "phpunit"
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/register/composer.4.expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"scripts": {
"post-update-cmd": [
"noop",
"Mleko\\Wingman\\Composer\\EventHandler::format"
]
}
}
5 changes: 5 additions & 0 deletions tests/fixtures/register/composer.4.input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scripts": {
"post-update-cmd": "noop"
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/register/composer.5.expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"scripts": {
"post-update-cmd": [
"noop",
"Mleko\\Wingman\\Composer\\EventHandler::format"
]
}
}
7 changes: 7 additions & 0 deletions tests/fixtures/register/composer.5.input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"scripts": {
"post-update-cmd": [
"noop"
]
}
}
5 changes: 5 additions & 0 deletions tests/fixtures/register/composer.6.expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scripts": {
"post-update-cmd": "Mleko\\Wingman\\Composer\\EventHandler::format"
}
}
5 changes: 5 additions & 0 deletions tests/fixtures/register/composer.6.input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scripts": {
"post-update-cmd": "Mleko\\Wingman\\Composer\\EventHandler::format"
}
}

0 comments on commit 7cae5d5

Please sign in to comment.