@atomist/sdm-pack-spring
Version:
Atomist software delivery machine extension pack for Spring and Spring Boot applications
179 lines • 8.07 kB
JavaScript
;
/*
* Copyright © 2018 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const automation_client_1 = require("@atomist/automation-client");
const _ = require("lodash");
const formatUtils_1 = require("../../util/formatUtils");
const XmldocFileParser_1 = require("../../xml/XmldocFileParser");
const fromPom_1 = require("../parse/fromPom");
/**
* Add the given plugin to projects. It's not an error
* if the project doesn't have a POM. The transform will do nothing
* in this case.
* @param {Plugin} plugin
* @return {CodeTransform}
*/
function addPluginTransform(plugin) {
return (p) => __awaiter(this, void 0, void 0, function* () {
if (yield p.hasFile("pom.xml")) {
const plg = yield fromPom_1.findDeclaredPlugins(p);
if (plg.length === 0) {
throw new Error("No plugins in POM: Cannot add plugin");
}
if (plg.some(pl => plugin.artifact === pl.artifact && plugin.group === pl.group)) {
automation_client_1.logger.info("Plugin [%s] already present. Nothing to do", plugin.artifact);
}
else {
automation_client_1.logger.info("Adding plugin [%s]", plugin.artifact);
// Add after last dependency
const lastPlugin = _.last(plg);
yield automation_client_1.astUtils.doWithAllMatches(p, new XmldocFileParser_1.XmldocFileParser(), "pom.xml", `//project/build/plugins/plugin[/artifactId[@innerValue='${lastPlugin.artifact}']]`, m => {
const pluginContent = formatUtils_1.indent(pluginStanza(plugin), " ", 4);
m.append("\n" + pluginContent);
});
}
}
});
}
exports.addPluginTransform = addPluginTransform;
/**
* Add the given plugin to project in plugin management. It's not an error
* if the project doesn't have a POM. The transform will do nothing
* in this case.
* @param {Plugin} plugin
* @return {CodeTransform}
*/
function addManagedPluginTransform(plugin) {
return (p) => __awaiter(this, void 0, void 0, function* () {
if (yield p.hasFile("pom.xml")) {
const plg = yield fromPom_1.findDeclaredManagedPlugins(p);
if (plg.length === 0) {
throw new Error("No plugin management in POM: Cannot add plugin");
}
if (plg.some(pl => plugin.artifact === pl.artifact && plugin.group === pl.group)) {
automation_client_1.logger.info("Plugin [%s] already present. Nothing to do", plugin.artifact);
}
else {
automation_client_1.logger.info("Adding plugin [%s]", plugin.artifact);
// Add after last dependency
const lastPlugin = _.last(plg);
yield automation_client_1.astUtils.doWithAllMatches(p, new XmldocFileParser_1.XmldocFileParser(), "pom.xml", `//project/build/pluginmanagement/plugins/plugin[/artifactId[@innerValue='${lastPlugin.artifact}']]`, m => {
const pluginContent = formatUtils_1.indent(managedPluginStanza(plugin), " ", 5);
m.append("\n" + pluginContent);
});
}
}
});
}
exports.addManagedPluginTransform = addManagedPluginTransform;
/**
* Command to add a Maven plugin to the project in <build><plugins>
*/
exports.AddMavenPlugin = {
name: "add-maven-plugin",
intent: ["add maven plugin"],
description: "Add a Maven plugin to the project",
parameters: {
artifact: { description: "Plugin artifact id to add" },
group: { description: "Group of the artifact" },
version: { description: "Plugin version" },
},
transform: [
(p, ci) => __awaiter(void 0, void 0, void 0, function* () {
return addPluginTransform(Object.assign({}, ci.parameters))(p, ci);
}),
],
transformPresentation: ci => new automation_client_1.editModes.PullRequest(`add-dependency-${ci.parameters.artifact}`, `Add dependency ${ci.parameters.artifact}`),
};
/**
* Command to add a Maven plugin to the project in pluginManagement
*/
exports.AddManagedMavenPlugin = {
name: "add-maven-plugin",
intent: ["add managed maven plugin"],
description: "Add a Maven plugin oin pluginManagement to the project",
parameters: {
artifact: { description: "Plugin artifact id to add" },
group: { description: "Group of the artifact" },
version: { description: "Plugin version" },
},
transform: [
(p, ci) => __awaiter(void 0, void 0, void 0, function* () {
return addManagedPluginTransform(Object.assign({}, ci.parameters))(p, ci);
}),
],
transformPresentation: ci => new automation_client_1.editModes.PullRequest(`add-dependency-${ci.parameters.artifact}`, `Add dependency ${ci.parameters.artifact}`),
};
// TODO: add executions support
function pluginStanza(plugin) {
return `<plugin>
<groupId>${plugin.group}</groupId>
<artifactId>${plugin.artifact}</artifactId>` +
versionTag(plugin) +
configurationTag(plugin) +
extensionsTag(plugin) +
inheritedTag(plugin) +
`</plugin>`;
}
function managedPluginStanza(plugin) {
return `<plugin>
<groupId>${plugin.group}</groupId>
<artifactId>${plugin.artifact}</artifactId>` +
versionTag(plugin) +
configurationTag(plugin) +
`</plugin>`;
}
function versionTag(plugin) {
return `${!!plugin.version ? `\n <version>${plugin.version}</version>` : ""}`;
}
function inheritedTag(plugin) {
return `${!!plugin.inherited ? `\n <inherited>${plugin.inherited}</inherited>` : ""}`;
}
function extensionsTag(plugin) {
return `${!!plugin.extensions ? `\n <extensions>${plugin.extensions}</extensions>` : ""}`;
}
function configurationTag(plugin) {
return `${!!plugin.configuration ? `\n <configuration>
${_.join(getAsTagLines(plugin.configuration, 2), "\n")}
</configuration>` : ""}`;
}
function getAsTagLines(configuration, indentLevel) {
const configValues = [];
const propertyNames = Object.getOwnPropertyNames(configuration);
for (const propertyName of propertyNames) {
const configValue = configuration[propertyName];
if (typeof configValue === "string") {
configValues.push(`${_.padStart("", 4 * indentLevel, " ")}<${propertyName}>${configValue}</${propertyName}>`);
}
else {
configValues.push(`${_.padStart("", 4 * indentLevel, " ")}<${propertyName}>`);
configValues.push(...getAsTagLines(configValue, indentLevel + 1));
configValues.push(`${_.padStart("", 4 * indentLevel, " ")}</${propertyName}>`);
}
}
return configValues;
}
//# sourceMappingURL=addPluginTransform.js.map