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

Integrate PHP CSS Parser library into plugin and add method to build Composer compatible zip #5745

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
59ce329
Add export exclusions to gitattributes file
pierlon Dec 27, 2020
92131da
Bundle the PHP-CSS-Library library along with the plugin
pierlon Dec 28, 2020
a86e5ff
Add support for building Composer compatible zip
pierlon Dec 28, 2020
6974679
Add "composer" build to list of zips to be built for public use
pierlon Dec 28, 2020
56dfb7f
Add "composer" build link to comment body
pierlon Dec 28, 2020
52f95c4
Add PHP Scoper related files to Codecov ignore list
pierlon Dec 28, 2020
4cd4965
Add deprecated method to code coverage ignore list
pierlon Dec 28, 2020
be1d49c
Update phpcs config for scoper.inc.php
pierlon Dec 28, 2020
ae92531
Add workaround to always get php-scoper working on PHP < 7.3
pierlon Dec 28, 2020
fbc22c3
Simplify "composer_install" shell command
pierlon Dec 28, 2020
2bfd1c5
Set version in composer.json when processing the file
pierlon Dec 29, 2020
14e0c52
Remove unused function
pierlon Dec 29, 2020
e36e9b1
Fix comment body string
pierlon Dec 29, 2020
f77c742
Transform `upload-to-gcs` job to into a matrix one
pierlon Dec 29, 2020
4d4b329
Update composer.lock
pierlon Dec 30, 2020
bf3a641
Normailze composer.json
pierlon Dec 30, 2020
bbb771d
Merge branch 'develop' into fix/2649-composer-compatible-build
pierlon Dec 31, 2020
4a23c3a
Merge branch 'develop' into fix/2649-composer-compatible-build
pierlon Feb 10, 2021
eb1c9b5
Update composer.lock
pierlon Feb 10, 2021
0465afb
Merge branch 'develop' into fix/2649-composer-compatible-build
pierlon Apr 28, 2021
2786ae9
Update composer.lock
pierlon Apr 28, 2021
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
32 changes: 31 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
# Improve diff output for PHP files.
*.php diff=php
*.php diff=php

/.github export-ignore
/bin export-ignore
/tests export-ignore
/.browserslistrc export-ignore
/.devlib export-ignore
/.editorconfig export-ignore
/.eslintignore export-ignore
/.eslintrc export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.gitmodules export-ignore
/.npmrc export-ignore
/.nvmrc export-ignore
/.phpcs.xml.dist export-ignore
/.phpstorm.meta.php export-ignore
/.rtlcssrc export-ignore
/.stylelintignore export-ignore
/.stylelintrc.json export-ignore
/babel.config.js export-ignore
/codecov.yml export-ignore
/Gruntfile.js export-ignore
/package.json export-ignore
/package-lock.json export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/postcss.config.js export-ignore
/scoper.inc.php export-ignore
/sizereport.config.js export-ignore
/webpack.config.js export-ignore

# Mark generated files so diffs are hidden by default.
*.snap linguist-generated=true
Expand Down
61 changes: 36 additions & 25 deletions .github/workflows/build-test-measure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,6 @@ jobs:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, date, dom, gd, iconv, json, libxml, mysql, spl
coverage: ${{ matrix.coverage && 'pcov' || 'none' }}
ini-values: pcov.directory=.

- name: Shutdown default MySQL service
run: sudo service mysql stop

Expand Down Expand Up @@ -425,8 +417,31 @@ jobs:
restore-keys: |
${{ runner.os }}-composer-

# PHP-Scoper only works on PHP 7.3+ but is still a necessary tool to generate the scoped dependencies. To work
# around this, a compatible PHP version is temporarily installed and then used to install the dependencies.
# Once that is done, the PHP version is replaced with the one meant to be used and the vendor directory is
# removed so that the appropriate dependencies can then be later installed.

- name: Setup PHP 7.4 (to run PHP Scoper)
uses: shivammathur/setup-php@v2
with:
php-version: 7.4

