Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CI] Downgrade Rector to PHP 7.1 + prefix it #5880

Merged
merged 7 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions .github/workflows/build_scoped_rector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
name: Build Scoped Rector

on:
pull_request: null

push:
branches:
- main

# to avoid running tagged and non-tagged workflows at the same time
tags-ignore:
tags:
- '*'

jobs:
Expand All @@ -16,26 +16,67 @@ jobs:
steps:
-
uses: actions/checkout@v2
with:
fetch-depth: 0

-
uses: shivammathur/setup-php@v2
with:
php-version: 7.3
coverage: none

# fixes https://github.com/rectorphp/rector/pull/4559/checks?check_run_id=1359814403, see https://github.com/shivammathur/setup-php#composer-github-oauth
env:
COMPOSER_TOKEN: ${{ secrets.ACCESS_TOKEN }}

# 1. prepare dependencies
- run: sh build/build-rector-scoped.sh
# install only prod dependencies - do not use ramsey, it uses cache including "dev", we want to avoid it here
- run: composer install --no-dev --ansi

# 1. copy files to $NESTED_DIRECTORY directory Exclude the scoped/nested directories to prevent rsync from copying in a loop
- run: rsync --exclude rector-build -av * rector-build --quiet
- run: rm -rf rector-build/packages-tests rector-build/rules-tests rector-build/tests

# 2. downgrade rector
- run: sh build/downgrade-rector.sh rector-build

# 3. prefix classes
- run: sh build/build-rector-scoped.sh rector-build rector-prefixed-downgraded

# 4. lint the code for PHP 7.1 - this must happen here, as setup-php allows only one PHP version switch: https://github.com/shivammathur/setup-php/issues/434
-
uses: shivammathur/setup-php@v2
with:
php-version: 7.1
coverage: none
- run: composer create-project php-parallel-lint/php-parallel-lint php-parallel-lint --ansi
- run: php-parallel-lint/parallel-lint rector-prefixed-downgraded --exclude rector-prefixed-downgraded/stubs --exclude rector-prefixed-downgraded/vendor/symfony/polyfill-intl-grapheme/bootstrap80.php --exclude rector-prefixed-downgraded/vendor/symfony/polyfill-intl-normalizer/bootstrap80.php --exclude rector-prefixed-downgraded/vendor/symfony/polyfill-mbstring/bootstrap80.php --exclude rector-prefixed-downgraded/vendor/symfony/error-handler/Resources --exclude rector-prefixed-downgraded/vendor/symfony/http-kernel/Resources --exclude rector-prefixed-downgraded/vendor/symfony/polyfill/src/Mbstring/bootstrap80.php --exclude rector-prefixed-downgraded/vendor/symfony/polyfill/src/Apcu/bootstrap80.php --exclude rector-prefixed-downgraded/vendor/symfony/polyfill/src/Iconv/bootstrap80.php --exclude rector-prefixed-downgraded/vendor/symfony/polyfill/src/Intl/Idn/bootstrap80.php --exclude rector-prefixed-downgraded/vendor/symfony/polyfill/src/Intl/Normalizer/bootstrap80.php --exclude rector-prefixed-downgraded/vendor/symfony/polyfill/src/Intl/Grapheme/bootstrap80.php

# 5. copy repository meta files
- run: cp -R build/target-repository/. rector-prefixed-downgraded

# 6. publish it to remote repository without tag
-
uses: symplify/[email protected]
if: "!startsWith(github.ref, 'refs/tags/')"
env:
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
with:
package-directory: 'rector-prefixed-downgraded'
split-repository-organization: 'rectorphp'
split-repository-name: 'rector-prefixed'
user-name: "kaizen-ci"
user-email: "[email protected]"

