Skip to content

Commit

Permalink
feat(core): add an option to run-many by tags
Browse files Browse the repository at this point in the history
`nx run-many --target=deploy --tags=foo` runs the given target for all
projects that have a tag `foo` configured in nx.json.

Closes nrwl#2675.
  • Loading branch information
Schibum committed Jan 18, 2021
1 parent a1c4d94 commit a6d133b
Show file tree
Hide file tree
Showing 64 changed files with 34,977 additions and 6 deletions.
10 changes: 10 additions & 0 deletions docs/angular/cli/run-many.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ Build proj1 and proj2 and all their dependencies.:
nx run-many --target=test --projects=proj1,proj2 --with-deps
```

Test all projects that are tagged with foo or bar.:

```bash
nx run-many --target=test --tags=foo,bar
```

## Options

### all
Expand Down Expand Up @@ -78,6 +84,10 @@ Default: `false`

Rerun the tasks even when the results are available in the cache

### tags

Run projects having any of the given tags (comma delimited)

### target

Task to run for affected projects
Expand Down
10 changes: 10 additions & 0 deletions docs/node/cli/run-many.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ Build proj1 and proj2 and all their dependencies.:
nx run-many --target=test --projects=proj1,proj2 --with-deps
```

Test all projects that are tagged with foo or bar.:

```bash
nx run-many --target=test --tags=foo,bar
```

## Options

### all
Expand Down Expand Up @@ -78,6 +84,10 @@ Default: `false`

Rerun the tasks even when the results are available in the cache

### tags

Run projects having any of the given tags (comma delimited)

### target