- name: Install Composer dependencies (and scope necessary dependencies)
run: |
composer install --prefer-dist --no-suggest --no-progress --no-interaction
rm -rf vendor

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, date, dom, gd, iconv, json, libxml, mysql, spl
coverage: ${{ matrix.coverage && 'pcov' || 'none' }}
ini-values: pcov.directory=.

- name: Install Composer dependencies
run: composer install --prefer-dist --ignore-platform-reqs --no-progress --no-interaction
run: composer install --prefer-dist --ignore-platform-reqs --no-progress --no-interaction --no-scripts

- name: Get npm cache directory
id: npm-cache
Expand Down Expand Up @@ -670,7 +685,7 @@ jobs:
git-sha-8: ${{ steps.retrieve-git-sha-8.outputs.sha8 }}
strategy:
matrix:
build: ['dev', 'prod']
build: ['dev', 'prod', 'composer']

steps:
- name: Check out source files
Expand Down Expand Up @@ -755,30 +770,25 @@ jobs:
runs-on: ubuntu-latest
needs:
- build-zip
steps:
- name: Download dev build
uses: actions/download-artifact@v2
with:
name: amp-${{ needs.build-zip.outputs.branch-name }}-${{ needs.build-zip.outputs.git-sha-8 }}-dev
path: builds/dev
strategy:
matrix:
build: [ 'dev', 'prod', 'composer' ]

- name: Download prod build
steps:
- name: Download ${{ matrix.build }} build
uses: actions/download-artifact@v2
with:
name: amp-${{ needs.build-zip.outputs.branch-name }}-${{ needs.build-zip.outputs.git-sha-8 }}-prod
path: builds/prod
name: amp-${{ needs.build-zip.outputs.branch-name }}-${{ needs.build-zip.outputs.git-sha-8 }}-${{ matrix.build }}
path: builds/${{ matrix.build }}

- name: Setup Google Cloud SDK
uses: google-github-actions/setup-gcloud@master
with:
project_id: ${{ secrets.GCS_PROJECT_ID }}
service_account_key: ${{ secrets.GCS_APPLICATION_CREDENTIALS }}

- name: Upload dev build to bucket
run: gsutil cp -r builds/dev/amp.zip gs://ampwp_github_artifacts/${{ github.ref }}/dev/amp.zip

- name: Upload prod build to bucket
run: gsutil cp -r builds/prod/amp.zip gs://ampwp_github_artifacts/${{ github.ref }}/prod/amp.zip
- name: Upload ${{ matrix.build }} build to bucket
run: gsutil cp -r builds/${{ matrix.build }}/amp.zip gs://ampwp_github_artifacts/${{ github.ref }}/${{ matrix.build }}/amp.zip

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