# 2. publish it to remote repository
# 7. publish it to remote repository with tag
-
uses: symplify/[email protected]
if: "startsWith(github.ref, 'refs/tags/')"
env:
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
with:
package-directory: 'rector-scoped'
package-directory: 'rector-prefixed-downgraded'
split-repository-organization: 'rectorphp'
split-repository-name: 'rector-prefixed'
tag: ${GITHUB_REF#refs/tags/}
user-name: "kaizen-ci"
user-email: "[email protected]"
48 changes: 0 additions & 48 deletions .github/workflows/build_scoped_rector_tagged.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ rector-recipe.php
# testing
abz

# scoped & downgraded version
php-scoper.phar
box.phar
php-parallel-lint

tmp
1 change: 0 additions & 1 deletion bin/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

try {
$bootstrapConfigs = $rectorConfigsResolver->provide();

$rectorContainerFactory = new RectorContainerFactory();
$container = $rectorContainerFactory->createFromBootstrapConfigs($bootstrapConfigs);
} catch (SetNotFoundException $setNotFoundException) {
Expand Down
38 changes: 16 additions & 22 deletions build/build-rector-scoped.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/sh -l

# see https://stackoverflow.com/questions/66644233/how-to-propagate-colors-from-bash-script-to-github-action?noredirect=1#comment117811853_66644233
export TERM=xterm-color

# show errors
set -e

Expand All @@ -19,48 +22,39 @@ note()


# configure here
NESTED_DIRECTORY="rector-nested"
SCOPED_DIRECTORY="rector-scoped"
BUILD_DIRECTORY=$1
RESULT_DIRECTORY=$2

# ---------------------------

note "Starts"

note "Coping root files to $NESTED_DIRECTORY directory"
# Exclude the scoped/nested directories to prevent rsync from copying in a loop
rsync --exclude "$NESTED_DIRECTORY" --exclude "$SCOPED_DIRECTORY" -av * "$NESTED_DIRECTORY" --quiet

note "Running composer update without dev"
composer update --no-dev --no-progress --ansi --working-dir "$NESTED_DIRECTORY"

# this will remove dependency on dev packages that are imported in phpstan.neon
rm -f "$NESTED_DIRECTORY/phpstan-for-rector.neon"

# Avoid Composer v2 platform checks (composer.json requires PHP 7.4+, but below we are running 7.3)
note "Disabling platform check"
composer config platform-check false
rm -f "$BUILD_DIRECTORY/phpstan-for-rector.neon"

# 2. scope it
note "Running scoper to $SCOPED_DIRECTORY"
note "Running scoper to $RESULT_DIRECTORY"
wget https://github.com/humbug/php-scoper/releases/download/0.14.0/php-scoper.phar -N --no-verbose

# Work around possible PHP memory limits
php -d memory_limit=-1 php-scoper.phar add-prefix bin config packages rules src templates vendor composer.json --output-dir "../$SCOPED_DIRECTORY" --config scoper.php --force --ansi --working-dir "$NESTED_DIRECTORY"
php -d memory_limit=-1 php-scoper.phar add-prefix bin config src packages rules vendor composer.json --output-dir "../$RESULT_DIRECTORY" --config scoper.php --force --ansi --working-dir "$BUILD_DIRECTORY"


# add polyfill for downgraded code
composer require symfony/polyfill-php80 symfony/polyfill-php74 symfony/polyfill-php73 symfony/polyfill-php72 --update-no-dev --working-dir $RESULT_DIRECTORY --ansi

note "Dumping Composer Autoload"
composer dump-autoload --working-dir "$SCOPED_DIRECTORY" --ansi --optimize --classmap-authoritative --no-dev
composer dump-autoload --working-dir "$RESULT_DIRECTORY" --ansi --optimize --classmap-authoritative --no-dev

rm -rf "$NESTED_DIRECTORY"
rm -rf "$BUILD_DIRECTORY"


# copy metafiles needed for release
note "Copy metafiles like composer.json, .github etc to repository"
rm -f "$SCOPED_DIRECTORY/composer.json"
cp -R scoped/. "$SCOPED_DIRECTORY"
rm -f "$RESULT_DIRECTORY/composer.json"

# make bin/rector runnable without "php"
chmod 777 "$SCOPED_DIRECTORY/bin/rector"
chmod 777 "$SCOPED_DIRECTORY/bin/rector.php"
chmod 777 "$RESULT_DIRECTORY/bin/rector"
chmod 777 "$RESULT_DIRECTORY/bin/rector.php"

note "Finished"
64 changes: 64 additions & 0 deletions build/config/config-downgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Core\Stubs\PHPStanStubLoader;
use Rector\Set\ValueObject\DowngradeSetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

$phpStanStubLoader = new PHPStanStubLoader();
$phpStanStubLoader->loadStubs();

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();

$parameters->set(Option::SKIP, DowngradeRectorConfig::DEPENDENCY_EXCLUDE_PATHS);
$parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, __DIR__ . '/phpstan-for-downgrade.neon');

$parameters->set(Option::SETS, [
DowngradeSetList::PHP_80,
DowngradeSetList::PHP_74,
DowngradeSetList::PHP_73,
DowngradeSetList::PHP_72,
]);
};

