feat(core): pattern matching for target defaults (#26870)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior TargetDefaults can be specified by keys matching either executor or target name only ## Expected Behavior TargetDefaults can match based on a glob pattern that may match the target name. This is useful for things like `e2e-ci--*`. Only 1 target default will ever apply to a given target. We recognize this may be confusing, but is inline with current handling. If no default matches the target name or key, the first default that matches by pattern will be used. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
This commit is contained in:
parent
f2e1f0fad9
commit
92d0e15d1f
@ -32,6 +32,16 @@ describe('project-configuration-utils', () => {
|
|||||||
key: 'default-value-for-targetname',
|
key: 'default-value-for-targetname',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
'e2e-ci--*': {
|
||||||
|
options: {
|
||||||
|
key: 'default-value-for-e2e-ci',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'e2e-ci--file-*': {
|
||||||
|
options: {
|
||||||
|
key: 'default-value-for-e2e-ci-file',
|
||||||
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
it('should prefer executor key', () => {
|
it('should prefer executor key', () => {
|
||||||
@ -61,6 +71,14 @@ describe('project-configuration-utils', () => {
|
|||||||
).toBeNull();
|
).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return longest matching target', () => {
|
||||||
|
expect(
|
||||||
|
// This matches both 'e2e-ci--*' and 'e2e-ci--file-*', we expect the first match to be returned.
|
||||||
|
readTargetDefaultsForTarget('e2e-ci--file-foo', targetDefaults, null)
|
||||||
|
.options['key']
|
||||||
|
).toEqual('default-value-for-e2e-ci-file');
|
||||||
|
});
|
||||||
|
|
||||||
it('should not merge top level properties for incompatible targets', () => {
|
it('should not merge top level properties for incompatible targets', () => {
|
||||||
expect(
|
expect(
|
||||||
mergeTargetConfigurations(
|
mergeTargetConfigurations(
|
||||||
|
|||||||
@ -29,6 +29,7 @@ import {
|
|||||||
AggregateCreateNodesError,
|
AggregateCreateNodesError,
|
||||||
} from '../error-types';
|
} from '../error-types';
|
||||||
import { CreateNodesResult } from '../plugins';
|
import { CreateNodesResult } from '../plugins';
|
||||||
|
import { isGlobPattern } from '../../utils/globs';
|
||||||
|
|
||||||
export type SourceInformation = [file: string | null, plugin: string];
|
export type SourceInformation = [file: string | null, plugin: string];
|
||||||
export type ConfigurationSourceMaps = Record<
|
export type ConfigurationSourceMaps = Record<
|
||||||
@ -1036,10 +1037,27 @@ export function readTargetDefaultsForTarget(
|
|||||||
// If not, use build if it is present.
|
// If not, use build if it is present.
|
||||||
const key = [executor, targetName].find((x) => targetDefaults?.[x]);
|
const key = [executor, targetName].find((x) => targetDefaults?.[x]);
|
||||||
return key ? targetDefaults?.[key] : null;
|
return key ? targetDefaults?.[key] : null;
|
||||||
} else {
|
} else if (targetDefaults?.[targetName]) {
|
||||||
// If the executor is not defined, the only key we have is the target name.
|
// If the executor is not defined, the only key we have is the target name.
|
||||||
return targetDefaults?.[targetName];
|
return targetDefaults?.[targetName];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let matchingTargetDefaultKey: string | null = null;
|
||||||
|
for (const key in targetDefaults ?? {}) {
|
||||||
|
if (isGlobPattern(key) && minimatch(targetName, key)) {
|
||||||
|
if (
|
||||||
|
!matchingTargetDefaultKey ||
|
||||||
|
matchingTargetDefaultKey.length < key.length
|
||||||
|
) {
|
||||||
|
matchingTargetDefaultKey = key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (matchingTargetDefaultKey) {
|
||||||
|
return targetDefaults[matchingTargetDefaultKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
function createRootMap(projectRootMap: Record<string, ProjectConfiguration>) {
|
function createRootMap(projectRootMap: Record<string, ProjectConfiguration>) {
|
||||||
|
|||||||
@ -15,11 +15,9 @@ import { splitByColons } from '../utils/split-target';
|
|||||||
import { getExecutorInformation } from '../command-line/run/executor-utils';
|
import { getExecutorInformation } from '../command-line/run/executor-utils';
|
||||||
import { CustomHasher, ExecutorConfig } from '../config/misc-interfaces';
|
import { CustomHasher, ExecutorConfig } from '../config/misc-interfaces';
|
||||||
import { readProjectsConfigurationFromProjectGraph } from '../project-graph/project-graph';
|
import { readProjectsConfigurationFromProjectGraph } from '../project-graph/project-graph';
|
||||||
import {
|
import { findMatchingProjects } from '../utils/find-matching-projects';
|
||||||
GLOB_CHARACTERS,
|
|
||||||
findMatchingProjects,
|
|
||||||
} from '../utils/find-matching-projects';
|
|
||||||
import { minimatch } from 'minimatch';
|
import { minimatch } from 'minimatch';
|
||||||
|
import { isGlobPattern } from '../utils/globs';
|
||||||
|
|
||||||
export type NormalizedTargetDependencyConfig = TargetDependencyConfig & {
|
export type NormalizedTargetDependencyConfig = TargetDependencyConfig & {
|
||||||
projects: string[];
|
projects: string[];
|
||||||
@ -122,7 +120,7 @@ export function expandWildcardTargetConfiguration(
|
|||||||
dependencyConfig: NormalizedTargetDependencyConfig,
|
dependencyConfig: NormalizedTargetDependencyConfig,
|
||||||
allTargetNames: string[]
|
allTargetNames: string[]
|
||||||
): NormalizedTargetDependencyConfig[] {
|
): NormalizedTargetDependencyConfig[] {
|
||||||
if (!GLOB_CHARACTERS.some((char) => dependencyConfig.target.includes(char))) {
|
if (!isGlobPattern(dependencyConfig.target)) {
|
||||||
return [dependencyConfig];
|
return [dependencyConfig];
|
||||||
}
|
}
|
||||||
let cache = patternResultCache.get(allTargetNames);
|
let cache = patternResultCache.get(allTargetNames);
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import { minimatch } from 'minimatch';
|
import { minimatch } from 'minimatch';
|
||||||
import type { ProjectGraphProjectNode } from '../config/project-graph';
|
import type { ProjectGraphProjectNode } from '../config/project-graph';
|
||||||
|
import { isGlobPattern } from './globs';
|
||||||
|
|
||||||
const validPatternTypes = [
|
const validPatternTypes = [
|
||||||
'name', // Pattern is based on the project's name
|
'name', // Pattern is based on the project's name
|
||||||
@ -18,11 +19,6 @@ interface ProjectPattern {
|
|||||||
value: string;
|
value: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The presence of these characters in a string indicates that it might be a glob pattern.
|
|
||||||
*/
|
|
||||||
export const GLOB_CHARACTERS = ['*', '|', '{', '}', '(', ')'];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find matching project names given a list of potential project names or globs.
|
* Find matching project names given a list of potential project names or globs.
|
||||||
*
|
*
|
||||||
@ -169,7 +165,7 @@ function addMatchingProjectsByName(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!GLOB_CHARACTERS.some((c) => pattern.value.includes(c))) {
|
if (!isGlobPattern(pattern.value)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,7 +200,7 @@ function addMatchingProjectsByTag(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!GLOB_CHARACTERS.some((c) => pattern.value.includes(c))) {
|
if (!isGlobPattern(pattern.value)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,3 +2,14 @@ export function combineGlobPatterns(...patterns: (string | string[])[]) {
|
|||||||
const p = patterns.flat();
|
const p = patterns.flat();
|
||||||
return p.length > 1 ? '{' + p.join(',') + '}' : p.length === 1 ? p[0] : '';
|
return p.length > 1 ? '{' + p.join(',') + '}' : p.length === 1 ? p[0] : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const GLOB_CHARACTERS = new Set(['*', '|', '{', '}', '(', ')', '[']);
|
||||||
|
|
||||||
|
export function isGlobPattern(pattern: string) {
|
||||||
|
for (const c of pattern) {
|
||||||
|
if (GLOB_CHARACTERS.has(c)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user