From 2b1fbe3092133625e9c663a689aedce6ab7c916c Mon Sep 17 00:00:00 2001 From: jaybell Date: Mon, 3 Jan 2022 21:04:52 -0800 Subject: [PATCH 1/2] feat(core): filter affected projects by glob pattern on tags ISSUES CLOSED: #2675 --- docs/generated/cli/affected-apps.md | 6 +++++ docs/generated/cli/affected-build.md | 6 +++++ docs/generated/cli/affected-dep-graph.md | 6 +++++ docs/generated/cli/affected-e2e.md | 6 +++++ docs/generated/cli/affected-libs.md | 6 +++++ docs/generated/cli/affected-lint.md | 6 +++++ docs/generated/cli/affected-test.md | 6 +++++ docs/generated/cli/affected.md | 6 +++++ docs/generated/cli/format-check.md | 6 +++++ docs/generated/cli/format-write.md | 6 +++++ docs/generated/cli/print-affected.md | 6 +++++ .../generated/cli/affected-apps.md | 6 +++++ .../generated/cli/affected-build.md | 6 +++++ .../generated/cli/affected-dep-graph.md | 6 +++++ .../generated/cli/affected-e2e.md | 6 +++++ .../generated/cli/affected-libs.md | 6 +++++ .../generated/cli/affected-lint.md | 6 +++++ .../generated/cli/affected-test.md | 6 +++++ .../documentation/generated/cli/affected.md | 6 +++++ .../generated/cli/format-check.md | 6 +++++ .../generated/cli/format-write.md | 6 +++++ .../generated/cli/print-affected.md | 6 +++++ .../workspace/src/command-line/affected.ts | 24 ++++++++++++++++++- .../workspace/src/command-line/nx-commands.ts | 6 +++++ packages/workspace/src/command-line/utils.ts | 6 +++++ 25 files changed, 167 insertions(+), 1 deletion(-) diff --git a/docs/generated/cli/affected-apps.md b/docs/generated/cli/affected-apps.md index 456bfe0e43e46..17949363ef00b 100644 --- a/docs/generated/cli/affected-apps.md +++ b/docs/generated/cli/affected-apps.md @@ -87,6 +87,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/docs/generated/cli/affected-build.md b/docs/generated/cli/affected-build.md index c4f84847643b6..032a0fa8bc732 100644 --- a/docs/generated/cli/affected-build.md +++ b/docs/generated/cli/affected-build.md @@ -99,6 +99,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/docs/generated/cli/affected-dep-graph.md b/docs/generated/cli/affected-dep-graph.md index 0d89f2af21f23..9d08ca7925928 100644 --- a/docs/generated/cli/affected-dep-graph.md +++ b/docs/generated/cli/affected-dep-graph.md @@ -127,6 +127,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/docs/generated/cli/affected-e2e.md b/docs/generated/cli/affected-e2e.md index f91027a5599a5..a59d9f70ebe99 100644 --- a/docs/generated/cli/affected-e2e.md +++ b/docs/generated/cli/affected-e2e.md @@ -99,6 +99,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/docs/generated/cli/affected-libs.md b/docs/generated/cli/affected-libs.md index 21b3a97af4738..97b184161c1c5 100644 --- a/docs/generated/cli/affected-libs.md +++ b/docs/generated/cli/affected-libs.md @@ -87,6 +87,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/docs/generated/cli/affected-lint.md b/docs/generated/cli/affected-lint.md index 33fad6179ef0d..399de75d7bf39 100644 --- a/docs/generated/cli/affected-lint.md +++ b/docs/generated/cli/affected-lint.md @@ -99,6 +99,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/docs/generated/cli/affected-test.md b/docs/generated/cli/affected-test.md index 913a1e2032a60..5ed1ecb310b54 100644 --- a/docs/generated/cli/affected-test.md +++ b/docs/generated/cli/affected-test.md @@ -99,6 +99,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/docs/generated/cli/affected.md b/docs/generated/cli/affected.md index 455a5d8a3c51f..55850844973a3 100644 --- a/docs/generated/cli/affected.md +++ b/docs/generated/cli/affected.md @@ -105,6 +105,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### target Task to run for affected projects diff --git a/docs/generated/cli/format-check.md b/docs/generated/cli/format-check.md index c38f534c2c0d4..743ec6fbc9651 100644 --- a/docs/generated/cli/format-check.md +++ b/docs/generated/cli/format-check.md @@ -71,6 +71,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/docs/generated/cli/format-write.md b/docs/generated/cli/format-write.md index 30040747f4245..d6d69212b0bda 100644 --- a/docs/generated/cli/format-write.md +++ b/docs/generated/cli/format-write.md @@ -71,6 +71,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/docs/generated/cli/print-affected.md b/docs/generated/cli/print-affected.md index a3fbe641e70b1..119f66983685e 100644 --- a/docs/generated/cli/print-affected.md +++ b/docs/generated/cli/print-affected.md @@ -99,6 +99,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/nx-dev/nx-dev/public/documentation/generated/cli/affected-apps.md b/nx-dev/nx-dev/public/documentation/generated/cli/affected-apps.md index 456bfe0e43e46..17949363ef00b 100644 --- a/nx-dev/nx-dev/public/documentation/generated/cli/affected-apps.md +++ b/nx-dev/nx-dev/public/documentation/generated/cli/affected-apps.md @@ -87,6 +87,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/nx-dev/nx-dev/public/documentation/generated/cli/affected-build.md b/nx-dev/nx-dev/public/documentation/generated/cli/affected-build.md index c4f84847643b6..032a0fa8bc732 100644 --- a/nx-dev/nx-dev/public/documentation/generated/cli/affected-build.md +++ b/nx-dev/nx-dev/public/documentation/generated/cli/affected-build.md @@ -99,6 +99,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/nx-dev/nx-dev/public/documentation/generated/cli/affected-dep-graph.md b/nx-dev/nx-dev/public/documentation/generated/cli/affected-dep-graph.md index 0d89f2af21f23..9d08ca7925928 100644 --- a/nx-dev/nx-dev/public/documentation/generated/cli/affected-dep-graph.md +++ b/nx-dev/nx-dev/public/documentation/generated/cli/affected-dep-graph.md @@ -127,6 +127,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/nx-dev/nx-dev/public/documentation/generated/cli/affected-e2e.md b/nx-dev/nx-dev/public/documentation/generated/cli/affected-e2e.md index f91027a5599a5..a59d9f70ebe99 100644 --- a/nx-dev/nx-dev/public/documentation/generated/cli/affected-e2e.md +++ b/nx-dev/nx-dev/public/documentation/generated/cli/affected-e2e.md @@ -99,6 +99,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/nx-dev/nx-dev/public/documentation/generated/cli/affected-libs.md b/nx-dev/nx-dev/public/documentation/generated/cli/affected-libs.md index 21b3a97af4738..97b184161c1c5 100644 --- a/nx-dev/nx-dev/public/documentation/generated/cli/affected-libs.md +++ b/nx-dev/nx-dev/public/documentation/generated/cli/affected-libs.md @@ -87,6 +87,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/nx-dev/nx-dev/public/documentation/generated/cli/affected-lint.md b/nx-dev/nx-dev/public/documentation/generated/cli/affected-lint.md index 33fad6179ef0d..399de75d7bf39 100644 --- a/nx-dev/nx-dev/public/documentation/generated/cli/affected-lint.md +++ b/nx-dev/nx-dev/public/documentation/generated/cli/affected-lint.md @@ -99,6 +99,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/nx-dev/nx-dev/public/documentation/generated/cli/affected-test.md b/nx-dev/nx-dev/public/documentation/generated/cli/affected-test.md index 913a1e2032a60..5ed1ecb310b54 100644 --- a/nx-dev/nx-dev/public/documentation/generated/cli/affected-test.md +++ b/nx-dev/nx-dev/public/documentation/generated/cli/affected-test.md @@ -99,6 +99,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/nx-dev/nx-dev/public/documentation/generated/cli/affected.md b/nx-dev/nx-dev/public/documentation/generated/cli/affected.md index 455a5d8a3c51f..55850844973a3 100644 --- a/nx-dev/nx-dev/public/documentation/generated/cli/affected.md +++ b/nx-dev/nx-dev/public/documentation/generated/cli/affected.md @@ -105,6 +105,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### target Task to run for affected projects diff --git a/nx-dev/nx-dev/public/documentation/generated/cli/format-check.md b/nx-dev/nx-dev/public/documentation/generated/cli/format-check.md index c38f534c2c0d4..743ec6fbc9651 100644 --- a/nx-dev/nx-dev/public/documentation/generated/cli/format-check.md +++ b/nx-dev/nx-dev/public/documentation/generated/cli/format-check.md @@ -71,6 +71,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/nx-dev/nx-dev/public/documentation/generated/cli/format-write.md b/nx-dev/nx-dev/public/documentation/generated/cli/format-write.md index 30040747f4245..d6d69212b0bda 100644 --- a/nx-dev/nx-dev/public/documentation/generated/cli/format-write.md +++ b/nx-dev/nx-dev/public/documentation/generated/cli/format-write.md @@ -71,6 +71,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/nx-dev/nx-dev/public/documentation/generated/cli/print-affected.md b/nx-dev/nx-dev/public/documentation/generated/cli/print-affected.md index a3fbe641e70b1..119f66983685e 100644 --- a/nx-dev/nx-dev/public/documentation/generated/cli/print-affected.md +++ b/nx-dev/nx-dev/public/documentation/generated/cli/print-affected.md @@ -99,6 +99,12 @@ Default: `false` Rerun the tasks even when the results are available in the cache +### tags + +Default: `` + +Filter affected projects by tags using glob patterns + ### uncommitted Uncommitted changes diff --git a/packages/workspace/src/command-line/affected.ts b/packages/workspace/src/command-line/affected.ts index e96614c1eb8ec..ab759a9b33b87 100644 --- a/packages/workspace/src/command-line/affected.ts +++ b/packages/workspace/src/command-line/affected.ts @@ -22,6 +22,7 @@ import type { NxArgs, RawNxArgs } from './utils'; import { splitArgsIntoNxArgsAndOverrides } from './utils'; import { performance } from 'perf_hooks'; import type { Environment } from '../core/shared-interfaces'; +import * as minimatch from 'minimatch'; export async function affected( command: 'apps' | 'libs' | 'dep-graph' | 'print-affected' | 'affected', @@ -40,7 +41,8 @@ export async function affected( const projectGraph = await createProjectGraphAsync(); const projects = projectsToRun(nxArgs, projectGraph); - const projectsNotExcluded = applyExclude(projects, nxArgs); + const projectsMatchingTags = matchTags(projects, nxArgs); + const projectsNotExcluded = applyExclude(projectsMatchingTags, nxArgs); const env = readEnvironment(); const filteredProjects = applyOnlyFailed(projectsNotExcluded, nxArgs, env); @@ -132,6 +134,26 @@ export async function affected( } } +function matchTags(projects: Record, nxArgs: NxArgs) { + // We do not want to filter projects if there are no tags passed in to match on + if (!nxArgs.tags || nxArgs.tags.length === 0) { + return projects; + } + + const tagGlobs = nxArgs.tags; + + return Object.entries(projects) + .filter(([, project]) => { + const tags: string[] = project.data.tags; + + /* + * Ensure that each tag passed in (in glob format) exists in the projects tag array + */ + return tagGlobs.every((glob) => tags.some((tag) => minimatch(tag, glob))); + }) + .reduce((prev, [key, project]) => ({ ...prev, [key]: project }), {}); +} + function projectsToRun(nxArgs: NxArgs, projectGraph: ProjectGraph) { if (nxArgs.all) return projectGraph.nodes; diff --git a/packages/workspace/src/command-line/nx-commands.ts b/packages/workspace/src/command-line/nx-commands.ts index 8351558f01eff..0d84e1966ab5b 100644 --- a/packages/workspace/src/command-line/nx-commands.ts +++ b/packages/workspace/src/command-line/nx-commands.ts @@ -431,6 +431,12 @@ function withAffectedOptions(yargs: yargs.Argv): yargs.Argv { .option('verbose', { describe: 'Print additional error stack trace on failure', }) + .option('tags', { + describe: 'Filter affected projects by tags using glob patterns', + default: [], + type: 'array', + coerce: parseCSV, + }) .conflicts({ files: ['uncommitted', 'untracked', 'base', 'head', 'all'], untracked: ['uncommitted', 'files', 'base', 'head', 'all'], diff --git a/packages/workspace/src/command-line/utils.ts b/packages/workspace/src/command-line/utils.ts index 89b210f4631fa..10ee3d01d935c 100644 --- a/packages/workspace/src/command-line/utils.ts +++ b/packages/workspace/src/command-line/utils.ts @@ -33,6 +33,7 @@ const runAffected: string[] = [ 'files', 'plain', 'select', + 'tags', ]; export interface RawNxArgs extends NxArgs { @@ -66,6 +67,7 @@ export interface NxArgs { 'hide-cached-output'?: boolean; hideCachedOutput?: boolean; scan?: boolean; + tags?: string[]; } const ignoreArgs = ['$0', '_']; @@ -184,6 +186,10 @@ export function splitArgsIntoNxArgsAndOverrides( }); } } + + if (!nxArgs.tags) { + nxArgs.tags = []; + } } if (!nxArgs.skipNxCache) { From 8680f60a7b0d47c9c113a0b430be61ebad24a97e Mon Sep 17 00:00:00 2001 From: jaybell Date: Tue, 4 Jan 2022 11:16:32 -0800 Subject: [PATCH 2/2] cleanup(core): update tests for split args to account for tags ISSUES CLOSED: #2675 --- packages/workspace/src/command-line/utils.spec.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/workspace/src/command-line/utils.spec.ts b/packages/workspace/src/command-line/utils.spec.ts index 32c2d72b03494..1c5f7cd85db86 100644 --- a/packages/workspace/src/command-line/utils.spec.ts +++ b/packages/workspace/src/command-line/utils.spec.ts @@ -23,6 +23,7 @@ describe('splitArgs', () => { base: 'sha1', head: 'sha2', skipNxCache: false, + tags: [], }); }); @@ -53,6 +54,7 @@ describe('splitArgs', () => { ).toEqual({ base: 'main', skipNxCache: false, + tags: [], }); }); @@ -75,6 +77,7 @@ describe('splitArgs', () => { ).toEqual({ base: 'develop', skipNxCache: false, + tags: [], }); }); @@ -94,6 +97,7 @@ describe('splitArgs', () => { ).toEqual({ base: 'main', skipNxCache: false, + tags: [], }); }); @@ -128,6 +132,7 @@ describe('splitArgs', () => { base: 'sha1', head: 'sha2', skipNxCache: false, + tags: [], }); expect(overrides).toEqual({ notNxArg: true, @@ -155,6 +160,7 @@ describe('splitArgs', () => { base: 'envVarSha1', head: 'envVarSha2', skipNxCache: false, + tags: [], }); expect( @@ -171,6 +177,7 @@ describe('splitArgs', () => { base: 'envVarSha1', head: 'directlyOnCommandSha1', skipNxCache: false, + tags: [], }); expect( @@ -187,6 +194,7 @@ describe('splitArgs', () => { base: 'directlyOnCommandSha2', head: 'envVarSha2', skipNxCache: false, + tags: [], }); // Reset process data