Skip to content

Commit

Permalink
feat(core): support env vars and runtime deps as inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Jun 25, 2022
1 parent 0ae2154 commit 8d4e87d
Show file tree
Hide file tree
Showing 13 changed files with 438 additions and 484 deletions.
2 changes: 1 addition & 1 deletion docs/shared/tools-workspace-builders.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export const mimicNxHasher: CustomHasher = async (
task: Task,
context: HasherContext
) => {
return context.hasher.hashTaskWithDepsAndContext(task);
return context.hasher.hashTask(task);
};

export default mimicNxHasher;
Expand Down
30 changes: 16 additions & 14 deletions packages/linter/src/executors/eslint/hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,32 @@ export default async function run(
workspaceConfig: ProjectsConfigurations;
}
): Promise<Hash> {
const res = await context.hasher.hashTask(task);
if (task.overrides['hasTypeAwareRules'] === true) {
return context.hasher.hashTaskWithDepsAndContext(task);
return res;
}

const command = await context.hasher.hashCommand(task);
const source = await context.hasher.hashSource(task);
const deps = allDeps(task.id, context.taskGraph, context.projectGraph);
const tags = context.hasher.hashArray(
deps.map((d) => (context.workspaceConfig.projects[d].tags || []).join('|'))
);
const taskContext = await context.hasher.hashContext();

const command = res.details['command'];
const selfSource = res.details.nodes[`${task.target.project}:$filesets`];

const nodes = {};
const hashes = [] as string[];
for (const d of Object.keys(res.details.nodes)) {
if (d.indexOf('$fileset') === -1) {
nodes[d] = res.details.nodes[d];
hashes.push(res.details.nodes[d]);
}
}
return {
value: context.hasher.hashArray([
command,
source,
tags,
taskContext.implicitDeps.value,
taskContext.runtime.value,
]),
value: context.hasher.hashArray([command, selfSource, ...hashes, tags]),
details: {
command,
nodes: { [task.target.project]: source, tags },
implicitDeps: taskContext.implicitDeps.files,
runtime: taskContext.runtime.runtime,
nodes: { [task.target.project]: selfSource, tags, ...nodes },
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('NxPlugin Executor Generator', () => {
* you can consume workspace details from the context.
*/
export const myExecutorHasher: CustomHasher = async (task, context) => {
return context.hasher.hashTaskWithDepsAndContext(task)
return context.hasher.hashTask(task)
};
export default myExecutorHasher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { <%=propertyName%>Hasher } from './hasher';
describe('<%=propertyName%>Hasher', () => {
it('should generate hash', async () => {
const mockHasher: Hasher = {
hashTaskWithDepsAndContext: jest.fn().mockReturnValue({value: 'hashed-task'})
hashTask: jest.fn().mockReturnValue({value: 'hashed-task'})
} as unknown as Hasher
const hash = await <%=propertyName%>Hasher({
id: 'my-task-id',
Expand All @@ -19,4 +19,4 @@ describe('<%=propertyName%>Hasher', () => {
} as unknown as HasherContext)
expect(hash).toEqual({value: 'hashed-task'})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CustomHasher } from '@nrwl/devkit';
* you can consume workspace details from the context.
*/
export const <%=propertyName%>Hasher: CustomHasher = async (task, context) => {
return context.hasher.hashTaskWithDepsAndContext(task)
return context.hasher.hashTask(task)
};

export default <%=propertyName%>Hasher;
101 changes: 63 additions & 38 deletions packages/nx/schemas/nx-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
"$ref": "#/definitions/tasksRunnerOptions"
}
},
"namedInputs": {
"type": "object",
"description": "Named inputs used by inputs defined in targets",
"additionalProperties": {
"$ref": "#/definitions/inputs"
}
},
"targetDefaults": {
"type": "object",
"description": "Target defaults",
Expand Down Expand Up @@ -77,6 +84,61 @@
}
},
"definitions": {
"inputs": {
"type": "array",
"items": {
"oneOf": [
{
"type": "string"
},
{
"type": "object",
"properties": {
"fileset": {
"type": "string",
"description": "A glob"
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"projects": {
"type": "string",
"description": "The projects that the targets belong to.",
"enum": ["self", "dependencies"]
},
"input": {
"type": "string",
"description": "The name of the input."
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"runtime": {
"type": "string",
"description": "The command that will be executed and the results of which is added to the hash"
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"env": {
"type": "string",
"description": "The name of the env var which value is added to the hash"
}
},
"additionalProperties": false
}
]
}
},
"cliOptions": {
"type": "object",
"description": "Default generator collection.",
Expand Down Expand Up @@ -111,11 +173,6 @@
},
"additionalProperties": false
},
"namedInputs": {
"type": "object",
"description": "Named inputs used by inputs defined in targets",
"additionalProperties": true
},
"targetDependencyConfig": {
"type": "array",
"items": {
Expand Down Expand Up @@ -146,39 +203,7 @@
"description": "Target defaults",
"properties": {
"inputs": {
"type": "array",
"items": {
"oneOf": [
{
"type": "string"
},
{
"type": "object",
"properties": {
"fileset": {
"type": "string",
"description": "The name of the target."
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"projects": {
"type": "string",
"description": "The projects that the targets belong to.",
"enum": ["self", "dependencies"]
},
"input": {
"type": "string",
"description": "The name of the input."
}
},
"additionalProperties": false
}
]
}
"$ref": "#/definitions/inputs"
},
"dependsOn": {
"type": "array",
Expand Down
95 changes: 61 additions & 34 deletions packages/nx/schemas/project-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"namedInputs": {
"type": "object",
"description": "Named inputs used by inputs defined in targets",
"additionalProperties": true
"additionalProperties": {
"$ref": "#/definitions/inputs"
}
},
"targets": {
"type": "object",
Expand Down Expand Up @@ -36,39 +38,7 @@
}
},
"inputs": {
"type": "array",
"items": {
"oneOf": [
{
"type": "string"
},
{
"type": "object",
"properties": {
"fileset": {
"type": "string",
"description": "The name of the target."
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"projects": {
"type": "string",
"description": "The projects that the targets belong to.",
"enum": ["self", "dependencies"]
},
"input": {
"type": "string",
"description": "The name of the input."
}
},
"additionalProperties": false
}
]
}
"$ref": "#/definitions/inputs"
},
"dependsOn": {
"type": "array",
Expand Down Expand Up @@ -110,5 +80,62 @@
"type": "string"
}
}
},
"definitions": {
"inputs": {
"type": "array",
"items": {
"oneOf": [
{
"type": "string"
},
{
"type": "object",
"properties": {
"fileset": {
"type": "string",
"description": "A glob used to determine a fileset."
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"projects": {
"type": "string",
"description": "The projects that the input belong to.",
"enum": ["self", "dependencies"]
},
"input": {
"type": "string",
"description": "Named input."
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"runtime": {
"type": "string",
"description": "The command that will be executed and included into the hash."
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"env": {
"type": "string",
"description": "The env var that will be included into the hash."
}
},
"additionalProperties": false
}
]
}
}
}
}
6 changes: 3 additions & 3 deletions packages/nx/src/config/task-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ export interface Task {
*/
command: string;
/**
* Hashes of other nodes which are included in the hash
* Hashes of inputs used in the hash
*/
nodes: { [name: string]: string };
/**
* Hashes of implicit dependencies which are included in the hash
*/
implicitDeps: { [fileName: string]: string };
implicitDeps?: { [fileName: string]: string };
/**
* Hash of the runtime environment which the task was executed
*/
runtime: { [input: string]: string };
runtime?: { [input: string]: string };
};
}

Expand Down
4 changes: 3 additions & 1 deletion packages/nx/src/config/workspace-json-project-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ export interface TargetDependencyConfig {

export type InputDefinition =
| { input: string; projects: 'self' | 'dependencies' }
| { fileset: string };
| { fileset: string }
| { runtime: string }
| { env: string };

/**
* Target's configuration
Expand Down
Loading

0 comments on commit 8d4e87d

Please sign in to comment.