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

Commit

Permalink
[ECS] Make phar ready by putting value objects to own namespace and e…
Browse files Browse the repository at this point in the history
…xclude dir
  • Loading branch information
TomasVotruba committed Jan 10, 2020
1 parent aeca4d1 commit 30a03df
Show file tree
Hide file tree
Showing 32 changed files with 199 additions and 113 deletions.
117 changes: 97 additions & 20 deletions packages/EasyCodingStandard/bin/ecs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,10 @@ use Symplify\EasyCodingStandard\Configuration\Configuration;
gc_disable();

# 1. autoload
$possibleAutoloadPaths = [
// after split package
__DIR__ . '/../vendor',
// dependency
__DIR__ . '/../../..',
// monorepo
__DIR__ . '/../../../vendor',
];

foreach ($possibleAutoloadPaths as $possibleAutoloadPath) {
if (is_file($possibleAutoloadPath . '/autoload.php')) {
require_once $possibleAutoloadPath . '/autoload.php';
require_once $possibleAutoloadPath . '/squizlabs/php_codesniffer/autoload.php';

// initalize PHPCS tokens
new Tokens();

break;
}
}
$autoloadIncluder = new AutoloadIncluder();
$autoloadIncluder->includeCwdVendorAutoloadIfExists();
$autoloadIncluder->autoloadProjectAutoloaderFile('/../../autoload.php');
$autoloadIncluder->includeDependencyOrRepositoryVendorAutoloadIfExists();

# 2. create container
$configs = [];
Expand Down Expand Up @@ -112,3 +96,96 @@ $configuration->setFirstResolverConfig($configResolver->getFirstResolvedConfig()
# 3. run
$application = $container->get(EasyCodingStandardConsoleApplication::class);
exit($application->run());


/**
* Inspired by https://github.com/rectorphp/rector/pull/2373/files#diff-0fc04a2bb7928cac4ae339d5a8bf67f3
*/
final class AutoloadIncluder
{
/**
* @var string[]
*/
private $alreadyLoadedAutoloadFiles = [];

public function includeCwdVendorAutoloadIfExists(): void
{
$cwdVendorAutoload = getcwd() . '/vendor/autoload.php';
if (!is_file($cwdVendorAutoload)) {
return;
}
$this->loadIfNotLoadedYet($cwdVendorAutoload, __METHOD__ . '()" on line ' . __LINE__);
}

public function includeDependencyOrRepositoryVendorAutoloadIfExists(): void
{
// ECS' vendor is already loaded
if (class_exists('\Symplify\EasyCodingStandard\HttpKernel\EasyCodingStandardKernel')) {
return;
}

$devOrPharVendorAutoload = __DIR__ . '/../vendor/autoload.php';
if (! is_file($devOrPharVendorAutoload)) {
return;
}

$this->loadIfNotLoadedYet($devOrPharVendorAutoload, __METHOD__ . '()" on line ' . __LINE__);
}
/**
* Inspired by https://github.com/phpstan/phpstan-src/blob/e2308ecaf49a9960510c47f5a992ce7b27f6dba2/bin/phpstan#L19
*/
public function autoloadProjectAutoloaderFile(string $file): void
{
$path = dirname(__DIR__) . $file;
if (!extension_loaded('phar')) {
if (is_file($path)) {
$this->loadIfNotLoadedYet($path, __METHOD__ . '()" on line ' . __LINE__);
}
} else {
$pharPath = Phar::running(false);
if ($pharPath === '') {
if (is_file($path)) {
$this->loadIfNotLoadedYet($path, __METHOD__ . '()" on line ' . __LINE__);
}
} else {
$path = dirname($pharPath) . $file;
if (is_file($path)) {
$this->loadIfNotLoadedYet($path, __METHOD__ . '()" on line ' . __LINE__);
}
}
}
}

private function loadIfNotLoadedYet(string $file, string $location): void
{
if (in_array($file, $this->alreadyLoadedAutoloadFiles, true)) {
return;
}

if ($this->isDebugOption()) {
echo sprintf(sprintf(
'File "%s" is about to be loaded in "%s"' . PHP_EOL,
$file,
$location
));
}

$this->alreadyLoadedAutoloadFiles[] = realpath($file);
require_once $file;

$phpCodeSnifferAutoload = dirname($file) . '/squizlabs/php_codesniffer/autoload.php';
if (!file_exists($phpCodeSnifferAutoload)) {
return;
}

require_once $phpCodeSnifferAutoload;

// initalize PHPCS tokens
new Tokens();
}

private function isDebugOption(): bool
{
return in_array('--debug', $_SERVER['argv'], true);
}
}
2 changes: 1 addition & 1 deletion packages/EasyCodingStandard/compiler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```bash
composer install
php bin/compile
bin/compile
```

The compiled PHAR will be in `tmp/ecs.phar`. Test it:
Expand Down
10 changes: 9 additions & 1 deletion packages/EasyCodingStandard/compiler/build/scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,13 @@ function (string $filePath, string $prefix, string $content): string {
return str_replace('__DIR__ . \'/..', '\'phar://ecs.phar', $content);
},
],
'whitelist' => ['Symplify\*', 'PHP_CodeSniffer\*', 'PhpCsFixer\*'],
'whitelist' => [
'Symplify\EasyCodingStandard\*',
'Symplify\EasyCodingStandard*',
'Symplify\EasyCodingStandard',
'Symplify\CodingStandard\*',
'Symplify\CodingStandard',
'PHP_CodeSniffer\*',
'PhpCsFixer\*',
],
];
1 change: 0 additions & 1 deletion packages/EasyCodingStandard/compiler/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"symfony/console": "^4.4|^5.0",
"symfony/filesystem": "^4.4|^5.0",
"symfony/finder": "^4.4|^5.0",
"symfony/polyfill-php73": "^1.13",
"symfony/process": "^4.4|^5.0"
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symplify\EasyCodingStandard\Compiler\Exception\ShouldNotHappenException;
use Symplify\EasyCodingStandard\Compiler\Process\CompileProcessFactory;

