Skip to content

Commit

Permalink
Update to wp-env 8
Browse files Browse the repository at this point in the history
Update to @wordpress/env 8.0.0
  Fixes file permissions issues
  Has grouped containers for cli/wp/composer
  Disallow unknown config keys
Change in content mapping and paths
Change in functions to run wp, composer, cli commands
wp-env version check
  unexpected version is marked with a red cross
Change config .p4-env.json to .wp-env.json merge strategy
  external config keys moved to .p4-env.json, override for both config used
Fix on CI E2E results export
composer run-script site:custom works
  • Loading branch information
lithrel committed Jun 2, 2023
1 parent 6034a34 commit 83d1f9f
Show file tree
Hide file tree
Showing 29 changed files with 285 additions and 199 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
machine:
image: ubuntu-2204:2023.02.1
environment:
- WP_ENV_VERSION: "5.16.0"
- WP_ENV_VERSION: "8.0.0"
parameters:
notify:
type: boolean
Expand Down Expand Up @@ -99,8 +99,8 @@ jobs:
mkdir -p /home/circleci/artifacts/playwright/themes /home/circleci/artifacts/playwright/plugins
cp -r planet4/themes/planet4-master-theme/e2e-* /home/circleci/artifacts/playwright/themes || true
cp planet4/themes/planet4-master-theme/results.xml /home/circleci/artifacts/playwright/themes || true
cp -r planet4/plugins/planet4-plugins-gutenberg-blocks/e2e-* /home/circleci/artifacts/playwright/plugins || true
cp planet4/plugins/planet4-plugins-gutenberg-blocks/results.xml /home/circleci/artifacts/playwright/plugins || true
cp -r planet4/plugins/planet4-plugin-gutenberg-blocks/e2e-* /home/circleci/artifacts/playwright/plugins || true
cp planet4/plugins/planet4-plugin-gutenberg-blocks/results.xml /home/circleci/artifacts/playwright/plugins || true
- store_test_results:
path: /home/circleci/artifacts
- store_artifacts:
Expand All @@ -119,7 +119,7 @@ jobs:
image: ubuntu-2204:2023.02.1
environment:
- GOOGLE_PROJECT_ID: planet-4-151612
- WP_ENV_VERSION: "latest"
- WP_ENV_VERSION: "8.0.0"
parameters:
notify:
type: boolean
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ cache/*
content/*
planet4/*
node_modules/*
.p4-env.json
.p4-env.*.json
.wp-env.*.json
junit.xml
16 changes: 16 additions & 0 deletions .p4-env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"planet4": {
"content": {
"db": "v0.2.18",
"images": "1-25"
},
"repos": {
"planet4-base": "main",
"planet4-master-theme": "main",
"planet4-plugin-gutenberg-blocks": "main"
},
"composer": {
"processTimeout": "600"
}
}
}
19 changes: 1 addition & 18 deletions .wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
"phpVersion": "7.4",
"core": "WordPress/WordPress#6.2",
"mappings": {
"wp-content/themes": "./planet4/themes",
"wp-content/plugins": "./planet4/plugins",
"wp-content/uploads": "./planet4/uploads",
"wp-content/languages": "./planet4/languages"
"wp-content": "./planet4"
},
"config": {
"WP_SITEURL": "http://www.planet4.test/",
Expand All @@ -15,19 +12,5 @@
"development": {
"port": 80
}
},
"planet4": {
"content": {
"db": "v0.2.23",
"images": "1-25"
},
"repos": {
"planet4-base": "main",
"planet4-master-theme": "main",
"planet4-plugin-gutenberg-blocks": "main"
},
"composer": {
"processTimeout": "600"
}
}
}
18 changes: 1 addition & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,9 @@ npm run
- you can add or create any theme in this folder, it will be available in your local instance
- plugins are installed in `planet4/plugins`
- if a plugin you want to work on is not writable, either use `npm run env:fix-permissions`, or remove it and clone your own repo to replace it
- you can add or create any theme in this folder, it will be available in your local instance
- you can add or create any plugin in this folder, it will be available in your local instance

## Resources

- https://github.com/WordPress/gutenberg/tree/trunk/packages/env
- https://github.com/WordPress/developer-blog-content/issues/89

## To do

### Perfs
- Add Redis server to check if instance is faster that way

### Permissions
- check why everything is root (missing user with UID used by wp-env ?)
- https://github.com/WordPress/gutenberg/issues/28201 maybe

### Composer
- execute @site:custom scripts
- figure out replacement of `wp` commands

### WPML
- String Translation issues writing in languages folder
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "planet4-develop",
"version": "v0.4",
"version": "v0.5",
"description": "Planet 4 development environment",
"author": "Planet 4",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions scripts/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const config = getConfig()

run('wp-env stop')
run('wp-env clean')
if (config.appDir.length > 0 && !config.appDir.includes('..') && isDir(config.appDir)) {
console.log(`Deleting all files in <${config.appDir}> ...`)
run(`sudo rm -rf ${config.appDir}`)
if (config.paths.local.app.length > 0 && !config.paths.local.app.includes('..') && isDir(config.paths.local.app)) {
console.log(`Deleting all files in <${config.paths.local.app}> ...`)
run(`rm -rf ./${config.paths.local.app}`)
}
4 changes: 2 additions & 2 deletions scripts/db-import.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { existsSync } = require('fs')
const { run } = require('./lib/run')
const { wp } = require('./lib/run')
const { createDatabase, importDatabase, useDatabase } = require('./lib/mysql')

if (!process.argv[2] || !process.argv[3]) {
Expand All @@ -23,4 +23,4 @@ if (!dbName.match(/^[a-z0-9_]*$/)) {
createDatabase(dbName)
importDatabase(filepath, dbName)
useDatabase(dbName)
run('wp-env run cli user update admin --user_pass=admin --role=administrator')
wp('user update admin --user_pass=admin --role=administrator')
6 changes: 3 additions & 3 deletions scripts/db-use.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { run } = require('./lib/run')
const { useDatabase } = require('./lib/mysql')

if (!process.argv[2]) {
console.log('Please use npm run db:use <dataase name>')
Expand All @@ -8,8 +8,8 @@ if (!process.argv[2]) {
const dbName = process.argv[2]

if (!dbName.match(/^[a-z0-9_]*$/)) {
console.log(`DB name <${dbName}> is invalid. Please use ^[a-z_]*$`)
console.log(`DB name <${dbName}> is invalid. Please use ^[a-z0-9_]*$`)
process.exit(1)
}

run(`wp-env run cli config set DB_NAME ${dbName}`)
useDatabase(dbName)
10 changes: 5 additions & 5 deletions scripts/e2e.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { run } = require('./lib/run')
const { getConfig } = require('./lib/config')
const { nodeCheck } = require('./lib/node-check')
const { nodeCheck } = require('./lib/env-check')
const { parseArgs } = require('./lib/utils')

/**
Expand All @@ -21,11 +21,11 @@ console.log(process.cwd(), '\n', config)
const args = parseArgs(process.argv, { command: 'run' })

if (args.command === 'install') {
run(`npx playwright install ${args.options.join(' ')}`, { cwd: `${config.themesDir}/planet4-master-theme` })
run(`npx playwright install ${args.options.join(' ')}`, { cwd: `${config.pluginsDir}/planet4-plugin-gutenberg-blocks` })
run(`npx playwright install ${args.options.join(' ')}`, { cwd: `${config.paths.local.themes}/planet4-master-theme` })
run(`npx playwright install ${args.options.join(' ')}`, { cwd: `${config.paths.local.plugins}/planet4-plugin-gutenberg-blocks` })
}

if (args.command === 'run') {
run(`npx playwright test ${args.options.join(' ')} --pass-with-no-tests`, { cwd: `${config.themesDir}/planet4-master-theme` })
run(`npx playwright test ${args.options.join(' ')} --pass-with-no-tests`, { cwd: `${config.pluginsDir}/planet4-plugin-gutenberg-blocks` })
run(`npx playwright test ${args.options.join(' ')} --pass-with-no-tests`, { cwd: `${config.paths.local.themes}/planet4-master-theme` })
run(`npx playwright test ${args.options.join(' ')} --pass-with-no-tests`, { cwd: `${config.paths.local.plugins}/planet4-plugin-gutenberg-blocks` })
}
16 changes: 8 additions & 8 deletions scripts/elastic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { run } = require('./lib/run')
const { run, wp } = require('./lib/run')
const { parseArgs } = require('./lib/utils')

/**
Expand All @@ -9,21 +9,21 @@ const dcConfig = '-f $(wp-env install-path)/docker-compose.yml -f scripts/docker

if (args.command === 'activate') {
run(`docker compose ${dcConfig} up --force-recreate -d elasticsearch`)
run('wp-env run cli option update ep_host "http://elasticsearch:9200/"')
run('wp-env run cli plugin activate elasticpress')
run('wp-env run cli elasticpress index --setup --yes')
wp('option update ep_host "http://elasticsearch:9200/"')
wp('plugin activate elasticpress')
wp('elasticpress index --setup --yes')

// clear redis cache ?
run('wp-env run cli timber clear_cache &>/dev/null')
run('wp-env run cli cache flush')
wp('timber clear_cache &>/dev/null')
wp('cache flush')
}

if (args.command === 'deactivate') {
run(`docker compose ${dcConfig} stop elasticsearch`)
run('wp-env run cli plugin deactivate elasticpress')
wp('plugin deactivate elasticpress')
}

if (args.command === 'stop') {
run(`docker compose ${dcConfig} stop elasticsearch`)
run('wp-env run cli option delete ep_host')
wp('option delete ep_host')
}
8 changes: 4 additions & 4 deletions scripts/fix-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const all = process.argv[2] || null
const config = getConfig()

if (all) {
run(`sudo find ${config.appDir} -not -user $(whoami) -exec chown -f $(whoami) {} \\+`)
run(`sudo chmod 777 ${config.appDir}/uploads`)
run(`sudo find ${config.paths.local.app} -not -user $(whoami) -exec chown -f $(whoami) {} \\+`)
run(`sudo chmod 777 ${config.paths.local.uploads}`)
} else {
run(`sudo find ${config.themesDir}/planet4-master-theme -not -user $(whoami) -exec chown -f $(whoami) {} \\+`)
run(`sudo find ${config.pluginsDir}/planet4-plugin-gutenberg-blocks -not -user $(whoami) -exec chown -f $(whoami) {} \\+`)
run(`sudo find ${config.paths.local.themes}/planet4-master-theme -not -user $(whoami) -exec chown -f $(whoami) {} \\+`)
run(`sudo find ${config.paths.local.plugins}/planet4-plugin-gutenberg-blocks -not -user $(whoami) -exec chown -f $(whoami) {} \\+`)
}
12 changes: 6 additions & 6 deletions scripts/install.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { existsSync, mkdirSync } = require('fs')
const { nodeCheck } = require('./lib/node-check')
const { nodeCheck } = require('./lib/env-check')
const { getConfig } = require('./lib/config')
const { run } = require('./lib/run')
const { run, composer, wp } = require('./lib/run')
const { download } = require('./lib/download')
const { getBaseRepoFromGit, getMainReposFromGit, installRepos, buildAssets } = require('./lib/main-repos')
const { generateBaseComposerRequirements } = require('./lib/composer-requirements')
Expand Down Expand Up @@ -51,7 +51,7 @@ generateBaseComposerRequirements(config)
* Install themes/plugins
*/
console.log('Installing & activating plugins ...')
run(`wp-env run composer -d /app/${config.appDir}/ update --ignore-platform-reqs`)
composer('update', config.paths.container.app)
installPluginsDependencies(config)

