Skip to content

Commit

Permalink
fix(core): use default configuration when the provided one is not ava…
Browse files Browse the repository at this point in the history
…ilable
  • Loading branch information
vsavkin committed Mar 4, 2020
1 parent b70fe05 commit 115a1ab
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 77 deletions.
16 changes: 16 additions & 0 deletions e2e/run-many.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ forEachCli(() => {
describe('Run Many', () => {
it('should build specific and all projects', () => {
newProject();
const appA = uniq('appa-rand');
const libA = uniq('liba-rand');
const libB = uniq('libb-rand');
const libC = uniq('libc-rand');
const libD = uniq('libd-rand');

l(runCLI(`generate @nrwl/angular:app ${appA}`));
l(runCLI(`generate @nrwl/angular:lib ${libA} --publishable --defaults`));
l(runCLI(`generate @nrwl/angular:lib ${libB} --publishable --defaults`));
l(runCLI(`generate @nrwl/angular:lib ${libC} --publishable --defaults`));
Expand Down Expand Up @@ -75,6 +77,20 @@ forEachCli(() => {
expect(buildWithDeps).toContain('Running target "build" succeeded');

l('=======> testing run many --with-deps complete');

l('=======> testing run many --configuration');

const buildConfig = l(
runCLI(
`run-many --target=build --projects="${appA},${libA}" --configuration=production`
)
);
expect(buildConfig).toContain(`Running target build for projects:`);
expect(buildConfig).toContain(`build ${appA} --configuration production`);
expect(buildConfig).toContain(`build ${libA}`);
expect(buildConfig).toContain('Running target "build" succeeded');

l('=======> testing run many --configuration');
}, 1000000);
});
});
2 changes: 1 addition & 1 deletion e2e/tasks-runner-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
forEachCli(() => {
describe('Task Runner V2', () => {
describe('run-one with deps', () => {
it('should be able to run tasks in parallel', () => {
it('should be able to run the task for the specified project and its dependencies', () => {
newProject();

updateFile('nx.json', c => {
Expand Down
3 changes: 2 additions & 1 deletion packages/tao/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,14 @@ export function validateTargetAndConfiguration(
}
const targets = architect.targets;

const availableTargets = [...targets.keys()];
const target = targets.get(opts.target);
if (!target) {
throw new Error(
`Could not find target "${opts.target}" in the ${
opts.project
} project. Valid targets are: ${terminal.bold(
Object.keys(targets).join(', ')
availableTargets.join(', ')
)}`
);
}
Expand Down
19 changes: 7 additions & 12 deletions packages/workspace/src/command-line/affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '../core/project-graph';
import { calculateFileChanges, readEnvironment } from '../core/file-utils';
import { printAffected } from './print-affected';
import { projectHasTargetAndConfiguration } from '../utils/project-has-target-and-configuration';
import { projectHasTarget } from '../utils/project-graph-utils';
import { DefaultReporter } from '../tasks-runner/default-reporter';

export function affected(command: string, parsedArgs: yargs.Arguments): void {
Expand Down Expand Up @@ -87,12 +87,12 @@ export function affected(command: string, parsedArgs: yargs.Arguments): void {

case 'print-affected':
if (nxArgs.target) {
const projectWithTargetAndConfig = allProjectsWithTargetAndConfiguration(
const projectsWithTarget = allProjectsWithTarget(
affectedProjects,
nxArgs
);
printAffected(
projectWithTargetAndConfig,
projectsWithTarget,
affectedProjects,
projectGraph,
nxArgs,
Expand All @@ -104,13 +104,13 @@ export function affected(command: string, parsedArgs: yargs.Arguments): void {
break;

case 'affected': {
const projectWithTargetAndConfig = allProjectsWithTargetAndConfiguration(
const projectsWithTarget = allProjectsWithTarget(
affectedProjects,
nxArgs
);
printArgsWarning(nxArgs);
runCommand(
projectWithTargetAndConfig,
projectsWithTarget,
projectGraph,
env,
nxArgs,
Expand All @@ -126,13 +126,8 @@ export function affected(command: string, parsedArgs: yargs.Arguments): void {
}
}

function allProjectsWithTargetAndConfiguration(
projects: ProjectGraphNode[],
nxArgs: NxArgs
) {
return projects.filter(p =>
projectHasTargetAndConfiguration(p, nxArgs.target, nxArgs.configuration)
);
function allProjectsWithTarget(projects: ProjectGraphNode[], nxArgs: NxArgs) {
return projects.filter(p => projectHasTarget(p, nxArgs.target));
}

function printError(e: any, verbose?: boolean) {
Expand Down
24 changes: 7 additions & 17 deletions packages/workspace/src/command-line/run-many.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as yargs from 'yargs';
import { runCommand } from '../tasks-runner/run-command';
import { splitArgsIntoNxArgsAndOverrides, NxArgs } from './utils';
import { output } from '../utils/output';
import { NxArgs, splitArgsIntoNxArgsAndOverrides } from './utils';
import {
createProjectGraph,
ProjectGraph,
ProjectGraphNode,
withDeps
} from '../core/project-graph';
import { readEnvironment } from '../core/file-utils';
import { projectHasTargetAndConfiguration } from '../utils/project-has-target-and-configuration';
import { DefaultReporter } from '../tasks-runner/default-reporter';
import { projectHasTarget } from '../utils/project-graph-utils';
import { output } from '@nrwl/workspace';

export function runMany(parsedArgs: yargs.Arguments): void {
const { nxArgs, overrides } = splitArgsIntoNxArgsAndOverrides(
Expand All @@ -37,11 +37,7 @@ export function runMany(parsedArgs: yargs.Arguments): void {
function projectsToRun(nxArgs: NxArgs, projectGraph: ProjectGraph) {
const allProjects = Object.values(projectGraph.nodes);
if (nxArgs.all) {
return runnableForTargetAndConfiguration(
allProjects,
nxArgs.target,
nxArgs.configuration
);
return runnableForTarget(allProjects, nxArgs.target);
} else {
checkForInvalidProjects(nxArgs, allProjects);
let selectedProjects = allProjects.filter(
Expand All @@ -52,12 +48,7 @@ function projectsToRun(nxArgs: NxArgs, projectGraph: ProjectGraph) {
withDeps(projectGraph, selectedProjects).nodes
);
}
return runnableForTargetAndConfiguration(
selectedProjects,
nxArgs.target,
nxArgs.configuration,
true
);
return runnableForTarget(selectedProjects, nxArgs.target, true);
}
}

Expand All @@ -73,17 +64,16 @@ function checkForInvalidProjects(
}
}

function runnableForTargetAndConfiguration(
function runnableForTarget(
projects: ProjectGraphNode[],
target: string,
configuration?: string,
strict = false
): ProjectGraphNode[] {
const notRunnable = [];
const runnable = [];

for (let project of projects) {
if (projectHasTargetAndConfiguration(project, target, configuration)) {
if (projectHasTarget(project, target)) {
runnable.push(project);
} else {
notRunnable.push(project);
Expand Down
32 changes: 16 additions & 16 deletions packages/workspace/src/command-line/run-one.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { runCommand } from '../tasks-runner/run-command';
import {
createProjectGraph,
onlyWorkspaceProjects,
ProjectGraph,
withDeps
} from '../core/project-graph';
import { createProjectGraph, ProjectGraph } from '../core/project-graph';
import { readEnvironment } from '../core/file-utils';
import { EmptyReporter } from '../tasks-runner/empty-reporter';
import { splitArgsIntoNxArgsAndOverrides } from './utils';
import { DefaultReporter } from '../tasks-runner/default-reporter';
import { projectHasTarget } from '../utils/project-graph-utils';

export function runOne(opts: {
project: string;
Expand All @@ -30,11 +25,12 @@ export function runOne(opts: {
const { projects, projectsMap } = getProjects(
projectGraph,
nxArgs.withDeps,
opts.project
opts.project,
opts.target
);
const env = readEnvironment(opts.target, projectsMap);
const reporter = nxArgs.withDeps
? new DefaultReporter()
? new (require(`../tasks-runner/default-reporter`)).DefaultReporter()
: new EmptyReporter();

runCommand(projects, projectGraph, env, nxArgs, overrides, reporter);
Expand All @@ -43,20 +39,24 @@ export function runOne(opts: {
function getProjects(
projectGraph: ProjectGraph,
includeDeps: boolean,
project: string
) {
project: string,
target: string
): any {
let projects = [projectGraph.nodes[project]];
let projectsMap = {
[project]: projectGraph.nodes[project]
};

if (includeDeps) {
const projectWithDeps = onlyWorkspaceProjects(
withDeps(projectGraph, projects)
).nodes;
const s = require(`../core/project-graph`);
const deps = s.onlyWorkspaceProjects(s.withDeps(projectGraph, projects))
.nodes;
const projectsWithTarget = Object.values(deps).filter((p: any) =>
projectHasTarget(p, target)
);
return {
projects: Object.values(projectWithDeps),
projectsMap: projectWithDeps
projects: projectsWithTarget,
projectsMap: deps
};
} else {
return { projects, projectsMap };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export enum DependencyType {
export interface ProjectGraphNode<T extends {} = {}> {
type: string;
name: string;
data: T & { files: FileData[]; [k: string]: any };
data: T & {
architect?: { [k: string]: any };
files: FileData[];
[k: string]: any;
};
}

export type ProjectGraphNodeRecords = Record<string, ProjectGraphNode>;
Expand Down
18 changes: 13 additions & 5 deletions packages/workspace/src/tasks-runner/run-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Environment, NxJson } from '../core/shared-interfaces';
import { NxArgs } from '@nrwl/workspace/src/command-line/utils';
import { isRelativePath } from '../utils/fileutils';
import { Hasher } from './hasher';
import { projectHasTargetAndConfiguration } from '../utils/project-graph-utils';

type RunArgs = yargs.Arguments & ReporterArgs;

Expand All @@ -26,14 +27,14 @@ export async function runCommand<T extends RunArgs>(
...overrides
});

const tasks: Task[] = projectsToRun.map(project =>
createTask({
const tasks: Task[] = projectsToRun.map(project => {
return createTask({
project,
target: nxArgs.target,
configuration: nxArgs.configuration,
overrides: overrides
})
);
});
});

if (tasksRunner !== require('./default-tasks-runner').defaultTasksRunner) {
const hasher = new Hasher(projectGraph, nxJson, tasksOptions);
Expand Down Expand Up @@ -96,10 +97,17 @@ export function createTask({
configuration,
overrides
}: TaskParams): Task {
const config = projectHasTargetAndConfiguration(
project,
target,
configuration
)
? configuration
: undefined;
const qualifiedTarget = {
project: project.name,
target,
configuration
configuration: config
};
return {
id: getId(qualifiedTarget),
Expand Down
19 changes: 19 additions & 0 deletions packages/workspace/src/utils/project-graph-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ProjectGraphNode } from '../core/project-graph/project-graph-models';

export function projectHasTarget(project: ProjectGraphNode, target: string) {
return (
project.data && project.data.architect && project.data.architect[target]
);
}

export function projectHasTargetAndConfiguration(
project: ProjectGraphNode,
target: string,
configuration: string
) {
return (
projectHasTarget(project, target) &&
project.data.architect[target].configurations &&
project.data.architect[target].configurations[configuration]
);
}

This file was deleted.

0 comments on commit 115a1ab

Please sign in to comment.