/**
Expand All @@ -35,7 +36,7 @@ final class CompileCommand extends Command
private $originalComposerJsonFileContent;

/**
* @var string
* @var string|null
*/
private $symplifyVersionToRequire;

Expand Down Expand Up @@ -77,6 +78,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->buildDir
);

// remove bugging packages
$dirsToRemove = [__DIR__ . '/../../../vendor/symfony/polyfill-php70'];
foreach ($dirsToRemove as $dirToRemove) {
NetteFileSystem::delete($dirToRemove);
}

// parallel prevention is just for single less-buggy process
$this->compileProcessFactory->create(['php', 'box.phar', 'compile', '--no-parallel'], $this->dataDir);

Expand All @@ -98,6 +105,11 @@ private function fixComposerJson(string $composerJsonFile): void
// simplify autoload (remove not packed build directory]
$json['autoload']['psr-4']['Symplify\\EasyCodingStandard\\'] = 'src';

// remove dev content
unset($json['minimum-stability']);
unset($json['prefer-stable']);
unset($json['extra']);

// use stable version for symplify packages
foreach (array_keys($json['require']) as $package) {
/** @var string $package */
Expand All @@ -109,6 +121,15 @@ private function fixComposerJson(string $composerJsonFile): void
$json['require'][$package] = $symplifyVersionToRequire;
}

// cleanup
$filesToRemove = [
__DIR__ . '/../../../vendor/friendsofphp/php-cs-fixer/src/Test/AbstractIntegrationTestCase.php',
];

foreach ($filesToRemove as $fileToRemove) {
NetteFileSystem::delete($fileToRemove);
}

$encodedJson = Json::encode($json, Json::PRETTY);

$this->filesystem->dumpFile($composerJsonFile, $encodedJson);
Expand All @@ -126,14 +147,20 @@ private function restoreComposerJson(string $composerJsonFile): void

