Skip to content

Commit

Permalink
Merge pull request #7 from Dcard/copy-exec-files
Browse files Browse the repository at this point in the history
fix(docker-build): also copy exec protocol files
  • Loading branch information
Fonger committed Mar 5, 2021
2 parents 0ff0388 + 9b9ce98 commit c782dc6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
23 changes: 21 additions & 2 deletions packages/docker-build/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
StreamReport,
execUtils,
} from '@yarnpkg/core';
import { patchUtils } from '@yarnpkg/plugin-patch';
import getDockerFilePath from '../utils/getDockerFilePath';
import getRequiredWorkspaces from '../utils/getRequiredWorkspaces';
import copyRcFile from '../utils/copyRcFile';
Expand All @@ -19,7 +20,8 @@ import copyCacheMarkedFiles from '../utils/copyCacheMarkedFiles';
import generateLockfile from '../utils/generateLockfile';
import packWorkspace from '../utils/packWorkspace';
import copyAdditional from '../utils/copyAdditional';
import copyPatchFiles from '../utils/copyPatchFiles';
import copyProtocolFiles from '../utils/copyProtocolFiles';
import { parseSpec } from '../utils/execUtils';

export default class DockerBuildCommand extends BaseCommand {
@Command.String()
Expand Down Expand Up @@ -128,10 +130,27 @@ export default class DockerBuildCommand extends BaseCommand {
report,
});

await copyPatchFiles({
await copyProtocolFiles({
destination: manifestDir,
workspaces: project.workspaces,
report,
parseDescriptor: (descriptor) => {
if (descriptor.range.startsWith('exec:')) {
const parsed = parseSpec(descriptor.range);
if (!parsed || !parsed.parentLocator) return;
return {
parentLocator: parsed.parentLocator,
paths: [parsed.path],
};
} else if (descriptor.range.startsWith('patch:')) {
const {
parentLocator,
patchPaths: paths,
} = patchUtils.parseDescriptor(descriptor);
if (!parentLocator) return;
return { parentLocator, paths };
}
},
});

await copyCacheMarkedFiles({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import { Report, structUtils, Workspace } from '@yarnpkg/core';
import {
Descriptor,
Locator,
Report,
structUtils,
Workspace,
} from '@yarnpkg/core';
import { PortablePath, ppath, xfs } from '@yarnpkg/fslib';
import { patchUtils } from '@yarnpkg/plugin-patch';

// https://github.com/yarnpkg/berry/blob/d38d573/packages/plugin-patch/sources/patchUtils.ts#L10
const BUILTIN_REGEXP = /^builtin<([^>]+)>$/;

export default async function copyPatchFiles({
export default async function copyProtocolFiles({
destination,
workspaces,
report,
parseDescriptor,
}: {
destination: PortablePath;
workspaces: Workspace[];
report: Report;
parseDescriptor: (
descriptor: Descriptor,
) => { parentLocator: Locator; paths: PortablePath[] } | undefined;
}): Promise<void> {
const copiedPaths = new Set<string>();

Expand All @@ -22,21 +31,18 @@ export default async function copyPatchFiles({
? structUtils.devirtualizeDescriptor(descriptor)
: descriptor;

if (!patchDescriptor.range.startsWith('patch:')) continue;
const parsed = parseDescriptor(patchDescriptor);
if (!parsed) continue;

const { parentLocator, patchPaths } = patchUtils.parseDescriptor(
patchDescriptor,
);
const { parentLocator, paths } = parsed;

for (const path of patchPaths) {
for (const path of paths) {
// Ignore builtin modules
if (BUILTIN_REGEXP.test(path)) continue;

// TODO: Handle absolute path
if (ppath.isAbsolute(path)) continue;

if (!parentLocator) continue;

// Get the workspace by parentLocator
const parentWorkspace = ws.project.getWorkspaceByLocator(parentLocator);

Expand Down
20 changes: 20 additions & 0 deletions packages/docker-build/src/utils/execUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copy from @yarnpkg/plugin-exec
// https://github.com/yarnpkg/berry/blob/63a77b5/packages/plugin-exec/sources/execUtils.ts

import { Locator, structUtils } from '@yarnpkg/core';
import { npath, PortablePath } from '@yarnpkg/fslib';

export function parseSpec(
spec: string,
): { parentLocator: Locator | null; path: PortablePath } | undefined {
const { params, selector } = structUtils.parseRange(spec);

const path = npath.toPortablePath(selector);

const parentLocator =
params && typeof params.locator === `string`
? structUtils.parseLocator(params.locator)
: null;

return { parentLocator, path };
}

0 comments on commit c782dc6

Please sign in to comment.