Task to run for affected projects
Expand Down
10 changes: 10 additions & 0 deletions docs/react/cli/run-many.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ Build proj1 and proj2 and all their dependencies.:
nx run-many --target=test --projects=proj1,proj2 --with-deps
```

Test all projects that are tagged with foo or bar.:

```bash
nx run-many --target=test --tags=foo,bar
```

## Options

### all
Expand Down Expand Up @@ -78,6 +84,10 @@ Default: `false`

Rerun the tasks even when the results are available in the cache

### tags

Run projects having any of the given tags (comma delimited)

### target

Task to run for affected projects
Expand Down
16 changes: 16 additions & 0 deletions e2e/workspace/src/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,20 @@ describe('run-many', () => {
const libB = uniq('libb-rand');
const libC = uniq('libc-rand');
const libD = uniq('libd-rand');
const tagA = uniq('taga-rand');

runCLI(`generate @nrwl/angular:app ${appA}`);
runCLI(`generate @nrwl/angular:lib ${libA} --buildable --defaults`);
runCLI(`generate @nrwl/angular:lib ${libB} --buildable --defaults`);
runCLI(`generate @nrwl/angular:lib ${libC} --buildable --defaults`);
runCLI(`generate @nrwl/angular:lib ${libD} --defaults`);

// Add tagA to libA and libB
const nxJson: NxJson = readJson('nx.json');
nxJson.projects[libA].tags = [tagA];
nxJson.projects[libB].tags = [tagA];
updateFile('nx.json', JSON.stringify(nxJson));

// libA depends on libC
updateFile(
`libs/${libA}/src/lib/${libA}.module.spec.ts`,
Expand All @@ -97,6 +104,15 @@ describe('run-many', () => {
expect(buildParallel).not.toContain(`- ${libD}`);
expect(buildParallel).toContain('Running target "build" succeeded');

// testing run many tags starting
const buildTags = runCLI(`run-many --target=build --tags="${tagA}"`);
expect(buildTags).toContain(`Running target build for projects:`);
expect(buildTags).toContain(`- ${libA}`);
expect(buildTags).toContain(`- ${libB}`);
expect(buildTags).not.toContain(`- ${libC}`);
expect(buildTags).not.toContain(`- ${libD}`);
expect(buildTags).toContain('Running target "build" succeeded');

// testing run many --all starting
const buildAllParallel = runCLI(`run-many --target=build --all`);
expect(buildAllParallel).toContain(`Running target build for projects:`);
Expand Down
17 changes: 14 additions & 3 deletions packages/workspace/src/command-line/nx-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,25 @@ function withRunManyOptions(yargs: yargs.Argv): yargs.Argv {
describe: 'Projects to run (comma delimited)',
type: 'string',
})
.option('tags', {
describe: 'Run projects having any of the given tags (comma delimited)',
type: 'string',
})
.option('all', {
describe: 'Run the target on all projects in the workspace',
type: 'boolean',
default: undefined,
})
.check(({ all, projects }) => {
if ((all && projects) || (!all && !projects))
throw new Error('You must provide either --all or --projects');
.check(({ all, projects, tags }) => {
if (
(all && projects) ||
(projects && tags) ||
(all && tags) ||
(!all && !projects && !tags)
)
throw new Error(
'You must provide either --all or --projects or --tags'
);
return true;
})
.options('runner', {
Expand Down
18 changes: 16 additions & 2 deletions packages/workspace/src/command-line/run-many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ProjectGraphNode,
withDeps,
} from '../core/project-graph';
import { readEnvironment } from '../core/file-utils';
import { readEnvironment, readNxJson } from '../core/file-utils';
import { DefaultReporter } from '../tasks-runner/default-reporter';
import { projectHasTarget } from '../utils/project-graph-utils';
import { output } from '../utils/output';
Expand Down Expand Up @@ -44,14 +44,28 @@ export async function runMany(parsedArgs: yargs.Arguments) {
);
}

function selectProjectsFromTags(tags: string[]): string[] {
let nxJson = readNxJson();
return Object.entries(nxJson.projects)
.filter(([name, project]) => {
let projectTags = project.tags || [];
return tags.some((tag) => projectTags.includes(tag));
})
.map(([name]) => name);
}

function projectsToRun(nxArgs: NxArgs, projectGraph: ProjectGraph) {
const allProjects = Object.values(projectGraph.nodes);
if (nxArgs.all) {
return runnableForTarget(allProjects, nxArgs.target);
} else {
let projects = nxArgs.projects;
if (projects.length === 0) {
projects = selectProjectsFromTags(nxArgs.tags);
}
checkForInvalidProjects(nxArgs, allProjects);
let selectedProjects = allProjects.filter(
(p) => nxArgs.projects.indexOf(p.name) > -1
(p) => projects.indexOf(p.name) > -1
);
if (nxArgs.withDeps) {
selectedProjects = Object.values(
Expand Down
10 changes: 9 additions & 1 deletion packages/workspace/src/command-line/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const runOne = [
'scan',
];

const runMany = [...runOne, 'projects', 'quiet', 'all', 'verbose'];
const runMany = [...runOne, 'projects', 'tags', 'quiet', 'all', 'verbose'];

const runAffected = [
...runOne,
Expand Down Expand Up @@ -65,6 +65,7 @@ export interface NxArgs {
withDeps?: boolean;
'with-deps'?: boolean;
projects?: string[];
tags?: string[];
select?: string;
skipNxCache?: boolean;
'skip-nx-cache'?: boolean;
Expand Down Expand Up @@ -102,6 +103,13 @@ export function splitArgsIntoNxArgsAndOverrides(
.split(',')
.map((p: string) => p.trim());
}
if (!nxArgs.tags) {
nxArgs.tags = [];
} else {
nxArgs.tags = (args.tags as string)
.split(',')
.map((p: string) => p.trim());
}
}

if (nxArgs.prod) {
Expand Down
13 changes: 13 additions & 0 deletions play/play/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Editor configuration, see http://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
max_line_length = off
trim_trailing_whitespace = false
33 changes: 33 additions & 0 deletions play/play/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"root": true,
"ignorePatterns": ["**/*"],
"plugins": ["@nrwl/nx"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {
"@nrwl/nx/enforce-module-boundaries": [
"error",
{
"enforceBuildableLibDependency": true,
"allow": [],
"depConstraints": [
{ "sourceTag": "*", "onlyDependOnLibsWithTags": ["*"] }
]
}
]
}
},
{
"files": ["*.ts", "*.tsx"],
"extends": ["plugin:@nrwl/nx/typescript"],
"parserOptions": { "project": "./tsconfig.*?.json" },
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"extends": ["plugin:@nrwl/nx/javascript"],
"rules": {}
}
]
}
39 changes: 39 additions & 0 deletions play/play/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# compiled output
/dist
/tmp
/out-tsc

# dependencies
/node_modules

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# misc
/.sass-cache
/connect.lock
/coverage
/libpeerconnection.log
npm-debug.log
yarn-error.log
testem.log
/typings

# System Files
.DS_Store
Thumbs.db
4 changes: 4 additions & 0 deletions play/play/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Add files here to ignore them from prettier formatting

/dist
/coverage
3 changes: 3 additions & 0 deletions play/play/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
7 changes: 7 additions & 0 deletions play/play/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"recommendations": [
"ms-vscode.vscode-typescript-tslint-plugin",
"esbenp.prettier-vscode",
"firsttris.vscode-jest-runner"
]
}
Loading

0 comments on commit a6d133b

Please sign in to comment.