1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.any = exports.all = exports.whenNot = void 0;
|
19 | const logger_1 = require("@atomist/automation-client/lib/util/logger");
|
20 | const PredicateMapping_1 = require("../PredicateMapping");
|
21 | const defaultPredicateMappingCostAnalyzer_1 = require("./defaultPredicateMappingCostAnalyzer");
|
22 | const PredicateMappingCostAnalyzer_1 = require("./PredicateMappingCostAnalyzer");
|
23 |
|
24 |
|
25 |
|
26 | function whenNot(t) {
|
27 | return {
|
28 | name: `not (${t.name})`,
|
29 | mapping: async (pi) => !(await t.mapping(pi)),
|
30 | structure: {
|
31 | components: [t],
|
32 | compositionStyle: PredicateMapping_1.PredicateMappingCompositionStyle.Not,
|
33 | },
|
34 | };
|
35 | }
|
36 | exports.whenNot = whenNot;
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | function all(predicates, analyzer = defaultPredicateMappingCostAnalyzer_1.DefaultPredicateMappingCostAnalyzer) {
|
45 | return {
|
46 | name: predicates.map(g => g.name).join(" && "),
|
47 | mapping: async (pci) => optimizedAndEvaluation(predicates, analyzer)(pci),
|
48 | structure: {
|
49 | components: predicates,
|
50 | compositionStyle: PredicateMapping_1.PredicateMappingCompositionStyle.And,
|
51 | },
|
52 | };
|
53 | }
|
54 | exports.all = all;
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | function any(predicates, analyzer = defaultPredicateMappingCostAnalyzer_1.DefaultPredicateMappingCostAnalyzer) {
|
63 | return {
|
64 | name: predicates.map(g => g.name).join(" || "),
|
65 | mapping: async (pci) => {
|
66 |
|
67 | const allResults = await gatherResults(predicates)(pci);
|
68 | return allResults.includes(true);
|
69 | },
|
70 | structure: {
|
71 | components: predicates,
|
72 | compositionStyle: PredicateMapping_1.PredicateMappingCompositionStyle.Or,
|
73 | },
|
74 | };
|
75 | }
|
76 | exports.any = any;
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | function optimizedAndEvaluation(predicates, analyzer) {
|
84 | const cheap = [];
|
85 | const remaining = [];
|
86 | for (const p of predicates) {
|
87 | const cost = analyzer(p);
|
88 | if (cost !== PredicateMappingCostAnalyzer_1.ExpectedPredicateMappingCost.expensive) {
|
89 | cheap.push(p);
|
90 | }
|
91 | else {
|
92 | remaining.push(p);
|
93 | }
|
94 | }
|
95 | logger_1.logger.debug("Cheap: [%j], remaining: [%j]", cheap, remaining);
|
96 | return async (pci) => {
|
97 | const cheapResults = await gatherResults(cheap)(pci);
|
98 | if (cheapResults.includes(false)) {
|
99 | return false;
|
100 | }
|
101 | const remainingResults = await gatherResults(remaining)(pci);
|
102 | return !remainingResults.includes(false);
|
103 | };
|
104 | }
|
105 | function gatherResults(predicates) {
|
106 | return pci => {
|
107 | return Promise.all(predicates.map(async (pt) => {
|
108 | const result = await pt.mapping(pci);
|
109 | logger_1.logger.debug(`Result of PushTest '${pt.name}' was ${result}`);
|
110 | return result;
|
111 | }));
|
112 | };
|
113 | }
|
114 |
|
\ | No newline at end of file |