Expand Down Expand Up @@ -807,7 +817,8 @@ jobs:
run: |
body="Plugin builds for ${{ github.event.pull_request.head.sha }} are ready :bellhop_bell:!
- Download [development build](https://storage.googleapis.com/ampwp_github_artifacts/${{ github.ref }}/dev/amp.zip)
- Download [production build](https://storage.googleapis.com/ampwp_github_artifacts/${{ github.ref }}/prod/amp.zip)"
- Download [production build](https://storage.googleapis.com/ampwp_github_artifacts/${{ github.ref }}/prod/amp.zip)
- Download [composer build](https://storage.googleapis.com/ampwp_github_artifacts/${{ github.ref }}/composer/amp.zip)"
body="${body//$'\n'/'%0A'}"
echo "::set-output name=body::$body"

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ built
/phpcs.xml
/phpunit.xml
/*.sql
/third-party

# Generated via bin/transform-readme.php
/readme.txt
5 changes: 5 additions & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@
</properties>
</rule>

<rule ref="Squiz.Commenting.FileComment.Missing">
<exclude-pattern>scoper.inc.php</exclude-pattern>
</rule>

<arg value="s"/>
<arg name="extensions" value="php"/>
<file>.</file>
Expand All @@ -146,4 +150,5 @@
<exclude-pattern type="relative">^build/*</exclude-pattern>
<exclude-pattern>includes/sanitizers/class-amp-allowed-tags-generated.php</exclude-pattern>
<exclude-pattern>assets/js/*.asset.php</exclude-pattern>
<exclude-pattern>third-party/*</exclude-pattern>
</ruleset>
59 changes: 44 additions & 15 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module.exports = function( grunt ) {
'use strict';

// Root paths to include in the plugin build ZIP when running `npm run build:prod`.
// Root paths to include in the plugin build ZIP.
const productionIncludedRootFiles = [
'LICENSE',
'amp.php',
Expand All @@ -12,7 +12,6 @@ module.exports = function( grunt ) {
'includes',
'src',
'templates',
'vendor',
];

// These patterns paths will be excluded from among the above directory.
Expand Down Expand Up @@ -43,6 +42,17 @@ module.exports = function( grunt ) {
'vendor/ampproject/amp-toolbox/conceptual-diagram.svg',
'vendor/ampproject/amp-toolbox/phpstan.neon.dist',
'vendor/bin',
'third-party/composer.json',
'scoper.inc.php',
];

// These will be removed from the Composer build of the plugin prior to creating a ZIP.
// ⚠️ Warning: These paths are passed straight to rm command in the shell, without any escaping.
const productionComposerExcludedFilePatterns = [
'vendor',
'composer.lock',
'third-party/composer.json',
'scoper.inc.php',
];

grunt.initConfig( {
Expand Down Expand Up @@ -81,10 +91,12 @@ module.exports = function( grunt ) {
composer_install: {
command: [
'if [ ! -e build ]; then echo "Run grunt build first."; exit 1; fi',
'mkdir -p build/vendor/bin',
'cp vendor/bin/php-scoper build/vendor/bin/',
'cd build',
'composer install --no-dev -o',
'composer remove cweagans/composer-patches --update-no-dev -o',
'rm -rf ' + productionVendorExcludedFilePatterns.join( ' ' ),
'COMPOSER_DISCARD_CHANGES=true composer remove --no-interaction --no-scripts --update-no-dev -o cweagans/composer-patches sabberworm/php-css-parser',
'rm -rf ' + ( 'composer' === process.env.BUILD_TYPE ? productionComposerExcludedFilePatterns : productionVendorExcludedFilePatterns ).join( ' ' ),
].join( ' && ' ),
},
create_build_zip: {
Expand Down Expand Up @@ -155,6 +167,9 @@ module.exports = function( grunt ) {
paths.push( 'readme.txt' );

paths.push( 'composer.*' ); // Copy in order to be able to do run composer_install.
paths.push( 'scoper.inc.php' ); // Copy in order generate scoped Composer dependencies.

// Also copy recently built assets.
paths.push( 'assets/js/**/*.js' );
paths.push( 'assets/js/**/*.asset.php' );
paths.push( 'assets/css/*.css' );
Expand All @@ -164,30 +179,44 @@ module.exports = function( grunt ) {
paths.push( 'assets/css/*.css.map' );
}

// Get build version from amp.php.
const versionRegex = /(\*\s+Version:\s+)(?<version>\d+(\.\d+)+)(?<identifier>-\w+)?/;
const { groups: matches } = grunt.file.read( 'amp.php' ).match( versionRegex );

if ( ! matches || ! matches.version ) {
throw new Error( 'Plugin version could not be retrieved from amp.php' );
}

const version = matches.version;

grunt.config.set( 'copy', {
build: {
src: paths,
dest: 'build',
expand: true,
options: {
noProcess: [ '*/**', 'LICENSE' ], // That is, only process amp.php and README.md.
noProcess: [ '**/*', '!amp.php', '!composer.json' ],
process( content, srcpath ) {
let matches, version, versionRegex;
if ( /amp\.php$/.test( srcpath ) ) {
versionRegex = /(\*\s+Version:\s+)(\d+(\.\d+)+-\w+)/;

if ( /^amp\.php$/.test( srcpath ) ) {
// If not a stable build (e.g. 0.7.0-beta), amend the version with the git commit and current timestamp.
matches = content.match( versionRegex );
if ( matches ) {
version = matches[ 2 ] + '-' + versionAppend;
console.log( 'Updating version in amp.php to ' + version ); // eslint-disable-line no-console
content = content.replace( versionRegex, '$1' + version );
content = content.replace( /(define\(\s*'AMP__VERSION',\s*')(.+?)(?=')/, '$1' + version );
if ( matches.identifier ) {
const pluginVersion = version + matches.identifier + '-' + versionAppend;
console.log( 'Updating version in amp.php to ' + pluginVersion ); // eslint-disable-line no-console
content = content.replace( versionRegex, '$1' + pluginVersion );
content = content.replace( /(define\(\s*'AMP__VERSION',\s*')(.+?)(?=')/, '$1' + pluginVersion );
}

// Remove dev mode code blocks.
content = content.replace( /\n\/\/\s*DEV_CODE.+?\n}\n/s, '' );

if ( 'composer' === process.env.BUILD_TYPE ) {
content = content.replace( "require_once AMP__DIR__ . '/vendor/autoload.php';", '' );
}
} else if ( /^composer\.json$/.test( srcpath ) && 'composer' === process.env.BUILD_TYPE ) {
console.log( 'Setting version in composer.json to ' + version ); // eslint-disable-line no-console
content = content.replace( /"name": "ampproject\/amp-wp",/, '$&\n "version": "' + version + '",' );
}

return content;
},
},
Expand Down
3 changes: 2 additions & 1 deletion amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
unset( $_amp_required_extensions, $_amp_missing_extensions, $_amp_required_constructs, $_amp_missing_classes, $_amp_missing_functions, $_amp_required_extension, $_amp_construct_type, $_amp_construct, $_amp_constructs );

// DEV_CODE. This block of code is removed during the build process.
if ( ! file_exists( AMP__DIR__ . '/vendor/autoload.php' ) || ! file_exists( AMP__DIR__ . '/vendor/sabberworm/php-css-parser' ) || ! file_exists( AMP__DIR__ . '/assets/js/amp-block-editor.js' ) ) {
if ( ! file_exists( AMP__DIR__ . '/vendor/autoload.php' ) || ! file_exists( AMP__DIR__ . '/third-party/vendor/scoper-autoload.php' ) || ! file_exists( AMP__DIR__ . '/assets/js/amp-block-editor.js' ) ) {
$_amp_load_errors->add(
'build_required',
sprintf(
Expand Down Expand Up @@ -251,6 +251,7 @@ function _amp_incorrect_plugin_slug_admin_notice() {
}

require_once AMP__DIR__ . '/vendor/autoload.php';
require_once AMP__DIR__ . '/third-party/vendor/scoper-autoload.php';

register_activation_hook( __FILE__, 'amp_activate' );

Expand Down
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ comment:
ignore:
- "/bin"
- "/back-compat"
- "/third-party"
- "/docs/**/*"
- ".phpstorm.meta.php"
- "/qa-tester/**"
- "scoper.inc.php"
19 changes: 18 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
},
"extra": {
"downloads": {
"php-scoper": {
"path": "vendor/bin/php-scoper",
"type": "phar",
"url": "https://github.com/humbug/php-scoper/releases/download/0.14.0/php-scoper.phar"
},
"phpstan": {
"path": "vendor/bin/phpstan",
"type": "phar",
Expand Down Expand Up @@ -91,10 +96,22 @@
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-install-cmd": [
"@scope-dependencies"
],
"post-update-cmd": [
"@scope-dependencies"
],
"analyze": "if [ -z $TEST_SKIP_PHPSTAN ]; then phpstan --version; phpstan analyze --ansi; fi",
"pre-commit": [
"npm run lint:staged"
],
"prepare-tests": "install-package-tests"
"prepare-tests": "install-package-tests",
"scope-dependencies": [
"php-scoper add-prefix --output-dir=third-party --force --quiet",
"echo '{ \"autoload\": { \"classmap\": [\"\"] } }' > third-party/composer.json",
"composer dump-autoload --working-dir third-party --no-dev --no-plugins --classmap-authoritative",
"sed -i'.bak' -e 's/Composer\\\\Autoload/AmpProject\\\\AmpWP\\\\Composer\\\\Autoload/' third-party/vendor/composer/*.php && rm -rf third-party/vendor/composer/*.php.bak"
]
}
}
Loading