/**
Expand All @@ -62,7 +62,7 @@ download(
`https://storage.googleapis.com/planet4-default-content/${imagesDump}`,
`content/${imagesDump}`
)
run(`unzip -qo content/${imagesDump} -d ${config.uploadsDir}`)
run(`unzip -qo content/${imagesDump} -d ${config.paths.local.uploads}`)

/**
* Database
Expand All @@ -80,7 +80,7 @@ download(
createDatabase(dbName)
importDatabase(`content/${dbDump}`, dbName)
useDatabase(dbName)
run('wp-env run cli plugin activate --all')
run('wp-env run cli user update admin --user_pass=admin --role=administrator')
wp('plugin activate --all')
wp('user update admin --user_pass=admin --role=administrator')

console.log(`The local instance is now available at ${config.config.WP_SITEURL}`)
49 changes: 28 additions & 21 deletions scripts/lib/composer-requirements.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
const { readFileSync, writeFileSync, copyFileSync } = require('fs')
const { run } = require('./run')
const { composer } = require('./run')
const { isDir } = require('./utils')

function generateBaseComposerRequirements (config) {
copyFileSync(`${config.baseDir}/composer.json`, `${config.appDir}/composer.json`)
const planet4Base = `${config.paths.local.app}/planet4-base`

run(`wp-env run composer -d /app/${config.appDir}/ config --unset repositories.0`)
run(`wp-env run composer -d /app/${config.appDir}/ config --unset extra.merge-plugin`)
run(`wp-env run composer -d /app/${config.appDir}/ config --json extra.installer-paths '"{\\"plugins/{\\$name}/\\": [\\"type:wordpress-plugin\\"],\\"themes/{\\$name}/\\": [\\"type:wordpress-theme\\"]}"'`)
run(`wp-env run composer -d /app/${config.appDir}/ config platform.php "${config.phpVersion}"`)
run(`wp-env run composer -d /app/${config.appDir}/ config process-timeout ${config.planet4.composer.processTimeout}`)
copyFileSync(
`${planet4Base}/composer.json`,
`${config.paths.local.app}/composer.json`
)

if (isDir(`${config.themesDir}/planet4-master-theme`)) {
run(`wp-env run composer -d /app/${config.appDir}/ remove --no-update greenpeace/planet4-master-theme`)
composer('config --unset repositories.0', config.paths.container.app)
composer('config --unset extra.merge-plugin', config.paths.container.app)
composer('config --json extra.installer-paths "{\\"plugins/{\\$name}/\\": [\\"type:wordpress-plugin\\"],\\"themes/{\\$name}/\\": [\\"type:wordpress-theme\\"]}"', config.paths.container.app)
// composer(`config platform.php "${config.phpVersion}"`, config.paths.container.app)
if (config?.planet4?.composer?.processTimeout) {
composer(`config process-timeout ${config.planet4.composer.processTimeout}`, config.paths.container.app)
}

if (isDir(`${config.pluginsDir}/planet4-plugin-gutenberg-blocks`)) {
run(`wp-env run composer -d /app/${config.appDir}/ remove --no-update greenpeace/planet4-plugin-gutenberg-blocks`)
if (isDir(`${config.paths.local.themes}/planet4-master-theme`)) {
composer('remove --no-update greenpeace/planet4-master-theme', config.paths.container.app)
}

run(`wp-env run composer -d /app/${config.appDir}/ remove --no-update greenpeace/planet4-nginx-helper`)
if (isDir(`${config.paths.local.plugins}/planet4-plugin-gutenberg-blocks`)) {
composer('remove --no-update greenpeace/planet4-plugin-gutenberg-blocks', config.paths.container.app)
}

composer('remove --no-update greenpeace/planet4-nginx-helper', config.paths.container.app)

const baseComposerConfig = JSON.parse(readFileSync(`${config.appDir}/composer.json`))
const baseComposerConfig = JSON.parse(readFileSync(`${config.paths.local.app}/composer.json`))
console.log('baseComposerConfig', baseComposerConfig)
if (baseComposerConfig?.repositories) {
for (const k in baseComposerConfig.repositories) {
if (baseComposerConfig.repositories[k]?.type !== 'package'
|| !baseComposerConfig.repositories[k]?.package.name.startsWith('plugins/')
if (baseComposerConfig.repositories[k]?.type === 'package'
&& baseComposerConfig.repositories[k]?.package.name.startsWith('plugins/')
) {
continue
baseComposerConfig.repositories[k].package.type = 'wordpress-plugin'
}
baseComposerConfig.repositories[k].package.type = 'wordpress-plugin'
}
}

writeFileSync(`${config.appDir}/composer.json`, JSON.stringify(baseComposerConfig, null, ' '))
writeFileSync(`${config.paths.local.app}/composer.json`, JSON.stringify(baseComposerConfig, null, ' '))
return baseComposerConfig
}

function generateNROComposerRequirements (config) {
const baseComposerConfig = JSON.parse(readFileSync(`${config.appDir}/composer.json`))
const baseComposerConfig = JSON.parse(readFileSync(`${config.paths.local.app}/composer.json`))
const nroComposerConfig = getNroComposerRequirements(config)

const merged = {
Expand All @@ -48,12 +55,12 @@ function generateNROComposerRequirements (config) {
}
const composerConfig = { ...baseComposerConfig, ...merged }
// @todo: resolve composer scripts and/or `wp` usage from composer container
writeFileSync(`${config.appDir}/composer.json`, JSON.stringify(composerConfig, null, ' '))
writeFileSync(`${config.paths.local.app}/composer.json`, JSON.stringify(composerConfig, null, ' '))
return composerConfig
}

function getNroComposerRequirements (config) {
return JSON.parse(readFileSync(`${config.nro.dir}/composer-local.json`)) || {}
return JSON.parse(readFileSync(`${config.paths.local.app}/${config.nro.dir}/composer-local.json`)) || {}
}

module.exports = {
Expand Down
Loading

0 comments on commit 83d1f9f

Please sign in to comment.