-
Notifications
You must be signed in to change notification settings - Fork 384
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
Add a more reliable build step #3183
Changes from all commits
e951317
1b9241f
7034b52
bcf6e4f
c2ea84d
2a86f5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,42 @@ | |
module.exports = function( grunt ) { | ||
'use strict'; | ||
require( 'dotenv' ).config(); | ||
|
||
// Root paths to include in the plugin build ZIP when running `npm run build`. | ||
const productionIncludedRootFiles = [ | ||
'LICENSE', | ||
'amp.php', | ||
'assets', | ||
'back-compat', | ||
'includes', | ||
'readme.txt', | ||
'templates', | ||
'vendor', | ||
]; | ||
|
||
// These patterns paths will be excluded from among the above directory. | ||
const productionExcludedPathPatterns = [ | ||
/.*\/src\/.*/, | ||
/.*images\/stories-editor\/.*\.svg/, | ||
]; | ||
|
||
// These will be removed from the vendor directory after installing but prior to creating a ZIP. | ||
// ⚠️ Warning: These paths are passed straight to rm command in the shell, without any escaping. | ||
const productionVendorExcludedFilePatterns = [ | ||
'composer.*', | ||
'vendor/*/*/.editorconfig', | ||
'vendor/*/*/.gitignore', | ||
'vendor/*/*/composer.*', | ||
'vendor/*/*/Doxyfile', | ||
'vendor/*/*/LICENSE', | ||
'vendor/*/*/phpunit.*', | ||
'vendor/*/*/*.md', | ||
'vendor/*/*/*.txt', | ||
'vendor/*/*/*.yml', | ||
'vendor/*/*/.*.yml', | ||
'vendor/*/*/tests', | ||
]; | ||
|
||
grunt.initConfig( { | ||
|
||
pkg: grunt.file.readJSON( 'package.json' ), | ||
|
@@ -32,6 +68,9 @@ module.exports = function( grunt ) { | |
verify_matching_versions: { | ||
command: 'php bin/verify-version-consistency.php', | ||
}, | ||
composer_install: { | ||
command: 'if [ ! -e build ]; then echo "Run grunt build first."; exit 1; fi; cd build; composer install --no-dev -o && composer remove cweagans/composer-patches --update-no-dev -o && rm -r ' + productionVendorExcludedFilePatterns.join( ' ' ), | ||
}, | ||
create_build_zip: { | ||
command: 'if [ ! -e build ]; then echo "Run grunt build first."; exit 1; fi; if [ -e amp.zip ]; then rm amp.zip; fi; cd build; zip -r ../amp.zip .; cd ..; echo; echo "ZIP of build: $(pwd)/amp.zip"', | ||
}, | ||
|
@@ -95,17 +134,24 @@ module.exports = function( grunt ) { | |
const versionAppend = new Date().toISOString().replace( /\.\d+/, '' ).replace( /-|:/g, '' ) + '-' + commitHash; | ||
|
||
const paths = lsOutput.trim().split( /\n/ ).filter( function( file ) { | ||
return ! /^(blocks|\.|bin|([^/]+)+\.(md|json|xml)|Gruntfile\.js|tests|wp-assets|readme\.md|composer\..*|patches|webpack.*|assets\/images\/stories-editor\/.*\.svg|assets\/src|assets\/css\/src|docker-compose\.yml|.*\.config\.js|codecov\.yml|example\.env)/.test( file ); | ||
const topSegment = file.replace( /\/.*/, '' ); | ||
if ( ! productionIncludedRootFiles.includes( topSegment ) ) { | ||
return false; | ||
} | ||
|
||
for ( const productionExcludedPathPattern of productionExcludedPathPatterns ) { | ||
if ( productionExcludedPathPattern.test( file ) ) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} ); | ||
|
||
paths.push( 'vendor/autoload.php' ); | ||
paths.push( 'composer.*' ); // Copy in order to be able to do run composer_install. | ||
paths.push( 'assets/js/*.js' ); | ||
paths.push( 'assets/js/*.deps.json' ); | ||
paths.push( 'assets/css/*.css' ); | ||
paths.push( 'vendor/composer/**' ); | ||
paths.push( 'vendor/sabberworm/php-css-parser/lib/**' ); | ||
paths.push( 'vendor/fasterimage/fasterimage/src/**' ); | ||
paths.push( 'vendor/willwashburn/stream/src/**' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a lot of files now being included which should not be part of the build (e.g. the
Can these be excluded? The files add to the ZIP size and they are unused in production. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 2a86f5d. This should make it easier to maintain going forward, though it's surely not a good as it can be. |
||
|
||
grunt.config.set( 'copy', { | ||
build: { | ||
|
@@ -138,6 +184,7 @@ module.exports = function( grunt ) { | |
} ); | ||
grunt.task.run( 'readme' ); | ||
grunt.task.run( 'copy' ); | ||
grunt.task.run( 'shell:composer_install' ); | ||
|
||
done(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/bin/bash | ||
|
||
# Exit if any command fails. | ||
set -e | ||
|
||
# Change to the expected directory. | ||
cd "$(dirname "$0")" | ||
cd .. | ||
PLUGIN_DIR=$(pwd) | ||
|
||
# Enable nicer messaging for build status. | ||
BLUE_BOLD='\033[1;34m'; | ||
GREEN_BOLD='\033[1;32m'; | ||
RED_BOLD='\033[1;31m'; | ||
YELLOW_BOLD='\033[1;33m'; | ||
COLOR_RESET='\033[0m'; | ||
error () { | ||
echo -e "\n${RED_BOLD}$1${COLOR_RESET}\n" | ||
} | ||
status () { | ||
echo -e "\n${BLUE_BOLD}$1${COLOR_RESET}\n" | ||
} | ||
success () { | ||
echo -e "\n${GREEN_BOLD}$1${COLOR_RESET}\n" | ||
} | ||
warning () { | ||
echo -e "\n${YELLOW_BOLD}$1${COLOR_RESET}\n" | ||
} | ||
|
||
status "Time to release AMP ⚡️" | ||
|
||
status "Setting up a fresh build environment in a temporary folder. ✨" | ||
|
||
# Create a fresh temporary folder in a way that works across platforms. | ||
BUILD_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'amp-production-build') | ||
|
||
# Do a local clone to move the current files across. | ||
git clone -l . "$BUILD_DIR" | ||
cd "$BUILD_DIR" | ||
|
||
# Run the build. | ||
status "Installing dependencies... 📦" | ||
composer install | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is needed right now because things are a bit too intertwined. The grunt task https://github.com/ampproject/amp-wp/blob/1.2.2/Gruntfile.js#L26-L27 I didn't want to rewrite half of the build pipeline in this PR. That's one of the things the |
||
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true npm install | ||
|
||
status "Generating build... ⚙️" | ||
npm run build | ||
|
||
status "Copying the ZIP file back over... ↩️" | ||
rm -f "$PLUGIN_DIR/amp.zip" | ||
cp "$BUILD_DIR/amp.zip" "$PLUGIN_DIR/amp.zip" | ||
|
||
status "Removing the temporary folder again... 🗑️" | ||
rm -rf "$BUILD_DIR" | ||
|
||
success "You've built AMP! 🎉 \nThe ZIP file can be found in the following location:\n$PLUGIN_DIR/amp.zip" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm very glad this mess of a regex is now gone 😄