Skip to content

Commit

Permalink
build: improve php scoping logic
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Oct 24, 2023
1 parent 533cda0 commit 48281ef
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 126 deletions.
15 changes: 10 additions & 5 deletions .github/actions/build/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ runs:
--no-scripts \
"humbug/php-scoper:${{ inputs.php-scoper-version }}"
# The actual compilation is done in pdk.config.js
- name: 'Cache compiled php'
# The scoper is run in pdk.config.js
- name: 'Cache scoped source php'
uses: actions/cache@v3
id: compiled-php-cache
with:
path: .tmp/build
key: compiled-php-${{ hashFiles('**/composer.json', '**/composer.lock', 'src/**/*', 'config/**/*', '*.php') }}
path: .tmp/scoped-src/
key: scoped-php-src-${{ 'src/**/*', 'config/**/*', 'controllers/**/*', 'upgrade/**/*', 'myparcelnl.php', 'scoper.inc.php') }}

- name: 'Cache scoped vendor php'
uses: actions/cache@v3
with:
path: .tmp/scoped-vendor
key: scoped-php-vendor-${{ hashFiles('**/composer.json', '**/composer.lock', 'scoper.inc.php', 'scoper.vendor.inc.php') }}

- name: 'Build frontend'
shell: bash
Expand Down
115 changes: 58 additions & 57 deletions composer.lock

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

108 changes: 61 additions & 47 deletions pdk.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,50 @@ import fs from 'fs';
import glob from 'fast-glob';
import path from 'path';

const TMP_DIR = '.tmp';
const SCOPED_DIR = `${TMP_DIR}/scoped`;

const scopePhp = (context, dest, config = 'scoper.inc.php') => {
const {debug, env} = context;
const destDir = path.resolve(env.cwd, dest);

if (!fs.existsSync(destDir)) {
debug(`Creating directory ${dest}`);
fs.mkdirSync(destDir, {recursive: true});
}

if (fs.readdirSync(destDir).length > 0) {
debug(`Skipping scoping php files to ${dest} because it already exists.`);
return;
}

return executeCommand(
context,
'php',
[
'-d memory_limit=-1',
`${TMP_DIR}/php-scoper/vendor/bin/php-scoper`,
'add-prefix',
`--config=${config}`,
`--output-dir=${dest}`,
'--force',
'--no-ansi',
'--no-interaction',
],
{stdio: 'inherit'},
);
};

export default defineConfig({
name: 'prestashop',
platformFolderName: '{{platform}}',
platforms: [PdkPlatformName.MyParcelNl, PdkPlatformName.MyParcelBe],
source: [
'!**/node_modules/**',
// Exclude autoload.php to regenerate it with a new hash
'!.tmp/build/vendor/autoload.php',
'.tmp/build/composer.json',
'.tmp/build/config/**/*',
'.tmp/build/controllers/**/*',
'.tmp/build/myparcelnl.php',
'.tmp/build/src/**/*',
'.tmp/build/upgrade/**/*',
'.tmp/build/vendor/**/*',
`!${SCOPED_DIR}/vendor/autoload.php`,
`${SCOPED_DIR}/vendor/**/*`,
`${SCOPED_DIR}/source/**/*`,
'mails/**/*',
'private/carrier-logos/**/*',
'views/PrestaShop/**/*',
Expand Down Expand Up @@ -55,52 +84,33 @@ export default defineConfig({
/**
* Prefix the vendor and source php files.
*/
async beforeCopy(args) {
const {debug} = args.context;

debug('Prefixing build files...');

if (fs.existsSync('.tmp/build/composer.json')) {
debug('Build files already exist, skipping prefixing.');
return;
}

if (!args.dryRun) {
await executeCommand(
args.context,
'php',
[
'-d memory_limit=-1',
'.tmp/php-scoper/vendor/bin/php-scoper',
'add-prefix',
'--output-dir=.tmp/build',
'--force',
'--no-ansi',
'--no-interaction',
],
{stdio: 'inherit'},
);
}

debug('Finished prefixing build files.');
async beforeCopy({context}) {
const {debug} = context;

debug('Scoping php files...');

await scopePhp(context, `${SCOPED_DIR}/source`);
await scopePhp(context, `${SCOPED_DIR}/vendor`, 'scoper.vendor.inc.php');

debug('Scoped all php files.');
},

async afterCopy(args) {
const {config, env, debug} = args.context;
async afterCopy({context}) {
const {config, debug, args, env} = context;

debug('Copying scoped build files to root');

await executePromises(
args,
config.platforms.map(async (platform) => {
const platformDistPath = getPlatformDistPath({config, env, platform});
const platformDistPath = getPlatformDistPath({...context, platform});

const files = glob.sync('.tmp/build/**/*', {cwd: platformDistPath});
const files = glob.sync([`${SCOPED_DIR}/source/**/*`, `${SCOPED_DIR}/vendor/**/*`], {cwd: platformDistPath});

await Promise.all(
files.map(async (file) => {
const oldPath = `${platformDistPath}/${file}`;
const newPath = oldPath.replace('.tmp/build/', '');
const newPath = oldPath.replace(`${SCOPED_DIR}/source/`, '').replace(`${SCOPED_DIR}/`, '');

if (!args.dryRun) {
await fs.promises.mkdir(path.dirname(newPath), {recursive: true});
Expand All @@ -110,30 +120,34 @@ export default defineConfig({
);

if (!args.dryRun) {
await fs.promises.rm(`${platformDistPath}/.tmp`, {recursive: true});
await fs.promises.rm(`${platformDistPath}/${TMP_DIR}`, {recursive: true});
}

if (args.verbose >= 1) {
debug(`Removed ${path.relative(env.cwd, `${platformDistPath}/${TMP_DIR}`)}`);
}
}),
);

debug('Copied scoped build files to root.');
},

async afterTransform(args) {
const {config, debug, env} = args.context;
async afterTransform({context}) {
const {config, debug, env, args} = context;

await Promise.all(
config.platforms.map(async (platform) => {
debug(`Dumping composer autoloader for platform ${platform}...`);

const distPath = getPlatformDistPath({...args.context, platform});
const distPath = getPlatformDistPath({...context, platform});
const relativeDistPath = path.relative(env.cwd, distPath);

if (!args.dryRun) {
await executeCommand(
args.context,
context,
'composer',
['dump-autoload', `--working-dir=${relativeDistPath}`, '--classmap-authoritative'],
{stdio: 'inherit'},
args.verbose >= 1 ? {stdio: 'inherit'} : {},
);
}
}),
Expand Down
Loading

0 comments on commit 48281ef

Please sign in to comment.