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

Commit

Permalink
[ECS] Add ecs.phar (#1734)
Browse files Browse the repository at this point in the history
[ECS] Add ecs.phar
  • Loading branch information
TomasVotruba authored Jan 10, 2020
2 parents edac483 + 9305e13 commit 3cd46ea
Show file tree
Hide file tree
Showing 51 changed files with 758 additions and 114 deletions.
1 change: 0 additions & 1 deletion .github/workflows/code_checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ jobs:
name: Run
run: composer rector


binary_files:
runs-on: ubuntu-latest

Expand Down
83 changes: 83 additions & 0 deletions .github/workflows/ecs_prefixed.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: Prefixed ECS Deploy

on:
pull_request: null
push:
branches:
- master

jobs:
ecs_phar_compile:
runs-on: ubuntu-latest

steps:
-
name: Checkout code
uses: actions/checkout@v2

-
name: Setup PHP
uses: shivammathur/setup-php@v1
with:
php-version: 7.2
coverage: none

-
name: Install
run: |
# install
cd packages/EasyCodingStandard/compiler
composer install
-
name: Compile ecs.phar with Box and PHP Scoper
run: |
# compile
cd packages/EasyCodingStandard/compiler
bin/compile
-
name: Run ecs.phar
run: |
# remove local vendor, to prevent duplicated content
rm -rf packages/EasyCodingStandard/vendor
cd packages/EasyCodingStandard
tmp/ecs.phar
-
name: Run ecs.phar with PSR-12 set
run: |
cd packages/EasyCodingStandard
# create dummy file
echo "<?php echo 'hi';" >> someFile.php
tmp/ecs.phar check someFile.php --set dead-code
# Deploy PHAR to https://github.com/Symplify/EasyCodingStandardPrefixed
-
name: Publish ecs.phar to Symplify/EasyCodingStandardPrefixed
run: |
cd packages/EasyCodingStandard
# reuse tmp/ecs.phar from previous job
git clone https://${ACCESS_TOKEN}@github.com/Symplify/EasyCodingStandardPrefixed.git > /dev/null 2>&1
# copy phar files inside cloned repository
cp tmp/ecs.phar EasyCodingStandardPrefixed/ecs.phar
cp tmp/ecs.phar EasyCodingStandardPrefixed/ecs
# go to clone repository
cd EasyCodingStandardPrefixed
git config user.email "[email protected]"
git config user.name "Github Actions"
git add ecs ecs.phar
# commit with new tag, if this commit is tagged, or with normal commit
if [ "${TRAVIS_TAG}" != "" ]; then COMMIT_MSG="ECS ${TRAVIS_TAG}"; else COMMIT_MSG="Updated ECS to commit ${TRAVIS_COMMIT}"; fi
git commit -m "${COMMIT_MSG}"
git push --quiet origin master
# push tag, if this commit is tagged, or with normal push
if [ "${TRAVIS_TAG}" != "" ]; then git tag "${TRAVIS_TAG}" && git push --quiet origin ${TRAVIS_TAG}; fi
env:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ package-lock.json
# phpunit
.phpunit.result.cache

packages/EasyCodingStandard/compiler/
packages/EasyCodingStandard/vendor/
packages/EasyCodingStandard/tmp/
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"Symplify\\EasyCodingStandardTester\\": "packages/EasyCodingStandardTester/src",
"Symplify\\EasyCodingStandard\\": "packages/EasyCodingStandard/src",
"Symplify\\EasyCodingStandard\\ChangedFilesDetector\\": "packages/EasyCodingStandard/packages/ChangedFilesDetector/src",
"Symplify\\EasyCodingStandard\\Compiler\\": "packages/EasyCodingStandard/compiler/src",
"Symplify\\EasyCodingStandard\\Configuration\\": "packages/EasyCodingStandard/packages/Configuration/src",
"Symplify\\EasyCodingStandard\\FixerRunner\\": "packages/EasyCodingStandard/packages/FixerRunner/src",
"Symplify\\EasyCodingStandard\\SniffRunner\\": "packages/EasyCodingStandard/packages/SniffRunner/src",
Expand Down
Empty file removed configuration-
Empty file.
2 changes: 2 additions & 0 deletions ecs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ parameters:
# tests
- 'packages/*/tests/**Source/*.php'
- 'packages/CodingStandard/tests/Fixer/Order/MethodOrderByTypeFixer/Wrong/*'
# own scope
- 'packages/EasyCodingStandard/compiler/build/scoper.inc.php'

skip:
# run form time to time, very slow and making many changes
Expand Down
3 changes: 2 additions & 1 deletion packages/EasyCodingStandard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<a href="https://github.com/lmc-eu/php-coding-standard"><img src="/docs/logos/lmc.png"></a>
<img src="/docs/logos/space.png">
<a href="https://github.com/nette/coding-standard"><img src="/docs/logos/nette.png"></a>
<img src="/docs/logos/space.png">
<br>
<a href="https://github.com/shopsys/coding-standards"><img src="/docs/logos/shopsys.png"></a>
<img src="/docs/logos/space.png">
<a href="https://github.com/SyliusLabs/CodingStandard"><img src="/docs/logos/sylius.png"></a>
Expand All @@ -23,6 +23,7 @@
- **2nd run under few seconds** with caching
- [Skipping files](#ignore-what-you-cant-fix) for specific checkers
- [Prepared checker sets](#use-prepared-checker-sets) - PSR12, Symfony, Common, Symplify and more...
- Use [Prefixed version](https://github.com/Symplify/EasyCodingStandardPrefixed) to prevent conflicts on install

Are you already using another tool?

Expand Down
158 changes: 133 additions & 25 deletions packages/EasyCodingStandard/bin/ecs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,11 @@ 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();
$autoloadIncluder->includePhpCodeSnifferAutoloadIfNotInPharAndInitliazeTokens();

# 2. create container
$configs = [];
Expand All @@ -48,20 +33,18 @@ $input = new ArgvInput();
$inputConfig = $configResolver->resolveFromInputWithFallback($input,
['easy-coding-standard.yml', 'easy-coding-standard.yaml', 'ecs.yml', 'ecs.yaml']
);

if ($inputConfig) {
$configs[] = $inputConfig;
}

// 2. --set CLI option
$setInputConfig = $configResolver->resolveSetFromInputAndDirectory($input, __DIR__ . '/../config/set');
$setInputConfig = $configResolver->resolveSetFromInputAndDirectory($input, Set::SET_DIRECTORY);
if ($setInputConfig) {
$configs[] = $setInputConfig;
}

// 3. "parameters > set" in provided yaml files
$parameterSetsConfigs = $configResolver->resolveFromParameterSetsFromConfigFiles($configs, Set::SET_DIRECTORY);

if ($parameterSetsConfigs !== []) {
$configs = array_merge($configs, $parameterSetsConfigs);
}
Expand Down Expand Up @@ -89,8 +72,9 @@ foreach ($configs as $config) {
}
}

$mainConfigShiftedAsLast[] = $inputConfig;

if ($inputConfig !== null) {
$mainConfigShiftedAsLast[] = $inputConfig;
}

$easyCodingStandardKernel = new EasyCodingStandardKernel($environment, InputDetector::isDebug());
if ($configs !== []) {
Expand All @@ -112,3 +96,127 @@ $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 isInPhar(): bool
{
if (!extension_loaded('phar')) {
return false;
}

if (Phar::running(false) === '') {
return false;
}

return true;
}

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;
}

private function isDebugOption(): bool
{
return in_array('--debug', $_SERVER['argv'], true);
}

public function includePhpCodeSnifferAutoloadIfNotInPharAndInitliazeTokens()
{
// file is autoloaded with classmap in PHAR
// without phar, we still need to autoload it
if (! $this->isInPhar()) {
# 1. autoload
$possibleAutoloadPaths = [
// after split package
__DIR__ . '/../vendor',
// dependency
__DIR__ . '/../../..',
// monorepo
__DIR__ . '/../../../vendor',
];

foreach ($possibleAutoloadPaths as $possibleAutoloadPath) {
if (!is_file($possibleAutoloadPath . '/autoload.php')) {
continue;
}

require_once $possibleAutoloadPath . '/squizlabs/php_codesniffer/autoload.php';
}
}

// initalize PHPCS tokens
new Tokens();
}
}
3 changes: 3 additions & 0 deletions packages/EasyCodingStandard/compiler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/temp
!/temp/.gitkeep
/vendor
29 changes: 29 additions & 0 deletions packages/EasyCodingStandard/compiler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# PHAR Compiler for EasyCodingStandard

## Compile the PHAR

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

The compiled PHAR will be in `tmp/ecs.phar`. Test it:

```bash
php ../tmp/ecs.phar
```

Please note that running the compiler will change the contents of `composer.json` file and `vendor` directory. Revert those changes after running it.

## Notes

This section si needed in `composer.json`, because it was causing autolaoding bugs.
Box aliases existing Symfony stubs to php, see https://ayesh.me/composer-replace-polyfills.

```json
{
"replace": {
"symfony/polyfill-php70": "*"
}
}
```
23 changes: 23 additions & 0 deletions packages/EasyCodingStandard/compiler/bin/compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env php
<?php

declare(strict_types = 1);

use Symplify\EasyCodingStandard\Compiler\Console\CompileCommand;
use Symplify\EasyCodingStandard\Compiler\Process\CompileProcessFactory;
use Symfony\Component\Console\Application;

// use EasyCodingStandard package autoload.php
require_once __DIR__ . '/../vendor/autoload.php';

$compileCommand = new CompileCommand(
new CompileProcessFactory(),
__DIR__ . '/../build',
__DIR__ . '/../..'
);

/** @var Application $application */
$application = new Application();
$application->add($compileCommand);
$application->setDefaultCommand($compileCommand->getName(), true);
$application->run();
Loading

0 comments on commit 3cd46ea

Please sign in to comment.