git.schokokeks.org
Repositories
Help
Report an Issue
fs-draft.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
cc6f685
Branches
Tags
master
midgard
vorlage
fs-draft.git
tools
koralle.js
updated build tool koralle
Christian Fraß
commited
cc6f685
at 2017-07-25 08:22:10
koralle.js
Blame
History
Raw
/* Build system abstractor "Koralle" Copyright (C) 2016 Fenris Wolf, Christian Neubauer (fenris@greenscale.de, neubauer@greenscale.de) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); // module lib_base { // } // Web /* declare class Window { console : any; navigator : any; setTimeout : (function_ : (...params : Array<any>)=>void, delay : number, ...params : Array<any>)=>number; }; declare var window : Window; declare class Document { }; declare var document : Document; */ ; var lib_base; (function (lib_base) { /** * @author frac */ function environment() { var entries = [ { "id": "web", "name": "Web", "predicate": function () { return (typeof (document) !== "undefined"); }, }, { "id": "node", "name": "Node.js", "predicate": function () { return (typeof (process) !== "undefined"); }, }, { "id": "rhino", "name": "Rhino", "predicate": function () { return (typeof (java) !== "undefined"); }, }, ]; var id; var found = entries.some(function (entry) { if (entry.predicate()) { id = entry.id; return true; } else { return false; } }); if (found) { return id; } else { throw (new Error("unknown environment")); } } lib_base.environment = environment; })(lib_base || (lib_base = {})); /** * @author fenris */ var instance_verbosity = 0; /** * @author fenris */ function instance_collate(value1, value2) { if (typeof (value1) === "object") { if (value1 == null) { return (value2 == null); } else { if ("_collate" in value1) { return value1["_collate"](value2); } else { throw (new Error("[collate]" + " " + "object has no such method")); } } } else { if (instance_verbosity >= 1) { lib_log.warn("[collate]" + " " + "primitive value; using default implementation"); } return (value1 === value2); } } /** * @author fenris */ function instance_compare(value1, value2) { if (typeof (value1) === "object") { if ("_compare" in value1) { return value1["_compare"](value2); } else { throw (new Error("[compare]" + " " + "object has no such method")); } } else { if (instance_verbosity >= 1) { lib_log.warn("[compare]" + " " + "primitive value; using default implementation"); } return (value1 <= value2); } } /** * @author fenris */ function instance_clone(value) { if (typeof (value) === "object") { if ("_clone" in value) { return value["_clone"](); } else { throw (new Error("[clone]" + " " + "object has no such method")); } } else { if (instance_verbosity >= 1) { lib_log.warn("[clone]" + " " + "primitive value; using default implementation"); } return value; } } /** * @desc the ability to generate a string out of the element, which identifies it to a high degree * @author fenris */ function instance_hash(value) { if (typeof (value) === "object") { if ("_hash" in value) { return value["_hash"](); } else { throw (new Error("[hash]" + " " + "object has no such method")); } } else { if (instance_verbosity >= 1) { lib_log.warn("[hash]" + " " + "primitive value; using default implementation"); } return String(value); } } /** * @desc the ability to map the element to a textual representation (most likely not injective) * @author fenris */ function instance_show(value) { if (typeof (value) === "object") { if (value == null) { return "NULL"; } else { if ("_show" in value) { return value["_show"](); } else { // throw (new Error("[show]" + " " + "object has no such method")); return JSON.stringify(value); } } } else { if (instance_verbosity >= 1) { lib_log.warn("[show]" + " " + "primitive value; using default implementation"); } return String(value); } } /** * @todo outsource to dedicated plankton-lib */ var lib_log; (function (lib_log) { /** * @author fenris */ function log() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } /*window.*/ console.log.apply(console, args); } lib_log.log = log; /** * @author fenris */ function info() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } /*window.*/ console.info.apply(console, args); } lib_log.info = info; /** * @author fenris */ function warn() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } /*window.*/ console.warn.apply(console, args); } lib_log.warn = warn; /** * @author fenris */ function error() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } /*window.*/ console.error.apply(console, args); } lib_log.error = error; })(lib_log || (lib_log = {})); /** * @author frac */ var class_observer = (function () { /** * @author frac */ function class_observer() { this.counter = 0; this.actions = {}; this.buffer = []; } /** * @author frac */ class_observer.prototype.empty = function () { return (Object.keys(this.actions).length == 0); }; /** * @author frac */ class_observer.prototype.flush = function () { this.actions = {}; }; /** * @author frac */ class_observer.prototype.set = function (id, action) { this.actions[id] = action; }; /** * @author frac */ class_observer.prototype.del = function (id) { delete this.actions[id]; }; /** * @author frac */ class_observer.prototype.add = function (action) { this.set((this.counter++).toString(), action); }; /** * @author frac */ class_observer.prototype.notify = function (information, delayed) { var _this = this; if (information === void 0) { information = {}; } if (delayed === void 0) { delayed = false; } if (delayed) { this.buffer.push(information); } else { Object.keys(this.actions).forEach(function (id) { return _this.actions[id](information); }); } }; /** * @author frac */ class_observer.prototype.rollout = function () { var _this = this; this.buffer.forEach(function (information) { return _this.notify(information, false); }); this.buffer = []; }; return class_observer; }()); /** * @author frac */ /* export interface interface_readable<type_value> { |** * @author frac *| read() : type_executor<type_value, Error>; } */ /** * @author frac */ /* export interface interface_writeable<type_value> { |** * @author frac *| write(value : type_value) : type_executor<void, Error>; } */ /** * @author fenris */ /*export*/ var class_maybe = (function () { function class_maybe() { } /** * @desc whether the wrapper is nothing * @author fenris */ class_maybe.prototype.is_nothing = function () { throw (new Error("not implemented: class_maybe.is_nothing")); }; /** * @desc whether the wrapper is just * @author fenris */ class_maybe.prototype.is_just = function () { throw (new Error("not implemented: class_maybe.is_just")); }; /** * @desc return the value, stored in the maybe-wrapper * @author fenris */ class_maybe.prototype.cull = function () { throw (new Error("not implemented: class_maybe.cull")); }; /** * @author fenris */ class_maybe.prototype.toString = function () { throw (new Error("not implemented: class_maybe.cull")); }; /** * @author fenris */ class_maybe.prototype.distinguish = function (action_just, action_nothing) { if (action_nothing === void 0) { action_nothing = function () { }; } throw (new Error("not implemented: class_maybe.distinguish")); }; /** * @author fenris */ class_maybe.prototype.propagate = function (action) { throw (new Error("not implemented: class_maybe.propagate")); }; /** * @desc [implementation] * @author fenris */ class_maybe.prototype._show = function () { return this.toString(); }; return class_maybe; }()); /** * @author fenris */ /*export*/ var class_nothing = (function (_super) { __extends(class_nothing, _super); /** * @author fenris */ function class_nothing(reason) { if (reason === void 0) { reason = null; } var _this = _super.call(this) || this; _this.reason = reason; return _this; } /** * @author fenris */ class_nothing.prototype.is_nothing = function () { return true; }; /** * @author fenris */ class_nothing.prototype.is_just = function () { return false; }; /** * @author fenris */ class_nothing.prototype.cull = function () { var message = "you shouldn't cull a nothing-value …"; lib_log.warn(message); return null; }; /** * @author fenris */ class_nothing.prototype.toString = function () { return "<\u00B7>"; }; /** * @author fenris */ class_nothing.prototype.reason_get = function () { var content = ((this.reason == null) ? "·" : this.reason); return "<- " + content + " ->"; }; /** * @author fenris */ class_nothing.prototype.distinguish = function (action_just, action_nothing) { if (action_nothing === void 0) { action_nothing = function () { }; } action_nothing(this.reason); }; /** * @author fenris */ class_nothing.prototype.propagate = function (action) { return (new class_nothing(this.reason)); }; return class_nothing; }(class_maybe)); /** * @author fenris */ /*export*/ var class_just = (function (_super) { __extends(class_just, _super); /** * @author fenris */ function class_just(value) { var _this = _super.call(this) || this; _this.value = value; return _this; } /** * @author fenris */ class_just.prototype.is_nothing = function () { return false; }; /** * @author fenris */ class_just.prototype.is_just = function () { return true; }; /** * @author fenris */ class_just.prototype.cull = function () { return this.value; }; /** * @author fenris */ class_just.prototype.toString = function () { var content = instance_show(this.value); return "<+ " + content + " +>"; }; /** * @author fenris */ class_just.prototype.distinguish = function (action_just, action_nothing) { if (action_nothing === void 0) { action_nothing = function () { }; } action_just(this.value); }; /** * @author fenris */ class_just.prototype.propagate = function (action) { return action(this.value); }; return class_just; }(class_maybe)); /** * @author frac */ var class_error = (function (_super) { __extends(class_error, _super); /** * @author frac */ function class_error(message, suberrors) { if (suberrors === void 0) { suberrors = []; } var _this = _super.call(this, message) || this; _this.suberrors = suberrors; _this.mess = message; return _this; } /** * @override * @author frac */ class_error.prototype.toString = function () { return (this.mess + " " + ("[" + this.suberrors.map(function (x) { return x.toString(); }).join(",") + "]")); }; return class_error; }(Error)); var lib_call; (function (lib_call) { /** * @desc this is kind of an ugly hack; the motivation is, that the JS-interpreter of many Gecko-based browsers * (e.g. Firefox) won't accept a larger number of stack-frames, which come up when using executor-chains often. * This can be circumvented by deferring inner calls, such that they are moved outside the own stack-frame. This * is possible due to the asynchronous nature of executors. However this does also significantly increase the * overall-time for a long executor-chain (or rather executor-tree) to finish. Therefore it is not recommended * to do this by default. * @author frac */ lib_call.default_deferred = ((lib_base.environment() == "web") && (window.navigator.userAgent.indexOf("Firefox") >= 0)); /** * @author fenris */ function schedule(function_, deferred = lib_call.default_deferred) { if (!deferred) { function_(); } else { setTimeout(function_, 0); } } lib_call.schedule = schedule; })(lib_call || (lib_call = {})); var lib_call; (function (lib_call) { /** * @author fenris */ function timeout(function_, delay, ...params) { return ( /*window.*/ setTimeout.apply( /*window.*/ setTimeout, [function_, delay].concat(params))); } lib_call.timeout = timeout; /** * @desc a definition for a value being "defined" * @author neuc */ function is_def(obj, null_is_valid = false) { return (!((typeof (obj) === "undefined") || (!null_is_valid && (obj === null)))); } lib_call.is_def = is_def; /** * @desc returns the value if set and, when a type is specified, if the type is corret, if not return default_value * @author neuc */ function def_val(value, default_value, type = null, null_is_valid = false) { if (is_def(value, null_is_valid) && (is_def(type) ? ((typeof value === type) || ((value === null) && null_is_valid)) : true)) { return value; } else { return default_value; } } lib_call.def_val = def_val; ; /** * @desc just the empty function; useful for some callbacks etc. * @author fenris */ function nothing() { } lib_call.nothing = nothing; /** * @desc just the identity; useful for some callbacks etc. * @author fenris */ function id(x) { return x; } lib_call.id = id; /** * @author fenris */ function use(input, function_) { return function_(input); } lib_call.use = use; /** * @desc outputs * @author fenris */ function output(...args) { lib_log.info.apply(lib_log, args); } lib_call.output = output; /** * @desc converts the "arguments"-map into an array * @param {Object} args * @author fenris */ function args2list(args) { return Object.keys(args).map(key => args[key]); } lib_call.args2list = args2list; /** * @desc provides the call for an attribute of a class as a regular function * @param {string} name the name of the attribute * @return {*} * @author fenris */ function attribute(name) { return ((object) => object[name]); } lib_call.attribute = attribute; /** * @desc provides a method of a class as a regular function * @param {string} name the name of the method * @return {function} * @author fenris */ function method(name) { return (function (object) { return object[name].apply(object, args2list(arguments).slice(1)); }); } lib_call.method = method; /** * @desc composes two functions (i.e. returns a function that return the result of the successive execution of both input-functions) * @param {function} function_f * @param {function} function_g * @author fenris */ function compose(function_f, function_g) { return (function (x) { // return function_g(function_f(x)); return function_g(function_f.apply(function_f, args2list(arguments))); }); } lib_call.compose = compose; /** * @desc transforms a function with sequential input into a function with leveled input; example: add(2,3) = curryfy(add)(2)(3) * @param {function} f * @param {int} n (don't set manually) * @return {function} the currified version of the in put function * @author fenris */ function curryfy(f, n = f.length) { switch (n) { case 0: { throw (new Error("[curryfy] impossible")); // break; } case 1: { return f; // break; } default: { return (function (x) { return (curryfy(function () { return f.apply(f, [x].concat(args2list(arguments))); }, n - 1)); }); // break; } } } lib_call.curryfy = curryfy; /** * returns a function which goes through a process step by step * a process is an array of objects like { func : {function}, state : {string}} * trigger the start of the process by calling the returned function with one argument * which represents the parameters of the first function which is in the process * @param {Array<object>} _process * @param {function} on_stateChange called before the next function is called * @returns {function} */ function simple_process(_process, on_stateChange = function (msg) { lib_log.info("State changed " + msg); }, on_progress = function (msg, pos, max) { lib_log.info("Progress '" + msg + "' " + pos + "/" + max); }) { var data_hashmap = {}; var _orchestrate = function (data, pos = 0) { if (is_def(data)) { // data_hashmap[pos] = object_merge_objects({}, data, ["parents", "parent", "children"]); data_hashmap[pos] = {}; Object.keys(data).filter(key => (["parents", "parent", "children"].indexOf(key) < 0)).forEach(key => (data_hashmap[pos][key] = data[key])); } else { if (is_def(data_hashmap[pos]) && is_def(data_hashmap[pos].processed)) { data = data_hashmap[pos]; } } if (pos < _process.length) { var _func; if (typeof (_process[pos]) !== "undefined") { _func = _process[pos].func; } if (pos === 0) { data = { processed: data }; } if (_process[pos]) { on_stateChange(_process[pos].state); } return (_func(data, function (processed_data) { timeout(_orchestrate({ "processed": processed_data }, pos + 1), 0); }, on_progress)); } else { lib_log.error("o.O."); } }; return _orchestrate; } lib_call.simple_process = simple_process; })(lib_call || (lib_call = {})); ///<reference path="../../base/build/logic-decl.d.ts"/> "use strict"; var lib_call; (function (lib_call) { /** * @author fenris */ function executor_resolve(result) { return ((resolve, reject) => resolve(result)); } lib_call.executor_resolve = executor_resolve; /** * @author fenris */ function executor_reject(reason) { return ((resolve, reject) => reject(reason)); } lib_call.executor_reject = executor_reject; /** * @author fenris */ function executor_transform(executor, transform_result, transform_reason) { return ((resolve, reject) => { executor(result => resolve(transform_result(result)), reason => reject(transform_reason(reason))); }); } lib_call.executor_transform = executor_transform; /** * @author fenris */ function executor_transform_default(executor, transform_result, wrap_string = null) { let transform_reason = (error => ((wrap_string == null) ? error : new class_error(wrap_string, [error]))); return (executor_transform(executor, transform_result, transform_reason)); } lib_call.executor_transform_default = executor_transform_default; /** * @author fenris */ function executor_compose_sequential(first, second, deferred = undefined) { return ((resolve, reject) => { first(result => { lib_call.schedule(() => second(result)(resolve, reject), deferred); }, reason => { reject(reason); }); }); } lib_call.executor_compose_sequential = executor_compose_sequential; /** * @author fenris */ function executor_chain(state, executors, deferred = lib_call.default_deferred) { return ((resolve, reject) => { if (executors.length == 0) { return resolve(state); } else { return executors[0](state)(result => { /* schedule( () => executor_chain(result, executors.slice(1))(resolve, reject) ); */ /* setTimeout( () => executor_chain(result, executors.slice(1))(resolve, reject), 0 ); */ executor_chain(result, executors.slice(1))(resolve, reject); }, reject); } }); /* */ /* if (executors.length == 0) { return executor_resolve<type_state, type_error>(state); } else if (executors.length == 1) { return executors[0](state); } else { return ( executor_chain<type_state, type_error>( state, [ state => (resolve, reject) => executors[0](state)(result => executors[1](result)(resolve, reject), reject) ].concat(executors.slice(2)) ) ); } */ /* return ( executors.reduce( (chain, current) => executor_compose_sequential<type_state, type_state, type_error>(chain, current, deferred), executor_resolve<type_state, type_error>(state) ) ); */ } lib_call.executor_chain = executor_chain; /** * @author fenris */ function executor_first(executors) { /* return ( (resolve, reject) => { if (executors.length == 0) { reject(new Error("all failed")); } else { executors[0]( result => { resolve(result); }, reason => { executor_first<type_result, type_reason>(executors.slice(1))(resolve, reject); } ) } } ); */ return ((resolve, reject) => { executor_chain([], executors.map(executor => reasons => (resolve_, reject_) => { executor(result => reject_(result), reason => resolve_(reasons.concat([reason]))); }))(errors => reject(errors), result => resolve(result)); }); } lib_call.executor_first = executor_first; /** * @author fenris */ function executor_condense(executors) { return (executor_chain([], executors.map(executor => result => (resolve, reject) => { executor(element => resolve(result.concat([element])), reject); }))); } lib_call.executor_condense = executor_condense; /** * @author fenris * @deprecated use condense */ function executor_filter(executors, predicate) { return (executor_chain([], executors.map(executor => result => (resolve, reject) => { executor(element => resolve(predicate(element) ? result.concat([element]) : result), reject); }))); } lib_call.executor_filter = executor_filter; /** * @author fenris * @deprecated use condense */ function executor_map(executors, transformator) { return (executor_chain([], executors.map(executor => result => (resolve, reject) => { executor(element1 => resolve(result.concat([transformator(element1)])), reject); }))); } lib_call.executor_map = executor_map; /** * @author fenris * @deprecated use condense */ function executor_reduce(executors, initial, accumulator) { return (executor_chain(initial, executors.map(executor => result => (resolve, reject) => { executor(element => resolve(accumulator(result, element)), reject); }))); } lib_call.executor_reduce = executor_reduce; /** * @desc useful for e.g. asynchronous singletons * @author fenris */ class class_executor_initializer { /** * @author fenris */ constructor(fetcher) { /** * @author fenris * 0 : initial * 1 : waiting * 2 : done, successful * 3 : done, failed */ this.state = 0; this.fetcher = fetcher; this.state = 0; this.queue = []; this.output = null; this.error = null; } /** * @author fenris */ actuate() { switch (this.state) { case 2: { this.queue.forEach(entry => entry.resolve(this.output)); break; } case 3: { this.queue.forEach(entry => entry.reject(this.error)); break; } default: { throw (new Error(`unhandled state ${this.state}`)); break; } } } /** * @author fenris */ reset() { this.state = 0; this.queue = []; } /** * @author fenris */ get() { switch (this.state) { case 0: { this.state = 1; return ((resolve, reject) => { this.queue.push({ "resolve": resolve, "reject": reject }); this.fetcher(output => { this.state = 2; this.output = output; this.actuate(); }, error => { this.state = 3; this.error = error; this.actuate(); }); }); break; } case 1: { return ((resolve, reject) => this.queue.push({ "resolve": resolve, "reject": reject })); break; } case 2: { return executor_resolve(this.output); break; } case 3: { return executor_reject(this.error); break; } default: { throw (new Error(`unhandled state ${this.state}`)); break; } } } /** * @author fenris */ get_sync() { switch (this.state) { case 2: { return this.output; break; } case 3: { throw this.error; break; } default: { throw (new Error(`unhandled state ${this.state}`)); break; } } } /** * @author fenris */ set_sync(output) { switch (this.state) { case 2: { this.output = output; break; } case 3: { this.state = 2; this.output = output; break; } default: { throw (new Error(`unhandled state ${this.state}`)); break; } } } } lib_call.class_executor_initializer = class_executor_initializer; })(lib_call || (lib_call = {})); var lib_call; (function (lib_call) { /** * @author fenris */ var knot_loglevel_stack = [0]; /** * @author fenris */ function knot_loglevel_get() { return knot_loglevel_stack.slice(-1)[0]; } lib_call.knot_loglevel_get = knot_loglevel_get; /** * @author fenris */ function knot_loglevel_push(loglevel) { knot_loglevel_stack.push(loglevel); } lib_call.knot_loglevel_push = knot_loglevel_push; /** * @author fenris */ function knot_loglevel_pop() { knot_loglevel_stack.pop(); } lib_call.knot_loglevel_pop = knot_loglevel_pop; /** * @author fenris */ function knot_resolver(output) { return (input => (resolve, reject) => resolve(output)); } lib_call.knot_resolver = knot_resolver; /** * @author fenris */ function knot_id() { return (input => (resolve, reject) => resolve(input)); } lib_call.knot_id = knot_id; /** * @author fenris */ function knot_rejector(error) { return (input => (resolve, reject) => reject(error)); } lib_call.knot_rejector = knot_rejector; /** * @author fenris */ function knot_from_function(function_) { return (input => (resolve, reject) => { try { let output = function_(input); resolve(output); } catch (exception) { reject((exception)); } }); } lib_call.knot_from_function = knot_from_function; /** * @author fenris */ function knot_wrap(inner, convert_input, convert_output, convert_error) { return (input_outer => (resolve, reject) => { let input_inner = convert_input(input_outer); inner(input_inner)(output_inner => { let output_outer = convert_output(output_inner); return resolve(output_outer); }, error_inner => { let error_outer = convert_error(error_inner); return reject(error_outer); }); }); } lib_call.knot_wrap = knot_wrap; /** * @author fenris */ function knot_wrap_log(inner) { return (knot_wrap(inner, input => { lib_log.error("--", "input:", JSON.stringify(input)); return input; }, output => { lib_log.error("--", "output:", JSON.stringify(output)); return output; }, error => { lib_log.error("--", "error:", JSON.stringify(error)); return error; })); } lib_call.knot_wrap_log = knot_wrap_log; /** * @author fenris */ function knot_compose_sequential(first, second) { let second_ = ((knot_loglevel_get() >= 1) ? knot_wrap_log(second) : second); return (input => (resolve, reject) => { first(input)(between => lib_call.schedule(() => second_(between)(resolve, reject)), reject); }); } lib_call.knot_compose_sequential = knot_compose_sequential; /** * @author fenris */ function knot_chain(knots, logging = false) { /* return ( knots.reduce<type_knot<type_input, type_output, type_error>>( knot_compose_sequential, knot_id<any, type_error>() ) ); */ let knots_ = (logging ? knots.map(knot_wrap_log) : knots); if (knots_.length == 0) { return (input => (resolve, reject) => { resolve(input); }); } else { return (input => (resolve, reject) => { return knots_[0](input)(result => { return knot_chain(knots_.slice(1), false)(result)(resolve, reject); }, error => { return reject(error); }); }); } } lib_call.knot_chain = knot_chain; /** * @author fenris */ /* export function knot_compose_parallel<>( upper : type_knot<type_input, type_output_upper, type_error_upper>, lower : type_knot<type_input, type_output_lower, type_error_lower> ) { return ( input => (resolve, reject) => { upper(input)( ) lower(input)( ) } ); } */ /** * @author fenris */ function knot_bunch(knots) { return (input => (resolve, reject) => { let done = false; let master_output = {}; let ready = {}; let master_resolve = (id, output) => { if (!done) { master_output[id] = output; ready[id] = true; if (Object.keys(knots).every(id => (id in ready))) { done = true; resolve(master_output); } else { // nothing to do } } else { // nothing to do } }; let master_reject = (id, error) => { if (!done) { done = true; reject(error); } else { // nothing to do } }; Object.keys(knots).forEach(id => { knots[id](input)(output => master_resolve(id, output), error => master_reject(id, error)); }); }); } lib_call.knot_bunch = knot_bunch; /** * @author fenris */ function knot_condense(knots) { /* return ( input => knot_chain<Array<type_output>, Array<type_output>, type_error>( knots.map<type_knot<Array<type_output>, Array<type_output>, type_error>>( knot => list => (resolve, reject) => { knot(input)( element => resolve(list.concat([element])), reject ); } ) )([]) ); */ if (knots.length == 0) { return (input => (resolve, reject) => { resolve([]); }); } else { return (input => (resolve, reject) => { knots[0](input)(element => { knot_condense(knots.slice(1))(input)(restlist => { resolve([element].concat(restlist)); }, reject); }, error => { reject(error); }); }); } } lib_call.knot_condense = knot_condense; /** * @author fenris */ function knot_first(knots) { return (input => (resolve, reject) => { knot_condense(knots.map(knot => input_ => (resolve_, reject_) => knot(input)(reject_, resolve_)))(input)(reject, resolve); }); } lib_call.knot_first = knot_first; /** * @author fenris */ function knot_repeat(knot, attempts = 5, delay = (attempt => 250)) { let seq = (n => ((n == 0) ? [] : seq(n - 1).concat([n - 1]))); return (knot_first(seq(attempts).map((_, attempt) => input => (resolve, reject) => knot(input)(resolve, error => { setTimeout(() => reject(error), delay(attempt)); })))); } lib_call.knot_repeat = knot_repeat; /** * @desc useful for e.g. asynchronous singletons * @author fenris */ class class_knot_initializer { /** * @author fenris */ constructor(fetcher) { this.core = new lib_call.class_executor_initializer(fetcher(null)); } /** * @author fenris */ reset() { this.core.reset(); } /** * @author fenris */ get(input) { return (_ => this.core.get()); } } lib_call.class_knot_initializer = class_knot_initializer; })(lib_call || (lib_call = {})); var lib_call; (function (lib_call) { /** * @author fenris */ function promise_reject(reason) { return Promise.reject(reason); } lib_call.promise_reject = promise_reject; /** * @author fenris */ function promise_resolve(result) { return Promise.resolve(result); } lib_call.promise_resolve = promise_resolve; /** * @author fenris */ function promise_make(executor) { return (new Promise(executor)); } lib_call.promise_make = promise_make; /** * @author fenris */ function promise_then_close(promise, resolver, rejector) { return (promise.then(resolver, rejector)); } lib_call.promise_then_close = promise_then_close; /** * @author fenris */ function promise_then_append(promise, resolver, rejector = null) { if (rejector == null) { rejector = (reason) => promise_reject(reason); } return (promise.then(resolver, rejector)); } lib_call.promise_then_append = promise_then_append; /** * @author fenris */ function promise_all(promises) { return Promise.all(promises); } lib_call.promise_all = promise_all; /** * @author fenris */ function promise_chain(promises, start = undefined) { return (promises.reduce((chain, promise) => promise_then_append(chain, promise), promise_resolve(start))); } lib_call.promise_chain = promise_chain; /** * @author fenris */ function promise_wrap(promise, transformator_result, transformator_reason = lib_call.id) { return (promise_make((resolve, reject) => { promise_then_close(promise, result => resolve(transformator_result(result)), reason => reject(transformator_reason(reason))); })); } lib_call.promise_wrap = promise_wrap; /** * @author fenris */ function promise_show(label) { return (result => promise_make((resolve, reject) => { lib_log.info(label + ": " + instance_show(result)); resolve(result); })); } lib_call.promise_show = promise_show; /** * @author fenris */ function promise_log(result) { return promise_show("log"); } lib_call.promise_log = promise_log; /** * @author fenris */ function promise_attach(state, promise, name) { return (promise_wrap(promise, result => { state[name] = result; return state; })); } lib_call.promise_attach = promise_attach; })(lib_call || (lib_call = {})); /** * @author fenris */ var lib_trait; (function (lib_trait) { /** * @author fenris */ lib_trait._verbosity = 1; /** * @author fenris */ function type_show(type) { return JSON.stringify(type); } /** * @author fenris */ function type_resolve(raw, bindings) { switch (raw.kind) { case "variable": { var variable_name = raw.parameters["name"]; if (variable_name in bindings) { return bindings[variable_name]; } else { var message = "binding missing for variable '" + variable_name + "'"; throw (new Error(message)); } break; } case "object": { return { "kind": "object", "parameters": { "fields": raw.parameters["fields"].map(function (field) { return { "name": field.name, "type": type_resolve(field.type, bindings) }; }) } }; break; } default: { return raw; break; } } } /** * @desc holds all defined traits * @author fenris */ var _pool = {}; /** * @author fenris */ var _typechecker; /** * @author fenris */ function set_typechecker(typechecker) { _typechecker = typechecker; } lib_trait.set_typechecker = set_typechecker; /** * @author fenris */ function domain_instance(domain, value) { return { "domain": domain, "value": value }; } lib_trait.domain_instance = domain_instance; /** * @desc adds a trait * @author fenris */ function describe(trait_id, variable_conditions, facet_signatures) { if (trait_id in _pool) { var error = new Error("trait '" + trait_id + "' already registered"); throw error; } else { var trait_1 = { "variables": {}, "facets": {} }; Object.keys(variable_conditions).forEach(function (variable_name) { var variable = { "conditions": variable_conditions[variable_name], "bindings": {} }; trait_1.variables[variable_name] = variable; }); Object.keys(facet_signatures).forEach(function (facet_name) { var facet = { "signature": facet_signatures[facet_name], "handlers": {} }; trait_1.facets[facet_name] = facet; }); _pool[trait_id] = trait_1; } } lib_trait.describe = describe; /** * @desc adhoc binding * @author fenris */ function assign(domain_kind, trait_id, bindings, handlers, strict_binding, strict_handling) { if (strict_binding === void 0) { strict_binding = true; } if (strict_handling === void 0) { strict_handling = true; } if (trait_id in _pool) { var trait_2 = _pool[trait_id]; // assignments { // variables { Object.keys(bindings).forEach(function (variable_name) { if (variable_name in trait_2.variables) { var variable = trait_2.variables[variable_name]; if (!(domain_kind in variable.bindings)) { variable.bindings[domain_kind] = bindings[variable_name]; } else { var message = "variable '" + variable_name + "' already bound for domain '" + domain_kind + "' in trait '" + trait_id + "'"; throw (new Error(message)); } } else { var message = "no variable '" + variable_name + "' to bind in trait '" + trait_id + "'"; console.warn(message); // throw (new Error(message)); } }); } // facets { Object.keys(handlers).forEach(function (facet_name) { if (facet_name in trait_2.facets) { var facet = trait_2.facets[facet_name]; if (!(domain_kind in facet.handlers)) { facet.handlers[domain_kind] = handlers[facet_name]; } else { var message = "facet '" + facet_name + "' already implemented for domain '" + domain_kind + "' in trait '" + trait_id + "'"; throw (new Error(message)); } } else { var message = "no facet '" + facet_name + "' to implement in trait '" + trait_id + "'"; console.warn(message); // throw (new Error(message)); } }); } } // checks { var messages_1 = []; if (strict_binding) { // variables { var variable_names_given_1 = Object.keys(bindings); var variable_names_present = Object.keys(trait_2.variables); var variable_names_missing = variable_names_present.filter(function (variable_name) { return !variable_names_given_1.some(function (variable_name_) { return (variable_name == variable_name_); }); }); variable_names_missing.forEach(function (variable_name) { var message = "binding missing for variable '" + variable_name + "'"; messages_1.push(message); }); } } if (strict_handling) { // facets { var facet_names_given_1 = Object.keys(handlers); var facet_names_present = Object.keys(trait_2.facets); var facet_names_missing = facet_names_present.filter(function (facet_name) { return !facet_names_given_1.some(function (facet_name_) { return (facet_name == facet_name_); }); }); facet_names_missing.forEach(function (facet_name) { var message = "implementations missing for the facet '" + facet_name + "'"; messages_1.push(message); }); } if (messages_1.length > 0) { var message = "assignment of domain '" + domain_kind + "' to trait '" + trait_id + "' incomplete: " + messages_1.join(", "); throw (new Error(message)); } } } } else { var message = "no trait '" + trait_id + "'"; throw (new Error(message)); } } lib_trait.assign = assign; /** * @author fenris */ function call(trait_id, facet_name, domain, input) { if (trait_id in _pool) { var trait_3 = _pool[trait_id]; if (facet_name in trait_3.facets) { var facet = trait_3.facets[facet_name]; if (domain.kind in facet.handlers) { var handler = facet.handlers[domain.kind]; var bindings_1 = {}; Object.keys(trait_3.variables).forEach(function (variable_name) { bindings_1[variable_name] = trait_3.variables[variable_name].bindings[domain.kind]; }); if ((_typechecker != null) && (facet.signature.type_input != undefined)) { var type = type_resolve(facet.signature.type_input, bindings_1); var valid_input = _typechecker(input, type); if (!valid_input) { var message = "input '" + String(input) + "' for domain '" + domain.kind + "' implementing facet '" + facet_name + "' in trait '" + trait_id + "' doesn't match the expected type '" + type_show(type) + "'"; console.warn(message); } } var output = handler(domain.parameters)(input); if ((_typechecker != null) && (facet.signature.type_output != undefined)) { var type = type_resolve(facet.signature.type_output, bindings_1); var valid_output = _typechecker(output, type); if (!valid_output) { var message = "output '" + String(output) + "' for domain '" + domain.kind + "' implementing facet '" + facet_name + "' in trait '" + trait_id + "' doesn't match the expected type '" + type_show(type) + "'"; console.warn(message); } } return output; } else { var message = "implementation for facet '" + facet_name + "' missing for domain '" + domain.kind + "' belonging to the trait '" + trait_id + "'"; throw (new Error(message)); } } else { var message = "no facet '" + facet_name + "' in trait '" + trait_id + "'"; throw (new Error(message)); } } else { var message = "no trait '" + trait_id + "'"; throw (new Error(message)); } } lib_trait.call = call; /** * @author fenris */ function show() { console.info(JSON.stringify(_pool, undefined, "\t")); // console.info(_pool); } lib_trait.show = show; })(lib_trait || (lib_trait = {})); /** * @author fenris */ var lib_trait; (function (lib_trait) { /** * @desc the ability to check for equality with another element of the same domain * @author fenris */ function define_collatable() { lib_trait.describe("collatable", { "value": [] }, { "collate": { "type_input": { "kind": "object", "parameters": { "fields": [ { "name": "left", "type": { "kind": "variable", "parameters": { "name": "value" } } }, { "name": "right", "type": { "kind": "variable", "parameters": { "name": "value" } } } ] } }, "type_output": { "kind": "boolean" } } }); lib_trait.assign("auto", "collatable", { "value": { "kind": "any" } }, { "collate": function (_) { return function (_a) { var x = _a["left"], y = _a["right"]; if (typeof (x) === "object") { if (x == null) { return (y == null); } else { if ("_collate" in x) { return x["_collate"](y); } else { throw (new Error("[collate]" + " " + "object has no such method")); } } } else { if (lib_trait._verbosity >= 1) { console.warn("[collate]" + " " + "primitive value; using default implementation"); } return (x === y); } }; } }); } lib_trait.define_collatable = define_collatable; })(lib_trait || (lib_trait = {})); /** * @author fenris */ var lib_trait; (function (lib_trait) { /** * @desc the ability to compare with another element of the same domain for determining if the first is "smaller than or equal to" the latter * @author fenris */ function define_comparable() { lib_trait.describe("comparable", { "value": [] }, { "compare": { "type_input": { "kind": "object", "parameters": { "fields": [ { "name": "left", "type": { "kind": "variable", "parameters": { "name": "value" } } }, { "name": "right", "type": { "kind": "variable", "parameters": { "name": "value" } } } ] } }, "type_output": { "kind": "boolean" } } }); lib_trait.assign("auto", "comparable", { "value": { "kind": "any" } }, { "compare": function (_) { return function (_a) { var x = _a["left"], y = _a["right"]; if (typeof (x) === "object") { if ("_compare" in x) { return x["_compare"](y); } else { throw (new Error("[compare]" + " " + "object has no such method")); } } else { if (lib_trait._verbosity >= 1) { console.warn("[compare]" + " " + "primitive value; using default implementation"); } return (x <= y); } }; } }); } lib_trait.define_comparable = define_comparable; })(lib_trait || (lib_trait = {})); /** * @author fenris */ var lib_trait; (function (lib_trait) { /** * @desc the ability to create an exact copy * @author fenris */ function define_clonable() { lib_trait.describe("clonable", { "value": [] }, { "clone": { "type_input": { "kind": "variable", "parameters": { "name": "value" } }, "type_output": { "kind": "variable", "parameters": { "name": "value" } } } }); lib_trait.assign("auto", "clonable", { "value": { "kind": "any" } }, { "clone": function (_) { return function (x) { if (typeof (x) === "object") { if ("_clone" in x) { return x["_clone"](); } else { throw (new Error("[clone]" + " " + "object has no such method")); } } else { if (lib_trait._verbosity >= 1) { console.warn("[clone]" + " " + "primitive value; using default implementation"); } return x; } }; } }); } lib_trait.define_clonable = define_clonable; })(lib_trait || (lib_trait = {})); /** * @author fenris */ var lib_trait; (function (lib_trait) { /** * @desc the ability to generate a string out of the element, which identifies it to a high degree * @author fenris */ function define_hashable() { lib_trait.describe("hashable", { "value": [] }, { "hash": { "type_input": { "kind": "variable", "parameters": { "name": "value" } }, "type_output": { "kind": "string" } } }); lib_trait.assign("auto", "hashable", { "value": { "kind": "any" } }, { "hash": function (_) { return function (x) { if (typeof (x) === "object") { if ("_hash" in x) { return x["_hash"](); } else { throw (new Error("[hash]" + " " + "object has no such method")); } } else { if (lib_trait._verbosity >= 1) { console.warn("[hash]" + " " + "primitive value; using default implementation"); } return String(x); } }; } }); } lib_trait.define_hashable = define_hashable; })(lib_trait || (lib_trait = {})); /** * @author fenris */ var lib_trait; (function (lib_trait) { /** * @desc the ability to map the element to a textual representation (most likely not injective) * @author fenris */ function define_showable() { lib_trait.describe("showable", { "value": [] }, { "show": { "type_input": { "kind": "variable", "parameters": { "name": "value" } }, "type_output": { "kind": "string" } } }); lib_trait.assign("auto", "showable", { "value": { "kind": "any" } }, { "show": function (_) { return function (x) { if (typeof (x) === "object") { if (x == null) { return "NULL"; } else { if ("_show" in x) { return x["_show"](); } else { // throw (new Error("[show]" + " " + "object has no such method")); return JSON.stringify(x); } } } else { if (lib_trait._verbosity >= 1) { console.warn("[show]" + " " + "primitive value; using default implementation"); } return String(x); } }; } }); } lib_trait.define_showable = define_showable; })(lib_trait || (lib_trait = {})); /** * @author fenris */ var lib_trait; (function (lib_trait) { /** * @author fenris */ function define_common() { lib_trait.define_collatable(); lib_trait.define_comparable(); lib_trait.define_clonable(); lib_trait.define_hashable(); lib_trait.define_showable(); } lib_trait.define_common = define_common; })(lib_trait || (lib_trait = {})); ///<reference path="../../base/build/logic-decl.d.ts"/> var lib_object; (function (lib_object) { /** * @author fenris */ function fetch(object, fieldname, fallback, escalation) { if (fallback === void 0) { fallback = null; } if (escalation === void 0) { escalation = 1; } if ((fieldname in object) && (object[fieldname] !== undefined)) { return object[fieldname]; } else { switch (escalation) { case 0: { return fallback; break; } case 1: { var message = ("field '" + fieldname + "' not in structure"); message += ("; using fallback value '" + String(fallback) + "'"); // console.warn(message); return fallback; break; } case 2: { var message = ("field '" + fieldname + "' not in structure"); throw (new Error(message)); break; } default: { throw (new Error("invalid escalation level " + escalation)); break; } } } } lib_object.fetch = fetch; /** * @author fenris */ function map(object_from, transformator) { var object_to = {}; Object.keys(object_from).forEach(function (key) { return (object_to[key] = transformator(object_from[key], key)); }); return object_to; } lib_object.map = map; /** * @author fenris */ function filter(object_from, predicate) { var object_to = {}; Object.keys(object_from).forEach(function (key) { var value = object_from[key]; if (predicate(value, key)) { object_to[key] = value; } }); return object_to; } lib_object.filter = filter; /** * @author fenris */ function from_array(array) { var object = {}; array.forEach(function (entry) { return (object[entry.key] = entry.value); }); return object; } lib_object.from_array = from_array; /** * @author fenris */ function to_array(object) { var array = []; Object.keys(object).forEach(function (key) { return array.push({ "key": key, "value": object[key] }); }); return array; } lib_object.to_array = to_array; /** * @author fenris */ function keys(object) { return Object.keys(object); } lib_object.keys = keys; /** * @author fenris */ function values(object) { return to_array(object).map(function (entry) { return entry.value; }); } lib_object.values = values; /** * @author fenris */ function path_read(object, path, fallback, escalation) { if (fallback === void 0) { fallback = null; } if (escalation === void 0) { escalation = 1; } var steps = ((path.length == 0) ? [] : path.split(".")); if (steps.length == 0) { throw (new Error("empty path")); } else { var position_1 = object; var reachable = (position_1 != null) && steps.slice(0, steps.length - 1).every(function (step) { position_1 = object_fetch(position_1, step, null, 0); return (position_1 != null); }); if (reachable) { return object_fetch(position_1, steps[steps.length - 1], fallback, escalation); } else { return object_fetch({}, "_dummy_", fallback, escalation); } } } lib_object.path_read = path_read; /** * @author fenris */ function path_write(object, path, value, construct) { if (construct === void 0) { construct = true; } var steps = ((path.length == 0) ? [] : path.split(".")); if (steps.length == 0) { throw (new Error("empty path")); } else { var position_2 = object; var reachable = steps.slice(0, steps.length - 1).every(function (step) { var position_ = object_fetch(position_2, step, null, 0); if (position_ == null) { if (construct) { position_2[step] = {}; position_2 = position_2[step]; return true; } else { return false; } } else { position_2 = position_; return true; } }); if (reachable) { position_2[steps[steps.length - 1]] = value; } else { throw (new Error("path " + path + " does not exist and may not be constructed")); } } } lib_object.path_write = path_write; /** * @author fenris */ function matches(object, pattern, collate) { if (collate === void 0) { collate = instance_collate; } return Object.keys(pattern).every(function (key) { return collate(pattern[key], object[key]); }); } lib_object.matches = matches; /** * @author fenris */ function flatten(value) { var integrate = function (result, key_, value_) { if (value_ == null) { result[key_] = value_; } else { if (typeof (value_) != "object") { result[key_] = value_; } else { var result_1 = flatten(value_); Object.keys(result_1).forEach(function (key__) { var value__ = result_1[key__]; result[key_ + "." + key__] = value__; }); } } }; if (value == null) { return null; } else { var result_2 = {}; if (typeof (value) != "object") { result_2["value"] = value; } else { if (value instanceof Array) { var array = (value); array.forEach(function (element, index) { return integrate(result_2, "element_" + index, element); }); } else { var object_1 = (value); Object.keys(object_1).forEach(function (key) { return integrate(result_2, key, object_1[key]); }); } } return result_2; } } lib_object.flatten = flatten; /** * @author fenris */ function clash(x, y, _a) { var _b = _a === void 0 ? {} : _a, _c = _b["overwrite"], overwrite = _c === void 0 ? true : _c, _d = _b["hooks"]["existing"], hook_existing = _d === void 0 ? null : _d; if (hook_existing == null) { (function (key, value_old, value_new) { return console.warn("field " + key + " already defined"); }); } var z = {}; Object.keys(x).forEach(function (key) { z[key] = x[key]; }); Object.keys(y).forEach(function (key) { if (key in z) { hook_existing(key, z[key], y[key]); if (overwrite) { z[key] = y[key]; } } else { z[key] = y[key]; } }); return z; } lib_object.clash = clash; /** * @author fenris */ function patch(core, mantle, deep, path) { if (deep === void 0) { deep = true; } if (path === void 0) { path = null; } if (mantle == null) { console.warn("mantle is null; core was", core); } else { Object.keys(mantle).forEach(function (key) { var path_ = ((path == null) ? key : path + "." + key); var value_mantle = mantle[key]; if (!(key in core)) { if ((typeof (value_mantle) == "object") && (value_mantle != null) && deep) { if (value_mantle instanceof Array) { core[key] = []; value_mantle.forEach(function (element) { if ((typeof (element) == "object") && (element != null)) { var element_ = {}; patch(element_, element); core[key].push(element_); } else { core[key].push(element); } }); } else { core[key] = {}; patch(core[key], value_mantle, deep, path_); } } else { core[key] = value_mantle; } } else { var value_core = core[key]; if (typeof (value_core) == typeof (value_mantle)) { if ((typeof (value_mantle) == "object") && (value_mantle != null) && deep) { patch(core[key], value_mantle, deep, path_); } else { core[key] = value_mantle; } } else { if ((value_core != null) && (value_mantle != null)) { var message = "objects have different shapes at path '" + path_ + "'; core has type '" + typeof (value_core) + "' and mantle has type '" + typeof (value_mantle) + "'"; console.warn(message); } core[key] = value_mantle; // throw (new Error(message)); } } }); } } lib_object.patch = patch; /** * @author fenris */ function patched(core, mantle, deep) { if (deep === void 0) { deep = undefined; } var result = {}; patch(result, core, deep); patch(result, mantle, deep); return result; } lib_object.patched = patched; /** * @author fenris */ function attached(object, key, value) { var mantle = {}; mantle[key] = value; return patched(object, mantle, false); } lib_object.attached = attached; /** * @author fenris */ function copy(object) { return patched({}, object); } lib_object.copy = copy; })(lib_object || (lib_object = {})); /** * @desc adapters for old syntax * @author fenris */ var object_fetch = lib_object.fetch; var object_map = lib_object.map; var object_a2o = lib_object.from_array; var object_o2a = lib_object.to_array; var object_matches = lib_object.matches; var object_clash = lib_object.clash; ///<reference path="../../base/build/logic-decl.d.ts"/> /** * @param {Object} map * @return {string} * @author frac */ /*export*/ var object_map2string = function (map) { return (" " + Object.keys(map) .filter(function (key) { return (key != "isMapped"); }) .map(function (key) { return ("" + ((map[key] == null) ? "-" : map[key].toString()) + ""); }) .join(" ") + ""); }; /** * @param {Array} array * @return {string} * @author frac */ /*export*/ var object_array2string = function (array) { return ("" + array.map(function (element, index) { switch (typeof (element)) { case "object": return object_map2string(element); default: return String(element); } }).join(",") + ""); }; /** * @desc follows a path in an object-tree * @param {Object} object the object in which the path lies * @param {string} path the steps * @param {boolean} [create] whether to create not yet existing branches * @return {Object} {'successful': successful, 'position': position} where the branch or leaf at the end of the path * @author frac */ var object_path_walk = function (object, path, create, null_on_missing) { if (create === void 0) { create = true; } if (null_on_missing === void 0) { null_on_missing = false; } var steps = ((path == "") ? [] : path.split(".")); if (steps.length == 0) { return object; } else { var head = steps[0]; // create { if (!(head in object)) { if (create) { var value = null; if (steps.length >= 2) { var next = steps[1]; var index = parseInt(next); if (!isNaN(index)) { value = []; } else { value = {}; } } else { value = {}; } object[head] = value; } else { // console.info("[object_path_walk] object is ", object); var message = "[object_path_walk] can not walk step \u00BB" + head + "\u00AB in path \u00BB" + path + "\u00AB on object"; if (null_on_missing) { console.warn(message); return null; } else { throw (new Error(message)); } } } } // execute rest { var object_ = object[head]; var path_ = steps.slice(1).join("."); return object_path_walk(object_, path_, create, null_on_missing); } } /* return ( string_split(path, ".").reduce( function (position : any, step : string) : any { if (! lib_call.is_def(position[step], true)) { if (create) { position[step] = {}; } else { // console.info("[object_path_walk] object is ", object); let message : string = sprintf("[object_path_walk] can not walk step »%s« in path »%s« on object", [step, path]); if (null_on_missing) { console.warn(message); return null; } else { throw (new Error(message)); } } } return position[step]; }, object ) ); */ }; /** * @desc reads a branch/leaf from an object-tree * @author frac */ /*export*/ var object_path_read = function (object, path, null_on_missing) { if (null_on_missing === void 0) { null_on_missing = false; } return object_path_walk(object, path, false, null_on_missing); }; /** * @desc writes a branch/leaf to an object-tree * @author frac */ /*export*/ var object_path_write = function (object, path, value) { // for "initializing" the object (important if the value to write is an entry in a yet not existing array) /*let old : any = */ object_path_walk(object, path, true, true); var steps = ((path == "") ? [] : path.split(".")); var position = object_path_walk(object, steps.slice(0, steps.length - 1).join("."), true); if (position == undefined) { console.warn("can't set \u00BB" + steps[steps.length - 1] + "\u00AB in undefined"); } else { position[steps[steps.length - 1]] = value; } }; /*export*/ var object_object_path_write_ex = function (obj, path, val) { var ref = obj; var paths = path.split("."); var i; for (i = 0; i < paths.length - 1; i++) { if (ref[paths[i]] === void 0) { if (/^(0|[1-9][0-9]*)$/.test(paths[i + 1])) { ref[paths[i]] = []; } else { ref[paths[i]] = {}; } } ref = ref[paths[i]]; } ref[paths[i]] = val; }; /** * @desc filters branches from an object * @param {Object} object the object to read from * @param {Array} paths a list of string-lists, that are the paths to be propagated * @return {Object} the object with only the selected branches * @author frac */ /*export*/ var object_path_filter = function (object, paths) { var result = {}; paths.forEach(function (path) { var value = null; try { value = object_path_read(object, path); } catch (exception) { console.warn(exception); } if (value != null) { object_path_write(result, path, value); } else { console.warn("skipped path \"" + path + "\" while filtering"); } }); return result; }; /** * @desc dunno… returns a list of object-paths? * @param {Object} object * @param {string} p * @todo can probably be merged with getLeafg */ /*export*/ var object_path_list = function (object, path, visited) { if (path === void 0) { path = null; } if (visited === void 0) { visited = []; } var result = []; visited.push(object); for (var key in object) { var value = object[key]; if (visited.indexOf(value) === -1) { var key_ = (path == null) ? key : (path + "." + key); if (typeof (value) === "object") { result = result.concat(object_path_list(value, key_, visited)); } else { result.push({ "key": key_, "value": value }); } } } return result; }; /** * theroreticaly loop prof walk through all elements and subelements of an object * and call a callback for each entry * @param {object} obj object to iterate through * @param {function} callback */ /*export*/ var object_iterate = function (obj, callback, leafs_only, path, visited) { if (leafs_only === void 0) { leafs_only = false; } if (visited === void 0) { visited = []; } var have_seen = function (ob) { return visited.some(function (e) { return ((typeof (ob) === "object") && (ob !== null) && (e === ob)); }); }; var next = []; Object.keys(obj).forEach(function (key) { var elem = obj[key]; if (!have_seen(elem)) { visited.push(elem); var _path = ""; if (typeof path === "undefined") { _path = key; } else { _path += [path, key].join("."); } if (!leafs_only) callback(_path, elem, key); if (typeof (elem) === "object") { (function (elem_, callback_, _path_, visited_) { next.push(function () { object_iterate(elem_, callback_, leafs_only, _path_, visited_); }); })(elem, callback, _path, visited); } else { if (leafs_only) callback(_path, elem, key); } } }); var func; while (func = next.shift()) { func(); } }; /** * @desc get the leaf-nodes of an object * @param {object} object * @return {Array<string>} a list containing all leaf-nodes * @author frac */ /*export*/ var getLeafs = function (object) { var skip = { "className": true, "timeStamp": true, "parentId": true, "transactionID": true, "guid": true, "_id": true, "parents": true, "children": true }; return (Object.keys(object).reduce(function (leafs, key) { try { var value = object[key]; if (key in skip) { console.warn("skipping field \"" + key + "\""); return leafs; } else { if ((typeof (value) === "object") && (value != null)) { return leafs.concat(getLeafs(value).map(function (leaf) { return (key + "." + leaf); })); } else { return leafs.concat([key]); } } } catch (exception) { console.warn(exception); console.info("key: ", key); return null; } }, new Array())); }; /** * * @desc merges two arrays by probing * @param {Array} core * @param {Array} mantle * @param {function} match */ /*export*/ var merge_array = function (core, mantle, match) { if (match === void 0) { match = (function (x, y) { return (x === y); }); } if ((core == undefined) || (mantle == undefined)) { throw (new Error("Error: " + ((core == undefined) ? " core must be an array and not '" + typeof (core) + "'" : "") + ((mantle == undefined) ? " mantle must be an array and not '" + typeof (mantle) + "'" : ""))); } var ret = core; for (var i = 0; i < mantle.length; i++) { var entry = mantle[i]; try { var matching_index = core.find(function (element) { return match(element, entry); }); ret[matching_index] = object_merge_objects(core[matching_index], entry); } catch (e) { ret.push(entry); } } return ret; }; /** * @desc merges two objects recursivly * @param {Object} object1 core * @param {Object} object2 mantle * @param {Array} [ignore_keys] * @param [do_not_overwrite_existing_values] * @returns {Object} a clone of object1 will be returned */ /*export*/ var object_merge_objects = function (object1, object2, ignore_keys, do_not_overwrite_existing_values, ignore_null, path) { if (object1 === void 0) { object1 = null; } if (object2 === void 0) { object2 = null; } if (ignore_keys === void 0) { ignore_keys = ["parents"]; } if (do_not_overwrite_existing_values === void 0) { do_not_overwrite_existing_values = false; } if (ignore_null === void 0) { ignore_null = false; } if (path === void 0) { path = []; } if (object1 == null) { if (object2 instanceof Array) { object1 = []; } else { object1 = {}; } } var iteration_keys = Object.keys(object2); if (ignore_keys === []) { if (path.indexOf(object2) >= 0) return undefined; path.push(object2); } // for (var i = 0; i < iteration_keys.length; i += 1) { var key = iteration_keys[i]; if (ignore_keys.some(function (k) { return key == k; })) { // } else if (object2[key] === null) { if (!ignore_null) object1[key] = null; } else if ((typeof (object2[key]) === "object") && ((typeof (object1[key]) === "object") || (typeof (object1[key]) === "undefined"))) { object1[key] = object_merge_objects(object1[key], object2[key], ignore_keys, do_not_overwrite_existing_values, ignore_null, path); } else { if ((do_not_overwrite_existing_values === false) || (typeof (object1[key]) === "undefined")) { object1[key] = object2[key]; } } } return object1; }; /* * @param {object} recipie ex: { "name" : { extract : function(o) { return o["name"]; }}} * */ var flatten_object = function (obj, recipie, drop_key) { if (drop_key === void 0) { drop_key = (function (k) { return ["parents", "parent", "children"].indexOf(k) > -1; }); } var ret = {}; for (var key in recipie) { if (!drop_key(key)) { var prefix = (recipie[key].prefix || ""); var recursive = (recipie[key].recursive || -1); var extract = (recipie[key].extract || (function (x) { return x; })); var _obj = extract(obj[key]); if ((_obj !== null) && ((typeof _obj == "object") || (obj[key] instanceof Array)) && (!(recursive == 0))) { var tmp = {}; var _recipie = {}; for (var _i = 0, _a = Object.keys(_obj); _i < _a.length; _i++) { var k = _a[_i]; _recipie[k] = { "prefix": (prefix + key + "."), "recursive": (recursive - 1), "extract": (function (x) { return x; }) }; } tmp = flatten_object(_obj, _recipie, drop_key); ret = object_merge_objects(ret, tmp); } else { ret[prefix + key] = _obj; } } } return ret; }; /** * use the complete path of an objects entry as key to make an one dimensional object * @param {object} object the object which should be moade flat * @param {string} [path] for the recursive call the current path */ /*export*/ var object_make_flat = function (object, path, filter, split_char, objects) { if (path === void 0) { path = null; } if (filter === void 0) { filter = ["parent", "children"]; } if (split_char === void 0) { split_char = "."; } if (objects === void 0) { objects = []; } if (object.toFlat != undefined) { return object.toFlat(); } else { var ret = {}; var default_visited_key = "___visited_path___"; var visited_key; if (object != void 0) { var iterate = function (key) { var newkey = key; if ((path != undefined) && (path !== "")) { newkey = path + split_char + newkey; } // do not touch objects we alrdy know if ((obj_ref[key] != undefined) && (!objects.some(function (e) { return (e === obj_ref); }))) { //if (lib_call.is_def(obj_ref[key]) && (! obj_ref[key].hasOwnProperty(visited_key)) && (key !== visited_key)) { if (typeof obj_ref[key] === "object") { ret = object_merge_objects(ret, object_make_flat(obj_ref[key], newkey, filter, split_char, objects.concat(object))); } else if (typeof obj_ref[key] === "function") { // o.O a function ... doing nothing might be the best choice } else { var value = obj_ref[key]; ret[newkey] = value; } } }; visited_key = default_visited_key; //object[visited_key] = true; var obj_ref = object; Object.keys(object).filter(function (key) { return (filter.indexOf(key) < 0); }).forEach(iterate); if (typeof object.getComputedValues == "function") { visited_key = default_visited_key + "_" + Math.random().toString(); obj_ref = object.getComputedValues(); obj_ref[visited_key] = true; Object.keys(obj_ref).filter(function (key) { return (filter.indexOf(key) < 0); }).forEach(iterate); } } else { //console.warn("something went wrong with that object: ", object, "on this path:", path); } return ret; } }; /** * splits a flat oject into an array of objects if there are paths containing numbers, which indicates * that there might be an array * used for normalisation of imports * @param entry * @param number_replace_string * @param {function} [match_function] how to test key if it causes a split * @returns {Array} */ var object_split_flat_object = function (entry, number_replace_string, fab_function, match_function) { if (typeof (match_function) === "undefined") { match_function = function (key) { return (!key.match(/^custom/)) && key.match(/\.[0-9]+\./); }; } if (typeof (fab_function) === "undefined") { fab_function = function (obj, e) { return obj; }; } if (typeof (number_replace_string) === "undefined") { number_replace_string = "%d"; } var ret = {}; var _ret = []; var keys = Object.keys(entry); var group_keys = keys.filter(match_function); keys.forEach(function (key) { var index = 0; var nkey = key; if (match_function(key)) { index = Number(key.match(/[0-9]+/)[0]).valueOf(); nkey = key.replace(/\.[0-9]+\./, "." + number_replace_string + "."); } if (!ret[index]) { ret[index] = {}; } ret[index][nkey] = entry[key]; }); keys = Object.keys(ret).sort(); _ret.push(ret[0]); for (var index = 1; index < keys.length; index++) { _ret.push(fab_function(ret[keys[index]], entry)); } _ret[0] = object_merge_objects(_ret[0], ret[0]); return _ret; }; // TODO: move to exporter, it's to specific // to normalize the objects convert paths of a tree-like structure to a // key-value list with complete paths as key // the info object is passed to the next function as it is // and a flat_object (key : value) /*export*/ var object_make_flat_async = function (data, callback, on_progress) { setTimeout((function (_obj, _cb, _info) { return (function () { var ret = _obj.map(function (o) { return object_make_flat(o); }); _cb({ "flat_object": ret, "objects": ret, "info": _info }); }); })((typeof (data.processed.objects) === "undefined") ? data.processed.source_object : data.processed.objects, callback, data.processed.info), 0); }; var object_flatten = function (object, paths, prefix) { if (prefix === void 0) { prefix = ""; } var ret = {}; var paths_ = paths.reduce(function (prev, current) { if (current.split(".").some(function (x) { return (x === "%d"); })) { var path = current.split(".%d").shift(); var len = object_path_read(object, path).length; for (var i = 0; i < len; i++) { // prev.push(sprintf(current, [i])); prev.push(current.replace(new RegExp("%d"), i.toFixed(0))); } } else { prev.push(current); } return prev; }, []); for (var _i = 0, paths_1 = paths_; _i < paths_1.length; _i++) { var path = paths_1[_i]; var tmp = object_path_read(object, path, true); if ((tmp != undefined) && (tmp.toFlat != undefined)) { var tmp_ = tmp.toFlat([path, "."].join("")); for (var key in tmp_) { ret[key] = tmp_[key]; } } else { ret[prefix + path] = tmp; } } return ret; }; /** * parse * @param {String} value * @returns {Object} */ var object_parse = function (value) { var content = JSON.parse(value); var m = { "root": content }; (new Mapper()).mapClasses(m); return m["root"]; }; /** * stringify * * @description stringify object as JSON */ var object_stringify = function (object, readable) { if (readable === void 0) { readable = false; } return (JSON.stringify(object, function (key, value) { if ((key == "parents") && (value !== null)) { return null; } if (key == "changeActions") { return undefined; } if (key == "observer") { return undefined; } if (key == "isMapped") { return undefined; } /* if (value === null) { return undefined; } */ return value; }, readable ? 1 : 0)); }; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var lib_meta; (function (lib_meta) { /** * @author frac */ function type_toString(type) { // return ("<" + type.id + ">"); return JSON.stringify(type); } lib_meta.type_toString = type_toString; /** * @author frac */ var class_pool = {}; /** * @author frac */ function class_set(name, class_) { class_pool[name] = class_; } lib_meta.class_set = class_set; /** * @author frac */ function class_get(name) { if (name in class_pool) { return class_pool[name]; } else { throw (new Error("no class registered for name '" + name + "'")); } } lib_meta.class_get = class_get; /** * @author frac */ function transform_field(name, attributes) { var type = attributes["type"]; var path = name; var label = ((attributes["title"] != undefined) ? attributes["title"] : name); var display = ((attributes["display"] != undefined) ? attributes["display"] : true); return ({ "path": path, "type": type, "label": label, "display": display }); } lib_meta.transform_field = transform_field; /** * @author frac */ function transform_description(label, description, groups_raw) { if (groups_raw === void 0) { groups_raw = null; } var fieldmap; var fields = Object.keys(description).map(function (key) { return transform_field(key, description[key]); }); var groups = ((groups_raw == null) ? null : groups_raw.map(function (group_raw) { return { "label": group_raw["label"], "fields": group_raw["fields"].map(function (field_name) { var index = fields["findIndex"](function (field) { return (field.path == field_name); }); if (index < 0) { throw (new Error("field " + field_name + " not found in model-description")); } return index; }) }; })); return { "fields": fields, "description": null, "title": label, "groups": groups }; } lib_meta.transform_description = transform_description; /** * @author frac */ function transform_description_groups(label, description_model, description_groups) { return ({ "label": label, "groups": description_groups.map(function (group_raw) { return { "label": group_raw["label"], "fields": group_raw["fields"].map(function (path) { var field_raw = description_model[path]; return transform_field(path, field_raw); }) }; }) }); } lib_meta.transform_description_groups = transform_description_groups; })(lib_meta || (lib_meta = {})); /// <reference path="../../base/build/logic-decl.d.ts"/> /// <reference path="../../object/build/logic-decl.d.ts"/> var lib_meta; (function (lib_meta) { /** * @author fenris */ lib_meta._logprefix = "[lib_meta] "; /** * @author fenris */ lib_meta._verbosity = 1; })(lib_meta || (lib_meta = {})); var lib_meta; (function (lib_meta) { /** * @author fenris */ var class_shape = (function () { /** * @desc [constructor] * @author fenris */ function class_shape(_a) { var _b = _a["primitive"], primitive = _b === void 0 ? false : _b, _c = _a["soft"], soft = _c === void 0 ? true : _c, _d = _a["defaultvalue"], defaultvalue = _d === void 0 ? null : _d; /* if ((! soft) && (defaultvalue == null)) { throw (new Error("'null' is not a valid defaultvalue for hard shapes")); } */ this.primitive = primitive; this.soft = soft; this.defaultvalue = defaultvalue; } /** * @desc [accessor] [getter] * @author fenris */ class_shape.prototype.primitive_get = function () { return this.primitive; }; /** * @desc [accessor] [getter] * @author fenris */ class_shape.prototype.soft_get = function () { return this.soft; }; /** * @desc [accessor] [getter] * @author fenris */ class_shape.prototype.defaultvalue_get = function () { return this.defaultvalue; }; /** * @desc [accessor] * @author fenris */ class_shape.prototype.inspect = function (value) { var messages = []; if ((!this.soft) && (value == null)) { messages.push("shape does not allow 'null' as value"); } return messages; }; /** * @desc [accessor] * @author fenris */ class_shape.prototype.check = function (value) { var messages = this.inspect(value); if (lib_meta._verbosity >= 1) { messages.forEach(function (message) { return console.warn("" + lib_meta._logprefix + message); }); } return (messages.length == 0); }; return class_shape; }()); lib_meta.class_shape = class_shape; /** * @author fenris */ var class_shape_buildin = (function (_super) { __extends(class_shape_buildin, _super); /** * @author fenris */ function class_shape_buildin(_a) { var typename = _a["typename"], _b = _a["primitive"], primitive = _b === void 0 ? undefined : _b, _c = _a["soft"], soft = _c === void 0 ? undefined : _c, _d = _a["defaultvalue"], defaultvalue = _d === void 0 ? undefined : _d; var _this = _super.call(this, { "primitive": primitive, "soft": soft, "defaultvalue": defaultvalue }) || this; _this.typename = typename; return _this; } /** * @override * @author fenris */ class_shape_buildin.prototype.inspect = function (value) { var messages = _super.prototype.inspect.call(this, value); if ((!this.soft) && (typeof (value) != this.typename)) { messages.push("typename is not '" + this.typename + "'"); } return messages; }; /** * @desc [implementation] * @author fenris */ class_shape_buildin.prototype._show = function () { var str = ""; str = this.typename; if (this.soft) { str = str + "_or_null"; } return str; }; return class_shape_buildin; }(class_shape)); lib_meta.class_shape_buildin = class_shape_buildin; /** * @author fenris */ var _pool = {}; /** * @author fenris */ function register(id, factory) { if (id in _pool) { var message = "shape '" + id + "' already registered"; throw (new Error(message)); } else { _pool[id] = factory; } } lib_meta.register = register; /** * @author fenris */ function construct(id, parameters) { if (parameters === void 0) { parameters = {}; } if (!(id in _pool)) { var message = "no shape found with id " + id; throw (new Error(message)); } else { return _pool[id](parameters); } } lib_meta.construct = construct; /** * @author fenris */ function from_raw(shape_raw) { return construct(shape_raw.id, shape_raw.parameters); } lib_meta.from_raw = from_raw; /** * @author fenris */ function define(id, shape_raw) { register(id, function (parameters) { return from_raw(shape_raw); }); } lib_meta.define = define; /** * @author fenris */ function retrieve(id) { return construct(id, {}); } lib_meta.retrieve = retrieve; })(lib_meta || (lib_meta = {})); var lib_meta; (function (lib_meta) { /** * @author fenris */ var class_shape_any = (function (_super) { __extends(class_shape_any, _super); /** * @author fenris */ function class_shape_any(_a) { var _b = _a["soft"], soft = _b === void 0 ? false : _b, _c = _a["defaultvalue"], defaultvalue = _c === void 0 ? 0 : _c; return _super.call(this, { "soft": soft, "defaultvalue": defaultvalue }) || this; } /** * @override * @author fenris */ class_shape_any.prototype.inspect = function (value) { var messages = _super.prototype.inspect.call(this, value); return messages; }; /** * @override * @author fenris */ class_shape_any.prototype.to_raw = function () { return { "id": "any", "parameters": { "soft": this.soft } }; }; /** * @desc [implementation] * @author fenris */ class_shape_any.prototype._show = function () { var str = "any"; return str; }; return class_shape_any; }(lib_meta.class_shape)); lib_meta.class_shape_any = class_shape_any; lib_meta.register("any", function (parameters) { return (new class_shape_any({ "soft": parameters["soft"] })); }); })(lib_meta || (lib_meta = {})); var lib_meta; (function (lib_meta) { /** * @author fenris */ var class_shape_boolean = (function (_super) { __extends(class_shape_boolean, _super); /** * @author fenris */ function class_shape_boolean(_a) { var _b = _a["soft"], soft = _b === void 0 ? false : _b, _c = _a["defaultvalue"], defaultvalue = _c === void 0 ? false : _c; return _super.call(this, { "typename": "boolean", "primitive": true, "soft": soft, "defaultvalue": defaultvalue }) || this; } /** * @override * @author fenris */ class_shape_boolean.prototype.to_raw = function () { return { "id": "boolean", "parameters": { "soft": this.soft } }; }; return class_shape_boolean; }(lib_meta.class_shape_buildin)); lib_meta.class_shape_boolean = class_shape_boolean; lib_meta.register("boolean", function (parameters) { return (new class_shape_boolean({ "soft": parameters["soft"] })); }); lib_meta.register("bool", function (parameters) { return lib_meta.construct("boolean", parameters); }); })(lib_meta || (lib_meta = {})); var lib_meta; (function (lib_meta) { /** * @author fenris */ var class_shape_integer = (function (_super) { __extends(class_shape_integer, _super); /** * @author fenris */ function class_shape_integer(_a) { var _b = _a["soft"], soft = _b === void 0 ? false : _b, _c = _a["defaultvalue"], defaultvalue = _c === void 0 ? 0 : _c, _d = _a["min"], min = _d === void 0 ? null : _d, _e = _a["max"], max = _e === void 0 ? null : _e; var _this = _super.call(this, { "typename": "number", "primitive": true, "soft": soft, "defaultvalue": defaultvalue }) || this; _this.min = min; _this.max = max; return _this; } /** * @desc [accessor] [getter] * @author fenris */ class_shape_integer.prototype.min_get = function () { return this.min; }; /** * @desc [accessor] [getter] * @author fenris */ class_shape_integer.prototype.max_get = function () { return this.max; }; /** * @override * @author fenris */ class_shape_integer.prototype.inspect = function (value) { var messages = _super.prototype.inspect.call(this, value); if (value != null) { if ((this.min != null) && (value < this.min)) { messages.push("value is below minimum of " + this.min.toString()); } if ((this.max != null) && (value > this.max)) { messages.push("value is over maximum of " + this.max.toString()); } } return messages; }; /** * @override * @author fenris */ class_shape_integer.prototype.to_raw = function () { return { "id": "integer", "parameters": { "soft": this.soft, "min": this.min, "max": this.max } }; }; return class_shape_integer; }(lib_meta.class_shape_buildin)); lib_meta.class_shape_integer = class_shape_integer; lib_meta.register("integer", function (parameters) { return (new class_shape_integer({ "min": parameters["min"], "max": parameters["max"], "soft": parameters["soft"] })); }); lib_meta.register("int", function (parameters) { return lib_meta.construct("integer", parameters); }); })(lib_meta || (lib_meta = {})); var lib_meta; (function (lib_meta) { /** * @author fenris */ var class_shape_float = (function (_super) { __extends(class_shape_float, _super); /** * @author fenris */ function class_shape_float(_a) { var _b = _a["soft"], soft = _b === void 0 ? false : _b, _c = _a["defaultvalue"], defaultvalue = _c === void 0 ? 0.0 : _c, _d = _a["min"], min = _d === void 0 ? null : _d, _e = _a["max"], max = _e === void 0 ? null : _e; var _this = _super.call(this, { "typename": "number", "primitive": true, "soft": soft, "defaultvalue": defaultvalue }) || this; _this.min = min; _this.max = max; return _this; } /** * @desc [accessor] [getter] * @author fenris */ class_shape_float.prototype.min_get = function () { return this.min; }; /** * @desc [accessor] [getter] * @author fenris */ class_shape_float.prototype.max_get = function () { return this.max; }; /** * @override * @author fenris */ class_shape_float.prototype.inspect = function (value) { var messages = _super.prototype.inspect.call(this, value); if (value != null) { if ((this.min != null) && (value < this.min)) { messages.push("value is below minimum of " + this.min.toString()); } if ((this.max != null) && (value > this.max)) { messages.push("value is over maximum of " + this.max.toString()); } } return messages; }; /** * @override * @author fenris */ class_shape_float.prototype.to_raw = function () { return { "id": "float", "parameters": { "soft": this.soft, "min": this.min, "max": this.max } }; }; return class_shape_float; }(lib_meta.class_shape_buildin)); lib_meta.class_shape_float = class_shape_float; lib_meta.register("float", function (parameters) { return (new class_shape_float({ "min": parameters["min"], "max": parameters["max"], "soft": parameters["soft"] })); }); })(lib_meta || (lib_meta = {})); var lib_meta; (function (lib_meta) { /** * @author fenris */ var class_shape_string = (function (_super) { __extends(class_shape_string, _super); /** * @author fenris */ function class_shape_string(_a) { var _b = _a["soft"], soft = _b === void 0 ? false : _b, _c = _a["defaultvalue"], defaultvalue = _c === void 0 ? "" : _c; return _super.call(this, { "typename": "string", "primitive": true, "soft": soft, "defaultvalue": defaultvalue }) || this; } /** * @override * @author fenris */ class_shape_string.prototype.to_raw = function () { return { "id": "string", "parameters": { "soft": this.soft } }; }; return class_shape_string; }(lib_meta.class_shape_buildin)); lib_meta.class_shape_string = class_shape_string; /** * @author fenris */ lib_meta.register("string", function (parameters) { return (new class_shape_string({ "soft": parameters["soft"] })); }); lib_meta.register("str", function (parameters) { return lib_meta.construct("string", parameters); }); })(lib_meta || (lib_meta = {})); var lib_meta; (function (lib_meta) { /** * @author fenris */ var class_shape_email = (function (_super) { __extends(class_shape_email, _super); /** * @author fenris */ function class_shape_email(_a) { var _b = _a["soft"], soft = _b === void 0 ? false : _b, _c = _a["defaultvalue"], defaultvalue = _c === void 0 ? "" : _c; return _super.call(this, { "primitive": true, "soft": soft, "defaultvalue": defaultvalue }) || this; } /** * @override * @author fenris */ class_shape_email.prototype.to_raw = function () { return { "id": "email", "parameters": { "soft": this.soft } }; }; /** * @desc [implementation] * @author fenris */ class_shape_email.prototype._show = function () { var str = "email"; return str; }; return class_shape_email; }(lib_meta.class_shape)); lib_meta.class_shape_email = class_shape_email; lib_meta.register("email", function (parameters) { return (new class_shape_email({ "soft": parameters["soft"] })); }); })(lib_meta || (lib_meta = {})); var lib_meta; (function (lib_meta) { /** * @author fenris */ var class_shape_array = (function (_super) { __extends(class_shape_array, _super); /** * @author fenris */ function class_shape_array(_a) { var shape_element = _a["shape_element"], _b = _a["soft"], soft = _b === void 0 ? false : _b, _c = _a["defaultvalue"], defaultvalue = _c === void 0 ? [] : _c; var _this = _super.call(this, { "primitive": false, "soft": soft, "defaultvalue": defaultvalue }) || this; _this.shape_element = shape_element; return _this; } /** * @desc [accessor] [getter] * @author fenris */ class_shape_array.prototype.shape_element_get = function () { return this.shape_element; }; /** * @override * @author fenris */ class_shape_array.prototype.inspect = function (value) { var _this = this; var messages = _super.prototype.inspect.call(this, value); if (value != null) { if (!((typeof (value) == "object") && (value instanceof Array))) { messages.push("value is not an array"); } else { var array = (value); array.forEach(function (element, index) { messages = messages.concat(_this.shape_element.inspect(element).map(function (message) { return "array element #" + index.toString() + ": " + message; })); }); } } return messages; }; /** * @override * @author fenris */ class_shape_array.prototype.to_raw = function () { return { "id": "array", "parameters": { "shape_element": this.shape_element.to_raw(), "soft": this.soft } }; }; /** * @desc [implementation] * @author fenris */ class_shape_array.prototype._show = function () { var str = "array"; str += "<" + instance_show(this.shape_element) + ">"; return str; }; return class_shape_array; }(lib_meta.class_shape)); lib_meta.class_shape_array = class_shape_array; lib_meta.register("array", function (parameters) { var shape_element_raw = lib_object.fetch(parameters, "shape_element", null, 2); var shape_element = lib_meta.from_raw(shape_element_raw); return (new class_shape_array({ "shape_element": shape_element, "soft": parameters["soft"] })); }); lib_meta.register("arr", function (parameters) { return lib_meta.construct("array", parameters); }); })(lib_meta || (lib_meta = {})); var lib_meta; (function (lib_meta) { /** * @author fenris */ var class_shape_object = (function (_super) { __extends(class_shape_object, _super); /** * @author fenris */ function class_shape_object(_a) { var fields = _a["fields"], _b = _a["soft"], soft = _b === void 0 ? true : _b, _c = _a["defaultvalue"], defaultvalue = _c === void 0 ? null : _c; var _this = this; if (defaultvalue == null) { defaultvalue = {}; fields.forEach(function (field) { defaultvalue[field.name] = field.shape.defaultvalue_get(); }); } _this = _super.call(this, { "typename": "object", "primitive": false, "soft": soft, "defaultvalue": defaultvalue }) || this; _this.fields = fields; return _this; } /** * @desc [accessor] [getter] * @author fenris */ class_shape_object.prototype.fields_get = function () { return this.fields; }; /** * @override * @author fenris * @todo check for superfluous fields? */ class_shape_object.prototype.inspect = function (value) { var messages = _super.prototype.inspect.call(this, value); if (value != null) { this.fields.forEach(function (field, index) { var value_ = value[field.name]; messages = messages.concat(field.shape.inspect(value_).map(function (message) { return "object field '" + field.name + "': " + message; })); }); } return messages; }; /** * @override * @author fenris */ class_shape_object.prototype.to_raw = function () { return { "id": "object", "parameters": { "soft": this.soft, "fields": this.fields.map(function (field) { return { "name": field.name, "shape": field.shape.to_raw(), "label": field.label }; }) } }; }; /** * @desc [implementation] * @author fenris */ class_shape_object.prototype._show = function () { var str = _super.prototype._show.call(this); var str_ = this.fields.map(function (field) { return field.name + ":" + instance_show(field.shape); }).join(","); str = str + "<" + str_ + ">"; return str; }; return class_shape_object; }(lib_meta.class_shape_buildin)); lib_meta.class_shape_object = class_shape_object; lib_meta.register("object", function (parameters) { var fields_ = lib_object.fetch(parameters, "fields", [], 1); return (new class_shape_object({ "fields": fields_.map(function (field_) { var name = lib_object.fetch(field_, "name", null, 2); var shape_raw = lib_object.fetch(field_, "shape", null, 2); var shape = lib_meta.construct(shape_raw.id, shape_raw.parameters); var label = lib_object.fetch(field_, "label", null, 1); return { "name": name, "shape": shape, "label": label }; }), "soft": parameters["soft"] })); }); lib_meta.register("obj", function (parameters) { return lib_meta.construct("object", parameters); }); })(lib_meta || (lib_meta = {})); var lib_meta; (function (lib_meta) { /** * @author fenris */ var class_shape_date = (function (_super) { __extends(class_shape_date, _super); /** * @author fenris */ function class_shape_date(_a) { var _b = _a["soft"], soft = _b === void 0 ? false : _b, _c = _a["defaultvalue"], defaultvalue = _c === void 0 ? new Date(Date.now()) : _c; return _super.call(this, { "primitive": true, "soft": soft, "defaultvalue": defaultvalue }) || this; } /** * @override * @author fenris */ class_shape_date.prototype.inspect = function (value) { var messages = _super.prototype.inspect.call(this, value); if (value != null) { if (!((typeof (value) == "object") && (value instanceof Date))) { messages.push("value is not a date"); } } return messages; }; /** * @override * @author fenris */ class_shape_date.prototype.to_raw = function () { return { "id": "date", "parameters": { "soft": this.soft } }; }; /** * @desc [implementation] * @author fenris */ class_shape_date.prototype._show = function () { var str = "date"; return str; }; return class_shape_date; }(lib_meta.class_shape)); lib_meta.class_shape_date = class_shape_date; lib_meta.register("date", function (parameters) { return (new class_shape_date({ "soft": parameters["soft"] })); }); })(lib_meta || (lib_meta = {})); var lib_meta; (function (lib_meta) { /** * @desc represented as milliseconds of day * @author fenris */ var class_shape_time = (function (_super) { __extends(class_shape_time, _super); /** * @author fenris */ function class_shape_time(_a) { var _b = _a["soft"], soft = _b === void 0 ? false : _b, _c = _a["defaultvalue"], defaultvalue = _c === void 0 ? class_shape_time.from_timestamp(Date.now()) : _c; return _super.call(this, { "primitive": true, "soft": soft, "defaultvalue": defaultvalue }) || this; } /** * @override * @author fenris */ class_shape_time.prototype.inspect = function (value) { var messages = _super.prototype.inspect.call(this, value); if (value != null) { if (!(typeof (value) == "object")) { messages.push("value is not a time"); } else { if (!("hours" in value)) { messages.push("hour is missing"); } if (!("minutes" in value)) { messages.push("minutes is missing"); } if (!("seconds" in value)) { messages.push("seconds is missing"); } } } return messages; }; /** * @override * @author fenris */ class_shape_time.prototype.to_raw = function () { return { "id": "time", "parameters": { "soft": this.soft } }; }; /** * @desc [implementation] * @author fenris */ class_shape_time.prototype._show = function () { var str = "time"; return str; }; /** * @author fenris */ class_shape_time.from_date = function (date) { var hours = date.getHours(); var minutes = date.getMinutes(); var seconds = date.getSeconds(); return { "hours": hours, "minutes": minutes, "seconds": seconds }; }; /** * @author fenris */ class_shape_time.from_timestamp = function (timestamp) { var date = new Date(timestamp); return this.from_date(date); }; /** * @author fenris */ class_shape_time.now = function () { return this.from_timestamp(Date.now()); }; return class_shape_time; }(lib_meta.class_shape)); lib_meta.class_shape_time = class_shape_time; /** * @author fenris */ lib_meta.register("time", function (parameters) { return (new class_shape_time({ "soft": parameters["soft"] })); }); })(lib_meta || (lib_meta = {})); var lib_meta; (function (lib_meta) { /** * @author fenris */ var class_shape_enumeration = (function (_super) { __extends(class_shape_enumeration, _super); /** * @author fenris */ function class_shape_enumeration(_a) { var shape_option = _a["shape_option"], options = _a["options"], _b = _a["soft"], soft = _b === void 0 ? false : _b, _c = _a["defaultvalue"], defaultvalue = _c === void 0 ? undefined : _c; var _this = this; if (defaultvalue === undefined) { if (soft) { defaultvalue = null; } else { if (options.length == 0) { throw (new Error("a hard enumeration must have at least one option")); } else { defaultvalue = options[0].value; } } } _this = _super.call(this, { "primitive": true, "soft": soft, "defaultvalue": defaultvalue }) || this; _this.shape_option = shape_option; _this.options = options; return _this; } /** * @desc [accessor] [getter] * @author fenris */ class_shape_enumeration.prototype.shape_option_get = function () { return this.shape_option; }; /** * @desc [accessor] [getter] * @author fenris */ class_shape_enumeration.prototype.options_get = function () { return this.options; }; /** * @override * @author fenris */ class_shape_enumeration.prototype.inspect = function (value) { var messages = _super.prototype.inspect.call(this, value); if (value != null) { if (!this.shape_option.check(value)) { messages.push("value has not the specified option shape"); } else { var found = this.options.some(function (option) { return instance_collate(option.value, value); }); if (!found) { messages.push("value is not one of the specified options"); } } } return messages; }; /** * @override * @author fenris */ class_shape_enumeration.prototype.to_raw = function () { return { "id": "enumeration", "parameters": { "shape_option": this.shape_option.to_raw(), "options": this.options.map(function (option) { return { "value": option.value, "label": option.label }; }), "soft": this.soft } }; }; /** * @desc [implementation] * @author fenris */ class_shape_enumeration.prototype._show = function () { var str = "enumeration"; { str = str + "<" + instance_show(this.shape_option) + ">"; } { var str_ = this.options.map(function (option) { return instance_show(option.value); }).join(","); str = str + "[" + str_ + "]"; } return str; }; return class_shape_enumeration; }(lib_meta.class_shape)); lib_meta.class_shape_enumeration = class_shape_enumeration; lib_meta.register("enumeration", function (parameters) { var shape_option_raw = lib_object.fetch(parameters, "shape_option", null, 2); var shape_option = lib_meta.from_raw(shape_option_raw); var options = lib_object.fetch(parameters, "options", [], 2); return (new class_shape_enumeration({ "shape_option": shape_option, "options": options, "soft": parameters["soft"] })); }); })(lib_meta || (lib_meta = {})); var lib_meta; (function (lib_meta) { /** * @author fenris */ var class_shape_map = (function (_super) { __extends(class_shape_map, _super); /** * @author fenris */ function class_shape_map(_a) { var shape_key = _a["shape_key"], shape_value = _a["shape_value"], _b = _a["soft"], soft = _b === void 0 ? false : _b, _c = _a["defaultvalue"], defaultvalue = _c === void 0 ? {} : _c; var _this = _super.call(this, { "primitive": true, "soft": soft, "defaultvalue": defaultvalue }) || this; _this.shape_key = shape_key; _this.shape_value = shape_value; return _this; } /** * @desc [accessor] [getter] * @author fenris */ class_shape_map.prototype.shape_key_get = function () { return this.shape_key; }; /** * @desc [accessor] [getter] * @author fenris */ class_shape_map.prototype.shape_value_get = function () { return this.shape_value; }; /** * @override * @author fenris */ class_shape_map.prototype.inspect = function (value) { var _this = this; var messages = _super.prototype.inspect.call(this, value); if (value != null) { if (typeof (value) != "object") { messages.push("value is not an object"); } else { Object.keys(value).forEach(function (key, index) { var value_ = value[key]; messages = messages.concat(_this.shape_key.inspect(key).map(function (message) { return "map entry #" + index.toString() + " key: " + message; })); messages = messages.concat(_this.shape_value.inspect(value_).map(function (message) { return "map entry #" + index.toString() + " value: " + message; })); }); } } return messages; }; /** * @override * @author fenris */ class_shape_map.prototype.to_raw = function () { return { "id": "map", "parameters": { "shape_key": this.shape_key.to_raw(), "shape_value": this.shape_value.to_raw(), "soft": this.soft } }; }; /** * @desc [implementation] * @author fenris */ class_shape_map.prototype._show = function () { var str = "map"; str += "<" + instance_show(this.shape_key) + "," + instance_show(this.shape_value) + ">"; return str; }; return class_shape_map; }(lib_meta.class_shape)); lib_meta.class_shape_map = class_shape_map; lib_meta.register("map", function (parameters) { var shape_key_raw = lib_object.fetch(parameters, "shape_key", null, 2); var shape_key = lib_meta.construct(shape_key_raw.id, shape_key_raw.parameters); var shape_value_raw = lib_object.fetch(parameters, "shape_value", null, 2); var shape_value = lib_meta.from_raw(shape_value_raw); return (new class_shape_map({ "shape_key": shape_key, "shape_value": shape_value, "soft": parameters["soft"] })); }); })(lib_meta || (lib_meta = {})); var lib_meta; (function (lib_meta) { /** * @author fenris */ var class_shape_function = (function (_super) { __extends(class_shape_function, _super); /** * @author fenris */ function class_shape_function(_a) { var shape_input = _a["shape_input"], shape_output = _a["shape_output"], _b = _a["soft"], soft = _b === void 0 ? false : _b, _c = _a["defaultvalue"], defaultvalue = _c === void 0 ? {} : _c; var _this = _super.call(this, { "primitive": true, "soft": soft, "defaultvalue": defaultvalue }) || this; _this.shape_input = shape_input; _this.shape_output = shape_output; return _this; } /** * @desc [accessor] [getter] * @author fenris */ class_shape_function.prototype.shape_input_get = function () { return this.shape_input; }; /** * @desc [accessor] [getter] * @author fenris */ class_shape_function.prototype.shape_output_get = function () { return this.shape_output; }; /** * @override * @author fenris */ class_shape_function.prototype.inspect = function (value) { var messages = _super.prototype.inspect.call(this, value); if (value != null) { if (typeof (value) != "function") { messages.push("value is not an object"); } else { } } return messages; }; /** * @override * @author fenris */ class_shape_function.prototype.to_raw = function () { return { "id": "function", "parameters": { "shape_input": this.shape_input.to_raw(), "shape_output": this.shape_output.to_raw(), "soft": this.soft } }; }; /** * @desc [implementation] * @author fenris */ class_shape_function.prototype._show = function () { var str = "function"; str += "<" + instance_show(this.shape_input) + "," + instance_show(this.shape_output) + ">"; return str; }; return class_shape_function; }(lib_meta.class_shape)); lib_meta.class_shape_function = class_shape_function; lib_meta.register("function", function (parameters) { var shape_input_raw = lib_object.fetch(parameters, "shape_input", null, 2); var shape_input = lib_meta.construct(shape_input_raw.id, shape_input_raw.parameters); var shape_output_raw = lib_object.fetch(parameters, "shape_output", null, 2); var shape_output = lib_meta.from_raw(shape_output_raw); return (new class_shape_function({ "shape_input": shape_input, "shape_output": shape_output, "soft": parameters["soft"] })); }); })(lib_meta || (lib_meta = {})); var lib_meta; (function (lib_meta) { /** * @author fenris * @todo use a treemap-function (but first make one :P) */ function adjust_labels(shape, transformator) { if (false) { } else if (shape instanceof lib_meta.class_shape_enumeration) { var shape_ = (shape); return (new lib_meta.class_shape_enumeration({ "shape_option": adjust_labels(shape_.shape_option_get(), transformator), "options": shape_.options_get().map(function (option) { return { "value": option.value, "label": transformator(option.label) }; }), "soft": shape_.soft_get(), "defaultvalue": shape_.defaultvalue_get() })); } else if (shape instanceof lib_meta.class_shape_array) { var shape_ = (shape); return (new lib_meta.class_shape_array({ "shape_element": adjust_labels(shape_.shape_element_get(), transformator), "soft": shape_.soft_get(), "defaultvalue": shape_.defaultvalue_get() })); } else if (shape instanceof lib_meta.class_shape_object) { var shape_ = (shape); return (new lib_meta.class_shape_object({ "fields": shape_.fields_get().map(function (field) { return { "name": field.name, "shape": adjust_labels(field.shape, transformator), "label": transformator(field.label) }; }), "soft": shape_.soft_get(), "defaultvalue": shape_.defaultvalue_get() })); } else { // shape["label"] = ((shape["label"] == null) ? null : transformator(shape["label"])); return shape; } } lib_meta.adjust_labels = adjust_labels; })(lib_meta || (lib_meta = {})); var plain_text_to_html = function (text) { var ret = text; ret = ret.replace(/ /g, " "); // convert multiple whitespace to forced ones ret = ret.split("\n").join("<br/>"); return ret; }; /** * @desc makes a valid */ var format_sentence = function (str, rtl, caseSense) { if (rtl === void 0) { rtl = false; } if (caseSense === void 0) { caseSense = true; } if (str === "") { return str; } else { var marks = { ".": true, "?": true, "!": true }; var default_mark = "."; var ret = str.split(""); if (!rtl) { ret[0] = ret[0].toLocaleUpperCase(); if (!(ret[ret.length - 1] in marks)) { ret.push(default_mark); } } else { ret[ret.length - 1] = ret[ret.length - 1].toLocaleUpperCase(); if (!(ret[0] in marks)) { ret.unshift(default_mark); } } return ret.join(""); } }; var fill_string_template = function (template_string, object, fabric, delimiter, default_string, sloppy) { if (fabric === void 0) { fabric = function (object, key) { return object[key]; }; } if (delimiter === void 0) { delimiter = "%"; } if (default_string === void 0) { default_string = null; } function get_tags(str) { var r = new RegExp(delimiter + "[^\\s^" + delimiter + "]+" + delimiter, "gi"); return ((str.match(r) || []).map(function (e) { return e.slice(delimiter.length, e.length - delimiter.length); })); } function replace_tag(str, tag, value) { var r = new RegExp(delimiter + tag + delimiter, "gi"); return str.replace(r, value); } function replace_tags(str, obj) { return (get_tags(str).reduce(function (ret, key) { var value = ""; try { value = fabric(obj, key); if ((!sloppy && (value === void 0)) || (sloppy && (value == void 0))) { value = default_string; } } catch (e) { console.warn("invalid placeholder " + key); value = default_string; } return replace_tag(ret, key, value); }, str)); } return replace_tags(template_string, object); }; var make_string_template = function (_template, _fabrics) { if (_fabrics === void 0) { _fabrics = {}; } function replace_tag(str, tag, value) { var r = new RegExp("%" + tag + "%", "gi"); return str.replace(r, value); } function replace_tags(str, obj) { return (Object.keys(obj).reduce(function (ret, key) { return replace_tag(ret, key, _fabrics[key] || obj[key]); }, str)); } return (function (tags) { return replace_tags(_template, tags); }); }; var make_eml_header = (function () { var _template = ""; _template += "From: %from%\n"; _template += "To: %recipient%\n"; _template += "Subject: %subject%\n"; _template += "X-Mailer: greenscale-plankton.emlgen\n"; return make_string_template(_template); })(); var make_eml_body = (function () { var exports = {}; exports["simple_body"] = make_string_template("Content-Type: %contenttype%\n\n%body%\n\n"); // very basic implementation // parts = [{contenttype:"text/html; charset=UTF-8", body: "<h1>foo</h1>" }, {...}] exports["body_boundrary"] = function (parts, boundrary) { var _template = ""; _template += "--%boundrary%\n"; _template += "Content-Type: %contenttype%\n\n%body%\n\n"; //_template += "--%boundrary%--\n\n"; var maker = make_string_template(_template); return (parts.reduce(function (prev, curr) { curr.boundrary = boundrary; return [prev, maker(curr)].join(""); }, "")); }; // body must be base64 encoded! exports["attachment_boundrary"] = function (parts, boundrary) { var _template = ""; _template += "--%boundrary%\n"; _template += "Content-Type: %contenttype%\n"; _template += "Content-Transfer-Encoding: base64\n"; _template += "Content-Disposition: %disposition%; filename=\"%name%\"\n\n"; _template += "%body%\n\n"; //_template += "--%boundrary%--\n\n"; var maker = make_string_template(_template); return (parts.reduce(function (prev, curr) { curr.boundrary = boundrary; if (curr.disposition === void 0) curr.disposition = "inline"; return [prev, maker(curr)].join(""); }, "")); }; exports["gen_boundrary"] = function () { return ("xxxxxxxxxxxxxxxxxxxxxx".replace(/[xy]/g, function (c) { var r = crypto.getRandomValues(new Uint8Array(1))[0] % 16 | 0, v = c == "x" ? r : (r & 0x3 | 0x8); return v.toString(16); })); }; // simple implementation without alternatives (old rfc) exports["complete_boundrary"] = function (bodyparts, attachments) { var ret = ""; var boundrary = exports["gen_boundrary"](); ret += exports["body_boundrary"](bodyparts, boundrary); ret += exports["attachment_boundrary"](attachments, boundrary); ret += "--" + boundrary + "--\n\nINVISIBLE!!!!"; return (exports["simple_body"]({ "contenttype": sprintf("multipart/mixed; boundary=%s", [boundrary]), "body": ret })); }; return exports; })(); ///<reference path="../../base/build/logic-decl.d.ts"/> var lib_string; (function (lib_string) { /** * @author frac */ var hexdigits = 4; /** * @author frac */ var index_max = 1 << (4 * hexdigits); /** * @author frac */ var index_is = 0; /** * @author neuc,frac */ function empty(str) { var tmp = str.trim(); return (tmp === ""); } lib_string.empty = empty; /** * @desc returns a unique string * @param {string} prefix an optional prefix for the generated string * @return {string} * @author frac */ function generate(prefix) { if (prefix === void 0) { prefix = "string_"; } if (index_is > index_max) { throw (new Error("[string_generate] out of valid indices")); } else { return lib_string.sprintf(prefix + "%0" + hexdigits.toString() + "X", [index_is++]); } } lib_string.generate = generate; /** * @desc splits a string, but returns an empty list, if the string is empty * @param {string} chain * @param {string} separator * @return {Array<string>} * @author frac */ function split(chain, separator) { if (separator === void 0) { separator = " "; } if (chain.length == 0) { return []; } else { return chain.split(separator); } } lib_string.split = split; /** * @desc concats a given word with itself n times * @param {string} word * @param {int} * @return {string} * @author frac */ function repeat(word, count) { return ((count == 0) ? "" : (word + repeat(word, count - 1))); } lib_string.repeat = repeat; /** * @desc lengthens a string by repeatedly appending or prepending another string * @param {string} word the string to pad * @param {int} length the length, which the result shall have * @param {string} symbol the string, which will be added (multiple times) * @param {boolean} [prepend]; whether to prepend (~true) or append (~false); default: false * @return {string} the padded string * @author frac */ function pad(word, length, symbol, prepend) { if (prepend === void 0) { prepend = false; } if (prepend) { while (word.length < length) word = symbol + word; return word.substring(word.length - length); } else { while (word.length < length) word = word + symbol; return word.substring(0, length); } } lib_string.pad = pad; /** * @desc checks if a given string conttains a certain substring * @param {string} string * @param {string} part * @return {boolean} * @author frac */ function contains(chain, part) { if (typeof (chain) !== "string") return false; return (chain.indexOf(part) >= 0); } lib_string.contains = contains; /** * @desc checks if a given string starts with a certain substring * @param {string} string * @param {string} part * @return {boolean} * @author frac */ function startsWith(chain, part) { if (typeof (chain) !== "string") return false; // return (string.indexOf(part) === 0); return ((function (m, n) { if (n == 0) { return true; } else { if (m == 0) { return false; } else { return ((chain[0] == part[0]) && startsWith(chain.substring(1), part.substring(1))); } } })(chain.length, part.length)); } lib_string.startsWith = startsWith; /** * @desc checks if a given string ends with a certain substring * @param {string} string * @param {string} part * @return {boolean} * @author frac */ function endsWith(chain, part) { if (typeof (chain) !== "string") return false; // return (string.lastIndexOf(part) === string.length-part.length); return ((function (m, n) { if (n == 0) { return true; } else { if (m == 0) { return false; } else { // console.info(("(" + string[m-1] + " == " + part[n-1] + ")") + " = " + String(string[m-1] == part[n-1])); return ((chain[m - 1] == part[n - 1]) && endsWith(chain.substring(0, m - 1), part.substring(0, n - 1))); } } })(chain.length, part.length)); } lib_string.endsWith = endsWith; /** * @desc count the occourrences of a string in a string * @param string haystack_string the string wich should be examined * @param string needle_string the string which should be counted * @author neuc */ function count_occourrences(haystack_string, needle_string, check_escape) { var cnt = 0; var pos = -1; do { pos = haystack_string.indexOf(needle_string, pos + 1); if ((!check_escape) || (haystack_string[pos - 1] != "\\")) { cnt++; } } while (pos >= 0); return cnt - 1; } lib_string.count_occourrences = count_occourrences; ; /** * @author fenris */ function stance(str, args) { Object.keys(args).forEach(function (key) { var value = args[key]; var regexp_argument = new RegExp("\\${" + key + "}"); str = str.replace(regexp_argument, value); }); return str; } lib_string.stance = stance; /** * @author fenris */ function make_url(_a) { var _b = _a === void 0 ? {} : _a, _c = _b["protocol"], protocol = _c === void 0 ? null : _c, _d = _b["host"], host = _d === void 0 ? null : _d, _e = _b["port"], port = _e === void 0 ? null : _e, _f = _b["path"], path = _f === void 0 ? null : _f, _g = _b["arguments"], arguments_ = _g === void 0 ? null : _g; var url = ""; // protocol { if (protocol != null) { url = protocol + ":" + url; } } // host { if (host != null) { url = url + "//" + host; } } // port { if (port != null) { url = url + ":" + port.toString(); } } // path { if (path != null) { url = url + "/" + path; } } // arguments { if (arguments_ != null) { var suffix = Object.keys(arguments_).map(function (key) { return key + "=" + arguments_[key]; }).join("&"); url = url + "?" + suffix; } } return url; } lib_string.make_url = make_url; })(lib_string || (lib_string = {})); /** * @desc adapters for old syntax */ var string_generate = lib_string.generate; var string_split = lib_string.split; var string_repeat = lib_string.repeat; var string_pad = lib_string.pad; var string_contains = lib_string.contains; var string_startsWith = lib_string.startsWith; var string_endsWith = lib_string.endsWith; var string_count_occourrences = lib_string.count_occourrences; var lib_string; (function (lib_string) { var pattern = /%([-+#0 ]*)([0-9]*)[\.]{0,1}([0-9]*)([\w]{1})/; var gpattern = /%([-+#0 ]*)([0-9]*)[\.]{0,1}([0-9]*)([\w]{1})/g; function split_format(format) { var tmp = format.match(pattern); if (tmp === null) return null; return { 'flags': tmp[1].split(""), 'width': Number(tmp[2]), 'precision': tmp[3] === '' ? null : Number(tmp[3]), 'specifier': tmp[4], 'string': format }; } function make_err(format, arg, should) { return ("[sprintf]" + " " + "argument for '" + format.string + "' has to be '" + should + "' but '" + arg + "' is '" + typeof arg + "'!"); } function test_arg(format, arg, should) { if (typeof arg !== should) { console.warn(make_err(format, arg, should)); return false; } return true; } function string_fill(str, char, len, left) { while (str.length < len) { if (left) { str += char; } else { str = char + str; } } return str; } /** * the known_parameters are used to parse the different identifiers for the welln known syntax: * flag width precision identifier * %{[0#+- ]}{[0-9]*}.{[0-9]*}[fFdiueEgGsoxXaAsn] * flags: * 0 - fill with '0' instead of ' ' if the string length < width * # - not implemented * - - left-justified -> fill on the right side to reach width * + - force using '+' on positive numbers * ' ' - add a single space before positive numbers * * identifiers * %f, %F - interpret given number as float, width: the minimal total width (fill with ' ' or '0' if the * resulting string is too short, precision: cut more then given decimal places * %d, %i, %u - interpret number as integer, decimal places will be cut. width: like float, precision: * fill with '0' on right side until length given in precision is reached * %e - interpret as float and write as scientifical number, width & precision like in float * %E - same es %e but uppercase 'E' * %g - use the shortest string of %f or %e * %G - use the shortest string of %E or %E * %s - simply print a string * %o - print the given number in octal notation * %x - print the given number in hex notation * %X - same as %x but with uppercase characters * %a - alias to %x * %A - alias to %X * %n - just print nothing * @type {{}} */ var known_params = {}; known_params["f"] = function (format, arg) { if (!test_arg(format, arg, "number")) return "Ø"; var tmp = Math.abs(arg); var sign = (arg < 0) ? -1 : 1; var tmp_result = null; if (format.precision !== null) { tmp = Math.floor(Math.pow(10, format.precision) * tmp) / Math.pow(10, format.precision); var tmp_ = (tmp * sign).toString().split("."); if (tmp_.length === 1) tmp_.push(""); tmp_[1] = string_fill(tmp_[1], "0", format.precision, true); tmp_result = tmp_.join("."); } else { tmp_result = (sign * tmp).toString(); } if ((format.flags.indexOf(" ") >= 0) && (arg >= 0)) { tmp_result = " " + tmp; } else if ((format.flags.indexOf("+") >= 0) && (arg >= 0)) { tmp_result = "+" + tmp; } tmp_result = string_fill(tmp, (format.flags.indexOf("0") >= 0) ? "0" : " ", format.width, (format.flags.indexOf("-") >= 0)); return tmp_result; }; known_params["F"] = known_params["f"]; known_params["d"] = function (format, arg) { if (!test_arg(format, arg, 'number')) return 'Ø'; var tmp = (((arg < 0 && format.specifier !== 'u') ? -1 : 1) * Math.floor(Math.abs(arg))).toString(); if ((format.specifier === 'd' || format.specifier === 'i') && format.flags.indexOf(' ') >= 0 && arg >= 0) { tmp = ' ' + tmp; } else if ((format.specifier === 'd' || format.specifier === 'i') && format.flags.indexOf('+') >= 0 && arg >= 0) { tmp = '+' + tmp; } tmp = string_fill(tmp, format.flags.indexOf('0') >= 0 ? '0' : ' ', format.width, format.flags.indexOf('-') >= 0); tmp = string_fill(tmp, '0', format.precision === null ? 0 : format.precision, false); return tmp; }; known_params["i"] = known_params["d"]; known_params["u"] = known_params["d"]; known_params["e"] = function (format, arg) { if (!test_arg(format, arg, 'number')) return 'Ø'; var tmp = arg.toExponential(format.precision === null ? undefined : format.precision).toString(); if (format.flags.indexOf(' ') >= 0 && arg >= 0) { tmp = ' ' + tmp; } else if (format.flags.indexOf('+') >= 0 && arg >= 0) { tmp = '+' + tmp; } tmp = string_fill(tmp, format.flags.indexOf('0') >= 0 ? '0' : ' ', format.width, format.flags.indexOf('-') >= 0); return tmp; }; known_params["E"] = function (format, arg) { return known_params["e"](format, arg).toUpperCase(); }; known_params["g"] = function (format, arg) { if (!test_arg(format, arg, 'number')) return 'Ø'; var tmpf = known_params["f"](format, arg); var tmpe = known_params["e"](format, arg); if (tmpf.length < tmpe.length) { return tmpf; } else { return tmpe; } }; known_params["G"] = function (format, arg) { return known_params["g"](format, arg).toUpperCase(); }; known_params["s"] = function (format, arg) { if (!test_arg(format, arg, 'string')) return 'o.O'; var tmp = format.precision !== null ? arg.substr(0, format.precision) : arg; tmp = string_fill(tmp, format.flags.indexOf('0') >= 0 ? '0' : ' ', format.width, format.flags.indexOf('-') >= 0); return tmp; }; known_params["o"] = function (format, arg) { if (!test_arg(format, arg, 'number')) return 'Ø'; var tmp = Math.floor(Math.round(Math.abs(arg))) * ((arg < 0) ? -1 : 1); return known_params["s"](format, tmp.toString(8)); }; known_params["x"] = function (format, arg) { if (!test_arg(format, arg, 'number')) return 'Ø'; var tmp = Math.floor(Math.round(Math.abs(arg))) * ((arg < 0) ? -1 : 1); return known_params["s"](format, tmp.toString(16)); }; known_params["a"] = known_params["x"]; known_params["X"] = function (format, arg) { if (!test_arg(format, arg, 'number')) return 'Ø'; return known_params["x"](format, arg).toUpperCase(); }; known_params["A"] = known_params["X"]; known_params["c"] = function (format, arg) { var tmp = ""; if (typeof arg === "number") { tmp = String.fromCharCode(arg); } else if ((typeof arg === "string") && (arg.length === 1)) { tmp = arg[0]; } else { console.warn(make_err(format, arg, "number|string") + " and if string it needs to have the length of 1!"); } return known_params["s"](format, tmp); }; known_params["n"] = function () { return ""; }; var decompose = function (chain, regexp) { var result = regexp.exec(chain); if (result == null) { return null; } else { var front = chain.substring(0, result.index); var back = chain.substring(result.index + result[0].length); return { "front": front, "match": result[0], "back": back }; } }; /** * an implementation of c sprintf * @param {string} string format string * @param {array} args arguments which should be filled into * @returns {string} */ lib_string.sprintf = function (input, args, original) { if (args === void 0) { args = []; } if (original === void 0) { original = null; } if (original == null) original = input; var components = decompose(input, pattern); if (components == null) { if (args.length > 0) { console.warn("[sprintf] superfluous arguments while formatting '" + original + "': ", args); } return input; } else { var arg; var rest; if (args.length > 0) { arg = args[0]; rest = args.slice(1); } else { console.warn("[sprintf] out of arguments while formatting '" + original + "'"); arg = null; rest = []; return input; } var fmt = split_format(components["match"]); return (components["front"] + known_params[fmt.specifier](fmt, arg) + lib_string.sprintf(components["back"], rest, original)); } }; /** * an implementation of c printf * @param {string} string format string * @param {array} args arguments which should be filled into * @returns {string} */ function printf(format, args) { console.log(lib_string.sprintf(format, args)); } lib_string.printf = printf; })(lib_string || (lib_string = {})); var sprintf = lib_string.sprintf; var printf = lib_string.printf; /** * @author neuc */ var strftime; (function (strftime) { var currentDate = new Date(); var days = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]; var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; function set_days(day_names) { days = day_names; } strftime.set_days = set_days; function set_months(month_names) { months = month_names; } strftime.set_months = set_months; // source: https://stackoverflow.com/questions/8619879/javascript-calculate-the-day-of-the-year-1-366 function helper_dayOfYear(date) { var start = new Date(date.getFullYear(), 0, 0); var diff = date - start; var oneDay = 1000 * 60 * 60 * 24; return Math.floor(diff / oneDay); } // source: http://weeknumber.net/how-to/javascript function helper_weekOfYear(date_) { var date = new Date(date_.getTime()); date.setHours(0, 0, 0, 0); // Thursday in current week decides the year. date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7); // January 4 is always in week 1. var week1 = new Date(date.getFullYear(), 0, 4); // Adjust to Thursday in week 1 and count number of weeks from date to week1. return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7); } function helper_englishWeekOfYear(date) { var nr = helper_weekOfYear(date); if (date.getDay() === 0) { nr = nr - 1; } return nr; } function set_currentDate(date) { currentDate = date; } strftime.set_currentDate = set_currentDate; function parse(format, date) { if (!date) { date = currentDate; } var ret = format; var re = new RegExp("%[a-z]", "gi"); var match; while (match = re.exec(format)) { ret = ret.replace(match[0], parse_segment(match[0], date)); } return ret; } strftime.parse = parse; function parse_segment(segment, date) { if (!date) { date = currentDate; } var hm_segments = { "%a": function () { return days[date.getDay()].slice(0, 3); }, "%A": function () { return days[date.getDay()]; }, "%b": function () { return days[date.getMonth()].slice(0, 3); }, "%B": function () { return days[date.getMonth()]; }, "%c": function () { return date.toLocaleString(); }, "%C": function () { return Math.floor((date.getFullYear()) / 100).toString(); }, "%d": function () { return sprintf("%02d", [date.getDate()]); }, "%D": function () { return parse("%m/%d/%y", date); }, "%e": function () { return sprintf("%2d", [date.getDate()]); }, "%F": function () { return parse("%Y-%m-%d", date); }, "%g": function () { return sprintf("%02d", [date.getFullYear() % 1000]); }, "%G": function () { return date.getFullYear().toString(); }, "%h": function () { return parse("%b", date); }, "%H": function () { return sprintf("%02d", [date.getHours()]); }, "%I": function () { return sprintf("%02d", [ (date.getHours() > 12 ? date.getHours() - 12 : date.getHours()) ]); }, "%j": function () { return sprintf("%03d", [helper_dayOfYear(date)]); }, "%m": function () { return sprintf("%02d", [date.getMonth() + 1]); }, "%M": function () { return sprintf("%02d", [date.getMinutes()]); }, "%n": function () { return "\n"; }, "%p": function () { return (date.getHours() > 12 ? "PM" : "AM"); }, "%r": function () { return parse("%I:%M:%S %p", date); }, "%R": function () { return parse("%H:%M", date); }, "%S": function () { return date.getSeconds().toString(); }, "%t": function () { return "\t"; }, "%T": function () { return parse("%H:%M:%S", date); }, "%u": function () { return sprintf("%02d", [(date.getDay() === 0 ? 7 : date.getDay())]); }, "%U": function () { return sprintf("%02d", [helper_englishWeekOfYear(date)]); }, "%V": function () { return sprintf("%02d", [helper_weekOfYear(date)]); }, "%w": function () { return sprintf("%02d", [date.getDay().toString()]); }, "%W": function () { return parse("%w", date); }, "%x": function () { return parse("%m/%d/%G", date); }, "%X": function () { return parse("%T", date); }, "%y": function () { return parse("%g", date); }, "%Y": function () { return parse("%G", date); }, "%z": function () { return date.getTimezoneOffset().toString(); }, "%Z": function () { return date.toUTCString().split(' ').pop(); }, "%%": function () { return "%"; } }; if (!(segment in hm_segments)) { throw "unknown format argument '" + segment + "'"; } return hm_segments[segment](); } })(strftime || (strftime = {})); function locale_date(date, ignore_error) { if (date === void 0) { date = new Date(); } if (ignore_error === void 0) { ignore_error = false; } if (!(date instanceof Date)) { if (!ignore_error) { throw new SyntaxError("date must be instance of Date"); } else { console.warn("'" + date + "' seems not to be instance of Date try to force convert."); var tmp = date; date = new Date(tmp); if ((date.toString() === "Invalid Date") || (!(date < new Date(0)) && !(date > new Date(0)))) { console.warn("conversion didn't work, returning default value"); return "Ø"; } } } var conf = global_config.get_value("date") || { "use_locale_date": true, "format_string": "%d.%m.%Y" }; if (conf.use_locale_date) { return date.toLocaleDateString(); } else { return strftime.parse(conf.format_string, date); } } ; var make_logger = (function () { var _loggers = {}; var make_logger = function (prefix, current_loglevel) { var log = []; var level = [ "LOG", "INFO", "WARNING", "DEBUG" ]; var logger = function (obj, lvl) { var txt = obj.txt || obj; if (lvl == void 0) lvl = 0; var date = new Date(); log.push({ "message": sprintf("%s [%s:%s] %s", [date.toString(), level[lvl], prefix, txt]), "timeStamp": +(date) }); if (lvl <= current_loglevel) { var msg = ["[" + prefix + "]", txt]; if (obj.arg) msg = ["[" + prefix + "]"].concat(Array.prototype.slice.call(obj.arg)); if (lvl === 0) console["_log"].apply(console, msg); else if (lvl === 1) console["_info"].apply(console, msg); else if (lvl === 2) console["_warn"].apply(console, msg); else if (lvl >= 3) console["_log"].apply(console, msg); } }; _loggers[prefix] = { "logger": logger, "log": log }; return logger; }; make_logger["loggers"] = _loggers; make_logger["complete_log"] = function () { var logs = Object.keys(_loggers) .reduce(function (p, c) { return [].concat(p, _loggers[c].log); }, []); logs.sort(function (x, y) { return ((x.timeStamp > y.timeStamp) ? -1 : +1); }); return logs.map(function (x, i, a) { return x.message; }); }; if (true) { var _log_all = function (log, lvl, next) { if (next === void 0) { next = function () { }; } return function () { var msg = []; for (var i = 0; i < arguments.length; i++) { if (typeof arguments[i] === "string") { msg.push(arguments[i]); } else { msg.push(JSON.stringify(arguments[i])); } } var obj = { txt: msg.join("\t"), arg: arguments }; log(obj, lvl); next(); }; }; { var __warn = make_logger("deprecated console.warn", 99); var __error = make_logger("deprecated console.error", 99); var __log = make_logger("deprecated console.log", 99); var __info = make_logger("deprecated console.info", 99); // bad ass console["_log"] = console.log; console["_error"] = console.error; console["_warn"] = console.warn; console["_info"] = console.info; /* console["log"] = _log_all(__log, 0); console["error"] = _log_all(__error, 2); console["warn"] = _log_all(__warn, 2); console["info"] = _log_all(__info, 0); */ } /* { make_logger["send_log"] = function(){ eml_log( function () { alert("fehlerbericht wurde gesendet!"); } ); }; var error_log = make_logger("global.error", 99); window.onerror = _log_all( error_log, 1, function(){ if (global_config == undefined) { return false; } if (global_config.report_error) { make_logger["send_log"](); } } ); } */ } return make_logger; })(); var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); ///<reference path="../../base/build/logic-decl.d.ts"/> ///<reference path="../../string/build/logic-decl.d.ts"/> /** * @author fenris */ var lib_xml; (function (lib_xml) { /** * @author fenris */ var class_node = (function () { function class_node() { } return class_node; }()); lib_xml.class_node = class_node; /** * @author fenris */ var class_node_text = (function (_super) { __extends(class_node_text, _super); /** * @author fenris */ function class_node_text(content) { var _this = _super.call(this) || this; _this.content = content; return _this; } /** * @author fenris */ class_node_text.prototype.compile = function (depth) { if (depth === void 0) { depth = 0; } return (lib_string.repeat("\t", depth) + this.content + "\n"); }; return class_node_text; }(class_node)); lib_xml.class_node_text = class_node_text; /** * @author fenris */ var class_node_comment = (function (_super) { __extends(class_node_comment, _super); /** * @author fenris */ function class_node_comment(content) { var _this = _super.call(this) || this; _this.content = content; return _this; } /** * @author fenris */ class_node_comment.prototype.compile = function (depth) { if (depth === void 0) { depth = 0; } return (lib_string.repeat("\t", depth) + "<!-- " + this.content + " -->" + "\n"); }; return class_node_comment; }(class_node)); lib_xml.class_node_comment = class_node_comment; /** * @author fenris */ var class_node_complex = (function (_super) { __extends(class_node_complex, _super); /** * @author fenris */ function class_node_complex(name, attributes, children) { if (attributes === void 0) { attributes = {}; } if (children === void 0) { children = []; } var _this = _super.call(this) || this; _this.name = name; _this.attributes = attributes; _this.children = children; return _this; } /** * @author fenris */ class_node_complex.prototype.compile = function (depth) { var _this = this; if (depth === void 0) { depth = 0; } var output = ""; var attributes = Object.keys(this.attributes).map(function (key) { return (" " + key + "=" + ("\"" + _this.attributes[key] + "\"")); }).join(""); output += (lib_string.repeat("\t", depth) + "<" + this.name + attributes + ">" + "\n"); this.children.forEach(function (child) { return (output += child.compile(depth + 1)); }); output += (lib_string.repeat("\t", depth) + "</" + this.name + ">" + "\n"); return output; }; return class_node_complex; }(class_node)); lib_xml.class_node_complex = class_node_complex; })(lib_xml || (lib_xml = {})); var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var lib_path; (function (lib_path) { /** * @author fenris */ var class_step = (function () { function class_step() { } return class_step; }()); lib_path.class_step = class_step; /** * @author fenris */ var class_step_stay = (function (_super) { __extends(class_step_stay, _super); function class_step_stay() { return _super !== null && _super.apply(this, arguments) || this; } /** * @author fenris */ class_step_stay.prototype.invert = function () { return (new class_step_stay()); }; /** * @author fenris */ class_step_stay.prototype.toString = function () { return "."; }; return class_step_stay; }(class_step)); lib_path.class_step_stay = class_step_stay; /** * @author fenris */ var class_step_back = (function (_super) { __extends(class_step_back, _super); function class_step_back() { return _super !== null && _super.apply(this, arguments) || this; } /** * @author fenris */ class_step_back.prototype.invert = function () { throw (new Error("impossible")); }; /** * @author fenris */ class_step_back.prototype.toString = function () { return ".."; }; return class_step_back; }(class_step)); lib_path.class_step_back = class_step_back; /** * @author fenris */ var class_step_regular = (function (_super) { __extends(class_step_regular, _super); /** * @author fenris */ function class_step_regular(name) { var _this = _super.call(this) || this; _this.name = name; return _this; } /** * @author fenris */ class_step_regular.prototype.invert = function () { return (new class_step_back()); }; /** * @author fenris */ class_step_regular.prototype.toString = function () { return this.name; }; return class_step_regular; }(class_step)); lib_path.class_step_regular = class_step_regular; /** * @author fenris */ function step_read(s) { switch (s) { case ".": { return (new class_step_stay()); // break; } case "..": { return (new class_step_back()); // break; } default: { return (new class_step_regular(s)); // break; } } } lib_path.step_read = step_read; })(lib_path || (lib_path = {})); ///<reference path="../../../plankton/object/build/logic-decl.d.ts"/> var lib_path; (function (lib_path) { /** * @author fenris */ var class_chain = (function () { /** * @author fenris */ function class_chain(steps) { if (steps === void 0) { steps = []; } this.steps = steps; } /** * @author fenris */ class_chain.splitter = function (system) { if (system === void 0) { system = "linux"; } return (object_fetch({ "linux": "/", "bsd": "/", "win": "\\" }, system, "/", 2)); }; /** * @desc removes superfluent steps from the chain, e.g. infix ".." * @author fenris */ class_chain.prototype.normalize = function () { var steps = this.steps; // filter "stay" { steps = steps.filter(function (step) { return (!(step instanceof lib_path.class_step_stay)); }); } // filter "regular-back" { var _loop_1 = function () { if (steps.length < 1) { return "break"; } else { var last_1 = steps[0]; var found = steps.slice(1).some(function (step, index) { if (step instanceof lib_path.class_step_back) { if (last_1 instanceof lib_path.class_step_regular) { steps.splice(index, 2); return true; } } last_1 = step; return false; }); if (!found) { return "break"; } } }; while (true) { var state_1 = _loop_1(); if (state_1 === "break") break; } } return (new class_chain(steps)); }; /** * @author fenris */ class_chain.prototype.invert = function () { return (new class_chain(this.steps.map(function (step) { return step.invert(); }))); }; /** * @author fenris */ class_chain.prototype.add = function (step) { return (new class_chain(this.steps.concat([step]))).normalize(); }; /** * @author fenris */ class_chain.prototype.extend = function (chain) { return (new class_chain(this.steps.concat(chain.steps))).normalize(); }; /** * @author fenris */ class_chain.prototype.as_string = function (system) { if (system === void 0) { system = "linux"; } var splitter = class_chain.splitter(system); return ((this.steps.length == 0) ? ("." + splitter) : this.steps.map(function (step) { return (step.toString() + splitter); }).join("")); }; /** * @author fenris */ class_chain.prototype.toString = function () { return this.as_string(); }; return class_chain; }()); lib_path.class_chain = class_chain; /** * @author fenris */ function chain_read(str, system) { if (system === void 0) { system = "linux"; } var splitter = class_chain.splitter(system); var parts = str.split(splitter); if (parts[parts.length - 1] == "") parts.pop(); return (new class_chain(parts.map(lib_path.step_read))); } lib_path.chain_read = chain_read; })(lib_path || (lib_path = {})); ///<reference path="../../../plankton/object/build/logic-decl.d.ts"/> var lib_path; (function (lib_path) { /** * @author fenris */ var class_location = (function () { /** * @author fenris */ function class_location(anchor, chain) { this.anchor = anchor; this.chain = chain; } /** * @author fenris */ class_location.anchorpattern = function (system) { if (system === void 0) { system = "linux"; } return (object_fetch({ "linux": new RegExp("/"), "bsd": new RegExp("/"), "win": new RegExp("[A-Z]:\\\\>") }, system, new RegExp("/"), 1)); }; /** * @desc [accessor] * @author fenris */ class_location.prototype.is_absolute = function () { return (this.anchor != null); }; /** * @author fenris */ class_location.prototype.normalize = function () { return (new class_location(this.anchor, this.chain.normalize())); }; /** * @author fenris */ class_location.prototype.extend = function (chain) { return (new class_location(this.anchor, this.chain.extend(chain))); }; /** * @desc [accessor] * @author fenris */ class_location.prototype.relocate = function (location) { if (this.is_absolute()) { return (new class_location(this.anchor, this.chain)); } else { return location.extend(this.chain); } }; /** * @author fenris */ class_location.prototype.go_thither = function () { // console.error(">>", this.toString()); process.chdir(this.toString()); }; /** * @author fenris */ class_location.prototype.expedition = function (core) { var that = this; var current = location_read(process.cwd()); function begin() { // (new class_message("changing directory to '" + that.toString() + "'")).stderr(); that.go_thither(); } function end() { // (new class_message("changing directory to '" + current.toString() + "'")).stderr(); current.go_thither(); } begin(); core(end); }; /** * @author fenris */ class_location.prototype.as_string = function (system) { if (system === void 0) { system = "linux"; } return (((this.anchor != null) ? this.anchor : "") + this.chain.as_string(system)); }; /** * @author fenris */ class_location.prototype.toString = function () { return this.as_string(); }; /** * @author fenris */ class_location.current = function () { // return class_location.read(process.cwd()); return location_read(process.cwd()); }; /** * @author fenris */ class_location.tempfolder = function (system) { if (system === void 0) { system = "linux"; } return (object_fetch({ "linux": new class_location("/", new lib_path.class_chain([new lib_path.class_step_regular("tmp")])), "bsd": new class_location("/", new lib_path.class_chain([new lib_path.class_step_regular("tmp")])), "win": new class_location(null, new lib_path.class_chain([new lib_path.class_step_regular("%TEMP%")])) }, system, null, 2)); }; return class_location; }()); lib_path.class_location = class_location; /** * @author fenris */ function location_read(str, system) { if (system === void 0) { system = "linux"; } var regexp = class_location.anchorpattern(system); var matching = regexp.exec(str); if ((matching == null) || (matching.index > 0)) { return (new class_location(null, lib_path.chain_read(str, system))); } else { return (new class_location(matching[0], lib_path.chain_read(str.slice(matching[0].length), system))); } } lib_path.location_read = location_read; })(lib_path || (lib_path = {})); var lib_path; (function (lib_path) { /** * @author fenris */ var class_filepointer = (function () { /** * @author fenris */ function class_filepointer(location, filename) { this.location = location; this.filename = filename; } /** * @author fenris */ class_filepointer.prototype.normalize = function () { return (new class_filepointer(this.location.normalize(), this.filename)); }; /** * @author fenris */ class_filepointer.prototype.foo = function (filepointer) { return (new class_filepointer(this.location.extend(filepointer.location.chain), filepointer.filename)); }; /** * @desc [accessor] * @author fenris */ class_filepointer.prototype.relocate = function (location) { return (new class_filepointer(this.location.relocate(location), this.filename)); }; /** * @author fenris */ class_filepointer.prototype.as_string = function (system) { if (system === void 0) { system = "linux"; } return (this.location.as_string(system) /* + "/"*/ + ((this.filename == null) ? "" : this.filename)); }; /** * @author fenris */ class_filepointer.prototype.toString = function () { return this.as_string(); }; return class_filepointer; }()); lib_path.class_filepointer = class_filepointer; /** * @author fenris */ function filepointer_read(str, system) { if (system === void 0) { system = "linux"; } var splitter = lib_path.class_chain.splitter(system); var parts = str.split(splitter); var last = parts[parts.length - 1]; if (last == "") { return (new class_filepointer(lib_path.location_read(parts.join(splitter), system), null)); } else { return (new class_filepointer(lib_path.location_read(parts.slice(0, parts.length - 1).join(splitter), system), last)); } } lib_path.filepointer_read = filepointer_read; })(lib_path || (lib_path = {})); ///<reference path="../../call/build/logic-decl.d.ts"/> var lib_file; (function (lib_file) { /** * @author fenris */ class class_file_abstract { /** * @desc reads a json file * @author fenris */ read_json(path) { return ((resolve, reject) => { lib_call.executor_chain({}, [ state => (resolve_, reject_) => { this.read(path)(content => { state.content = content; resolve_(state); }, reject_); }, state => (resolve_, reject_) => { let error; try { state.data = JSON.parse(state.content); error = null; } catch (exception) { error = new class_error("invalid json '" + path + "'", [exception]); } if (error == null) { resolve_(state); } else { reject_(error); } }, ])(state => resolve(state.data), reject); }); } /** * @desc writes a json file * @author fenris */ write_json(path, data) { return this.write(path, JSON.stringify(data, undefined, "\t")); } } lib_file.class_file_abstract = class_file_abstract; })(lib_file || (lib_file = {})); ///<reference path="../../call/build/logic-decl.d.ts"/> var lib_file; (function (lib_file) { /** * @author fenris */ class class_file_node extends lib_file.class_file_abstract { /** * @author maspr */ determine_handler(path) { if (/^https?:\/\//.test(path)) { return "http"; } else { return "file"; } } /** * @override * @author fenris,maspr * @todo clear up if http(s)-handling belongs here or not */ read(path, skip_error = false) { switch (this.determine_handler(path)) { case "file": { let nm_fs = require("fs"); return ((resolve, reject) => { nm_fs.readFile(path, { "encoding": "utf8", "flag": "r", }, (error, content) => { if (error == null) { resolve(content); } else { reject(error); } }); }); } break; case "http": { return (resolve, reject) => { let nm_http = require("http"); let nm_https = require("https"); let nm_url = require("url"); let parsed_url = nm_url.parse(path, false, true); let client = (parsed_url.protocol == "https:") ? nm_https : nm_http; let default_port = ((parsed_url.protocol == "https:") ? 443 : 80); let options = { hostname: parsed_url.hostname, port: parsed_url.port || default_port, path: parsed_url.path, method: "GET" }; let req = client.request(options, (res) => { let data = ""; // @todo res.on("data", (chunk) => { data += chunk; }); res.on("end", () => { resolve(data); }); }); req.end(); req.on("error", (error) => { reject(error); }); }; } break; default: { return ((resolve, reject) => reject(new Error("unhandled protocol"))); } } } /** * @override * @author fenris */ write(path, content) { let nm_fs = require("fs"); return ((resolve, reject) => { nm_fs.writeFile(path, content, { "encoding": "utf8", "flag": "w", }, (error) => { if (error == null) { resolve(undefined); } else { reject(error); } }); }); } } lib_file.class_file_node = class_file_node; })(lib_file || (lib_file = {})); ///<reference path="../../call/build/logic-decl.d.ts"/> var lib_file; (function (lib_file) { /** * @author fenris * @todo move to a dedicated lib (e.g. "http", "transport", etc.) */ function ajax({ "target": target /*: string*/, "data": data /*: {[key : string] : string}*/ = null, "method": method /* : string*/ = "GET" }) { method = method.toLowerCase(); return ((resolve, reject) => { let datastring = ((data == null) ? null : Object.keys(data).map(key => `${key}=${data[key]}`).join("&")); let suffix = ((method == "get") ? ("?" + datastring) : ""); let sending = ((method == "get") ? null : datastring); let request = new XMLHttpRequest(); request.open(method.toUpperCase(), target + suffix, true); request.onreadystatechange = function () { if (request.readyState === 4) { if (request.status === 200) { resolve(request.responseText); } else { reject(new Error("XMLHttpRequest failed")); } } }; request.send(sending); }); } /** * @author fenris */ class class_file_web extends lib_file.class_file_abstract { /** * @override * @author fenris */ read(path, skip_error = false) { return ((resolve, reject) => { ajax({ "target": path, "method": "GET", })(resolve, reason => (skip_error ? resolve(null) : reject(reason))); }); } /** * @override * @author fenris */ write(path, content) { return ((resolve, reject) => { reject(new Error("not implemented / not possible")); }); } } lib_file.class_file_web = class_file_web; })(lib_file || (lib_file = {})); ///<reference path="../../base/build/logic-decl.d.ts"/> ///<reference path="../../call/build/logic-decl.d.ts"/> var lib_file; (function (lib_file) { /** * @desc selects the implementation which fits for the detected environment * @author fenris */ function auto() { let environment = lib_base.environment(); switch (environment) { case "node": { return (new lib_file.class_file_node()); break; } case "web": { return (new lib_file.class_file_web()); break; } default: { throw (new Error(`no implementation for environment '${environment}'`)); break; } } } lib_file.auto = auto; /** * @author fenris */ class class_file extends lib_file.class_file_abstract { /** * @author fenris */ constructor() { super(); this.core = auto(); } /** * @override * @author fenris */ read(path, skip_error = false) { return this.core.read(path, skip_error); } /** * @override * @author fenris */ write(path, content) { return this.core.write(path, content); } } lib_file.class_file = class_file; /** * @author fenris */ var instance = auto(); /** * @author fenris */ function read(path, skip_error = false) { return instance.read(path, skip_error); } lib_file.read = read; /** * @author fenris */ function write(path, content) { return instance.write(path, content); } lib_file.write = write; /** * @author fenris */ function read_json(path) { return instance.read_json(path); } lib_file.read_json = read_json; /** * @author fenris */ function write_json(path, data) { return instance.write_json(path, data); } lib_file.write_json = write_json; })(lib_file || (lib_file = {})); var lib_dot; (function (lib_dot) { /** * @author fenris */ function make_line(head, attributes) { let list = ((object => { let list = []; Object.keys(object).forEach(key => (list.push({ "key": key, "value": object[key] }))); return list; })(attributes)); let attributes_ = list .filter(entry => (entry.value != null)) .map(entry => `${entry.key}="${entry.value}"`) .join(", "); return (`${head} [${attributes_}];`); } /** * @author fenris */ function format_line(line) { return `\t${line}\n`; } /** * @author fenris */ function format_lines(lines) { return lines.map(format_line).join(""); } /** * @author fenris */ function generate_graph(graphdefinition) { let attributes = {}; if (graphdefinition.fontname != undefined) { attributes["fontname"] = graphdefinition.fontname; } if (graphdefinition.dimensions != undefined) { attributes["dimen"] = graphdefinition.dimensions.toFixed(0); } if (graphdefinition.bgcolor != undefined) { attributes["bgcolor"] = graphdefinition.bgcolor; } return format_lines([make_line("graph", attributes)] .concat([])); } /** * @author fenris */ function generate_nodes(nodedefinition) { return format_lines([make_line("node", nodedefinition.head)] .concat(nodedefinition.list.map(node => make_line(node.id, node.attributes)))); } /** * @author fenris */ function generate_edges(edgedefinition, directed) { let connector = (directed ? "->" : "--"); return format_lines([make_line("edge", edgedefinition.head)] .concat(edgedefinition.list.map(edge => make_line(`${edge.id_from} ${connector} ${edge.id_to}`, edge.attributes)))); } /** * @author fenris */ function generate({ "name": name = null, "directed": directed = true, "common": graphdefinition = {}, "nodes": nodedefinition, "edges": edgedefinition } = {}) { let output = ""; output += ((directed ? "digraph" : "graph") + ((name == null) ? "" : (" " + name)) + "\n"); output += `{\n`; output += generate_graph(graphdefinition); output += `\t\n`; output += generate_nodes(nodedefinition); output += `\t\n`; output += generate_edges(edgedefinition, directed); output += `}\n`; return output; } lib_dot.generate = generate; })(lib_dot || (lib_dot = {})); var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var lib_structures; (function (lib_structures) { /** * @author fenris */ var class_pair = (function () { /** * @author fenris */ function class_pair(first, second) { this.first = first; this.second = second; } /** * @desc [accessor] [getter] * @author fenris */ class_pair.prototype.first_get = function () { return this.first; }; /** * @desc [accessor] [getter] * @author fenris */ class_pair.prototype.second_get = function () { return this.second; }; /** * @desc [mutator] [setter] * @author fenris */ class_pair.prototype.first_set = function (first) { this.first = first; }; /** * @desc [mutator] [setter] * @author fenris */ class_pair.prototype.second_set = function (second) { this.second = second; }; /** * @desc [accessor] * @author fenris */ class_pair.prototype.swap = function () { return (new class_pair(this.second, this.first)); }; /** * @desc [accessor] * @author fenris */ class_pair.prototype.transform = function (transform_first, transform_second) { return (new class_pair(transform_first(this.first), transform_second(this.second))); }; /** * @desc [accessor] [implementation] * @author fenris */ class_pair.prototype._clone = function () { return (new class_pair(instance_clone(this.first), instance_clone(this.second))); }; /** * @desc [accessor] [implementation] * @author fenris */ class_pair.prototype._hash = function () { return ("pair_" + instance_hash(this.first) + "_" + instance_hash(this.second) + ""); }; /** * @desc [accessor] [implementation] * @author fenris */ class_pair.prototype._collate = function (pair) { return (instance_collate(this.first, pair.first) && instance_collate(this.second, pair.second)); }; /** * @desc [accessor] [implementation] * @author fenris */ class_pair.prototype._show = function () { return ("(" + instance_show(this.first) + "," + instance_show(this.second) + ")"); }; return class_pair; }()); lib_structures.class_pair = class_pair; })(lib_structures || (lib_structures = {})); var lib_structures; (function (lib_structures) { /** * @author fenris */ var class_set = (function () { /** * @author fenris */ function class_set(elements_, equality /*<type_element>*/) { if (elements_ === void 0) { elements_ = []; } if (equality === void 0) { equality = instance_collate; } /*<type_element>*/ var _this = this; this.elements = []; this.equality = equality; elements_.forEach(function (element) { return _this.add(element); }); } /** * @desc [accessor] * @author fenris */ class_set.prototype.size = function () { return this.elements.length; }; /** * @desc [accessor] * @author fenris */ class_set.prototype.has = function (element) { var _this = this; return this.elements.some(function (element_) { return _this.equality(element, element_); }); }; /** * @desc [mutator] * @author fenris */ class_set.prototype.add = function (element) { if (!this.has(element)) { this.elements.push(element); } }; /** * @desc [mutator] * @author fenris */ class_set.prototype.pop = function () { if (this.elements.length == 0) { return (new class_nothing()); } else { return (new class_just(this.elements.pop())); } }; /** * @desc [accessor] * @author fenris */ class_set.prototype.forEach = function (function_) { this.elements.forEach(function (element) { return function_(element); }); }; /** * @desc [accessor] * @author fenris */ class_set.prototype.map = function (transformator) { return (new class_set(this.elements.map(transformator))); }; /** * @desc [accessor] * @author fenris */ class_set.prototype.filter = function (predicate) { return (new class_set(this.elements.filter(predicate))); }; /** * @desc [accessor] * @author fenris */ class_set.prototype.dump = function () { return this.elements; }; /** * @desc [accessor] * @author fenris */ class_set.prototype.subset = function (set) { var _this = this; return set.elements.every(function (element) { return _this.has(element); }); }; /** * @desc [accessor] * @author fenris */ class_set.prototype.superset = function (set) { return this.elements.every(function (element) { return set.has(element); }); }; /** * @desc [accessor] * @author fenris */ class_set.prototype.equals = function (set) { return (this.subset(set) && this.superset(set)); }; /** * @desc [accessor] * @author fenris */ class_set.prototype.toString = function () { return ("{" + this.elements.map(instance_show /*<type_element>*/).join(",") + "}"); }; /** * @desc [accessor] * @author fenris */ class_set.prototype.empty = function () { return (this.elements.length == 0); }; /** * @desc [accessor] * @author fenris */ class_set.prototype.union = function (set) { var set_ = new class_set([], this.equality); this.elements.forEach(function (element) { return set_.add(element); }); set.elements.forEach(function (element) { return set_.add(element); }); return set_; }; /** * @desc [accessor] * @author fenris */ class_set.prototype.intersection = function (set) { var set_ = new class_set([], this.equality); this.elements .filter(function (element) { return set.has(element); }) .forEach(function (element) { return set_.add(element); }); return set_; }; /** * @desc [accessor] * @author fenris */ class_set.prototype.difference = function (set) { var set_ = new class_set([], this.equality); this.elements .filter(function (element) { return !set.has(element); }) .forEach(function (element) { return set_.add(element); }); return set_; }; /** * @desc [accessor] * @author fenris */ class_set.prototype.symmetric_difference = function (set) { return this.difference(set).union(set.difference(this)); }; /** * @desc [accessor] [implementation] * @author fenris */ class_set.prototype._collate = function (set) { return this.equals(set); }; /** * @desc [accessor] [implementation] * @author fenris */ class_set.prototype._show = function () { return this.toString(); }; /** * @author fenris */ class_set.union_all = function (sets) { return (sets.reduce(function (x, y) { return ((x == null) ? y : x.union(y)); }, null)); }; /** * @author fenris */ class_set.intersection_all = function (sets) { return (sets.reduce(function (x, y) { return ((x == null) ? y : x.intersection(y)); }, null)); }; return class_set; }()); lib_structures.class_set = class_set; })(lib_structures || (lib_structures = {})); var lib_structures; (function (lib_structures) { /** * @author fenris */ var class_map_abstract = (function () { function class_map_abstract() { } /** * @desc [mutator] [syntactic sugar] * @author fenris */ class_map_abstract.prototype.del = function (key) { return this["delete"](key); }; /** * @desc [accessor] * @author fenris */ class_map_abstract.prototype.map = function (transformator) { var map_ = this.spawn(); this.forEach(function (value, key) { var value_ = transformator(value); map_.set(key, value_); }); return map_; }; /** * @desc [accessor] * @author fenris */ class_map_abstract.prototype.filter = function (predicate) { var map_ = this.spawn(); this.forEach(function (value, key) { if (predicate(value)) { map_.set(key, value); } }); return map_; }; /** * @desc [accessor] * @author fenris */ class_map_abstract.prototype.pairs = function () { var set = new lib_structures.class_set(); this.forEach(function (value, key) { set.add(new lib_structures.class_pair(key, value)); }); return set; }; /** * @desc [accessor] * @author fenris */ class_map_abstract.prototype.keys = function () { var set = new lib_structures.class_set(); this.forEach(function (value, key) { set.add(key); }); return set; }; /** * @desc [accessor] * @author fenris */ class_map_abstract.prototype.values = function () { var set = new lib_structures.class_set(); this.forEach(function (value, key) { set.add(value); }); return set; }; return class_map_abstract; }()); lib_structures.class_map_abstract = class_map_abstract; /** * @author fenris */ var class_map_simple = (function (_super) { __extends(class_map_simple, _super); /** * @author fenris */ function class_map_simple() { var _this = _super.call(this) || this; _this.object = {}; return _this; } /** * @desc [accessor] * @author fenris */ class_map_simple.from_object = function (object) { var map = new class_map_simple(); Object.keys(object).forEach(function (key) { return map.set(key, object[key]); }); return map; }; /** * @override * @author fenris */ class_map_simple.prototype.spawn = function () { return (new class_map_simple()); }; /** * @override * @author fenris */ class_map_simple.prototype.has = function (key) { return (key in this.object); }; /** * @override * @author fenris */ class_map_simple.prototype.get = function (key, strict, fallback) { if (this.has(key)) { return this.object[key]; } else { if (strict) { throw (new Error("key not found")); } else { return fallback; } } }; /** * @override * @author fenris */ class_map_simple.prototype.set = function (key, value) { this.object[key] = value; }; /** * @override * @author fenris */ class_map_simple.prototype.clear = function () { this.object = {}; }; /** * @override * @author fenris */ class_map_simple.prototype["delete"] = function (key) { delete this.object[key]; }; /** * @override * @author fenris */ class_map_simple.prototype.forEach = function (function_) { var _this = this; Object.keys(this.object).forEach(function (key) { var value = _this.object[key]; function_(value, key); }); }; /** * @desc [accessor] * @author fenris * @todo implement */ /* public equals(map : class_map_simple<type_value>) : boolean { } */ /** * @desc [accessor] * @author fenris */ class_map_simple.prototype.toString = function () { return ("{" + this.pairs().map(function (pair) { return instance_show(pair.first_get()) + " -> " + instance_show(pair.second_get()); }).dump().join(", ") + "}"); }; /** * @desc [accessor] [implementation] * @author fenris */ class_map_simple.prototype._clone = function () { return (this.map(function (value) { return instance_clone(value); })); }; /** * @desc [accessor] [implementation] * @author fenris */ class_map_simple.prototype._show = function () { return this.toString(); }; return class_map_simple; }(class_map_abstract)); lib_structures.class_map_simple = class_map_simple; /** * @author fenris */ var class_map_equality = (function (_super) { __extends(class_map_equality, _super); /** * @author fenris */ function class_map_equality(collate /*<type_key>*/) { if (collate === void 0) { collate = instance_collate; } /*<type_key>*/ var _this = _super.call(this) || this; _this.pairs_ = []; _this.collate = collate; return _this; } /** * @override * @author fenris */ class_map_equality.prototype.spawn = function () { return (new class_map_equality(this.collate)); }; /** * @override * @author fenris */ class_map_equality.prototype.has = function (key) { var _this = this; return this.pairs_.some(function (pair) { return _this.collate(pair.first_get(), key); }); }; /** * @override * @author fenris */ class_map_equality.prototype.get = function (key, strict, fallback) { var _this = this; if (strict === void 0) { strict = false; } if (fallback === void 0) { fallback = null; } var value; var found = this.pairs_.some(function (pair) { if (_this.collate(pair.first_get(), key)) { value = pair.second_get(); return true; } else { return false; } }); if (!found) { if (strict) { throw (new Error("key not found")); } else { value = fallback; } } return value; }; /** * @override * @author fenris */ class_map_equality.prototype.set = function (key, value) { var _this = this; var found = this.pairs_.some(function (pair) { if (_this.collate(pair.first_get(), key)) { pair.second_set(value); return true; } else { return false; } }); if (!found) { this.pairs_.push(new lib_structures.class_pair(key, value)); } }; /** * @override * @author fenris */ class_map_equality.prototype.clear = function () { this.pairs_ = []; }; /** * @override * @author fenris */ class_map_equality.prototype["delete"] = function (key) { var _this = this; var index; var found = this.pairs_.some(function (pair, index_) { if (_this.collate(pair.first_get(), key)) { index = index_; return true; } else { return false; } }); if (found) { this.pairs_.splice(index, 1); } }; /** * @desc [accessor] * @author fenris */ class_map_equality.prototype.forEach = function (function_) { this.pairs_.forEach(function (pair) { return function_(pair.second_get(), pair.first_get()); }); }; return class_map_equality; }(class_map_abstract)); lib_structures.class_map_equality = class_map_equality; /** * @author fenris */ var class_map = (function (_super) { __extends(class_map, _super); function class_map() { return _super !== null && _super.apply(this, arguments) || this; } return class_map; }(class_map_equality)); lib_structures.class_map = class_map; })(lib_structures || (lib_structures = {})); var lib_structures; (function (lib_structures) { /** * @author fenris */ var class_store = (function () { function class_store() { } return class_store; }()); lib_structures.class_store = class_store; })(lib_structures || (lib_structures = {})); var lib_structures; (function (lib_structures) { /** * @author fenris */ var class_stack = (function (_super) { __extends(class_stack, _super); /** * @author fenris */ function class_stack() { var _this = _super.call(this) || this; _this.elements = []; return _this; } /** * @override * @author fenris */ class_stack.prototype.size = function () { return this.elements.length; }; /** * @override * @author fenris */ class_stack.prototype.scan = function () { if (this.size() == 0) { throw (new Error("empty")); } else { return this.elements[this.elements.length - 1]; } }; /** * @override * @author fenris */ class_stack.prototype.give = function (element) { this.elements.push(element); }; /** * @override * @author fenris */ class_stack.prototype.take = function () { var element = this.scan(); this.elements.pop(); return element; }; return class_stack; }(lib_structures.class_store)); lib_structures.class_stack = class_stack; })(lib_structures || (lib_structures = {})); var lib_structures; (function (lib_structures) { /** * @author fenris */ var class_queue = (function (_super) { __extends(class_queue, _super); /** * @author fenris */ function class_queue() { var _this = _super.call(this) || this; _this.elements = []; return _this; } /** * @override * @author fenris */ class_queue.prototype.size = function () { return this.elements.length; }; /** * @override * @author fenris */ class_queue.prototype.scan = function () { if (this.size() == 0) { throw (new Error("empty")); } else { return this.elements[0]; } }; /** * @override * @author fenris */ class_queue.prototype.give = function (element) { this.elements.push(element); }; /** * @override * @author fenris */ class_queue.prototype.take = function () { var element = this.scan(); this.elements.shift(); return element; }; return class_queue; }(lib_structures.class_store)); lib_structures.class_queue = class_queue; })(lib_structures || (lib_structures = {})); var lib_structures; (function (lib_structures) { /** * @author fenris */ var class_graph = (function () { /** * @author fenris */ function class_graph(equality, nodes, edges) { if (equality === void 0) { equality = null; } if (nodes === void 0) { nodes = []; } if (edges === void 0) { edges = []; } if (equality == null) { // equality = ((node1, node2) => lib_trait.call("collatable", "collate", {"kind": "auto"}, {"first": node1, "second": node2})); equality = instance_collate /*<type_node>*/; } this.equality = equality; this.nodes = nodes; this.edges = edges; } /** * @desc [accessor] [getter] * @author fenris */ class_graph.prototype.nodes_get = function () { return this.nodes; }; /** * @desc [mutator] * @author fenris */ class_graph.prototype.add_node = function (node) { this.nodes.push(node); }; /** * @desc [accessor] [getter] * @author fenris */ class_graph.prototype.edges_get = function () { return this.edges; }; /** * @desc [mutator] * @author fenris */ class_graph.prototype.add_edge = function (edge) { this.edges.push(edge); }; /** * @desc [accessor] * @author fenris */ class_graph.prototype.has = function (node) { var _this = this; return this.nodes.some(function (node_) { return _this.equality(node, node_); }); }; /** * @desc [accessor] * @author fenris */ class_graph.prototype.outgoing = function (node) { var _this = this; return this.edges.filter(function (edge) { return _this.equality(edge.from, node); }); }; /** * @desc [accessor] * @author fenris */ class_graph.prototype.incoming = function (node) { var _this = this; return this.edges.filter(function (edge) { return _this.equality(edge.to, node); }); }; /** * @desc [accessor] * @author fenris */ class_graph.prototype.without = function (pivot) { var _this = this; return (new class_graph(this.equality, this.nodes.filter(function (node) { return (!_this.equality(node, pivot)); }), this.edges.filter(function (edge) { return ((!_this.equality(edge.from, pivot)) && (!_this.equality(edge.to, pivot))); }))); }; /** * @desc [accessor] returns the topologic sorting of the nodes (if it exists) * @author fenris */ class_graph.prototype.topsort = function () { var _this = this; var graph = this; if (graph.nodes.length == 0) { return []; } else { var pivot_1; var found = graph.nodes.some(function (node) { var count = graph.edges.filter(function (edge) { return _this.equality(edge.to, node); }).length; if (count == 0) { pivot_1 = node; return true; } else { // console.info("'" + String(node) + "' has " + count.toString() + " incoming edges"); return false; } }); if (found) { return [pivot_1].concat(graph.without(pivot_1).topsort()); } else { throw (new Error("circular dependencies found")); } } }; /** * @desc [accessor] returns the reduced version of a graph representing an order relation (implicit transitivity) * @author fenris */ class_graph.prototype.hasse = function () { var _this = this; return (new class_graph(this.equality, this.nodes, this.edges.filter(function (edge) { var reachable = (_this.outgoing(edge.from).map(function (edge_) { return edge_.to; }) .map(function (node) { return _this.outgoing(node).map(function (edge_) { return edge_.to; }); }) .reduce(function (x, y) { return x.concat(y); }, [])); return (!reachable.some(function (node) { return _this.equality(node, edge.to); })); }))); }; /** * @author fenris */ class_graph.prototype.output_dot = function (_a) { var _this = this; var _b = _a === void 0 ? {} : _a, _c = _b["extract_id"], extract_id = _c === void 0 ? null : _c, _d = _b["extract_label"], extract_label = _d === void 0 ? null : _d; var index = function (node) { var i = -1; var found = _this.nodes.some(function (node_, i_) { if (_this.equality(node, node_)) { i = i_; return true; } else { return false; } }); return i; }; if (extract_id == null) { // instance_hash extract_id = (function (node) { return index(node).toFixed(0); }); } if (extract_label == null) { extract_label = instance_show; } var nodeid = function (node) { return ("node_" + extract_id(node)); }; return (lib_dot.generate({ "common": { "fontname": "Monospace" }, "nodes": { "head": { "fontname": "Monospace", "style": "filled", "fillcolor": "0.35+0.6+0.8" }, "list": this.nodes_get() .map(function (node, index) { return { "id": nodeid(node), "attributes": { "label": extract_label(node) } }; }) }, "edges": { "head": { "fontname": "Monospace" }, "list": this.edges_get() .map(function (edge, index) { return { "id_from": nodeid(edge.from), "id_to": nodeid(edge.to), "attributes": {} }; }) } })); }; return class_graph; }()); lib_structures.class_graph = class_graph; })(lib_structures || (lib_structures = {})); var lib_args; (function (lib_args) { /** * @author fenris */ var class_argument = (function () { /** * @author fenris */ function class_argument(_a) { var name = _a["name"], _b = _a["type"], type = _b === void 0 ? "string" : _b, _c = _a["default"], default_ = _c === void 0 ? null : _c, _d = _a["info"], info = _d === void 0 ? null : _d, _e = _a["mode"], mode = _e === void 0 ? "replace" : _e, _f = _a["kind"], kind = _f === void 0 ? "positional" : _f, _g = _a["parameters"], parameters = _g === void 0 ? {} : _g, _h = _a["hidden"], hidden = _h === void 0 ? false : _h; this.name = name; this.type = type; this.default_ = default_; this.info = info; this.mode = mode; this.kind = kind; this.parameters = parameters; this.hidden = hidden; if (!this.check()) { throw (new Error("invalid argument-setup")); } } /** * @author fenris */ class_argument.prototype.check = function () { var _this = this; return [ function () { return ((!(_this.kind == "volatile")) || (("indicators_long" in _this.parameters) && (_this.parameters["indicators_long"].length >= 0))); }, ].every(function (condition) { return condition(); }); }; /** * @author fenris */ class_argument.prototype.name_get = function () { return this.name; }; /** * @author fenris */ class_argument.prototype.type_get = function () { return this.type; }; /** * @author fenris */ class_argument.prototype.default_get = function () { return this.default_; }; /** * @author fenris */ class_argument.prototype.kind_get = function () { return this.kind; }; /** * @author fenris */ class_argument.prototype.parameters_get = function () { return this.parameters; }; /** * @author fenris */ class_argument.prototype.hidden_get = function () { return this.hidden; }; /** * @author fenris */ class_argument.prototype.toString = function () { return "<" + this.name + ">"; }; /** * @author fenris */ class_argument.prototype.indicator_main = function () { if (this.kind == "volatile") { return this.parameters["indicators_long"][0]; } else { return null; } }; /** * @author fenris */ class_argument.prototype.pattern_value = function () { switch (this.type) { case "boolean": { return "false|true"; break; } case "int": { return "[0-9]+"; break; } case "float": { return "\\d*(?:\\.\\d+)?"; break; } case "string": { return "\\S+"; break; } default: { throw (new Error("unhandled type " + this.type)); break; } } }; /** * @author fenris */ class_argument.prototype.extract = function (raw) { switch (this.type) { case "boolean": { return (raw != "false"); break; } case "int": { return parseInt(raw); break; } case "float": { return parseFloat(raw); break; } case "string": { return raw; break; } default: { throw (new Error("unhandled type " + this.type)); break; } } }; /** * @author fenris */ class_argument.prototype.assign = function (data, raw) { var value = this.extract(raw); switch (this.mode) { case "replace": { data[this.name] = value; break; } case "accumulate": { /* if (! (this.name in data)) { data[this.name] = []; } */ data[this.name].push(value); break; } default: { throw (new Error("unhandled mode " + this.mode)); } } }; /** * @author fenris */ class_argument.prototype.make = function (data) { var value = data[this.name]; return value.toString(); }; /** * @author fenris */ class_argument.prototype.generate_help = function () { var output = ""; { var line = ""; line += "\t"; line += "<" + this.name + ">"; line += "\n"; output += line; } { var line = ""; line += "\t\t"; var infotext = ((this.info == null) ? "(no info available)" : this.info); line += infotext; if ((this.type != "boolean") && (this.default_ != null)) { line += "; default: " + this.default_.toString(); } line += "\n"; output += line; } return output; }; return class_argument; }()); lib_args.class_argument = class_argument; })(lib_args || (lib_args = {})); ///<reference path="../../base/build/logic-decl.d.ts"/> var lib_args; (function (lib_args) { /** * @author fenris */ var settings = { "environment": { "cli": { "symbols": { "delimiter": " ", "prefix": "--", "assignment": "=" } }, "url": { "symbols": { "delimiter": "&", "prefix": "", "assignment": "=" } } } }; /** * @author fenris */ lib_args.verbosity = 0; /** * @author fenris * @todo check validity */ var class_handler = (function () { /** * @author fenris */ function class_handler(arguments_) { this.arguments_ = arguments_; } /** * @author fenris */ class_handler.prototype.filter = function (kind) { return this.arguments_.filter(function (argument) { return (argument.kind_get() == kind); }); }; /** * @author fenris */ class_handler.prototype.read = function (environment, input, data) { var _this = this; if (data === void 0) { data = {}; } switch (environment) { case "cli": case "url": { // default values { this.arguments_.forEach(function (argument) { data[argument.name_get()] = argument.default_get(); }); } // preprocessing { // short indicators (lil hacky ...) { if (environment == "cli") { this.filter("volatile").forEach(function (argument) { // console.info(argument.parameters_get()["indicators_short"].join("|")); var pattern_from = ""; { pattern_from += "(?:^|" + settings["environment"][environment]["symbols"]["delimiter"] + ")"; pattern_from += "-" + argument.parameters_get()["indicators_short"].join("|"); pattern_from += "(?:$|" + settings["environment"][environment]["symbols"]["delimiter"] + ")"; } var pattern_to = ""; { pattern_to += settings["environment"][environment]["symbols"]["delimiter"]; pattern_to += settings["environment"][environment]["symbols"]["prefix"]; pattern_to += argument.indicator_main(); if (argument.type_get() == "boolean") { pattern_to += settings["environment"][environment]["symbols"]["delimiter"]; } else { pattern_to += settings["environment"][environment]["symbols"]["assignment"]; } } var result = input.replace(new RegExp(pattern_from, "g"), pattern_to); if (lib_args.verbosity >= 3) console.info("--", "replacing \"" + pattern_from + "\" by \"" + pattern_to + "\" in \"" + input + "\" to \"" + result + "\""); input = result; }); } } if (lib_args.verbosity >= 3) console.info("--", "input is now \"" + input + "\""); } // parsing { var parts = input .split(settings["environment"][environment]["symbols"]["delimiter"]) .filter(function (x) { return (x != ""); }); var index_expected_1 = 0; parts.forEach(function (part) { if (lib_args.verbosity >= 2) console.info("--", "analyzing \"" + part + "\""); var found = [ function () { if (lib_args.verbosity >= 3) console.info("--", "probing as volatile"); return (_this.filter("volatile") .some(function (argument) { if (lib_args.verbosity >= 4) console.info("--", "trying as " + argument.toString()); var pattern = ""; { var pattern_front = ""; pattern_front += "" + settings["environment"][environment]["symbols"]["prefix"]; pattern_front += "(?:" + argument.parameters_get()["indicators_long"].join("|") + ")"; pattern += pattern_front; } { var pattern_back = ""; pattern_back += "" + settings["environment"][environment]["symbols"]["assignment"]; pattern_back += "(" + argument.pattern_value() + ")"; if (argument.type_get() == "boolean") { pattern_back = "(?:" + pattern_back + ")?"; } pattern += pattern_back; } if (lib_args.verbosity >= 5) console.info("--", "pattern: \"" + pattern + "\""); var regexp = new RegExp(pattern); var matching = regexp.exec(part); if (lib_args.verbosity >= 5) console.info("--", "matching:", matching); if (matching == null) { return false; } else { argument.assign(data, matching[1]); return true; } })); }, function () { if (lib_args.verbosity >= 3) console.info("--", "probing as positional"); var positional = _this.filter("positional"); if (index_expected_1 >= positional.length) { if (lib_args.verbosity >= 4) console.info("--", "no positional arguments left"); return false; } else { var argument = positional[index_expected_1]; if (lib_args.verbosity >= 4) console.info("--", "trying as " + argument.toString()); var pattern = ""; { var pattern_back = ""; pattern_back += "(" + argument.pattern_value() + ")"; pattern += pattern_back; } if (lib_args.verbosity >= 5) console.info("--", "pattern: \"" + pattern + "\""); var regexp = new RegExp(pattern); var matching = regexp.exec(part); if (lib_args.verbosity >= 5) console.info("--", "matching:", matching); if (matching == null) { return false; } else { argument.assign(data, matching[1]); index_expected_1 += 1; return true; } } }, ].some(function (x) { return x(); }); if (!found) { if (lib_args.verbosity >= 1) console.warn("--", "couldn't parse \"" + part + "\""); } }); } return data; break; } default: { throw (new Error("unhandled environment " + environment)); break; } } }; /** * @author fenris * @todo handle if the data object doesn't have the required field or the type is wrong or sth. */ class_handler.prototype.write = function (environment, data) { switch (environment) { case "cli": case "url": { return (([] .concat(this.filter("volatile").map(function (argument) { var raw = ""; { var raw_front = ""; raw_front += settings["environment"][environment]["symbols"]["prefix"]; raw_front += argument.parameters_get()["indicators_long"][0]; raw += raw_front; } { var raw_back = ""; raw_back += settings["environment"][environment]["symbols"]["assignment"]; raw_back += argument.make(data); raw += raw_back; } return raw; })) .concat(this.filter("positional").map(function (argument) { var raw = ""; { var raw_back = ""; raw_back += argument.make(data); raw += raw_back; } return raw; }))) .join(settings["environment"][environment]["symbols"]["delimiter"])); break; } default: { throw (new Error("unhandled environment " + environment)); break; } } }; /** * @desc manpage-like info-sheet * @author fenris */ class_handler.prototype.generate_help = function (_a) { var _b = _a["programname"], programname = _b === void 0 ? null : _b, _c = _a["author"], author = _c === void 0 ? null : _c, _d = _a["description"], description = _d === void 0 ? null : _d, _e = _a["executable"], executable = _e === void 0 ? null : _e; var environment = "cli"; var output = ""; { var section = ""; { var line = ""; line += ""; line += "INFO"; line += "\n"; section += line; } { var line = ""; line += "\t"; line += programname + " -- " + description; line += "\n"; section += line; } section += "\n"; output += section; } { if (author != null) { var section = ""; { var line = ""; line += ""; line += "AUTHOR"; line += "\n"; section += line; } { var line = ""; line += "\t"; line += "" + author; line += "\n"; section += line; } section += "\n"; output += section; } } { var section = ""; { var line = ""; line += ""; line += "SYNOPSIS"; line += "\n"; section += line; } { var line = ""; line += "\t"; line += executable; line += settings["environment"][environment]["symbols"]["delimiter"]; line += this.filter("positional") .map(function (argument) { var part = ""; part += "<" + argument.name_get() + ">"; return part; }) .join(settings["environment"][environment]["symbols"]["delimiter"]); line += settings["environment"][environment]["symbols"]["delimiter"]; line += this.filter("volatile") .filter(function (argument) { return (!argument.hidden_get()); }) .map(function (argument) { var part = ""; part += settings["environment"][environment]["symbols"]["prefix"]; part += argument.parameters_get()["indicators_long"][0]; if (argument.type_get() != "boolean") { part += settings["environment"][environment]["symbols"]["assignment"]; part += "<" + argument.name_get() + ">"; } part = "[" + part + "]"; return part; }) .join(settings["environment"][environment]["symbols"]["delimiter"]); line += "\n"; section += line; } section += "\n"; output += section; } { var section = ""; { var line = ""; line += ""; line += "OPTIONS"; line += "\n"; section += line; } { section += (this.arguments_ .filter(function (argument) { return (!argument.hidden_get()); }) .map(function (argument) { return argument.generate_help(); }) .join("\n")); } section += "\n"; output += section; } return output; }; return class_handler; }()); lib_args.class_handler = class_handler; })(lib_args || (lib_args = {})); var nm_child_process = require("child_process"); var nm_fs = require("fs"); /** * @author fenris */ var globalvars = { "configuration": {}, }; /** * @author fenris */ function name_mark(name) { return (globalvars.configuration.name_prefix + name); } /** * @author fenris */ function dirwrap(location, core) { if (location == null) { return core; } else { return `cd ${location.as_string(globalvars.configuration.system)} > /dev/null && ${core} ; cd - > /dev/null`; } } /** * @author fenris */ function filepointer_adjust(filepointer, location) { return ((location == null) ? filepointer : filepointer.relocate(location)); } /** * @author fenris */ function path_augment(path, step, aggregate = true) { if (aggregate) { return path.concat([step]); } else { return [step]; } } /** * @author fenris */ function path_dump(path) { return path.join(globalvars.configuration.name_splitter); } /** * @author fenris */ class class_message { /** * @author fenris */ constructor(content, parameters = {}) { /** * @author fenris */ this.depth = 0; this.content = content; this.type = lib_object.fetch(parameters, "type", null, 0); this.depth = lib_object.fetch(parameters, "depth", 0, 0); this.prefix = lib_object.fetch(parameters, "prefix", null, 0); this.linebreak = lib_object.fetch(parameters, "linebreak", false, 0); } /** * @author fenris */ generate(with_type = true) { let output = ""; if (with_type) { if (this.type != null) { output += ("[" + this.type + "]" + " "); } } if (this.prefix != null) { output += ("<" + this.prefix + ">" + " "); } output += lib_string.repeat("\t", this.depth); output += this.content; if (this.linebreak) { output += "\n"; } return output; } /** * @author fenris */ stdout() { console.log(this.generate(true)); } /** * @author fenris */ stderr() { console.error(this.generate(true)); } /** * @author fenris */ console() { switch (this.type) { case "log": { console.log(this.generate(false)); break; } case "information": { console.info(this.generate(false)); break; } case "warning": { console.warn(this.generate(false)); break; } case "error": { console.error(this.generate(false)); break; } default: { throw (new Error("unhandled type '" + this.type + "'")); } } } } /** * @author fenris */ class class_cliout { /** * @author fenris */ static stdout(content, depth = 0) { console.log(lib_string.repeat("\t", depth) + content); } /** * @author fenris */ static stderr(content, depth = 0) { console.error(lib_string.repeat("\t", depth) + content); } /** * @author fenris */ static log(content, depth = 0) { this.stderr("-- " + content, depth); } /** * @author fenris */ static info(content, depth = 0) { this.stderr(">> " + content, depth); } /** * @author fenris */ static warn(content, depth = 0) { this.stderr(">> " + content, depth); } /** * @author fenris */ static error(content, depth = 0) { this.stderr(">> " + content, depth); } } /** * @author fenris */ var lib_gnumake; (function (lib_gnumake) { /** * @author fenris */ function macro_command({ "interpreter": interpreter = null, "path": path, "args": args = [], "output": output = null, "system": system = "linux", }) { switch (system) { case "bsd": case "linux": { let command = path; { if (interpreter != null) { command = `${interpreter} ${command}`; } } { let parts = []; args.forEach(arg => parts.push(arg)); command = `${command} ${parts.join(" ")}`; } { if (output != null) { command = `${command} > ${output}`; } } return command; break; } case "win": { let command = "cmd //c"; { command = `${command} ${path}`; } { if (interpreter != null) { command = `${command} ${interpreter}`; } } { let parts = []; args.forEach(arg => parts.push(arg)); command = `${command} ${parts.join(" ")}`; } { if (output != null) { command = `${command} > ${output}`; } } return command; break; } default: { throw (new Error(`unhandled system '${system}'`)); break; } } } lib_gnumake.macro_command = macro_command; /** * @author fenris */ class class_rule { /** * @author fenris */ // public constructor(name : string, dependencies : Array<string>, actions : Array<string>, phony : boolean = false) { constructor(parameters = {}) { this.name = object_fetch(parameters, "name", null, 2); this.dependencies = object_fetch(parameters, "dependencies", [], 0); this.actions = object_fetch(parameters, "actions", [], 0); this.phony = object_fetch(parameters, "phony", false, 0); } /** * @author fenris */ actions_get() { return this.actions; } /** * @author fenris */ compile(silent = false) { let output = ""; output += (`${this.name}: ${this.dependencies.map(dependency => (" " + dependency)).join("")}\n`); this.actions.forEach(action => (output += `\t${(silent ? "@ " : "")}${action}\n`)); if (this.phony) { output += (`.PHONY: ${this.name}\n`); } return output; } } lib_gnumake.class_rule = class_rule; /** * @author fenris */ class class_sheet { /** * @author fenris */ constructor(rules, comments = []) { this.rules = rules; this.comments = comments; } /** * @author fenris */ compile(silent = false) { return ([] .concat(this.comments.map(comment => ("# " + comment))) .concat([""]) .concat(this.rules.map(rule => rule.compile(silent))) .join("\n")); } } lib_gnumake.class_sheet = class_sheet; })(lib_gnumake || (lib_gnumake = {})); /** * @author fenris */ var lib_ant; (function (lib_ant) { /** * @author fenris */ class class_comment { /** * @author fenris */ constructor(content) { this.content = content; } /** * @author fenris */ compile() { return (new lib_xml.class_node_comment(this.content)); } } lib_ant.class_comment = class_comment; /** * @author fenris */ class class_action { /** * @author fenris */ constructor(representation) { this.representation = representation; } /** * @author fenris */ compile() { return this.representation; } /** * @author fenris */ static macro_exec({ "interpreter": interpreter = null, "path": path, "args": args = [], "output": output = null, "system": system = "linux", }) { switch (system) { case "linux": case "bsd": { let attributes = {}; let args_ = []; if (interpreter == null) { attributes["executable"] = path; } else { attributes["executable"] = interpreter; args.push(path); } if (output != null) { attributes["output"] = output; } args_ = args_.concat(args); return (new lib_ant.class_action(new lib_xml.class_node_complex("exec", attributes, args_.map(arg => new lib_xml.class_node_complex("arg", { "value": arg }))))); break; } case "win": { let attributes = {}; let args_ = []; attributes["executable"] = "cmd"; args_.push("/c"); if (interpreter == null) { } else { args_.push(interpreter); } args_.push(path); args_ = args_.concat(args); if (output != null) { attributes["output"] = output; } return (new lib_ant.class_action(new lib_xml.class_node_complex("exec", attributes, args_.map(arg => new lib_xml.class_node_complex("arg", { "value": arg }))))); break; } default: { throw (new Error(`unhandled system ${system}`)); break; } } } /** * @author fenris */ static macro_command(cmdparams) { return this.macro_exec(cmdparams); } } lib_ant.class_action = class_action; /** * @author fenris */ class class_target { /** * @author fenris */ constructor(parameters = {}) { this.name = object_fetch(parameters, "name", null, 2); this.dependencies = object_fetch(parameters, "dependencies", [], 1); this.actions = object_fetch(parameters, "actions", [], 0); } /** * @author fenris */ actions_get() { return this.actions; } /** * @author fenris */ compile() { return (new lib_xml.class_node_complex("target", { "name": this.name, "depends": this.dependencies.join(",") }, this.actions.map(action => action.compile()))); } } lib_ant.class_target = class_target; /** * @author fenris */ class class_project { /** * @author fenris */ constructor(parameters = {}) { this.name = object_fetch(parameters, "name", null, 2); this.default_ = object_fetch(parameters, "default", null, 2); this.targets = object_fetch(parameters, "targets", [], 1); this.comments = object_fetch(parameters, "comments", [], 0); } /** * @author fenris */ compile() { return (new lib_xml.class_node_complex("project", { "name": this.name, "default": this.default_, }, ([] .concat(this.comments.map(comment => comment.compile())) .concat([ new lib_xml.class_node_complex("property", { "environment": "env" }), ]) .concat(this.targets.map(target => target.compile()))))); } } lib_ant.class_project = class_project; })(lib_ant || (lib_ant = {})); var lib_markdown; (function (lib_markdown) { /** * @author fenris */ function italic(content) { return ("_" + content + "_"); } lib_markdown.italic = italic; /** * @author fenris */ function bold(content) { return ("__" + content + "__"); } lib_markdown.bold = bold; /** * @author fenris */ function code(content) { return ("`" + content + "`"); } lib_markdown.code = code; /** * @author fenris */ function section(level, title) { return ("#".repeat(level) + " " + title + "\n"); } lib_markdown.section = section; /** * @author fenris */ function paragraph(content = "") { return (content + "\n"); } lib_markdown.paragraph = paragraph; /** * @author fenris */ function listitem(level, content) { return (" ".repeat(level - 1) + "* " + content + "\n"); } lib_markdown.listitem = listitem; })(lib_markdown || (lib_markdown = {})); /** * @author fenris */ class class_action { /** * @author fenris */ constructor() { } } /** * @author fenris */ class class_action_adhoc extends class_action { /** * @author fenris */ constructor() { super(); } } /** * @author fenris */ class class_action_exec extends class_action_adhoc { /** * @author fenris */ constructor({ "inputs": inputs, "outputs": outputs, "path": path, "interpreter": interpreter, "workdir": workdir, }) { super(); this.paths_input = inputs; this.paths_output = outputs; this.path_script = path; this.path_interpreter = interpreter; this.workdir = workdir; } /** * @desc for defining directly how the action is to be converted into a target-piece * @author fenris */ compilation(output_identifier) { switch (output_identifier) { case "gnumake": { switch (globalvars.configuration.system) { case "linux": case "bsd": case "win": { let command = ""; { command = lib_gnumake.macro_command({ "interpreter": ((this.path_interpreter != null) ? this.path_interpreter.as_string(globalvars.configuration.system) : null), "path": this.path_script.as_string(globalvars.configuration.system), "args": [ ("'" + this.paths_input.map(filepointer => filepointer.as_string(globalvars.configuration.system)).join(",") + "'"), ("'" + this.paths_output.map(filepointer => filepointer.as_string(globalvars.configuration.system)).join(",") + "'"), ], "system": globalvars.configuration.system, }); } { if (this.workdir != null) { // command = `pushd ${this.workdir.as_string(globalvars.configuration.system)} && ${command} ; popd` command = `cd ${this.workdir.as_string(globalvars.configuration.system)} && ${command} ; cd -`; } } return command; break; } default: { throw (new Error("not implemented")); break; } } break; } case "ant": { return (lib_ant.class_action.macro_exec({ "interpreter": ((this.path_interpreter != null) ? this.path_interpreter.as_string(globalvars.configuration.system) : null), "path": this.path_script.as_string("linux"), "args": [ ("'" + this.paths_input.map(filepointer => filepointer.as_string("linux")).join(",") + "'"), ("'" + this.paths_output.map(filepointer => filepointer.as_string("linux")).join(",") + "'"), ], "system": globalvars.configuration.system, })); break; } default: { throw (new Error(`unhandled output '${output_identifier}'`)); break; } } } } /** * @author fenris */ class class_action_echo extends class_action_adhoc { /** * @author fenris */ constructor(message) { super(); this.message = message; } /** * @desc for defining directly how the action is to be converted into a target-piece * @todo escape message * @author fenris */ compilation(target_identifier) { switch (target_identifier) { case "gnumake": { switch (globalvars.configuration["system"]) { case "linux": case "bsd": case "win": { return (lib_gnumake.macro_command({ "path": "echo", "args": ["\"" + this.message + "\""], })); break; } default: { throw (new Error("not implemented")); break; } } break; } case "ant": { return (new lib_ant.class_action(new lib_xml.class_node_complex("echo", { "message": this.message }))); break; } default: { throw (new Error("unhandled target '" + target_identifier + "'")); break; } } } } /** * @author fenris */ class class_action_mkdir extends class_action_adhoc { /** * @author fenris */ constructor(location) { super(); this.location = location; } /** * @desc for defining directly how the action is to be converted into a target-piece * @author fenris */ compilation(target_identifier) { switch (target_identifier) { case "gnumake": { let parts = []; parts.push("mkdir"); switch (globalvars.configuration["system"]) { case "linux": { parts.push("--parents"); break; } case "bsd": { parts.push("-p"); break; } case "win": { break; } default: { throw (new Error("not implemented")); break; } } parts.push(this.location.as_string(globalvars.configuration["system"])); return parts.join(" "); } case "ant": { return (new lib_ant.class_action(new lib_xml.class_node_complex("mkdir", { "dir": this.location.as_string("linux") }))); } default: { throw (new Error("unhandled target '" + target_identifier + "'")); } } } } /** * @author fenris */ class class_action_touch extends class_action_adhoc { /** * @author fenris */ constructor(filepointer) { super(); this.filepointer = filepointer; } /** * @desc for defining directly how the action is to be converted into a target-piece * @author fenris */ compilation(target_identifier) { switch (target_identifier) { case "gnumake": { switch (globalvars.configuration["system"]) { case "bsd": case "linux": { let parts = []; parts.push("touch"); parts.push(this.filepointer.toString()); return parts.join(" "); } case "win": { let parts = []; parts.push("echo."); parts.push(">"); parts.push(this.filepointer.toString()); return parts.join(" "); } default: { throw (new Error("not implemented")); } } } case "ant": { return (new lib_ant.class_action(new lib_xml.class_node_complex("touch", { "file": this.filepointer.toString() }))); } default: { throw (new Error("unhandled target '" + target_identifier + "'")); break; } } } } /** * @author fenris */ class class_action_copy extends class_action_adhoc { /** * @author fenris */ constructor(filepointer_from, filepointer_to, folder) { super(); this.filepointer_from = filepointer_from; this.filepointer_to = filepointer_to; this.folder = folder; } /** * @desc for defining directly how the action is to be converted into a target-piece * @author fenris */ compilation(target_identifier) { switch (target_identifier) { case "gnumake": { switch (globalvars.configuration.system) { case "linux": { let args = []; if (this.folder) { args.push("--recursive"); args.push("--update"); args.push("--verbose"); args.push((new lib_path.class_filepointer(this.filepointer_from.location, "*")).as_string(globalvars.configuration.system)); } else { args.push(this.filepointer_from.as_string(globalvars.configuration.system)); } args.push(this.filepointer_to.as_string(globalvars.configuration.system)); return (lib_gnumake.macro_command({ "path": "cp", "args": args, })); break; } case "bsd": { let args = []; if (this.folder) { args.push("-r"); // args.push("-u"); args.push("-v"); args.push((new lib_path.class_filepointer(this.filepointer_from.location, "*")).as_string(globalvars.configuration.system)); } else { args.push(this.filepointer_from.as_string(globalvars.configuration.system)); } args.push(this.filepointer_to.as_string(globalvars.configuration.system)); return (lib_gnumake.macro_command({ "path": "cp", "args": args, })); break; } case "win": { throw (new Error("not implemented")); break; } default: { throw (new Error("not implemented")); break; } } break; } case "ant": { if (!this.folder) { return (new lib_ant.class_action(new lib_xml.class_node_complex("copy", { "file": this.filepointer_from.as_string("linux"), "tofile": this.filepointer_to.as_string("linux"), }))); } else { return (new lib_ant.class_action(new lib_xml.class_node_complex("copy", { "todir": this.filepointer_to.as_string("linux"), }, [ new lib_xml.class_node_complex("fileset", { "dir": this.filepointer_from.as_string("linux"), }) ]))); } break; } default: { throw (new Error("unhandled target '" + target_identifier + "'")); break; } } } } /** * @author fenris */ class class_action_move extends class_action_adhoc { /** * @author fenris */ constructor({ "from": from, "to": to, }) { super(); this.from = from; this.to = to; } /** * @desc for defining directly how the action is to be converted into a target-piece * @author fenris */ compilation(output_identifier) { switch (output_identifier) { case "gnumake": { switch (globalvars.configuration.system) { case "linux": case "bsd": case "win": { let from = this.from.as_string(globalvars.configuration.system); let to = this.to.as_string(globalvars.configuration.system); let command = ""; { command = lib_gnumake.macro_command({ "path": "mv", // "args": ["--verbose", from, to], "args": [from, to], "system": globalvars.configuration.system, }); } { command = `[ "${from}" -ef "${to}" ] || ${command}`; } return command; break; } default: { throw (new Error("not implemented")); break; } } break; } case "ant": { return (new lib_ant.class_action(new lib_xml.class_node_complex("move", { "file": this.from.as_string("linux"), "tofile": this.to.as_string("linux"), }))); break; } default: { throw (new Error(`unhandled output '${output_identifier}'`)); break; } } } } /** * @author fenris */ class class_action_concat extends class_action_adhoc { /** * @author fenris */ constructor(sources, destination, schwamminput = null) { super(); this.sources = sources; this.destination = destination; this.schwamminput = schwamminput; } /** * @desc for defining directly how the action is to be converted into a target-piece * @author fenris */ compilation(output_identifier) { switch (output_identifier) { case "gnumake": { if (this.sources.length > 0) { return (lib_gnumake.macro_command({ "path": { "linux": "cat", "bsd": "cat", "win": "type", }[globalvars.configuration.system], "args": this.sources.map(source => source.as_string(globalvars.configuration.system)), "output": this.destination.as_string(globalvars.configuration.system), })); } else { return (lib_gnumake.macro_command({ "path": "touch", "args": [this.destination.as_string(globalvars.configuration.system)], })); } break; } case "ant": { return (new lib_ant.class_action(new lib_xml.class_node_complex("concat", { "destfile": this.destination.as_string("linux") }, [ new lib_xml.class_node_complex("filelist", { "dir": "." }, this.sources.map(function (source) { return (new lib_xml.class_node_complex("file", { "name": source.as_string("linux") })); })) ]))); break; } default: { throw (new Error(`unhandled output '${output_identifier}'`)); break; } } } } /** * @author fenris */ class class_action_lessc extends class_action_adhoc { /** * @author fenris */ constructor(filepointer_from, filepointer_to) { super(); this.filepointer_from = filepointer_from; this.filepointer_to = filepointer_to; } /** * @override * @author fenris */ compilation(output_identifier) { let args = []; args.push(this.filepointer_from.as_string(globalvars.configuration.system)); let cmdparams = { "path": globalvars.configuration.programpaths.lessc, "args": args, "output": this.filepointer_to.as_string(globalvars.configuration.system), }; switch (output_identifier) { case "gnumake": { return lib_gnumake.macro_command(cmdparams); break; } case "ant": { return lib_ant.class_action.macro_command(cmdparams); break; } default: { throw (new Error(`unhandled output '${output_identifier}'`)); break; } } } } /** * @author neu3no */ class class_action_babel extends class_action_adhoc { /** * @author neu3no */ constructor(filepointers_from, filepointer_to, presets, plugins, minify) { super(); this.filepointers_from = filepointers_from; this.filepointer_to = filepointer_to; this.presets = presets; this.plugins = plugins; this.minify = minify; } /** * @override * @author neu3no,fenris */ compilation(output_identifier) { let args = []; args.push("--no-babelrc"); // input { this.filepointers_from.forEach(filepointer => args.push(filepointer.as_string(globalvars.configuration.system))); } // output { args.push("--out-file"); args.push(this.filepointer_to.as_string(globalvars.configuration.system)); } // presets { if ((this.presets !== null) && (this.presets.length > 0)) { args.push("--presets"); args.push(this.presets.join(",")); } } // plugins { if ((this.plugins != null) && (this.plugins.length > 0)) { args.push("--plugins"); args.push(this.plugins.join(",")); } } // minify { if (this.minify) { args.push("--minified"); } } let cmdparams = { "path": globalvars.configuration.programpaths.babel, "args": args, "output": this.filepointer_to.as_string(globalvars.configuration.system), }; switch (output_identifier) { case "gnumake": { return lib_gnumake.macro_command(cmdparams); break; } case "ant": { return lib_ant.class_action.macro_command(cmdparams); break; } default: { throw (new Error(`unhandled target '${output_identifier}'`)); break; } } } } /** * @author fenris */ class class_action_tsc extends class_action_adhoc { /** * @author fenris */ constructor(paths_input, path_output, target, allowUnreachableCode, declaration) { super(); this.paths_input = paths_input; this.path_output = path_output; this.target = target; this.allowUnreachableCode = allowUnreachableCode; this.declaration = declaration; } /** * @author fenris * @todo handle declarion-path */ compilation(output_identifier) { let args = []; { if (this.allowUnreachableCode) { args.push("--allowUnreachableCode"); } } { if (this.target != null) { args.push("--target"); args.push(this.target); } } { this.paths_input.forEach(filepointer => args.push(filepointer.as_string(globalvars.configuration.system))); } { if (this.declaration != null) { args.push("--declaration"); } } { args.push("--outFile"); args.push(this.path_output.as_string(globalvars.configuration.system)); } let cmdparams = { "path": globalvars.configuration.programpaths.tsc, "args": args, "system": globalvars.configuration.system, }; switch (output_identifier) { case "gnumake": { return lib_gnumake.macro_command(cmdparams); break; } case "ant": { return lib_ant.class_action.macro_command(cmdparams); break; } default: { throw (new Error(`unhandled output '${output_identifier}'`)); break; } } } } /** * @author fenris */ class class_action_php extends class_action_adhoc { /** * @author fenris */ constructor(filepointers_from, filepointer_to, only_last) { super(); this.filepointers_from = filepointers_from; this.filepointer_to = filepointer_to; this.only_last = only_last; } /** * @desc for defining directly how the action is to be converted into a target-piece * @author fenris */ compilation(output_identifier) { let args = []; { if (this.only_last) { this.filepointers_from.slice(-1).forEach(filepointer => args.push(filepointer.as_string(globalvars.configuration.system))); } else { this.filepointers_from.forEach(filepointer => args.push(filepointer.as_string(globalvars.configuration.system))); } } let cmdparams = { "path": globalvars.configuration.programpaths.php, "args": args, "system": globalvars.configuration.system, "output": this.filepointer_to.as_string(globalvars.configuration.system), }; switch (output_identifier) { case "gnumake": { return lib_gnumake.macro_command(cmdparams); break; } case "ant": { return lib_ant.class_action.macro_command(cmdparams); break; } default: { throw (new Error(`unhandled output '${output_identifier}'`)); break; } } } } /** * @author fenris */ class class_action_gitpull extends class_action_adhoc { /** * @author fenris */ constructor(url) { super(); this.url = url; } /** * @desc for defining directly how the action is to be converted into a target-piece * @todo escape message * @author fenris */ compilation(target_identifier) { switch (target_identifier) { case "gnumake": { switch (globalvars.configuration["system"]) { case "linux": case "win": { let parts = []; parts.push("git pull"); parts.push(this.url); return parts.join(" "); break; } default: { throw (new Error("not implemented")); break; } } break; } /* case "ant": { return ( new lib_ant.class_action( new lib_xml.class_node_complex( "echo", {"message": this.message} ) ) ); break; } */ default: { throw (new Error("unhandled target '" + target_identifier + "'")); break; } } } } /** * @author fenris */ class class_action_schwamm extends class_action_adhoc { /** * @author fenris */ constructor(includes, inputs, save, dump_group = null, dump_filepointer = null) { super(); this.includes = includes; this.inputs = inputs; this.save = save; this.dump_group = dump_group; this.dump_filepointer = dump_filepointer; } /** * @desc for defining directly how the action is to be converted into a target-piece * @author fenris */ compilation(target_identifier) { let args = []; this.includes.forEach(include => { args.push(`--include=${include.as_string(globalvars.configuration["system"])}`); }); lib_object.to_array(this.inputs).forEach(pair => { pair.value.forEach(member => { let filepointer = member; args.push(`--input=${filepointer.as_string(globalvars.configuration["system"])}:${pair.key}`); }); }); // args.push(`--file=${this.output.as_string(globalvars.configuration["system"])}`); // args.push(`--dir=${((this.dir != null) ? this.dir : this.output.location).as_string("system")}`); let target; if (this.save != undefined) { args.push(`--output=native`); target = this.save; } else if (this.dump_group != null) { args.push(`--output=dump:${this.dump_group}`); target = this.dump_filepointer; } else { console.warn("output missing?"); } let cmdparams = { "path": globalvars.configuration.programpaths.schwamm, "args": args, "output": target.as_string(globalvars.configuration["system"]), }; switch (target_identifier) { case "gnumake": { return lib_gnumake.macro_command(cmdparams); break; } case "ant": { return lib_ant.class_action.macro_command(cmdparams); break; } default: { throw (new Error(`unhandled target '${target_identifier}'`)); break; } } } } /** * @author fenris */ class class_action_schwamm_create extends class_action_adhoc { /** * @author fenris */ constructor(includes, adhoc, output, dir) { super(); this.includes = includes; this.adhoc = adhoc; this.output = output; this.dir = dir; } /** * @desc for defining directly how the action is to be converted into a target-piece * @author fenris */ compilation(target_identifier) { let args = []; this.includes.forEach(include => { args.push(`--include=${include.as_string(globalvars.configuration["system"])}`); }); Object.keys(this.adhoc).forEach(group => { this.adhoc[group].forEach(member => { let filepointer = member; args.push(`--input=${filepointer.as_string(globalvars.configuration["system"])}:${group}`); }); }); args.push(`--output=native`); // args.push(`--file=${this.output.as_string(globalvars.configuration["system"])}`); // args.push(`--dir=${((this.dir != null) ? this.dir : this.output.location).as_string("linux")}`); let cmdparams = { "path": "schwamm", "args": args, "output": this.output.as_string(globalvars.configuration["system"]), }; switch (target_identifier) { case "gnumake": { return lib_gnumake.macro_command(cmdparams); break; } case "ant": { return lib_ant.class_action.macro_command(cmdparams); break; } default: { throw (new Error(`unhandled target '${target_identifier}'`)); break; } } } } /** * @author fenris */ class class_action_schwamm_apply extends class_action_adhoc { /** * @author fenris */ constructor(path, output_group, output_filepointer) { super(); this.path = path; this.output_group = output_group; this.output_filepointer = output_filepointer; } /** * @desc for defining directly how the action is to be converted into a target-piece * @author fenris */ compilation(target_identifier) { let args = []; args.push(`--include=${this.path.as_string(globalvars.configuration["system"])}`); args.push(`--output=dump:${this.output_group}`); let filepointer = lib_path.filepointer_read(globalvars.configuration["path"]).foo(this.output_filepointer); let cmdparams = { "path": "schwamm", "args": args, "output": filepointer.as_string(globalvars.configuration["system"]), }; switch (target_identifier) { case "gnumake": { return lib_gnumake.macro_command(cmdparams); break; } case "ant": { return lib_ant.class_action.macro_command(cmdparams); break; } default: { throw (new Error(`unhandled target '${target_identifier}'`)); break; } } } } /** * @author fenris */ class class_action_locmerge extends class_action_adhoc { /** * @author fenris */ constructor(inputs, output) { super(); this.inputs = inputs; this.output = output; } /** * @override * @author fenris */ compilation(output_identifier) { let args = []; // inputs { this.inputs.forEach(input => args.push(input.as_string(globalvars.configuration.system))); } let cmdparams = { "path": globalvars.configuration.programpaths.locmerge, "args": args, "output": this.output.as_string(globalvars.configuration.system), }; switch (output_identifier) { case "gnumake": { return lib_gnumake.macro_command(cmdparams); break; } case "ant": { return lib_ant.class_action.macro_command(cmdparams); break; } default: { throw (new Error(`unhandled target '${output_identifier}'`)); break; } } } } /** * @author fenris */ class class_task { /** * @author fenris */ constructor({ "name": name = null, "active": active = true, "sub": sub = [], "inputs": _inputs = [], "outputs": _outputs = [], "actions": _actions = [], }) { this.name = name; this.sub = sub; this.active = active; this._inputs = _inputs; this._outputs = _outputs; this._actions = _actions; this.context = null; } /** * @desc [accessor] [getter] * @author fenris */ name_get() { return this.name; } /** * @desc [accessor] [getter] * @author fenris */ sub_get() { return this.sub; } /** * @desc [accessor] [getter] * @author fenris */ active_get() { return this.active; } /** * @desc [mutator] [setter] * @author fenris */ context_set(context) { this.context = context; } /** * @desc [accessor] [getter] * @author fenris */ context_get() { return this.context; } /** * @returns the subgraph of all active tasks * @author fenris */ clean(root = true) { if (root && (!this.active)) { throw (new Error("cant't clean inactive root")); } else { this.sub = this.sub.filter(task_ => task_.active); this.sub.forEach(task_ => task_.clean(false)); } } /** * @desc [accessor] [getter] * @author fenris */ values(raw) { return null; } /** * @desc [accessor] [getter] a list of paths which represent input-files of the task * @author fenris */ inputs() { return this._inputs; } /** * @desc [accessor] [getter] a list of paths which represent output-files of the task * @author fenris */ outputs() { return this._outputs; } /** * @desc [accessor] [getter] generates all actions which have to be executed in order to fulfil the task * @author fenris */ actions() { return this._actions; } } /** * @author fenris */ class class_taskparameter { /** * @author fenris */ constructor({ "name": name, "extraction": extraction = lib_call.id, "shape": shape = lib_meta.from_raw({ "id": "any" }), "default": default_ = new class_nothing(), "description": description = null, }) { this.name = name; this.extraction = extraction; this.shape = shape; this.default_ = default_; this.description = description; } /** * @author fenris */ static input_single({ "description": description = "the list of paths to the input files", "default": default_ = new class_nothing(), } = {}) { return (new class_taskparameter({ "name": "input", "extraction": raw => lib_path.filepointer_read(raw), "shape": lib_meta.from_raw({ "id": "string" }), "default": default_, "description": description, })); } /** * @author fenris */ static input_list({ "description": description = "the list of paths to the input files", "default": default_ = new class_nothing(), } = {}) { return (new class_taskparameter({ "name": "inputs", "extraction": raw => raw.map(x => lib_path.filepointer_read(x)), "shape": lib_meta.from_raw({ "id": "array", "parameters": { "shape_element": { "id": "string" } } }), "default": default_, "description": description, })); } /** * @author fenris */ static input_schwamm({ "description": description = "parameters for a schwamm which holds a list of files in a group", "default": default_ = new class_nothing(), } = {}) { return (new class_taskparameter({ "name": "input_from_schwamm", "extraction": raw => { if (raw == null) { return []; } else { let regexp; if (raw["filter"] != null) { regexp = new RegExp(raw["filter"]); } else { regexp = null; } let command = `schwamm --include=${raw["path"]} --output=list:${raw["group"]}`; let result = nm_child_process.execSync(command); let output = result.toString(); let paths = output.split("\n"); return (paths .filter(path => (path.trim().length > 0)) .filter(path => ((regexp == null) ? true : regexp.test(path))) .map(path => lib_path.filepointer_read(path))); } }, "shape": lib_meta.from_raw({ "id": "object", "parameters": { "fields": [ { "name": "path", "shape": { "id": "string" } }, { "name": "group", "shape": { "id": "string" } }, { "name": "filter", "shape": { "id": "string", "parameters": { "soft": true } } }, ] } }), "default": default_, "description": description, })); } /** * @author fenris */ static output_single({ "description": description = "the list of paths to the input files", } = {}) { return (new class_taskparameter({ "name": "output", "extraction": raw => lib_path.filepointer_read(raw), "shape": lib_meta.from_raw({ "id": "string" }), "default": new class_nothing(), "description": description, })); } /** * @author fenris */ static output_list({ "description": description = "the list of paths to the output files", "default": default_ = new class_just([]), } = {}) { return (new class_taskparameter({ "name": "outputs", "extraction": raw => raw.map(x => lib_path.filepointer_read(x)), "shape": lib_meta.from_raw({ "id": "array", "parameters": { "shape_element": { "id": "string" } } }), "default": default_, "description": description, })); } } /** * @desc defines how a raw task is converted into a real task and provides information to the user how to form the raw task * @author fenris */ class class_tasktemplate { /** * @author fenris */ constructor({ "description": description = null, "parameters": parameters = [], "factory": factory, }) { this.description = description; this.parameters = parameters; this.factory = factory; } /** * @desc converts raw parameter values to real ones * @author fenris */ convert(object_raw, task_name) { let object_ready = {}; this.parameters.forEach(parameter => { let value_raw; if ((object_raw != null) && (parameter.name in object_raw)) { value_raw = object_raw[parameter.name]; } else { if (parameter.default_.is_nothing()) { throw (new Error(`mandatory parameter '${parameter.name}' is missing in task '${task_name}'`)); } else { value_raw = parameter.default_.cull(); } } let messages = parameter.shape.inspect(value_raw); if (messages.length > 0) { let message = ""; message += `given value '${instance_show(value_raw)}'`; message += ` for parameter '${parameter.name}'`; message += ` with shape '${instance_show(parameter.shape)}'`; message += ` is malformed`; message += `: ${messages.join("; ")}`; throw (new Error(message)); } else { let value_ready = parameter.extraction(value_raw); object_ready[parameter.name] = value_ready; } }); if (object_raw != null) { Object.keys(object_raw) .filter(key => (!this.parameters.some(parameter => (parameter.name == key)))) .forEach(key => { (new class_message(`unrecognized parameter '${key}' in task '${task_name}'`, { "type": "warning", "prefix": "koralle" })).stderr(); }); } return object_ready; } /** * @desc does the actual conversion to a real task * @author fenris */ create(rawtask, nameprefix = null) { let data = this.convert(rawtask.parameters, rawtask.name); let stuff = this.factory(data, rawtask); let name = ""; { name = ((rawtask.name != undefined) ? rawtask.name : lib_string.generate("task_")); if (nameprefix != null) { name = `${nameprefix}-${name}`; } } let active; { active = ((rawtask.active != undefined) ? rawtask.active : true); } let sub = []; { if (rawtask["sub"] != undefined) { sub = sub.concat(rawtask.sub.map(rawtask_ => class_tasktemplate.create(rawtask_, nameprefix))); } if (stuff["sub"] != undefined) { sub = sub.concat(stuff["sub"]); } } let inputs = (stuff["inputs"] || []); let outputs = (stuff["outputs"] || []); let actions = (stuff["actions"] || []); return (new class_task({ "name": name, "active": active, "sub": sub, "inputs": inputs, "outputs": outputs, "actions": actions, })); } /** * @desc adds a tasktemplate to the pool * @author fenris */ static register(id, tasktemplate) { this.pool[id] = tasktemplate; } /** * @desc retrieves a registered tasktemplate from the pool * @author fenris */ static get(id) { if (id in this.pool) { return this.pool[id]; } else { throw (new Error(`no task registered with name '${id}'`)); } } /** * @desc searches for the corresponding registered tasktemplate and creates a real task * @author fenris */ static create(rawtask, nameprefix = null) { let tasktemplate = this.get(rawtask.type); return tasktemplate.create(rawtask, nameprefix); } /** * @desc returns an overview over all available tasktemplates in markdown format * @author fenris */ static list() { let str = ""; lib_object.to_array(this.pool).forEach(({ "key": id, "value": tasktemplate }) => { str += lib_markdown.section(2, "Task '" + lib_markdown.code(id) + "'"); { str += lib_markdown.section(3, "Description"); str += lib_markdown.paragraph(((tasktemplate.description != null) ? tasktemplate.description : "(missing)")); str += lib_markdown.paragraph(); } { str += lib_markdown.section(3, "Parameters"); tasktemplate.parameters.forEach(taskparameter => { let str_ = ""; { // name { str_ += lib_markdown.paragraph(lib_markdown.code(taskparameter.name)); } // shape { str_ += lib_markdown.listitem(2, "type: " + lib_markdown.italic(instance_show(taskparameter.shape))); } // kind { let content; if (taskparameter.default_.is_nothing()) { content = "mandatory"; } else { content = ("optional (default: " + lib_markdown.code(instance_show(taskparameter.default_.cull())) + ")"); } str_ += lib_markdown.listitem(2, "kind: " + content); } // description { str_ += lib_markdown.listitem(2, "description: " + ((taskparameter.description == null) ? "(missing)" : taskparameter.description)); } } str += lib_markdown.listitem(1, str_); }); str += lib_markdown.paragraph(); } }); return str; } } /** * @desc holds the registered tasktemplates * @author fenris */ class_tasktemplate.pool = {}; /** * @desc a tasktemplate for tasks which have a single input and a single output * @author fenris */ class class_tasktemplate_transductor extends class_tasktemplate { /** * @author fenris */ constructor({ "description": description = null, "parameters_additional": parameters_additional = [], "factory": factory, }) { super({ "description": description, "parameters": ([ class_taskparameter.input_single(), class_taskparameter.output_single(), ] .concat(parameters_additional)), "factory": factory, }); } } /** * @desc a tasktemplate for tasks which have a list of inputs and a single output * @author fenris */ class class_tasktemplate_aggregator extends class_tasktemplate { /** * @author fenris */ constructor({ "description": description = null, "parameters_additional": parameters_additional = [], "factory": factory, }) { super({ "description": description, "parameters": ([ class_taskparameter.input_single({ "default": new class_just("DUMMY") }), class_taskparameter.input_list({ "default": new class_just([]) }), class_taskparameter.input_schwamm({ "default": new class_just(null) }), class_taskparameter.output_single(), ] .concat(parameters_additional)), "factory": factory, }); } /** * @author fenris */ static inputs_all(data) { return ([] .concat((data["input"].as_string().includes("DUMMY")) ? [] : [data["input"]]) .concat(data["inputs"]) .concat((data["input_from_schwamm"] == null) ? [] : data["input_from_schwamm"])); } } /** * @author fenris */ class_tasktemplate.register("group", new class_tasktemplate({ "description": "does nothing but executing the sub tasks", "factory": (data) => { return {}; } })); /** * @author fenris */ function task_each_generate_output(input_raw, output_description, index) { switch (output_description.kind) { case "replace": { let regexp = new RegExp(output_description.parameters["from"]); // let filter : boolean = ((output_description.parameters["filter"] == undefined) || (output_description.parameters["from"] == true)); let execute = true; if (execute) { let output_raw = input_raw.replace(regexp, output_description.parameters["to"]); if (input_raw == output_raw) { (new class_message(`replacement for input '${input_raw}' resulted in the same string`, { "type": "warning", "prefix": "koralle" })).stderr(); } return output_raw; } else { return null; } break; } case "enumerate": { let folder = (output_description["parameters"]["folder"] || "build/"); let prefix = (output_description["parameters"]["prefix"] || "output_"); let suffix = (output_description["parameters"]["suffix"]); let output_raw = (folder + "/" + prefix + index.toString() + ((suffix == null) ? "" : ("." + suffix))); return output_raw; break; } default: { throw (new Error(`unhandled kind '${output_description.kind}'`)); break; } } } /** * @author fenris */ class_tasktemplate.register("each", new class_tasktemplate({ "description": "executes a specific task for a list of inputs", "parameters": [ class_taskparameter.input_list({ "default": new class_just([]) }), class_taskparameter.input_schwamm({ "default": new class_just(null) }), new class_taskparameter({ "name": "element_type", "shape": lib_meta.from_raw({ "id": "string" }), "default": new class_nothing(), "description": "the type of the inner task" }), new class_taskparameter({ "name": "element_parameters", "shape": lib_meta.from_raw({ "id": "any" }), "default": new class_just({}), "description": "the parameters for the inner task" }), new class_taskparameter({ "name": "output_description", "shape": lib_meta.from_raw({ "id": "any" }), "default": new class_nothing(), "description": "how the output paths are generated" }), ], "factory": (data, rawtask) => { let inputs = data["inputs"].concat(data["input_from_schwamm"]); return { "sub": inputs.map((input, index) => { let input_raw = input.as_string(); let output_raw = task_each_generate_output(input_raw, data["output_description"], index); if (output_raw == null) { return null; } else { return (class_tasktemplate.create({ "name": index.toString(), "type": data["element_type"], "parameters": lib_object.patched(data["element_parameters"], { "input": input_raw, "output": output_raw, }) }, rawtask["name"])); } }).filter(x => (x != null)), }; } })); /** * @author fenris */ class_tasktemplate.register("empty", new class_tasktemplate({ "description": "creates an empty output file", "parameters": [ new class_taskparameter({ "name": "output", "extraction": raw => lib_path.filepointer_read(raw), "shape": lib_meta.from_raw({ "id": "string" }), "default": new class_nothing(), "description": "the path to the output file" }) ], "factory": (data) => { return { "outputs": [data["output"]], "actions": [ new class_action_mkdir(data["output"].location), new class_action_touch(data["output"]), ], }; } })); /** * @author fenris */ class_tasktemplate.register("copy", new class_tasktemplate_transductor({ "description": "copies a file", "parameters_additional": [ new class_taskparameter({ "name": "folder", "default": new class_just(false), }), ], "factory": (data) => { return { "inputs": [data["input"]], "outputs": /*[data["output"]]*/ [], "actions": [ new class_action_mkdir(data["output"].location), new class_action_copy(data["input"], data["output"], data["folder"]), ], }; } })); /** * @author fenris */ class_tasktemplate.register("concat", new class_tasktemplate_aggregator({ "description": "concatenates a list of files", "factory": (data) => { let inputs = class_tasktemplate_aggregator.inputs_all(data); let output = data["output"]; return { "inputs": inputs, "outputs": [output], "actions": [ new class_action_mkdir(output.location), new class_action_concat(inputs, output), ], }; } })); /** * @author fenris */ class_tasktemplate.register("typescript", new class_tasktemplate_aggregator({ "description": "compiles a list of typescript input files to a single javascript output file", "parameters_additional": [ new class_taskparameter({ "name": "declaration", "extraction": raw => ((raw == null) ? null : lib_path.filepointer_read(raw)), "shape": lib_meta.from_raw({ "id": "string", "parameters": { "soft": true } }), "default": new class_just(null), "description": "the path of the file in which to write the declaration; if not set, no declaration-script will be created", }), new class_taskparameter({ "name": "target", "shape": lib_meta.from_raw({ "id": "string", "parameters": { "soft": true } }), "default": new class_just(null), "description": "the tsc-switch 'target'; value NULL means 'don't specify'", }), new class_taskparameter({ "name": "allowUnreachableCode", "shape": lib_meta.from_raw({ "id": "boolean", "parameters": { "soft": true } }), "default": new class_just(null), "description": "the tsc-switch 'allowUnreachableCode'; value NULL means 'don't specify'", }), ], "factory": (data) => { let inputs = class_tasktemplate_aggregator.inputs_all(data); let outputs = [data["output"]]; let actions = [ new class_action_mkdir(data["output"].location), new class_action_tsc(inputs, data["output"], data["target"], data["allowUnreachableCode"], data["declaration"]), ]; if (data["declaration"] != null) { outputs = outputs.concat([ data["declaration"], ]); actions = actions.concat([ new class_action_mkdir(data["declaration"].location), new class_action_move({ "from": new lib_path.class_filepointer(data["output"].location, data["output"].filename.replace(new RegExp(".js$"), ".d.ts")), "to": data["declaration"], }), ]); } return { "inputs": inputs, "outputs": outputs, "actions": actions, }; }, })); /** * @author fenris */ class_tasktemplate.register("lesscss", new class_tasktemplate_aggregator({ "description": "compiles a list of lesscss input files to a single css output file", "factory": (data) => { let filepointer_temp = new lib_path.class_filepointer(lib_path.location_read(globalvars.configuration["tempfolder"]), "_.less"); return { "inputs": class_tasktemplate_aggregator.inputs_all(data), "outputs": data["outputs"], "actions": [ new class_action_mkdir(data["output"].location), new class_action_concat(class_tasktemplate_aggregator.inputs_all(data), filepointer_temp), new class_action_lessc(filepointer_temp, data["output"]), ], }; }, })); /** * @author fenris */ class_tasktemplate.register("php", new class_tasktemplate_aggregator({ "description": "compiles a list of php input files to a single output file", "parameters_additional": [ new class_taskparameter({ "name": "only_last", "shape": lib_meta.from_raw({ "id": "boolean" }), "default": new class_just(false), "description": "only compile the last file in the list and use the others as dependencies", }), ], "factory": (data) => { let inputs = class_tasktemplate_aggregator.inputs_all(data); return { "inputs": inputs, "outputs": data["outputs"], "actions": [ new class_action_mkdir(data["output"].location), new class_action_php(inputs, data["output"], data["only_last"]), ], }; }, })); /** * @author fenris,neu3no */ class_tasktemplate.register("babel", new class_tasktemplate_aggregator({ "description": "executes the babel transpiler", "parameters_additional": [ new class_taskparameter({ "name": "preset", "shape": lib_meta.from_raw({ "id": "string" }), "default": new class_just(null), "description": "[deprecated] single plugin; use 'presets' parameter instead (you can still use this parameter; its value is added to the plugin list)", }), new class_taskparameter({ "name": "presets", "shape": lib_meta.from_raw({ "id": "array", "parameters": { "shape_element": { "id": "string" } } }), "default": new class_just([]), "description": "a list of presets to use", }), new class_taskparameter({ "name": "plugins", "shape": lib_meta.from_raw({ "id": "array", "parameters": { "shape_element": { "id": "string" } } }), "default": new class_just([]), "description": "a list of plugins to use", }), new class_taskparameter({ "name": "minify", "shape": lib_meta.from_raw({ "id": "boolean" }), "default": new class_just(false), "description": "whether to pass the 'minify' argument to the babel command", }), ], "factory": (data) => { let inputs = class_tasktemplate_aggregator.inputs_all(data); return { "inputs": inputs, "outputs": [data["output"]], "actions": [ new class_action_mkdir(data["output"].location), new class_action_babel(inputs, data["output"], data["presets"].concat((data["preset"] == null) ? [] : [data["preset"]]), data["plugins"], data["minify"]), ], }; } })); /** * @author fenris */ class_tasktemplate.register("schwamm", new class_tasktemplate({ "description": null, "parameters": [ new class_taskparameter({ "name": "includes", "extraction": raw => raw.map(path => lib_path.filepointer_read(path)), "shape": lib_meta.from_raw({ "id": "array", "parameters": { "shape_element": { "id": "string" } } }), "default": new class_just([]), }), new class_taskparameter({ "name": "inputs", "extraction": raw => lib_object.map(raw, paths => paths.map(path => lib_path.filepointer_read(path))), "shape": lib_meta.from_raw({ "id": "map", "parameters": { "shape_key": { "id": "string" }, "shape_value": { "id": "array", "parameters": { "shape_element": { "id": "string" } } } } }), "default": new class_just({}), }), new class_taskparameter({ "name": "output", "extraction": raw => { let result = {}; if ("save" in raw) { result["save"] = lib_path.filepointer_read(raw["save"]); } if ("dump" in raw) { result["dump"] = lib_object.map(raw["dump"], path => lib_path.filepointer_read(path)); } return result; }, "shape": lib_meta.from_raw({ "id": "object", "parameters": { "fields": [ { "name": "save", "shape": { "id": "string", "parameters": { "soft": true } } }, { "name": "dump", "shape": { "id": "map", "parameters": { "shape_key": { "id": "string" }, "shape_value": { "id": "string" }, "soft": true } } } ] } }), }), ], "factory": (data) => { let inputs = []; let outputs = []; let actions = []; // includes { inputs = inputs.concat(data["includes"]); } // inputs { inputs = inputs.concat(lib_object.values(data["inputs"]).reduce((x, y) => x.concat(y), [])); } // output { if ("save" in data["output"]) { outputs = outputs.concat(data["output"]["save"]); actions = actions.concat([ new class_action_mkdir(data["output"]["save"].location), new class_action_schwamm(data["includes"], data["inputs"], data["output"]["save"]), ]); } if ("dump" in data["output"]) { outputs = outputs.concat(lib_object.values(data["output"]["dump"]).reduce((x, y) => x.concat(y), [])); actions = actions.concat(lib_object.to_array(data["output"]["dump"]) .map(({ "key": key, "value": value }) => [ new class_action_mkdir(value.location), new class_action_schwamm(data["includes"], data["inputs"], undefined, key, value), ]) .reduce((x, y) => x.concat(y), [])); } } return { "inputs": inputs, "outputs": outputs, "actions": actions, }; }, })); /** * @author fenris */ class_tasktemplate.register("schwamm-create", new class_tasktemplate({ "description": null, "parameters": [ new class_taskparameter({ "name": "includes", "extraction": raw => raw.map(path => lib_path.filepointer_read(path)), "shape": lib_meta.from_raw({ "id": "array", "parameters": { "shape_element": { "id": "string" } } }), "default": new class_just([]), }), new class_taskparameter({ "name": "adhoc", "extraction": raw => lib_object.map(raw, paths => paths.map(path => lib_path.filepointer_read(path))), "shape": lib_meta.from_raw({ "id": "map", "parameters": { "shape_key": { "id": "string" }, "shape_value": { "id": "array", "parameters": { "shape_element": { "id": "string" } } } } }), "default": new class_just({}), }), class_taskparameter.output_single(), ], "factory": (data) => { let inputs = []; let outputs = []; let actions = []; // includes { inputs = inputs.concat(data["includes"]); } // adhoc { inputs = inputs.concat(lib_object.values(data["adhoc"]).reduce((x, y) => x.concat(y), [])); } // output { outputs = outputs.concat([data["output"]]); actions = actions.concat([ new class_action_mkdir(data["output"].location), new class_action_schwamm(data["includes"], data["adhoc"], data["output"]), ]); } return { "inputs": inputs, "outputs": outputs, "actions": actions, }; }, })); /** * @author fenris */ class_tasktemplate.register("schwamm-apply", new class_tasktemplate({ "description": null, "parameters": [ new class_taskparameter({ "name": "path", "extraction": raw => lib_path.filepointer_read(raw), "shape": lib_meta.from_raw({ "id": "string" }), }), new class_taskparameter({ "name": "outputs", "extraction": raw => lib_object.map(raw, paths => paths.map(path => lib_path.filepointer_read(path))), "shape": lib_meta.from_raw({ "id": "map", "parameters": { "shape_key": { "id": "string" }, "shape_value": { "id": "array", "parameters": { "shape_element": { "id": "string" } } } } }), "default": new class_just({}), }), ], "factory": (data) => { let inputs = []; let outputs = []; let actions = []; // path { inputs = inputs.concat([data["path"]]); } // output { outputs = outputs.concat(lib_object.values(data["output"]).reduce((x, y) => x.concat(y), [])); actions = actions.concat(lib_object.to_array(data["output"]) .map(({ "key": key, "value": value }) => [ new class_action_mkdir(value.location), new class_action_schwamm([data["path"]], {}, undefined, key, value), ]) .reduce((x, y) => x.concat(y), [])); } return { "inputs": inputs, "outputs": outputs, "actions": actions, }; }, })); /** * @author fenris */ class_tasktemplate.register("locmerge", new class_tasktemplate_aggregator({ "description": "executes a locmerge command", "parameters_additional": [], "factory": (data) => { let inputs = class_tasktemplate_aggregator.inputs_all(data); return { "inputs": inputs, "outputs": [], "actions": [ new class_action_mkdir(data["output"].location), new class_action_locmerge(inputs, data["output"]), ], }; }, })); /** * @author fenris */ class_tasktemplate.register("script", new class_tasktemplate({ "description": "executes an external script", "parameters": [ class_taskparameter.input_list({ "default": new class_just([]) }), class_taskparameter.output_list({ "default": new class_just([]) }), new class_taskparameter({ "name": "path", "extraction": raw => lib_path.filepointer_read(raw), "shape": lib_meta.from_raw({ "id": "string" }), "default": new class_nothing(), "description": "the path to the script", }), new class_taskparameter({ "name": "interpreter", "extraction": raw => ((raw == null) ? null : lib_path.filepointer_read(raw)), "shape": lib_meta.from_raw({ "id": "string", "parameters": { "soft": true } }), "default": new class_just(null), "description": "the path to the interpreter to use; if value NULL is given, it is assumed that the script is self-executable", }), new class_taskparameter({ "name": "workdir", "extraction": raw => ((raw == null) ? null : lib_path.location_read(raw)), "shape": lib_meta.from_raw({ "id": "string", "parameters": { "soft": true } }), "default": new class_just(null), "description": "the path to the directory from where the script shall be executed; if value NULL is given, the workdir is the location of the script", }), ], "factory": (data) => { return { "inputs": data["inputs"], "outputs": data["outputs"], "actions": ([] .concat(data["outputs"].map(output => new class_action_mkdir(output.location))) .concat([ new class_action_exec({ "inputs": data["inputs"], "outputs": data["outputs"], "path": data["path"], "interpreter": data["interpreter"], "workdir": data["workdir"], }), ])), }; }, })); /** * @author fenris */ class class_target { /** * @author fenris */ constructor() { } } /** * @author fenris */ class class_target_regular extends class_target { /** * @author fenris */ constructor(identifier) { super(); this.identifier = identifier; } /** * @author fenris */ compile_action(action) { if (action instanceof class_action_adhoc) { let action_ = (action); return (action_.compilation(this.identifier)); } else { throw (new Error("no delegation for action '" + JSON.stringify(action) + "'")); } } } /** * @author fenris */ class class_target_ant extends class_target_regular { /** * @author fenris */ constructor() { super("ant"); } /** * @override * @author fenris */ tempfolder() { switch (globalvars.configuration.system) { case "linux": { return "/tmp/"; break; } case "bsd": { return "/tmp/"; break; } case "win": { return "${env.TEMP}\\"; break; } default: { throw (new Error(`invalid system '${globalvars.configuration.system}'`)); break; } } } /** * @author fenris */ compile_task({ "task": task, "path": path = [], "context": context = null, }) { let aggregate = false; let path_ = path_augment(path, task.name_get(), aggregate); let targets_core = [ new lib_ant.class_target({ "name": path_dump(path_), "dependencies": (task.sub_get() .filter(task_ => task_.active_get()) .map(task_ => path_dump(path_augment(path_, task_.name_get(), aggregate)))), "actions": ([] .concat((context == null) ? ([] .concat(task.actions() .map(action => this.compile_action(action)))) : [ new lib_ant.class_action(new lib_xml.class_node_complex("ant", { "antfile": "${ant.file}", "dir": context.as_string("linux"), "target": path_dump(path_augment(path_, name_mark("inner"))), "inheritAll": String(true), "inheritRefs": String(true), })), ])) }) ]; let targets_sub = [] .concat((context == null) ? [] : [ new lib_ant.class_target({ "name": path_dump(path_augment(path_, name_mark("inner"))), "dependencies": [], "actions": (task.actions() .map(action => this.compile_action(action))), }) ]) .concat(task.sub_get() .map(task_ => this.compile_task({ "task": task_, "path": path_, "context": ((context == null) ? task_.context_get() : ((task_.context_get() == null) ? context : context.relocate(task_.context_get()))), })) .reduce((x, y) => x.concat(y), [])); return [].concat(targets_core).concat(targets_sub); } /** * @author fenris */ compile_project(project) { let comments = [ `Project \"${project.name_get()}\"`, `This build script was generated by Koralle ${globalvars.configuration.version}`, ].map(x => new lib_ant.class_comment(x)); let targets = this.compile_task({ "task": project.roottask_get() }); return (new lib_ant.class_project({ "name": project.name_get(), "default": name_mark("root"), "comments": comments, "targets": targets, })); } /** * @override * @author fenris */ compile_project_string(project) { return this.compile_project(project).compile().compile(); } /** * @override * @author fenris */ execute(filepointer, workdir = ".") { return ((resolve, reject) => { let cp = nm_child_process.spawn("ant", [ `-f`, `${filepointer.as_string(globalvars.configuration.system)}`, ], {}); cp.stdout.on("data", [x => x.toString(), x => x.slice(0, x.length - 1), console.log].reduce(lib_call.compose)); cp.stderr.on("data", [x => x.toString(), x => x.slice(0, x.length - 1), console.error].reduce(lib_call.compose)); cp.on("error", error => reject(new class_error("subprocess not finish successfully", [error]))); cp.on("close", code => { if (code == 0) { resolve(undefined); } else { reject(new Error("unknown error while subprocess execution")); } }); }); } } /** * @author fenris */ class class_target_gnumake extends class_target_regular { /** * @author fenris */ constructor() { super("gnumake"); } /** * @override * @author fenris */ tempfolder() { switch (globalvars.configuration.system) { case "linux": { return "/tmp/"; break; } case "bsd": { return "/tmp/"; break; } case "win": { return "%TEMP%\\"; break; } default: { throw (new Error(`invalid system '${globalvars.configuration.system}'`)); break; } } } /** * @author fenris */ compile_task({ "task": task, "branch": branch = [], "depth": depth = 0, "context": context = null, "prefix": prefix = null }) { let log_begin = true; let log_end = false; let aggregate = false; let branch_ = path_augment(branch, task.name_get(), aggregate); let logging_begin = new class_action_echo((new class_message(path_dump(branch_), { "type": "log", "depth": depth, "prefix": prefix })).generate()); let logging_end = new class_action_echo((new class_message("✔", { "type": "log", "depth": depth, "prefix": prefix })).generate()); let rules_core = []; { // meta rule rules_core.push(new lib_gnumake.class_rule({ "name": path_dump(branch_), "dependencies": ([] .concat(log_begin ? [path_dump(path_augment(branch_, name_mark("logging"), true))] : []) .concat(task.sub_get() .filter(task_ => task_.active_get()) .map(task_ => path_dump(path_augment(branch_, task_.name_get(), aggregate)))) .concat(task.outputs().map(filepointer => filepointer_adjust(filepointer, context).as_string(globalvars.configuration.system)))), "actions": ([] .concat((task.outputs().length == 0) ? task.actions().map(action => dirwrap(context, this.compile_action(action))) : []) .concat((log_end ? [logging_end] : []) .map(action => this.compile_action(action)))), "phony": true, })); // logging if (log_begin) { rules_core.push(new lib_gnumake.class_rule({ "name": path_dump(path_augment(branch_, name_mark("logging"), true)), "actions": [logging_begin].map(action => this.compile_action(action)), "phony": true, })); } // actual rule if (task.outputs().length > 0) { rules_core.push(new lib_gnumake.class_rule({ "name": task.outputs().map(filepointer => filepointer_adjust(filepointer, context).as_string(globalvars.configuration.system)).join(" "), "dependencies": task.inputs().map(filepointer => filepointer_adjust(filepointer, context).as_string(globalvars.configuration.system)), "actions": task.actions().map(action => this.compile_action(action)).map(x => dirwrap(context, x)), "phony": false, })); } } let rules_sub = []; { rules_sub = task.sub_get() .map(task_ => this.compile_task({ "task": task_, "branch": branch_, "depth": depth + 1, "context": ((context == null) ? task_.context_get() : ((task_.context_get() == null) ? context : context.relocate(task_.context_get()))), "prefix": prefix, })) .reduce((x, y) => x.concat(y), []); } return [].concat(rules_core).concat(rules_sub); } /** * @author fenris */ compile_project(project) { let comments = [ `Project \"${project.name_get()}\"`, `This makefile was generated by Koralle ${globalvars.configuration.version}`, ].map(x => x); let rules = this.compile_task({ "task": project.roottask_get(), "prefix": project.name_get(), }); return (new lib_gnumake.class_sheet(rules, comments)); } /** * @override * @author fenris */ compile_project_string(project) { return (this.compile_project(project).compile(true)); } /** * @override * @author fenris */ execute(filepointer, workdir = process.cwd()) { return ((resolve, reject) => { let cp = nm_child_process.spawn("make", [ // `--directory=${workdir}`, // `--file=${filepointer.as_string(globalvars.configuration.system)}`, `-f`, `${filepointer.as_string(globalvars.configuration.system)}`, ], {}); cp.stdout.on("data", [x => x.toString(), x => x.slice(0, x.length - 1), console.log].reduce(lib_call.compose)); cp.stderr.on("data", [x => x.toString(), x => x.slice(0, x.length - 1), console.error].reduce(lib_call.compose)); cp.on("error", error => reject(new class_error("subprocess not finish successfully", [error]))); cp.on("close", code => { if (code == 0) { resolve(undefined); } else { reject(new Error("unknown error while subprocess execution")); } }); }); } } /** * scans a project and its subprojects and constructs a dependency-graph (more precise: a depth first spanning tree) * @param {type_depgraphnode} node the filepointer to the project.json, relative to the current working directory * @author fenris */ function scan(node, graph = null, depth = 0) { log("exploring node " + JSON.stringify(node), 4); let make_node = function (filepointer, rawproject = null) { let node = { "filepointer": filepointer, "rawproject": rawproject }; return node; }; if (graph == null) { log("creating new graph", 4); graph = new lib_structures.class_graph((x, y) => (x.filepointer.toString() == y.filepointer.toString())); graph.add_node(node); } return ((resolve, reject) => { log("reading description file", 4); lib_file.read_json(node.filepointer.toString())(data => { log("got data", 4); node.rawproject = data; lib_call.executor_chain(graph, lib_object.fetch(lib_object.fetch(node, "rawproject", {}, 0), "dependencies", [], 0).map(path => graph_ => (resolve_, reject_) => { log("looking through path " + path, 4); let node_ = make_node(node.filepointer.foo(lib_path.filepointer_read(path))); let edge = { "from": node_, "to": node }; graph_.add_edge(edge); if (graph.has(node_)) { // return lib_call.executor_resolve<class_graph<type_depgraphnode>, Error>(graph); resolve_(graph_); } else { graph.add_node(node_); scan(node_, graph_, depth + 1)(graph_ => { resolve_(graph_ /*.hasse()*/); }, reject_); } }))(resolve, reject); }, reason => { reject(reason); }); }); } /** * @author fenris */ class class_project { /** * @author fenris */ constructor(name, version, task) { this.name = name; this.version = version; this.task = task; this.graph = null; } /** * @author fenris */ name_get() { return this.name; } /** * @author fenris */ roottask_get() { return this.task; } /** * @desc [mutator] [setter] * @author fenris */ graph_set(graph) { this.graph = graph; } /** * @desc [accessor] [getter] * @author fenris */ graph_get() { return this.graph; } /** * @author fenris */ /* public dependencytasks(output : string) : Array<class_task> { return ( this.dependencies_all.map( function (path : string, index : int) : class_task_dependency { return ( new class_task_dependency( { "name": `__dependency_${index.toString()}`, "parameters": { "path": path, "output": output, "raw": true, }, } ) ); } ) ); } */ /** * @author fenris */ static create(filepointer, nameprefix = null) { return ((resolve, reject) => { let node = { "filepointer": filepointer, "rawproject": null }; log("scanning dependencies", 3); scan(node)(graph => { log("got dependency graph", 3); let dependencynodes = null; let error = null; try { log("applying topsort", 3); dependencynodes = graph .topsort() .filter(node => (node.filepointer.toString() != filepointer.toString())); error = null; } catch (exception) { error = new class_error("could not sort dependencies; probably circular structure", [exception]); } if (error == null) { log("creating core task", 3); let core = class_tasktemplate.create(node.rawproject.roottask); log("creating dependency tasks", 3); let dependencies = dependencynodes.map((node, index) => { let task = class_tasktemplate.create(node.rawproject.roottask, name_mark("dependency_" + (node.rawproject.name || lib_string.generate()))); task.context_set(node.filepointer.location); return task; }); log("creating root task", 3); let task = new class_task({ "name": name_mark("root"), "sub": [ new class_task({ "name": name_mark("dependencies"), "sub": dependencies, }), new class_task({ "name": name_mark("core"), "sub": [core], }), ] }); log("creating project", 3); let project = new class_project(node.rawproject.name || "(nameless project)", node.rawproject.version || "0.0.0", task); project.graph = graph; resolve(project); } else { reject(error); } }, reason => reject(new class_error("scanning dependencies failed", [reason]))); }); } } globalvars.invocation = { "interpreter": null, "path": "koralle", }; globalvars.configuration = { "version": "0.2.1", "tempfolder": null, "system": null, "execute": false, "output_target": null, "output_path": null, "sheet_path": null, "showgraph": false, "verbosity": 0, "name_splitter": "_", "name_prefix": "~", "programpaths": { "php": null, "tsc": null, "babel": null, "lessc": null, "schwamm": null, "locmerge": null, } }; /** * @author fenris */ function log(message, level = 0) { if (level <= globalvars.configuration.verbosity) { (new class_message(message, { "type": "log", "prefix": "koralle" })).stderr(); } } /** * @author fenris */ function main(args) { log("starting", 2); let arghandler = new lib_args.class_handler([ new lib_args.class_argument({ "name": "path", "type": "string", "default": "project.json", "info": "the path of the project-description-file", "kind": "positional", "parameters": {}, }), new lib_args.class_argument({ "name": "verbosity", "type": "int", "info": "how much informational output shall be given", "kind": "volatile", "parameters": { "indicators_long": ["verbosity"], "indicators_short": ["b"], }, }), new lib_args.class_argument({ "name": "help", "type": "boolean", "info": "show this help and exit", "kind": "volatile", "parameters": { "indicators_long": ["help"], "indicators_short": ["h"], }, }), new lib_args.class_argument({ "name": "tasklist", "type": "boolean", "info": "show the list of available tasks and exit", "kind": "volatile", "parameters": { "indicators_long": ["tasklist"], "indicators_short": ["l"], }, }), new lib_args.class_argument({ "name": "version", "type": "boolean", "info": "print the version to stdout and exit", "kind": "volatile", "parameters": { "indicators_long": ["version"], "indicators_short": ["v"], }, }), new lib_args.class_argument({ "name": "output", "type": "string", "default": "gnumake", "info": "the output build system; valid values are 'gnumake', 'ant'", "kind": "volatile", "parameters": { "indicators_long": ["output"], "indicators_short": ["o"], }, }), new lib_args.class_argument({ "name": "system", "type": "string", "default": "own", "info": "the target platform; valid values are 'own' (the system executing koralle), 'linux', 'bsd', 'win'", "kind": "volatile", "parameters": { "indicators_long": ["system"], "indicators_short": ["s"], }, }), new lib_args.class_argument({ "name": "file", "type": "string", "default": null, "info": "the file in which the result build script shall be written", "kind": "volatile", "parameters": { "indicators_long": ["file"], "indicators_short": ["f"], }, }), new lib_args.class_argument({ "name": "execute", "type": "boolean", "info": "if set, the build script will be executed instead of being printed to stdout", "kind": "volatile", "parameters": { "indicators_long": ["execute"], "indicators_short": ["x"], }, }), new lib_args.class_argument({ "name": "showgraph", "type": "boolean", "info": "if set, the dot language description of the dependency graph is printed to stdout", "kind": "volatile", "parameters": { "indicators_long": ["showgraph"], "indicators_short": ["g"], }, }), new lib_args.class_argument({ "name": "path_php", "type": "string", "default": "php", "info": "the path to the program 'php'", "kind": "volatile", "parameters": { "indicators_long": ["path-php"], "indicators_short": [], }, }), new lib_args.class_argument({ "name": "path_tsc", "type": "string", "default": "tsc", "info": "the path to the program 'tsc'", "kind": "volatile", "parameters": { "indicators_long": ["path-tsc"], "indicators_short": [], }, }), new lib_args.class_argument({ "name": "path_babel", "type": "string", "default": "babel", "info": "the path to the program 'babel'", "kind": "volatile", "parameters": { "indicators_long": ["path-babel"], "indicators_short": [], }, }), new lib_args.class_argument({ "name": "path_lessc", "type": "string", "default": "lessc", "info": "the path to the program 'lessc'", "kind": "volatile", "parameters": { "indicators_long": ["path-lessc"], "indicators_short": [], }, }), new lib_args.class_argument({ "name": "path_schwamm", "type": "string", "default": "schwamm", "info": "the path to the program 'schwamm'", "kind": "volatile", "parameters": { "indicators_long": ["path-schwamm"], "indicators_short": [], }, }), new lib_args.class_argument({ "name": "path_locmerge", "type": "string", "default": "locmerge", "info": "the path to the program 'locmerge'", "kind": "volatile", "parameters": { "indicators_long": ["path-locmerge"], "indicators_short": [], }, }), ]); // lib_args.verbosity = 5; log("reading command line arguments", 2); let argdata = arghandler.read("cli", args.join(" ")); globalvars.configuration.sheet_path = argdata["path"]; { // todo: auslagern (nach "plankton/base" vllt.) let systemmap = { "linux": "linux", "freebsd": "bsd", "openbsd": "bsd", "win32": "win" }; globalvars.configuration.system = (argdata["system"] == "own") ? systemmap[process.platform] : argdata["system"]; } globalvars.configuration.output_target = argdata["output"]; globalvars.configuration.execute = argdata["execute"]; globalvars.configuration.show_help = argdata["help"]; globalvars.configuration.show_version = argdata["version"]; globalvars.configuration.show_tasklist = argdata["tasklist"]; globalvars.configuration.showgraph = argdata["showgraph"]; globalvars.configuration.output_path = argdata["file"]; globalvars.configuration.verbosity = argdata["verbosity"]; globalvars.configuration.programpaths.php = argdata["path_php"]; globalvars.configuration.programpaths.tsc = argdata["path_tsc"]; globalvars.configuration.programpaths.babel = argdata["path_babel"]; globalvars.configuration.programpaths.lessc = argdata["path_lessc"]; globalvars.configuration.programpaths.schwamm = argdata["path_schwamm"]; globalvars.configuration.programpaths.locmerge = argdata["path_locmerge"]; return (lib_call.promise_resolve({}) .then(state => { // help if (globalvars.configuration.show_help) { let message = arghandler.generate_help({ "programname": "Koralle Build System Abstractor", "executable": "koralle", "author": "Christian Fraß <frass@greenscale.de>", "description": "Koralle is not a build-system itself. Instead it generates scripts for existing build-systems (e.g. GNU Make, Apache Ant, …) on base of a common json-description-file (usually named 'project.json'). Koralle is designed for reducing the amount of text needed to define the build-process.", }); (new class_message(message)).stdout(); return lib_call.promise_resolve(state); } else if (globalvars.configuration.show_version) { (new class_message(globalvars.configuration.version.toString())).stdout(); return lib_call.promise_resolve(state); } else if (globalvars.configuration.show_tasklist) { (new class_message(class_tasktemplate.list())).stdout(); return lib_call.promise_resolve(state); } else { return (lib_call.promise_resolve(state) .then(state => lib_call.promise_make((resolve, reject) => { log("setting up environment", 2); let sheet_filepointer = lib_path.filepointer_read(globalvars.configuration.sheet_path); // sheet_filepointer.location.go_thither(); state.sheet_filepointer = sheet_filepointer; resolve(state); })) .then(state => lib_call.promise_make((resolve, reject) => { log("setting up output", 2); let mapping = { "ant": new class_target_ant(), "gnumake": new class_target_gnumake(), "make": new class_target_gnumake(), }; let output_target = lib_object.fetch(mapping, globalvars.configuration.output_target, null, 0); if (output_target == null) { reject(new class_error(`no implementation found for output_target '${globalvars.configuration.output_target}'`)); } else { state.output_target = output_target; resolve(state); } })) .then(state => lib_call.promise_make((resolve, reject) => { log("setting up temp-folder", 2); try { globalvars.configuration.tempfolder = state.output_target.tempfolder(); resolve(state); } catch (exception) { reject(new class_error("couldn't setup temp folder", [exception])); } })) .then(state => lib_call.promise_make((resolve, reject) => { log("setting up project", 2); class_project.create(state.sheet_filepointer)(project => { state.project = project; resolve(state); }, reject); })) .then(state => { // graph if (globalvars.configuration.showgraph) { return (lib_call.promise_resolve(state) .then(state => lib_call.promise_make((resolve, reject) => { log("showing graph", 2); let output = (state.project.graph_get() .hasse() .output_dot({ "extract_label": node => (node.rawproject.name || node.filepointer.toString()) })); (new class_message(output)).stdout(); resolve(state); }))); } else { return (lib_call.promise_resolve(state) .then(state => lib_call.promise_make((resolve, reject) => { log("generating output", 2); try { let output_script = state.output_target.compile_project_string(state.project); state.output_script = output_script; resolve(state); } catch (exception) { reject(new class_error("generating build script failed", [exception])); } })) .then(state => lib_call.promise_make((resolve, reject) => { log("writing to file", 2); let filepointer; if (globalvars.configuration.output_path == null) { if (!globalvars.configuration.execute) { filepointer = null; } else { filepointer = new lib_path.class_filepointer( // new lib_path.class_location(null, new lib_path.class_path(["."])), lib_path.location_read(globalvars.configuration.tempfolder, globalvars.configuration.system), // lib_path.class_location.tempfolder(globalvars.configuration.system), "__koralle"); } } else { filepointer = lib_path.filepointer_read(globalvars.configuration.output_path); } state.output_filepointer = filepointer; if (filepointer == null) { (new class_message(state.output_script)).stdout(); resolve(state); } else { nm_fs.writeFile(filepointer.toString(), state.output_script, error => { if (error == null) { resolve(state); } else { reject(new class_error("writing to file failed", [error])); } }); } })) .then(state => lib_call.promise_make((resolve, reject) => { log("executing", 2); if (!globalvars.configuration.execute) { resolve(state); } else { state.output_target.execute(state.output_filepointer)(result => resolve(state), reason => reject(new class_error("execution of build script failed", [reason]))); } }))); } })); } })); } log("intro", 2); globalvars.invocation = { "interpreter": process.argv[0], "path": process.argv[1], }; main(process.argv.slice(2)) .then(function (dummy) { // (new class_message("successfull", {"type": "information", "prefix": "koralle"})).stderr(); process.exit(0); }, function (reason) { // throw reason; console.error(reason); (new class_message(`the following error occured: '${reason.toString()}'`, { "type": "error", "prefix": "koralle" })).stderr(); process.exit(-1); });