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

Commit

Permalink
fix php code sniffer autoloading in phar
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 10, 2020
1 parent 06c6b8c commit e4798be
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 83 deletions.
44 changes: 0 additions & 44 deletions .github/workflows/code_checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,50 +73,6 @@ jobs:
name: Run
run: composer rector

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

-
run: |
cd packages/EasyCodingStandard/compiler
composer install
bin/compile
php build/box.phar info ../tmp/ecs.phar
../tmp/ecs.phar
cd ..
# Deploy PHAR to https://github.com/Symplify/EasyCodingStandardPrefixed
# reuse tmp/rector.phar from previous job
git clone https://${ACCESS_TOKEN}@github.com/Symplify/EasyCodingStandardPrefixed.git > /dev/null 2>&1
cp tmp/ecs.phar EasyCodingStandardPrefixed/ecs.phar
cp tmp/ecs.phar EasyCodingStandardPrefixed/ecs
cd EasyCodingStandardPrefixed
git config user.email "[email protected]"
git config user.name "Github Actions"
git add ecs ecs.phar
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
if [ "${TRAVIS_TAG}" != "" ]; then git tag "${TRAVIS_TAG}" && git push --quiet origin ${TRAVIS_TAG}; fi
env:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}

binary_files:
runs-on: ubuntu-latest

Expand Down
75 changes: 75 additions & 0 deletions .github/workflows/ecs_prefixed.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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: |
cd packages/EasyCodingStandard/compiler
../tmp/ecs.phar
-
name: Run ecs.phar with PSR-12 set
run: |
cd packages/EasyCodingStandard/compiler
../tmp/ecs.phar check ../src --set psr12 --dry-run --debug
# 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
cp tmp/ecs.phar EasyCodingStandardPrefixed/ecs.phar
cp tmp/ecs.phar EasyCodingStandardPrefixed/ecs
cd EasyCodingStandardPrefixed
git config user.email "[email protected]"
git config user.name "Github Actions"
git add ecs ecs.phar
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
if [ "${TRAVIS_TAG}" != "" ]; then git tag "${TRAVIS_TAG}" && git push --quiet origin ${TRAVIS_TAG}; fi
env:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
54 changes: 44 additions & 10 deletions packages/EasyCodingStandard/bin/ecs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ $autoloadIncluder = new AutoloadIncluder();
$autoloadIncluder->includeCwdVendorAutoloadIfExists();
$autoloadIncluder->autoloadProjectAutoloaderFile('/../../autoload.php');
$autoloadIncluder->includeDependencyOrRepositoryVendorAutoloadIfExists();
$autoloadIncluder->includePhpCodeSnifferAutoloadIfNotInPharAndInitliazeTokens();



# 2. create container
$configs = [];
Expand Down Expand Up @@ -156,6 +159,19 @@ final class AutoloadIncluder
}
}

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)) {
Expand All @@ -172,20 +188,38 @@ final class AutoloadIncluder

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

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

$this->loadIfNotLoadedYet($phpCodeSnifferAutoload, __METHOD__ . '()" on line ' . __LINE__);

// initalize PHPCS tokens
new Tokens();
}

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();
}
}
81 changes: 52 additions & 29 deletions packages/EasyCodingStandard/compiler/src/Console/CompileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,36 +99,11 @@ private function fixComposerJson(string $composerJsonFile): void

$json = Json::decode($fileContent, Json::FORCE_ARRAY);

// remove dev dependencies (they create conflicts)
unset($json['require-dev'], $json['autoload-dev']);
$json = $this->replaceDevSymplifyVersionWithLastStableVersion($json);
$json = $this->fixPhpCodeSnifferAutoloading($json);

// 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 */
if (! Strings::startsWith($package, 'symplify/')) {
continue;
}

$symplifyVersionToRequire = $this->getSymplifyStableVersionToRequire();
$json['require'][$package] = $symplifyVersionToRequire;
}

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

foreach ($filesToRemove as $fileToRemove) {
NetteFileSystem::delete($fileToRemove);
}
$json = $this->removeDevContent($json);
$this->cleanupPhpCsFixerBreakingFiles();

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

Expand Down Expand Up @@ -167,4 +142,52 @@ private function getSymplifyStableVersionToRequire(): string

return $this->symplifyVersionToRequire;
}

private function replaceDevSymplifyVersionWithLastStableVersion(array $json): array
{
$symplifyVersionToRequire = $this->getSymplifyStableVersionToRequire();

foreach (array_keys($json['require']) as $package) {
/** @var string $package */
if (! Strings::startsWith($package, 'symplify/')) {
continue;
}

$json['require'][$package] = $symplifyVersionToRequire;
}
return $json;
}

private function cleanupPhpCsFixerBreakingFiles(): void
{
// cleanup
$filesToRemove = [
__DIR__ . '/../../../vendor/friendsofphp/php-cs-fixer/src/Test/AbstractIntegrationTestCase.php',
];

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

private function removeDevContent(array $json): array
{
$keysToRemove = ['require-dev', 'autoload-dev', 'minimum-stability', 'prefer-stable', 'extra'];

foreach ($keysToRemove as $keyToRemove) {
unset($json[$keyToRemove]);
}

return $json;
}

/**
* Their autoloader is broken inside the phar :/
*/
private function fixPhpCodeSnifferAutoloading(array $json): array
{
$json['autoload']['classmap'][] = 'vendor/squizlabs/php_codesniffer/src';

return $json;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function __construct(ContainerBuilder $containerBuilder, FileLocatorInter
*/
protected function loadFile($file)
{
var_dump($file);

/** @var mixed[]|null $configuration */
$configuration = parent::loadFile($file);
if ($configuration === null) {
Expand Down

0 comments on commit e4798be

Please sign in to comment.