@atomist/sdm-pack-spring
Version:
Atomist software delivery machine extension pack for Spring and Spring Boot applications
126 lines • 4.72 kB
JavaScript
;
/*
* Copyright © 2019 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const microgrammar_1 = require("@atomist/microgrammar");
/**
* Parse the given file within the project, returning updatable properties
* @param {Project} p
* @param {string} path
* @return {Promise<PropertiesFile>}
*/
function parseProperties(p, path) {
return __awaiter(this, void 0, void 0, function* () {
const f = yield p.getFile(path);
if (!!f) {
const content = yield f.getContent();
const updatable = microgrammar_1.Microgrammar.updatable(propertiesGrammar.findMatches(content), content);
return new PropertiesFileImpl(path, updatable, p);
}
else {
return new PropertiesFileImpl(path, undefined, p);
}
});
}
exports.parseProperties = parseProperties;
function propertiesObject(p, path) {
return __awaiter(this, void 0, void 0, function* () {
return (yield parseProperties(p, path)).obj;
});
}
exports.propertiesObject = propertiesObject;
/**
* Code transform to add the property to the given file,
* creating it if it doesn't exist. Updates any existing property.
* @param {Property} prop
* @param {string} path
* @return {CodeTransform}
*/
function addProperty(prop, path) {
return (p) => __awaiter(this, void 0, void 0, function* () {
const propsFile = yield parseProperties(p, path);
return propsFile.addProperty(prop);
});
}
exports.addProperty = addProperty;
class PropertiesFileImpl {
constructor(path, updatable, project) {
this.path = path;
this.updatable = updatable;
this.project = project;
}
get properties() {
return !!this.updatable ? this.updatable.matches : [];
}
get obj() {
const props = this.properties;
const x = {};
for (const prop of props) {
Object.defineProperty(x, prop.key, {
get() {
return prop.value;
},
set(to) {
throw new Error("Set not supported");
},
});
}
return x;
}
addProperty(prop) {
return __awaiter(this, void 0, void 0, function* () {
const existing = this.properties.find(p => p.key === prop.key);
if (!!existing) {
existing.value = prop.value;
return this.flush();
}
const comment = !!prop.comment ? `# ${prop.comment}\n` : "";
const formattedProperty = `\n\n${comment}${prop.key}=${prop.value}\n`;
const f = yield this.project.getFile(this.path);
if (!!f) {
const oldContent = yield f.getContent();
yield f.setContent(oldContent + formattedProperty);
}
else {
yield this.project.addFile(this.path, formattedProperty);
}
});
}
flush() {
return __awaiter(this, void 0, void 0, function* () {
if (!!this.updatable) {
const f = yield this.project.getFile(this.path);
yield f.setContent(this.updatable.updated());
}
});
}
}
const PropertyKey = /[^[=\s]+/;
const propertiesGrammar = microgrammar_1.Microgrammar.fromString("${key}=${value}", {
key: PropertyKey,
value: /.*/,
comment: "",
});
//# sourceMappingURL=propertiesParser.js.map