private function getSymplifyStableVersionToRequire(): string
{
if ($this->symplifyVersionToRequire) {
if ($this->symplifyVersionToRequire !== null) {
return $this->symplifyVersionToRequire;
}

$symplifyPackageContent = NetteFileSystem::read('https://repo.packagist.org/p/symplify/symplify.json');
$symplifyPackageContent = file_get_contents('https://repo.packagist.org/p/symplify/symplify.json');
if ($symplifyPackageContent === null) {
throw new ShouldNotHappenException();
}

$symplifyPackageJson = Json::decode($symplifyPackageContent, Json::FORCE_ARRAY);
$symplifyPackageVersions = $symplifyPackageJson['packages']['symplify/symplify'];
end($symplifyPackageVersions);

$lastStableVersion = array_key_last($symplifyPackageJson['packages']['symplify/symplify']);
$lastStableVersion = key($symplifyPackageVersions);
$lastStableVersion = new Version($lastStableVersion);

$this->symplifyVersionToRequire = '^' . $lastStableVersion->getMajor()->getValue() . '.' . $lastStableVersion->getMinor()->getValue();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyCodingStandard\Compiler\Exception;

use Exception;

final class ShouldNotHappenException extends Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symplify\EasyCodingStandard\Compiler\Contract\Process\ProcessInterface;

final class CompileProcessFactory
{
Expand All @@ -23,7 +22,7 @@ public function __construct()
/**
* @param string[] $command
*/
public function create(array $command, string $cwd): ProcessInterface
public function create(array $command, string $cwd): SymfonyProcess
{
return new SymfonyProcess($command, $cwd, $this->output);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,17 @@

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use Symplify\EasyCodingStandard\Compiler\Contract\Process\ProcessInterface;

final class SymfonyProcess implements ProcessInterface
final class SymfonyProcess
{
/**
* @var Process
*/
private $process;

/**
* @param string[] $command
*/
public function __construct(array $command, string $cwd, OutputInterface $output)
{
$this->process = (new Process($command, $cwd, null, null, null))
(new Process($command, $cwd, null, null, null))
->mustRun(static function (string $type, string $buffer) use ($output): void {
$output->write($buffer);
});
}

public function getProcess(): Process
{
return $this->process;
}
}
41 changes: 8 additions & 33 deletions packages/EasyCodingStandard/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,27 @@
"require": {
"php": "^7.2",
"composer/xdebug-handler": "^1.4",
"friendsofphp/php-cs-fixer": "^2.16",
"jean85/pretty-package-versions": "^1.2",
"nette/utils": "^3.0",
"nette/robot-loader": "^3.2",
"ocramius/package-versions": "^1.4",
"psr/simple-cache": "^1.0",
"squizlabs/php_codesniffer": "^3.5",
"slevomat/coding-standard": "^6.0",
"symfony/cache": "^4.4|^5.0",
"symfony/console": "^4.4|^5.0",
"symfony/process": "^4.4|^5.0",
"symfony/config": "^4.4|^5.0",
"symfony/http-kernel": "^4.4|^5.0",
"symfony/dependency-injection": "^4.4|^5.0",
"symfony/finder": "^4.4|^5.0",
"symfony/filesystem": "^4.4|^5.0",
"symfony/yaml": "^4.4|^5.0",
"symplify/coding-standard": "^7.3",
"symplify/package-builder": "^7.3",
"symplify/set-config-resolver": "^7.3",
"symplify/smart-file-system": "^7.3",
"symplify/autowire-array-parameter": "^7.3",
"symplify/auto-bind-parameter": "^7.3",
"slevomat/coding-standard": "^6.0",
"friendsofphp/php-cs-fixer": "^2.16",
"squizlabs/php_codesniffer": "^3.5",
"jean85/pretty-package-versions": "^1.2",
"ocramius/package-versions": "^1.4",
"psr/simple-cache": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5",
"symplify/easy-coding-standard-tester": "^7.3"
},
"replace": {
"symplify/easy-coding-standard": "7.2",
"symplify/easy-coding-standard-tester": "7.2"
"symplify/auto-bind-parameter": "^7.3"
},
"autoload": {
"psr-4": {
Expand All @@ -48,20 +38,5 @@
"Symplify\\EasyCodingStandard\\FixerRunner\\": "packages/FixerRunner/src",
"Symplify\\EasyCodingStandard\\SniffRunner\\": "packages/SniffRunner/src"
}
},
"autoload-dev": {
"psr-4": {
"Symplify\\EasyCodingStandard\\ChangedFilesDetector\\Tests\\": "packages/ChangedFilesDetector/tests",
"Symplify\\EasyCodingStandard\\FixerRunner\\Tests\\": "packages/FixerRunner/tests",
"Symplify\\EasyCodingStandard\\SniffRunner\\Tests\\": "packages/SniffRunner/tests",
"Symplify\\EasyCodingStandard\\Tests\\": "tests"
}
},
"extra": {
"branch-alias": {
"dev-master": "7.3-dev"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
Loading

0 comments on commit 30a03df

Please sign in to comment.