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

Blueprints: Support zipped assets without top-level folder #491

Merged
merged 1 commit into from
Jun 1, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/playground/blueprints/src/lib/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class VFSResource extends Resource {

/** @inheritDoc */
get name() {
return this.resource.path;
return this.resource.path.split('/').pop() || '';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resource returned a full path as a file name and that threw off installAsset

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ foreach ( ( glob( $plugin_path . '/*.php' ) ?: array() ) as $file ) {
echo 'NO_ENTRY_FILE';
`,
});
if (result.errors) throw new Error(result.errors);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some plugins trigger warnings during the installation and they show as result.errors so we can't bale out here

if (result.text === 'NO_ENTRY_FILE') {
if (result.text.endsWith('NO_ENTRY_FILE')) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some plugins trigger warnings during the installation – let's check just the last part of the output text

throw new Error('Could not find plugin entry file.');
}
};
45 changes: 23 additions & 22 deletions packages/playground/blueprints/src/lib/steps/install-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ export async function installAsset(
// Extract to temporary folder so we can find asset folder name

const zipFileName = zipFile.name;
const tmpFolder = `/tmp/assets`;
const assetNameGuess = zipFileName.replace(/\.zip$/, '');

const tmpUnzippedFilesPath = `/tmp/assets/${assetNameGuess}`;
const tmpZipPath = `/tmp/${zipFileName}`;

const removeTmpFolder = () =>
playground.rmdir(tmpFolder, {
playground.rmdir(tmpUnzippedFilesPath, {
recursive: true,
});

if (await playground.fileExists(tmpFolder)) {
if (await playground.fileExists(tmpUnzippedFilesPath)) {
await removeTmpFolder();
}

Expand All @@ -54,35 +56,34 @@ export async function installAsset(
try {
await unzip(playground, {
zipPath: tmpZipPath,
extractToPath: tmpFolder,
extractToPath: tmpUnzippedFilesPath,
});

// Find extracted asset folder name

const files = await playground.listFiles(tmpFolder);
// Find the path asset folder name
const files = await playground.listFiles(tmpUnzippedFilesPath, {
prependPath: true,
});

/**
* If the zip only contains a single entry that is directory,
* we assume that's the asset folder. Otherwise, the zip
* probably contains the plugin files without an intermediate folder.
*/
const zipHasRootFolder =
files.length === 1 && (await playground.isDir(files[0]));
let assetFolderName;
let tmpAssetPath = '';

for (const file of files) {
tmpAssetPath = `${tmpFolder}/${file}`;
if (await playground.isDir(tmpAssetPath)) {
assetFolderName = file;
break;
}
}

if (!assetFolderName) {
throw new Error(
`The zip file should contain a single folder with files inside, but the provided zip file (${zipFileName}) does not contain such a folder.`
);
if (zipHasRootFolder) {
tmpAssetPath = files[0];
assetFolderName = files[0].split('/').pop()!;
} else {
tmpAssetPath = tmpUnzippedFilesPath;
assetFolderName = assetNameGuess;
}

// Move asset folder to target path

const assetFolderPath = `${targetPath}/${assetFolderName}`;
await playground.mv(tmpAssetPath, assetFolderPath);

await cleanup();

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ describe('Blueprint step installPlugin', () => {

it('should install a plugin', async () => {
// Create test plugin

const pluginName = 'test-plugin';

php.mkdir(`/${pluginName}`);
Expand All @@ -28,7 +27,10 @@ describe('Blueprint step installPlugin', () => {
const zipFileName = `${pluginName}-0.0.1.zip`;

await php.run({
code: `<?php $zip = new ZipArchive(); $zip->open("${zipFileName}", ZIPARCHIVE::CREATE); $zip->addFile("/${pluginName}/index.php"); $zip->close();`,
code: `<?php $zip = new ZipArchive();
$zip->open("${zipFileName}", ZIPARCHIVE::CREATE);
$zip->addFile("/${pluginName}/index.php");
$zip->close();`,
});

php.rmdir(`/${pluginName}`);
Expand Down Expand Up @@ -63,4 +65,50 @@ describe('Blueprint step installPlugin', () => {

expect(php.fileExists(`${pluginsPath}/${pluginName}`)).toBe(true);
});

it('should install a plugin even when it is zipped directly without a root-level folder', async () => {
// Create test plugin
const pluginName = 'test-plugin';

php.writeFile('/index.php', `/**\n * Plugin Name: Test Plugin`);

// Note the package name is different from plugin folder name
const zipFileName = `${pluginName}-0.0.1.zip`;

await php.run({
code: `<?php $zip = new ZipArchive();
$zip->open("${zipFileName}", ZIPARCHIVE::CREATE);
$zip->addFile("/index.php");
$zip->close();`,
});

expect(php.fileExists(zipFileName)).toBe(true);

// Create plugins folder
const rootPath = await php.documentRoot;
const pluginsPath = `${rootPath}/wp-content/plugins`;

php.mkdir(pluginsPath);

await runBlueprintSteps(
compileBlueprint({
steps: [
{
step: 'installPlugin',
pluginZipFile: {
resource: 'vfs',
path: zipFileName,
},
options: {
activate: false,
},
},
],
}),
php
);

php.unlink(zipFileName);
expect(php.fileExists(`${pluginsPath}/${pluginName}-0.0.1`)).toBe(true);
});
});