Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
[MonorepoBuilder] Add init command [closes #1082]
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 11, 2018
1 parent 512625f commit 88de2c8
Show file tree
Hide file tree
Showing 20 changed files with 173 additions and 10 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"Symplify\\EasyCodingStandard\\SniffRunner\\": "packages/EasyCodingStandard/packages/SniffRunner/src",
"Symplify\\LatteToTwigConverter\\": "packages/LatteToTwigConverter/src",
"Symplify\\MonorepoBuilder\\": "packages/MonorepoBuilder/src",
"Symplify\\MonorepoBuilder\\Init\\": "packages/MonorepoBuilder/packages/Init/src",
"Symplify\\MonorepoBuilder\\Split\\": "packages/MonorepoBuilder/packages/Split/src",
"Symplify\\PHPStanExtensions\\": "packages/PHPStanExtensions/src",
"Symplify\\PackageBuilder\\": "packages/PackageBuilder/src",
Expand Down
4 changes: 2 additions & 2 deletions packages/MonorepoBuilder/bin/monorepo-builder
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
require_once __DIR__ . '/autoload.php';

use Psr\Container\ContainerInterface;
use Symplify\PackageBuilder\Console\ConfigAwareApplication;
use Symfony\Component\Console\Application;
use Symplify\PackageBuilder\Console\ThrowableRenderer;

try {
/** @var ContainerInterface $container */
$container = require_once __DIR__ . '/container.php';

$application = $container->get(ConfigAwareApplication::class);
$application = $container->get(Application::class);
$application->run();
} catch (Throwable $throwable) {
(new ThrowableRenderer())->render($throwable);
Expand Down
3 changes: 2 additions & 1 deletion packages/MonorepoBuilder/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"autoload": {
"psr-4": {
"Symplify\\MonorepoBuilder\\": "src",
"Symplify\\MonorepoBuilder\\Split\\": "packages/Split/src"
"Symplify\\MonorepoBuilder\\Split\\": "packages/Split/src",
"Symplify\\MonorepoBuilder\\Init\\": "packages/Init/src"
}
},
"autoload-dev": {
Expand Down
7 changes: 7 additions & 0 deletions packages/MonorepoBuilder/packages/Init/config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
_defaults:
public: true
autowire: true

Symplify\MonorepoBuilder\Init\:
resource: '../src'
56 changes: 56 additions & 0 deletions packages/MonorepoBuilder/packages/Init/src/Command/InitCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php declare(strict_types=1);

namespace Symplify\MonorepoBuilder\Init\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Symplify\PackageBuilder\Console\Command\CommandNaming;

final class InitCommand extends Command
{
/**
* @var string
*/
private const OUTPUT = 'output';

/**
* @var Filesystem
*/
private $filesystem;

/**
* @var SymfonyStyle
*/
private $symfonyStyle;

public function __construct(Filesystem $filesystem, SymfonyStyle $symfonyStyle)
{
parent::__construct();
$this->filesystem = $filesystem;
$this->symfonyStyle = $symfonyStyle;
}

protected function configure(): void
{
$this->setName(CommandNaming::classToName(self::class));
$this->addArgument(self::OUTPUT, InputArgument::REQUIRED, 'Directory to generate monorepo into.', getcwd());
}

protected function execute(InputInterface $input, OutputInterface $output): void
{
$output = $input->getArgument(self::OUTPUT);

$this->filesystem->mirror(__DIR__ . '/../../templates/monorepo', $output);

$this->symfonyStyle->success('Congrats! Your first monorepo is here.');
$this->symfonyStyle->note(
'Now try the next step - merge composer.json files from packages to the root one: ' .
PHP_EOL .
'"vendor/bin/monorepo-builder merge"'
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "your-monorepo/your-monorepo",
"require": {
"php": "^7.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
data_to_append:
require-dev:
phpunit/phpunit: '^7.3'
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "your-monorepo/first-package",
"require": {
"php": "^7.1",
"symfony/http-kernel": "^4.1"
},
"autoload": {
"psr-4": {
"YourMonorepo\\FirstPackage\\": "src"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

namespace YourMonorepo\FirstPackage;

final class FirstClass
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "your-monorepo/second-package",
"require": {
"php": "^7.1",
"symfony/dependency-injection": "^4.1"
},
"autoload": {
"psr-4": {
"YourMonorepo\\SecondPackage\\": "src"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

namespace YourMonorepo\SecondPackage;

final class SecondClass
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ public function decorate(array $composerJson): array
}

$composerJson[$key] = $this->parametersMerger->merge($this->dataToAppend[$key], $composerJson[$key]);

unset($this->dataToAppend[$key]);
}

return $composerJson;
// add what was skipped
return array_merge($composerJson, $this->dataToAppend);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private function prefixPath(SplFileInfo $packageComposerFile, $path)
}

$composerDirectory = dirname($packageComposerFile->getRealPath());
$relativeDirectory = Strings::substring($composerDirectory, strlen(preg_quote(getcwd() . '/')));
$relativeDirectory = Strings::substring($composerDirectory, strlen(rtrim(getcwd(), '/') . '/'));

return $relativeDirectory . DIRECTORY_SEPARATOR . $path;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace Symplify\MonorepoBuilder\Console;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;

final class MonorepoBuilderApplication extends Application
{
protected function getDefaultInputDefinition(): InputDefinition
{
$inputDefinition = parent::getDefaultInputDefinition();

$inputDefinition->addOption(new InputOption(
'config',
'c',
InputOption::VALUE_REQUIRED,
'Path to config file.',
'monorepo-builder.(yml|yaml)'
));

return $inputDefinition;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Symplify\MonorepoBuilder\EventSubscriber;

use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symplify\MonorepoBuilder\Init\Command\InitCommand;
use Symplify\MonorepoBuilder\Validator\SourcesPresenceValidator;

final class ValidateBeforeCommandEventSubscriber implements EventSubscriberInterface
Expand All @@ -26,8 +28,12 @@ public static function getSubscribedEvents(): array
return [ConsoleEvents::COMMAND => 'validate'];
}

public function validate(): void
public function validate(ConsoleEvent $consoleEvent): void
{
if ($consoleEvent->getCommand() instanceof InitCommand) {
return;
}

$this->sourcesPresenceValidator->validate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace Symplify\MonorepoBuilder\Exception;

use Exception;

final class MissingDirectoryException extends Exception
{
}
1 change: 1 addition & 0 deletions packages/MonorepoBuilder/src/PackageComposerFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function getPackageComposerFiles(): array
$finder = Finder::create()
->files()
->in($this->packageDirectories)
->exclude('templates') // "init" command template data
->name('composer.json');

if (! $this->isPHPUnit()) {
Expand Down
1 change: 1 addition & 0 deletions packages/MonorepoBuilder/src/config/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
imports:
- { resource: 'services.yml' }
- { resource: '../../packages/**/config/config.yml' }
- { resource: '../../packages/**/src/config/config.yml' }

parameters:
Expand Down
8 changes: 5 additions & 3 deletions packages/MonorepoBuilder/src/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ services:

Symplify\PackageBuilder\Yaml\ParametersMerger: ~

Symplify\PackageBuilder\Console\ConfigAwareApplication:
Symplify\MonorepoBuilder\Console\MonorepoBuilderApplication:
calls:
- ['setDispatcher', ['@Symfony\Component\EventDispatcher\EventDispatcher']]
- ['setDispatcher', ['@Symfony\Component\EventDispatcher\EventDispatcher']]
Symfony\Component\Console\Application:
alias: 'Symplify\MonorepoBuilder\Console\MonorepoBuilderApplication'

# SymfonyStyle
Symfony\Component\Console\Input\ArgvInput: ~
Expand All @@ -22,9 +24,9 @@ services:
alias: 'Symfony\Component\Console\Output\ConsoleOutput'
Symfony\Component\Console\Style\SymfonyStyle: ~

Symfony\Component\EventDispatcher\EventDispatcher: ~

# alias needed for Symfony Console setter
Symfony\Component\EventDispatcher\EventDispatcher: ~
Symfony\Component\EventDispatcher\EventDispatcherInterface:
alias: 'Symfony\Component\EventDispatcher\EventDispatcher'

Expand Down
5 changes: 4 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ includes:

parameters:
excludes_analyse:
- *packages/MonorepoBuilder/packages/Init/templates/*
- *packages/CodingStandard/tests/**/correct*
- *packages/CodingStandard/tests/**/wrong*
# tests
Expand Down Expand Up @@ -115,6 +116,8 @@ parameters:

# getcwd
- '#Parameter \#1 \$gitDirectory of method Symplify\\MonorepoBuilder\\Split\\Git\\GitManager::getMostRecentTag\(\) expects string, string\|false given#'
- '#Parameter \#1 \$directory of function chdir expects string, string\|false given#'
- '#Parameter \#1 \$str of function rtrim expects string, string\|false given#'

# mixed type
- '#Call to an undefined method Psr\\Container\\ContainerInterface::getParameter\(\)#'
- '#Call to an undefined method Psr\\Container\\ContainerInterface::getParameter\(\)#'

0 comments on commit 88de2c8

Please sign in to comment.