Skip to content

Commit

Permalink
feat(core): update dependsOn configuration to use dependencies proper…
Browse files Browse the repository at this point in the history
…ty rather than tokens
  • Loading branch information
AgentEnder committed Apr 26, 2023
1 parent 308dea3 commit 9d28096
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 98 deletions.
35 changes: 30 additions & 5 deletions docs/shared/reference/project-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,37 @@ You can also express task dependencies with an object syntax:
```

{% /tab %}
{% tab label="Version 16+" %}
{% tab label="Version 16+ (self)" %}

```json
"build": {
"dependsOn": [{
"target": "build", // target name
"params": "ignore" // "forward" or "ignore", defaults to "ignore"
}]
}
```

{% /tab %}
{% tab label="Version 16+ (dependencies)" %}

```json
"build": {
"dependsOn": [{
"projects": "{dependencies}", // "{dependencies}", "{self}" or "project-name"
"dependencies": true, // Run this target on all dependencies first
"target": "build", // target name
"params": "ignore" // "forward" or "ignore", defaults to "ignore"
}]
}
```

{% /tab %}
{% tab label="Version 16+ (specific projects)" %}

```json
"build": {
"dependsOn": [{
"projects": ["my-app"], // Run build on "my-app" first
"target": "build", // target name
"params": "ignore" // "forward" or "ignore", defaults to "ignore"
}]
Expand Down Expand Up @@ -326,10 +351,10 @@ You can write the shorthand configuration above in the object syntax like this:

```json
"build": {
"dependsOn": [{ "projects": "{dependencies}", "target": "build" }]
"dependsOn": [{ "dependencies": true, "target": "build" }] // Run build on my dependencies first
},
"test": {
"dependsOn": [{ "projects": "{self}", "target": "build" }]
"dependsOn": [{ "target": "build" }] // Run build on myself first
}
```

Expand Down Expand Up @@ -395,7 +420,7 @@ This also works when defining a relation for the target of the project itself us
```json
"build": {
// forward params passed to this target to the project target
"dependsOn": [{ "projects": "{self}", "target": "pre-build", "params": "forward" }]
"dependsOn": [{ "target": "pre-build", "params": "forward" }]
}
```

Expand Down
1 change: 0 additions & 1 deletion nx-dev/nx-dev/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"dependsOn": [
{
"target": "build-base",
"projects": "{self}"
}
],
"executor": "nx:run-commands",
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"16.0.0-tokens-for-depends-on": {
"cli": "nx",
"version": "16.0.0-beta.0",
"description": "Replace `dependsOn.projects` with {self} or {dependencies} tokens so that it matches the new expected formatting.",
"description": "Replace `dependsOn.projects` and `inputs` definitions with new configuration format.",
"implementation": "./src/migrations/update-16-0-0/update-depends-on-to-tokens"
},
"16.0.0-update-nx-cloud-runner": {
Expand Down
27 changes: 23 additions & 4 deletions packages/nx/schemas/nx-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,20 @@
"oneOf": [
{
"type": "string",
"description": "{self}, {dependencies}, or a project name."
"description": "A project name"
},
{
"type": "array",
"description": "An array of project specifiers: {self}, {dependencies}, or a project name.",
"description": "An array of project names",
"items": {
"type": "string"
}
}
]
},
"dependencies": {
"type": "boolean"
},
"target": {
"type": "string",
"description": "The name of the target."
Expand All @@ -233,8 +236,24 @@
"default": "ignore"
}
},
"additionalProperties": false,
"required": ["projects", "target"]
"oneOf": [
{
"required": ["projects", "target"]
},
{
"required": ["dependencies", "target"]
},
{
"required": ["target"],
"not": {
"anyOf": [
{ "required": ["projects"] },
{ "required": ["dependencies"] }
]
}
}
],
"additionalProperties": false
}
]
}
Expand Down
27 changes: 23 additions & 4 deletions packages/nx/schemas/project-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,20 @@
"oneOf": [
{
"type": "string",
"description": "{self}, {dependencies}, or a project name."
"description": "A project name"
},
{
"type": "array",
"description": "An array of project specifiers: {self}, {dependencies}, or a project name.",
"description": "An array of project names",
"items": {
"type": "string"
}
}
]
},
"dependencies": {
"type": "boolean"
},
"target": {
"type": "string",
"description": "The name of the target."
Expand All @@ -80,8 +83,24 @@
"default": "ignore"
}
},
"additionalProperties": false,
"required": ["projects", "target"]
"oneOf": [
{
"required": ["projects", "target"]
},
{
"required": ["dependencies", "target"]
},
{
"required": ["target"],
"not": {
"anyOf": [
{ "required": ["projects"] },
{ "required": ["dependencies"] }
]
}
}
],
"additionalProperties": false
}
]
}
Expand Down
27 changes: 23 additions & 4 deletions packages/nx/schemas/workspace-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,20 @@
"oneOf": [
{
"type": "string",
"description": "{self}, {dependencies}, or a project name."
"description": "A project name"
},
{
"type": "array",
"description": "An array of project specifiers: {self}, {dependencies}, or a project name.",
"description": "An array of project names",
"items": {
"type": "string"
}
}
]
},
"dependencies": {
"type": "boolean"
},
"target": {
"type": "string",
"description": "The name of the target."
Expand All @@ -90,8 +93,24 @@
"default": "ignore"
}
},
"additionalProperties": false,
"required": ["projects", "target"]
"oneOf": [
{
"required": ["projects", "target"]
},
{
"required": ["dependencies", "target"]
},
{
"required": ["target"],
"not": {
"anyOf": [
{ "required": ["projects"] },
{ "required": ["dependencies"] }
]
}
}
],
"additionalProperties": false
}
]
}
Expand Down
20 changes: 11 additions & 9 deletions packages/nx/src/config/workspace-json-project-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,20 @@ export interface ProjectConfiguration {

export interface TargetDependencyConfig {
/**
* A list of projects that have `target`. Supports project names or two special values:
*
* - '{self}': This target depends on another target of the same project
* - '{dependencies}': This target depends on targets of the projects of it's deps.
*
* The special values {self}/{dependencies} should be preferred - they prevent cases where a project
* that needs to be built is missed.
* A list of projects that have `target`.
* Should not be specified together with `dependencies`.
*/
projects?: string[] | string;

/**
* If true, the target will be executed for each project that this project depends on.
* Should not be specified together with `projects`.
*/
projects: string[] | string;
dependencies?: boolean;

/**
* The name of the target
* The name of the target to run. If `projects` and `dependencies` are not specified,
* the target will be executed for the same project the the current target is running on`.
*/
target: string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ describe('update-depends-on-to-tokens', () => {
const nxJson = readNxJson(tree);
const build = nxJson.targetDefaults.build.dependsOn[0] as any;
const test = nxJson.targetDefaults.test.dependsOn[0] as any;
expect(build.projects).toEqual('{self}');
expect(test.projects).toEqual('{dependencies}');
expect(build.projects).not.toBeDefined();
expect(test.projects).not.toBeDefined();
expect(test.dependencies).toEqual(true);
expect(nxJson.targetDefaults.other.dependsOn).toEqual(['^deps']);
});

Expand Down Expand Up @@ -72,8 +73,10 @@ describe('update-depends-on-to-tokens', () => {
const project = readProjectConfiguration(tree, 'proj1');
const build = project.targets.build.dependsOn[0] as any;
const test = project.targets.test.dependsOn[0] as any;
expect(build.projects).toEqual('{self}');
expect(test.projects).toEqual('{dependencies}');
expect(build.projects).not.toBeDefined();
expect(build.dependencies).not.toBeDefined();
expect(test.projects).not.toBeDefined();
expect(test.dependencies).toEqual(true);
expect(project.targets.other.dependsOn).toEqual(['^deps']);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ export default async function (tree: Tree) {
for (const dependency of targetConfiguration.dependsOn ?? []) {
if (typeof dependency !== 'string') {
if (dependency.projects === 'self') {
dependency.projects = '{self}';
delete dependency.projects;
projectChanged = true;
} else if (dependency.projects === 'dependencies') {
dependency.projects = '{dependencies}';
delete dependency.projects
dependency.dependencies = true;
projectChanged = true;
}
}
Expand All @@ -41,10 +42,11 @@ function updateDependsOnInsideNxJson(tree: Tree) {
for (const dependency of defaults.dependsOn ?? []) {
if (typeof dependency !== 'string') {
if (dependency.projects === 'self') {
dependency.projects = '{self}';
delete dependency.projects;
nxJsonChanged = true;
} else if (dependency.projects === 'dependencies') {
dependency.projects = '{dependencies}';
delete dependency.projects
dependency.dependencies = true;
nxJsonChanged = true;
}
}
Expand Down
Loading

0 comments on commit 9d28096

Please sign in to comment.