UNPKG

@atomist/sdm

Version:

Atomist Software Delivery Machine SDK

128 lines 4.11 kB
"use strict"; /* * Copyright © 2019 Atomist, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.goals = exports.isGoals = exports.Goals = void 0; const _ = require("lodash"); const Locking_1 = require("./common/Locking"); const Goal_1 = require("./Goal"); /** * Represents goals set in response to a push */ class Goals { // tslint:disable-next-line:no-shadowed-variable constructor(name, ...goals) { this.name = name; this.goals = goals; } /** * Return a Goal set that contains these goals and one more goal, * with an appropriate name * @param {Goal} g goal to add * @return {Goals} */ and(g) { return new Goals(this.name + ", " + g.name, ...this.goals.concat(g)); } /** * Return a form of these goals that is locked, so that more goals cannot be * added through contribution model * @return {Goals} */ andLock() { return this.and(Locking_1.Locking); } } exports.Goals = Goals; function isGoals(a) { return !!a && !!a.goals; } exports.isGoals = isGoals; /** * Create Goals instance using a fluent API. * * const simpleGoals = goals("Simple Goals") * .plan(CodeInspectionGoal) * .plan(BuildGoal, Autofix).after(CodeInspectionGoal) * .plan(StagingEndpointGoal).after(BuildGoal) * .plan(ProductionDeploymentGoal).after(BuildGoal, StagingEndpointGoal); * * @param {string} name * @returns {Goals & GoalsBuilder} */ function goals(name) { return new DefaultGoalsBuilder(name); } exports.goals = goals; class DefaultGoalsBuilder extends Goals { constructor(name) { super(name); this.name = name; this.lastGoals = []; } plan(...newGoals) { const currentGoals = []; // Keep track of what goals where last added so that we can add the preConditions later convertToGoals(...newGoals).forEach(g => { if (isGoals(g)) { currentGoals.push(...g.goals); } else { currentGoals.push(g); } }); this.goals.push(...currentGoals); this.lastGoals = currentGoals; return this; } after(...newGoals) { const lastGoalsWithPreConditions = []; this.lastGoals.forEach(g => { // Add the preCondition into the last added goals const newGoal = new Goal_1.GoalWithPrecondition(g.definition, ...convertToGoals(...newGoals)); // Preserve the plan function if it exists as it might influence goal planning newGoal.plan = g.plan; lastGoalsWithPreConditions.push(newGoal); // Remove the previously added goals const ix = this.goals.indexOf(g); if (ix >= 0) { this.goals.splice(ix, 1); } else { throw new Error("Unable to remove previously planned goal"); } }); // Add the newly created goals with preConditions this.goals.push(...lastGoalsWithPreConditions); this.lastGoals = lastGoalsWithPreConditions; return this; } } function convertToGoals(...pgoals) { return _.flatten(pgoals.map(g => { if (g instanceof Goal_1.Goal) { return g; } else if (isGoals(g)) { return g.goals; } else if (!!g) { return new Goal_1.Goal(g); } return undefined; })).filter(g => !!g); } //# sourceMappingURL=Goals.js.map