UNPKG

raas-core

Version:
175 lines 6.71 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const model_1 = require("./model"); const fs = require("fs"); const events_1 = require("./events"); const path = require("path"); const analysisResults_1 = require("./analysisResults"); const PRETTIFY_WS = 4; class RhamtModelService { constructor(model, outDir) { this.model = model; this.outDir = outDir; this.loaded = false; this.onLoaded = new events_1.rhamtEvents.TypedEvent(); this.onCreated = new events_1.rhamtEvents.TypedEvent(); this.onDeleted = new events_1.rhamtEvents.TypedEvent(); this.onChanged = new events_1.rhamtEvents.TypedEvent(); this.onSaved = new events_1.rhamtEvents.TypedEvent(); } load() { return new Promise((resolve, reject) => { const location = path.join(this.outDir, 'model.json'); fs.exists(location, exists => { if (exists) { fs.readFile(location, (e, data) => { if (e) reject(e); else this.parse(data).then(() => resolve(this.model)).catch(reject); }); } else resolve(); }); }); } parse(data) { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { if (data.byteLength > 0) { const configs = JSON.parse(data).configurations; for (const entry of configs) { const config = new model_1.RhamtConfiguration(); RhamtModelService.copy(entry, config); yield RhamtModelService.loadResults(entry, config, this.outDir); this.model.configurations.set(config.id, config); } } this.loaded = true; this.onLoaded.emit(this.model); resolve(); })); } static loadResults(source, target, outDir) { return __awaiter(this, void 0, void 0, function* () { if (!source.results) { return Promise.resolve(); } return new Promise(resolve => { const results = path.join(outDir, 'output', source._id, `${source._id}.xml`); fs.exists(results, (exists) => __awaiter(this, void 0, void 0, function* () { if (exists) { try { const dom = yield analysisResults_1.AnaysisResultsUtil.loadFromLocation(results); target.results = new analysisResults_1.AnalysisResults(dom, source.results.summary); } catch (e) { console.log(`Error loading analysis results for configuration at ${results} - ${e}`); } } resolve(); })); }); }); } static copy(source, target) { target.id = source._id; Object.keys(source.options).forEach(function (key) { target.options[key] = source.options[key]; }); target.runtime = source.runtime; target.jvm = source.jvm; } save(outDir) { const data = { configurations: this.model.getConfigurations() }; this.doSave(outDir, data); } doSave(outDir, data) { return new Promise((resolve, reject) => { fs.writeFile(path.join(outDir, 'model.json'), JSON.stringify(data, null, PRETTIFY_WS), null, e => { if (e) reject(e); else resolve(); }); }); } getConfiguration(id) { return this.model.configurations.get(id); } createConfiguration() { return this.createConfigurationWithName(this.generateConfigurationName()); } createConfigurationWithName(name) { const config = new model_1.RhamtConfiguration(); config.options['name'] = name; this.loadDefaults(config); this.model.configurations.set(config.id, config); this.onCreated.emit(config); return config; } loadDefaults(config) { config.options['sourceMode'] = true; config.options['online'] = true; config.options['skipReports'] = true; config.options['overwrite'] = true; } deleteConfiguration(id, configuration) { id = id ? id : configuration ? configuration.id : undefined; if (id) { const toDelete = this.model.configurations.get(id); const deleted = this.model.configurations.delete(id); if (deleted && toDelete) { this.onDeleted.emit(toDelete); } return deleted; } return false; } update(id, name, cli, javaHome, options) { const config = this.getConfiguration(id); return config; } generateConfigurationName() { let newName = 'rhamtConfiguration'; if (this.model.exists(newName)) { for (let i = 0; i < 1000; i++) { if (!this.model.exists(`${newName}-${i}`)) { newName = `${newName}-${i}`; break; } } } return newName; } static generateUniqueId() { return `-${Math.random().toString(36).substr(2, 9)}-${Math.random().toString(36).substr(2, 9)}`; } static getTimestamp() { const date = new Date(); return `${date.getDate()}/ ${(date.getMonth() + 1)}/ ${date.getFullYear()}@ ${date.getHours()}: ${date.getMinutes()}: ${date.getSeconds()}`; } dispose() { } onModelLoaded(listen) { if (this.loaded) { listen(this.model); } return this.onLoaded.on(listen); } } exports.RhamtModelService = RhamtModelService; //# sourceMappingURL=modelService.js.map