/**
* Configuration consts for the different rector.php config files
*/
final class DowngradeRectorConfig
{
/**
* Exclude paths when downgrading a dependency
*/
public const DEPENDENCY_EXCLUDE_PATHS = [
'*/tests/*',
// symfony test are parts of package
'*/Test/*',

// missing phpunit test case
'packages/Testing/PHPUnit/AbstractCommunityRectorTestCase.php',
'packages/Testing/PHPUnit/AbstractRectorTestCase.php',

// Individual classes that can be excluded because
// they are not used by Rector, and they use classes
// loaded with "require-dev" so it'd throw an error

// use relative paths, so files are excluded on nested directory too
'vendor/symfony/http-kernel/HttpKernelBrowser.php',
'vendor/symfony/string/Slugger/AsciiSlugger.php',

// always excluded
'*vendor/symfony/polyfill*/bootstrap80.php',

// This class has an issue for PHP 7.1:
// https://github.com/rectorphp/rector/issues/4816#issuecomment-743209526
// It doesn't happen often, and Rector doesn't use it, so then
// we simply skip downgrading this class
'vendor/symfony/dependency-injection/ExpressionLanguage.php',
'vendor/symfony/dependency-injection/ExpressionLanguageProvider.php',
'vendor/symfony/var-dumper/Caster/*',
'vendor/symplify/package-builder/src/Testing/AbstractKernelTestCase.php',
];
}
11 changes: 11 additions & 0 deletions build/config/phpstan-for-downgrade.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# this config has extensions, that helps PHPStan inside Rector to resolve more precise types
parameters:
inferPrivatePropertyTypeFromConstructor: true

scanDirectories:
# this is needed for symfony/dependendency-injection as it has hidden dependency on symfony/expression-language that we don't use here
- ../../stubs

# see https://github.com/rectorphp/rector/issues/3490#issue-634342324
featureToggles:
disableRuntimeReflectionProvider: true
34 changes: 34 additions & 0 deletions build/downgrade-rector.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh -l

# see https://stackoverflow.com/questions/66644233/how-to-propagate-colors-from-bash-script-to-github-action?noredirect=1#comment117811853_66644233
export TERM=xterm-color

# show errors
set -e

# script fails if trying to access to an undefined variable
set -u


# configure - 1st argument, use like
# sh build/downgrade-rector.sh <directory-with-code-to-downgrade>
BUILD_DIRECTORY=$1

#---------------------------------------------

# 1. downgrade it
echo "[NOTE] Running downgrade in '$BUILD_DIRECTORY' directory\n";

# 3. provide directories to downgrade; includes the rector dirs
directories=$(php -d memory_limit=-1 bin/rector downgrade-paths 7.1 --config build/config/config-downgrade.php --working-dir $BUILD_DIRECTORY --ansi)

# split array see https://stackoverflow.com/a/1407098/1348344
export IFS=";"

# 4. downgrade the directories
for directory in $directories; do
echo "[NOTE] Downgrading '$directory' directory\n"

# --working-dir is needed, so "SKIP" parameter is applied in absolute path of nested directory
php -d memory_limit=-1 bin/rector process $directory --config build/config/config-downgrade.php --working-dir $BUILD_DIRECTORY --ansi
done
Loading