Build server prototype (integration with GitHub / NuGet / etc)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
micro-build-server/BuildServer/lib/task-processor.ts

100 lines
3.0 KiB

"use strict";
import * as _ from "underscore";
import tasks from "./tasks";
import { MessagesRoot, ProcessTaskContext, Settings, TaskInfo, TaskProcessor, TaskProcessorCallback, TaskProcessorCore } from "./types";
// TaskProcessor does not look like EventEmitter, so no need to extend EventEmitter and use `emit' here.
const createTaskProcessor = (task: TaskInfo, outerProcessor: TaskProcessorCore, callback: TaskProcessorCallback) => {
8 years ago
const errors: string[] = [];
const getOuterPrefix = (prefix) => {
if (task.name && prefix) {
return `${task.name}/${prefix}`;
}
return String(task.name || "") + String(prefix || "");
};
const onError = (message, prefix) => {
errors.push(message);
outerProcessor.onError(message, getOuterPrefix(prefix));
};
const onWarn = (message, prefix) => outerProcessor.onWarn(message, getOuterPrefix(prefix));
const onInfo = (message, prefix) => outerProcessor.onInfo(message, getOuterPrefix(prefix));
let result: TaskProcessor;
result = {
context: outerProcessor.context,
done: () => callback(errors.join("\r\n")),
onError,
onWarn,
onInfo,
process: () => tasks[task.type](task.params || {}, result)(),
processTask: (innerTask, innerCallback) => createTaskProcessor(innerTask, result, innerCallback).process(),
settings: outerProcessor.settings,
};
8 years ago
return result;
};
const pushMessage = (list, message, parts, index) => {
if (!index) {
8 years ago
list.$allMessages.push({
message,
8 years ago
prefix: parts.join("/"),
});
}
if (index < parts.length) {
if (!list[parts[index]]) {
list[parts[index]] = {};
}
return pushMessage(list[parts[index]], message, parts, index + 1);
}
if (!list.$messages) {
list.$messages = [];
}
return list.$messages.push(message);
};
const addFlag = (flags) => (flagName) => {
8 years ago
flags[flagName] = true;
};
const containsFlag = (flags) => (flagName) => flags[flagName];
export const processTask = (settings: Settings, task, context: ProcessTaskContext, callback) => {
const errors: MessagesRoot = { $allMessages: [] };
const warns: MessagesRoot = { $allMessages: [] };
const infos: MessagesRoot = { $allMessages: [] };
const messages: MessagesRoot = { $allMessages: [] };
const messageProcessor = (list) => (message, prefix) => {
const parts = prefix.split("/");
pushMessage(list, message, parts, 0);
pushMessage(messages, message, parts, 0);
};
const flags = {};
8 years ago
const processor = createTaskProcessor(task, {
context: {
...context,
8 years ago
addFlag: addFlag(flags),
containsFlag: containsFlag(flags),
},
8 years ago
onError: messageProcessor(errors),
onInfo: messageProcessor(infos),
onWarn: messageProcessor(warns),
settings,
}, (err) => callback(err, {
errors,
infos,
messages,
8 years ago
warns,
}));
processor.process();
};