@atomist/sdm-core
Version:
Atomist Software Delivery Machine - Implementation
171 lines • 6.44 kB
JavaScript
/*
* 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.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const automation_client_1 = require("@atomist/automation-client");
const sdm_1 = require("@atomist/sdm");
const _ = require("lodash");
const configureSdm_1 = require("../internal/machine/configureSdm");
const array_1 = require("../util/misc/array");
const machineFactory_1 = require("./machineFactory");
/**
* Function to create an SDM configuration constant to be exported from an index.ts/js.
*/
function configure(configurer, options = {}) {
return {
postProcessors: [
configureSdm_1.configureSdm((cfg) => __awaiter(this, void 0, void 0, function* () {
let cfgToUse = cfg;
// Modify the configuration before creating the SDM instance
if (!!options.preProcessors) {
for (const preProcessor of array_1.toArray(options.preProcessors)) {
cfgToUse = yield preProcessor(cfgToUse);
}
}
const sdm = machineFactory_1.createSoftwareDeliveryMachine({
name: options.name || cfgToUse.name,
configuration: cfgToUse,
});
const configured = yield invokeConfigurer(sdm, configurer);
if (Array.isArray(configured)) {
sdm.withPushRules(configured[0], ...configured.slice(1));
}
else if (!!configured) {
const goalContributions = convertGoalData(configured);
if (goalContributions.length > 0) {
sdm.withPushRules(goalContributions[0], ...(goalContributions.slice(1) || []));
}
}
return sdm;
}), options),
...(array_1.toArray(options.postProcessors || [])),
],
};
}
exports.configure = configure;
/**
* Convert the provided GoalData instance into an array of GoalContributions
*/
function convertGoalData(goalData) {
const goalContributions = [];
_.forEach(goalData, (v, k) => {
v.__goals = [];
const gs = sdm_1.goals(k.replace(/_/g, " "));
let lg;
if (!!v.dependsOn) {
lg = [];
array_1.toArray(v.dependsOn).forEach(d => {
if (typeof d === "string") {
if (!!goalData[d] && !!goalData[d].__goals) {
lg.push(...goalData[d].__goals);
}
else {
throw new Error(`Provided dependsOn goals with name '${d}' do not exist or is after current goals named '${k}'`);
}
}
else {
lg.push(...array_1.toArray(d));
}
});
}
array_1.toArray(v.goals || []).forEach(g => {
v.__goals.push(...(Array.isArray(g) ? (g) : [g]));
if (!!lg) {
gs.plan(...convertGoals(g)).after(...convertGoals(lg));
}
else {
gs.plan(...convertGoals(g));
}
lg = array_1.toArray(g);
});
goalContributions.push(sdm_1.whenPushSatisfies(convertPushTest(v.test)).setGoals(gs));
});
return goalContributions;
}
exports.convertGoalData = convertGoalData;
/**
* Invoke the given configurer
*/
function invokeConfigurer(sdm, configurer) {
return __awaiter(this, void 0, void 0, function* () {
try {
// Decorate the createGoals method onto the SDM
sdm.createGoals = (creator, configurers) => __awaiter(this, void 0, void 0, function* () {
let gc;
try {
gc = yield creator(sdm);
}
catch (e) {
e.message = `Creating goals failed: ${e.message}`;
automation_client_1.logger.error(e.message);
throw e;
}
try {
if (!!configurers) {
for (const c of array_1.toArray(configurers)) {
yield c(sdm, gc);
}
}
}
catch (e) {
e.message = `Configuring goals failed: ${e.message}`;
automation_client_1.logger.error(e.message);
throw e;
}
return gc;
});
return configurer(sdm);
}
finally {
delete sdm.createGoals;
}
});
}
exports.invokeConfigurer = invokeConfigurer;
function convertPushTest(test) {
if (Array.isArray(test)) {
return sdm_1.allSatisfied(...test.map(wrapTest));
}
else {
return wrapTest(test || sdm_1.AnyPush);
}
}
function wrapTest(test) {
if (!!test.pushTest) {
return test;
}
else {
return sdm_1.notGoalTest(test);
}
}
function convertGoals(gs) {
if (Array.isArray(gs)) {
return gs;
}
else {
return [gs];
}
}
//# sourceMappingURL=configure.js.map
;