2020-08-16 21:41:58 +00:00
|
|
|
import { doesExist } from '@apextoaster/js-utils';
|
|
|
|
|
2020-08-29 05:18:35 +00:00
|
|
|
import { BaseLabel, FlagLabel, getValueName, prioritySort, StateLabel, StateValue } from './labels';
|
2020-08-16 22:08:44 +00:00
|
|
|
import { defaultUntil } from './utils';
|
2020-08-12 00:14:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* How a label changed.
|
|
|
|
*/
|
2020-08-15 14:31:28 +00:00
|
|
|
export enum ChangeVerb {
|
2020-08-17 04:55:17 +00:00
|
|
|
BECAME = 'became',
|
2020-08-16 21:41:58 +00:00
|
|
|
CONFLICTED = 'conflicted',
|
2020-08-12 00:14:42 +00:00
|
|
|
CREATED = 'created',
|
|
|
|
REMOVED = 'removed',
|
2020-08-12 03:19:01 +00:00
|
|
|
REQUIRED = 'required',
|
2020-08-12 00:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Details of a label change.
|
|
|
|
*/
|
|
|
|
export interface ChangeRecord {
|
2020-08-15 14:31:28 +00:00
|
|
|
/**
|
|
|
|
* The label which caused this change.
|
|
|
|
*/
|
2020-08-12 00:14:42 +00:00
|
|
|
cause: string;
|
2020-08-15 14:31:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* How the label was changed.
|
|
|
|
*/
|
|
|
|
effect: ChangeVerb;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The label being changed.
|
|
|
|
*/
|
|
|
|
label: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ErrorRecord {
|
|
|
|
error: Error;
|
2020-08-12 00:14:42 +00:00
|
|
|
label: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collected inputs for a resolver run.
|
|
|
|
*/
|
|
|
|
export interface ResolveInput {
|
2020-08-12 01:39:53 +00:00
|
|
|
flags: Array<FlagLabel>;
|
2020-08-12 00:14:42 +00:00
|
|
|
labels: Array<string>;
|
2020-08-12 01:39:53 +00:00
|
|
|
states: Array<StateLabel>;
|
2020-08-12 00:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collected results from a resolver run.
|
|
|
|
*/
|
|
|
|
export interface ResolveResult {
|
|
|
|
changes: Array<ChangeRecord>;
|
2020-08-15 14:31:28 +00:00
|
|
|
errors: Array<ErrorRecord>;
|
2020-08-12 00:14:42 +00:00
|
|
|
labels: Array<string>;
|
|
|
|
}
|
|
|
|
|
2020-08-15 14:31:28 +00:00
|
|
|
/**
|
|
|
|
* Resolve the desired set of labels, given a starting set and the flags/states to be
|
|
|
|
* applied.
|
|
|
|
*/
|
2020-08-29 05:18:35 +00:00
|
|
|
function resolveBaseLabel(label: BaseLabel, anticipatedResult: ResolveResult, activeLabels: Set<string>) {
|
|
|
|
if (activeLabels.has(label.name) === false) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const requiredLabel of label.requires) {
|
|
|
|
if (!activeLabels.has(requiredLabel.name)) {
|
|
|
|
if (activeLabels.delete(label.name)) {
|
|
|
|
anticipatedResult.changes.push({
|
|
|
|
cause: requiredLabel.name,
|
|
|
|
effect: ChangeVerb.REQUIRED,
|
|
|
|
label: label.name,
|
|
|
|
});
|
|
|
|
}
|
2020-08-12 00:14:42 +00:00
|
|
|
|
2020-08-17 13:32:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-08-29 05:18:35 +00:00
|
|
|
}
|
2020-08-16 21:41:58 +00:00
|
|
|
|
2020-08-29 05:18:35 +00:00
|
|
|
for (const addedLabel of label.adds) {
|
|
|
|
// Set.add does not return a boolean, unlike the other methods
|
|
|
|
if (!activeLabels.has(addedLabel.name)) {
|
|
|
|
activeLabels.add(addedLabel.name);
|
|
|
|
anticipatedResult.changes.push({
|
|
|
|
cause: label.name,
|
|
|
|
effect: ChangeVerb.CREATED,
|
|
|
|
label: addedLabel.name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const removedLabel of label.removes) {
|
|
|
|
if (activeLabels.delete(removedLabel.name)) {
|
|
|
|
anticipatedResult.changes.push({
|
|
|
|
cause: label.name,
|
|
|
|
effect: ChangeVerb.REMOVED,
|
|
|
|
label: removedLabel.name,
|
|
|
|
});
|
2020-08-15 22:24:16 +00:00
|
|
|
}
|
2020-08-29 05:18:35 +00:00
|
|
|
}
|
2020-08-15 21:06:26 +00:00
|
|
|
|
2020-08-29 05:18:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolveBecomes(label: BaseLabel, anticipatedResult: ResolveResult, activeLabels: Set<string>, value: StateValue): boolean {
|
|
|
|
for (const become of value.becomes) {
|
|
|
|
const matches = become.matches.every((l) => activeLabels.has(l.name));
|
|
|
|
|
|
|
|
if (matches) {
|
|
|
|
resolveBaseLabel({
|
|
|
|
...label,
|
|
|
|
adds: become.adds,
|
|
|
|
removes: [...become.matches, ...become.removes],
|
|
|
|
requires: [],
|
|
|
|
}, anticipatedResult, activeLabels);
|
|
|
|
|
|
|
|
if (activeLabels.delete(name)) {
|
|
|
|
anticipatedResult.changes.push({
|
|
|
|
cause: name,
|
|
|
|
effect: ChangeVerb.REMOVED,
|
|
|
|
label: name,
|
2020-08-16 21:41:58 +00:00
|
|
|
});
|
|
|
|
}
|
2020-08-29 05:18:35 +00:00
|
|
|
return true;
|
2020-08-15 22:24:16 +00:00
|
|
|
}
|
2020-08-29 05:18:35 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-15 21:06:26 +00:00
|
|
|
|
2020-08-29 05:18:35 +00:00
|
|
|
/**
|
|
|
|
* Need to ensure that there is only 1 active value for the state
|
|
|
|
* If no, remove any lower priority active values for the state
|
|
|
|
* Need to run the normal (add, remove) rules
|
|
|
|
* Need to run the becomes rules
|
|
|
|
*/
|
|
|
|
function resolveState(state: StateLabel, anticipatedResult: ResolveResult, activeLabels: Set<string>) {
|
|
|
|
let activeValue;
|
|
|
|
|
|
|
|
const sortedValues = prioritySort(state.values);
|
|
|
|
for (const value of sortedValues) {
|
|
|
|
const name = getValueName(state, value);
|
|
|
|
if (!activeLabels.has(name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doesExist(activeValue)) { // there is already an active value
|
|
|
|
if (activeLabels.delete(name)) {
|
|
|
|
anticipatedResult.changes.push({
|
|
|
|
cause: name,
|
|
|
|
effect: ChangeVerb.CONFLICTED,
|
|
|
|
label: name,
|
2020-08-16 21:41:58 +00:00
|
|
|
});
|
|
|
|
}
|
2020-08-29 05:18:35 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const combinedValue: BaseLabel = {
|
|
|
|
adds: [...state.adds, ...value.adds],
|
|
|
|
name,
|
|
|
|
priority: defaultUntil(value.priority, state.priority, 0),
|
|
|
|
removes: [...state.removes, ...value.removes],
|
|
|
|
requires: [...state.requires, ...value.requires],
|
|
|
|
};
|
|
|
|
|
|
|
|
if (resolveBaseLabel(combinedValue, anticipatedResult, activeLabels)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resolveBecomes(combinedValue, anticipatedResult, activeLabels, value)) {
|
|
|
|
continue;
|
2020-08-12 00:14:42 +00:00
|
|
|
}
|
2020-08-15 22:44:40 +00:00
|
|
|
|
2020-08-29 05:18:35 +00:00
|
|
|
activeValue = name;
|
2020-08-12 00:14:42 +00:00
|
|
|
}
|
2020-08-29 05:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function resolveProject(options: ResolveInput): ResolveResult {
|
|
|
|
const result: ResolveResult = {
|
|
|
|
changes: [],
|
|
|
|
errors: [],
|
|
|
|
labels: [],
|
|
|
|
};
|
|
|
|
const activeLabels = new Set(options.labels);
|
2020-08-12 00:14:42 +00:00
|
|
|
|
2020-08-15 22:24:16 +00:00
|
|
|
const sortedFlags = prioritySort(options.flags);
|
|
|
|
for (const flag of sortedFlags) {
|
2020-08-29 05:18:35 +00:00
|
|
|
resolveBaseLabel(flag, result, activeLabels);
|
2020-08-15 22:24:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 17:38:33 +00:00
|
|
|
const sortedStates = prioritySort(options.states);
|
2020-08-12 00:14:42 +00:00
|
|
|
for (const state of sortedStates) {
|
2020-08-29 05:18:35 +00:00
|
|
|
resolveState(state, result, activeLabels);
|
2020-08-12 00:14:42 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 15:32:42 +00:00
|
|
|
result.labels = Array.from(activeLabels).sort();
|
2020-08-29 05:18:35 +00:00
|
|
|
|
|
|
|
return result;
|
2020-08-12 00:14:42 +00:00
|
|
|
}
|