Skip to content

Commit

Permalink
[Monorepo] init
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 28, 2018
0 parents commit 06b67ad
Show file tree
Hide file tree
Showing 20 changed files with 679 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
@@ -0,0 +1,6 @@
/tests export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
phpunit.xml export-ignore
README.md export-ignore
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/vendor
composer.lock
14 changes: 14 additions & 0 deletions .travis.yml
@@ -0,0 +1,14 @@
language: php

php:
- 7.1
- 7.2

install:
- composer install --prefer-source

script:
- vendor/bin/phpunit

notifications:
email: never
25 changes: 25 additions & 0 deletions LICENSE
@@ -0,0 +1,25 @@
The MIT License
---------------

Copyright (c) 2017-present Tomáš Votruba (http://tomasvotruba.cz)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
17 changes: 17 additions & 0 deletions README.md
@@ -0,0 +1,17 @@
# Monorepo - Build and Maintain Monorepo like a Boss

[![Build Status](https://img.shields.io/travis/Symplify/Monorepo/master.svg?style=flat-square)](https://travis-ci.org/Symplify/Monorepo)
[![Downloads](https://img.shields.io/packagist/dt/symplify/monorepo.svg?style=flat-square)](https://packagist.org/packages/symplify/monorepo)
[![Subscribe](https://img.shields.io/badge/subscribe-to--releases-green.svg?style=flat-square)](https://libraries.io/packagist/symplify%2Fmonorepo)


## Install

## Usage

### 1. Create Monolitic Repository from one

- empty directory
- output
- local monorepo config.
- command "monorepo build"
13 changes: 13 additions & 0 deletions bin/monorepo
@@ -0,0 +1,13 @@
#!/usr/bin/env php
<?php declare(strict_types=1);

require_once __DIR__ . '/monorepo-bootstrap.php';

use Symplify\Monorepo\Console\Application;
use Symplify\Monorepo\DependencyInjection\ContainerFactory;

$container = (new ContainerFactory())->create();

/** @var Application $application */
$application = $container->get(Application::class);
$application->run();
18 changes: 18 additions & 0 deletions bin/monorepo-bootstrap.php
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

$possibleAutoloadPaths = [
// composer create-project
__DIR__ . '/../../../autoload.php',
// composer require
__DIR__ . '/../vendor/autoload.php',
// mono-repository
__DIR__ . '/../../../vendor/autoload.php',
];

foreach ($possibleAutoloadPaths as $possibleAutoloadPath) {
if (is_file($possibleAutoloadPath)) {
require_once $possibleAutoloadPath;

break;
}
}
28 changes: 28 additions & 0 deletions composer.json
@@ -0,0 +1,28 @@
{
"name": "symplify/monorepo",
"description": "Build and maintain monorepo (monolithic repository) like a boss.",
"license": "MIT",
"require": {
"cpliakas/git-wrapper": "^2.0",
"symfony/console": "^4.0",
"symfony/process": "^4.0",
"symfony/http-kernel": "^4.0",
"symfony/config": "^4.0",
"symfony/dependency-injection": "^4.0",
"symfony/yaml": "^4.0",
"symplify/package-builder": "^3.2"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
},
"autoload": {
"psr-4": {
"Symplify\\Monoraper\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Symplify\\Monoraper\\Tests\\": "tests"
}
}
}
10 changes: 10 additions & 0 deletions phpunit.xml
@@ -0,0 +1,10 @@
<phpunit
bootstrap="tests/bootstrap.php"
colors="true"
verbose="true"
>
<!-- tests directories to run -->
<testsuite>
<directory>tests</directory>
</testsuite>
</phpunit>
45 changes: 45 additions & 0 deletions src/Console/Application.php
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

namespace Symplify\Monorepo\Console;

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

final class Application extends SymfonyApplication
{
public function __construct()
{
parent::__construct('Monorepo');
}

protected function getDefaultInputDefinition(): InputDefinition
{
$defaultInputDefinition = parent::getDefaultInputDefinition();

$this->removeUnusedOptions($defaultInputDefinition);
$this->addCustomOptions($defaultInputDefinition);

return $defaultInputDefinition;
}

private function removeUnusedOptions(InputDefinition $inputDefinition): void
{
$options = $inputDefinition->getOptions();

unset($options['quiet'], $options['version'], $options['no-interaction']);

$inputDefinition->setOptions($options);
}

private function addCustomOptions(InputDefinition $inputDefinition): void
{
$inputDefinition->addOption(new InputOption(
'config',
null,
InputOption::VALUE_REQUIRED,
'Path to config file.',
getcwd() . '/Monorepo.yml'
));
}
}
96 changes: 96 additions & 0 deletions src/Console/Command/MergeRepositoryToPackagesCommand.php
@@ -0,0 +1,96 @@
<?php declare(strict_types=1);

namespace Symplify\Monorepo\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symplify\Monorepo\Exception\MissingConfigurationException;
use Symplify\Monorepo\Filesystem\Filesystem;
use Symplify\Monorepo\Worker\MoveHistoryWorker;
use Symplify\Monorepo\Worker\RepositoryWorker;

final class MergeRepositoryToPackagesCommand extends Command
{
/**
* @var string
*/
private const DIRECTORY_OPTION_NAME = 'directory';

/**
* @var string
*/
private const REPOSITORY_ARGUMENT_NAME = 'git-repository';

/**
* @var RepositoryWorker
*/
private $fetchRepositoryWorker;

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

/**
* @var MoveHistoryWorker
*/
private $moveHistoryWorker;

public function __construct(
RepositoryWorker $fetchRepositoryWorker,
Filesystem $filesystem,
MoveHistoryWorker $moveHistoryWorker
) {
$this->fetchRepositoryWorker = $fetchRepositoryWorker;
$this->filesystem = $filesystem;
$this->moveHistoryWorker = $moveHistoryWorker;

parent::__construct();
}

protected function configure(): void
{
$this->setName('merge-repository-to-package');
$this->addArgument(self::REPOSITORY_ARGUMENT_NAME, InputArgument::REQUIRED, 'Path to .git repository');
$this->addOption(
self::DIRECTORY_OPTION_NAME,
null,
InputOption::VALUE_REQUIRED,
'Local directory'
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->ensurePackageDirectoryIsSet($input);

// run.sh - DONE
$repository = $input->getArgument(self::REPOSITORY_ARGUMENT_NAME);

$this->fetchRepositoryWorker->fetchAndMergeRepository($repository);

// run2.sh
$cwd = getcwd();
$newPackageDirectory = $input->getOption(self::DIRECTORY_OPTION_NAME);

$finder = $finder = $this->filesystem->findMergedPackageFiles($cwd);
$this->filesystem->copyFinderFilesToDirectory($finder, $newPackageDirectory);
$this->moveHistoryWorker->prependHistoryToNewPackageFiles($finder, $newPackageDirectory);
$this->filesystem->clearEmptyDirectories($cwd);
}

private function ensurePackageDirectoryIsSet(InputInterface $input): void
{
$directory = $input->getOption(self::DIRECTORY_OPTION_NAME);
if ($directory) {
return;
}

throw new MissingConfigurationException(sprintf(
'Configuration option "--%s" is missing',self::DIRECTORY_OPTION_NAME
));
}
}
22 changes: 22 additions & 0 deletions src/DependencyInjection/CompilerPass/CollectorCompilerPass.php
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace Symplify\Monorepo\DependencyInjection\CompilerPass;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symplify\PackageBuilder\DependencyInjection\DefinitionCollector;

final class CollectorCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $containerBuilder): void
{
DefinitionCollector::loadCollectorWithType(
$containerBuilder,
Application::class,
Command::class,
'add'
);
}
}
17 changes: 17 additions & 0 deletions src/DependencyInjection/ContainerFactory.php
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Symplify\Monorepo\DependencyInjection;

use Psr\Container\ContainerInterface;
use Symplify\Monorepo\MonorepoKernel;

final class ContainerFactory
{
public function create(): ContainerInterface
{
$MonorepoKernel = new MonorepoKernel();
$MonorepoKernel->boot();

return $MonorepoKernel->getContainer();
}
}
9 changes: 9 additions & 0 deletions src/Exception/MissingConfigurationException.php
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace Symplify\Monorepo\Exception;

use Exception;

final class MissingConfigurationException extends Exception
{
}
54 changes: 54 additions & 0 deletions src/Filesystem/Filesystem.php
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);

namespace Symplify\Monorepo\Filesystem;

use Nette\Utils\FileSystem as NetteFileSystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

final class Filesystem
{
/**
* @var string[]
*/
private const LOCAL_DIRS = ['packages', 'vendor', '.idea'];

public function findMergedPackageFiles(string $directory): Finder
{
return Finder::create()
->files()
->in($directory)
->exclude(self::LOCAL_DIRS)
// include .gitignore, .travis etc
->ignoreDotFiles(false);
}

/**
* - find new files with finder
* - copy to new directory
*/
public function copyFinderFilesToDirectory(Finder $finder, string $directory): void
{
foreach ($finder->getIterator() as $fileInfo) {
NetteFileSystem::copy($fileInfo->getRealPath(), $directory . '/' . $fileInfo->getRelativePathname());
}
}

public function clearEmptyDirectories(string $path): void
{
$emptyDirectoriesFinder = Finder::create()
->directories()
->in($path)
->exclude(self::LOCAL_DIRS)
// sort from deepest to top to allow removal in same direction
->sort(function (SplFileInfo $firstFileInfo, SplFileInfo $secondFileInfo) {
return strlen($firstFileInfo->getRealPath()) < strlen($secondFileInfo->getRealPath());
})
// empty directory
->size(0);

foreach ($emptyDirectoriesFinder->getIterator() as $emptyDirectoryFileInfo) {
rmdir($emptyDirectoryFileInfo->getRelativePathname());
}
}
}

0 comments on commit 06b67ad

Please sign in to comment.