UNPKG

@atomist/sdm-pack-spring

Version:

Atomist software delivery machine extension pack for Spring and Spring Boot applications

121 lines 5.2 kB
"use strict"; /* * 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 antlr_1 = require("@atomist/antlr"); const automation_client_1 = require("@atomist/automation-client"); const tree_path_1 = require("@atomist/tree-path"); const _ = require("lodash"); const formatUtils_1 = require("../../util/formatUtils"); const javaProjectUtils_1 = require("../javaProjectUtils"); const javaPathExpressions_1 = require("../query/javaPathExpressions"); const packageInfo_1 = require("../query/packageInfo"); /** * Return the imports already in this project * @param {Project} p * @param {string} path * @return {Promise<Import[]>} */ function existingImports(p, path) { return __awaiter(this, void 0, void 0, function* () { return automation_client_1.astUtils.gatherFromMatches(p, antlr_1.Java9FileParser, path, javaPathExpressions_1.JavaImports, m => { const fqnChild = tree_path_1.evaluateScalar(m, "//typeName"); return { fqn: fqnChild.$value, offset: m.$offset, }; }); }); } exports.existingImports = existingImports; /** * Add the given import if necessary * @return {CodeTransform} */ function addImport(opts) { return (p) => __awaiter(this, void 0, void 0, function* () { const f = yield p.getFile(opts.sourceFilePath); if (!f) { automation_client_1.logger.warn("Cannot add import %s to %s: path not found", opts.fqn, opts.sourceFilePath); return; } let newImportLine = `import ${opts.fqn};\n`; const imports = yield existingImports(p, opts.sourceFilePath); if (imports.some(i => i.fqn === opts.fqn)) { automation_client_1.logger.warn("Doing nothing: import %s already present %s", opts.fqn, opts.sourceFilePath); return; } // Where to insert let position; if (imports.length === 0) { // No existing imports. // Must find where to put imports in this case. // First look for package const pi = yield packageInfo_1.packageInfo(p, opts.sourceFilePath); if (!!pi) { position = pi.insertAfter; newImportLine = "\n\n" + newImportLine; } else { position = 0; } } else { const lastImport = _.last(imports); const content = yield f.getContent(); position = lastImport.offset + 1 + formatUtils_1.countTill(content.substr(lastImport.offset), ";"); if (content.charAt(position) === "\n") { ++position; } } yield formatUtils_1.insertAt(f, position, newImportLine); }); } exports.addImport = addImport; function removeUnusedImports(opts) { return (p) => __awaiter(this, void 0, void 0, function* () { const file = yield p.getFile(opts.sourceFilePath); if (!!file) { const source = yield file.getContent(); return automation_client_1.astUtils.doWithAllMatches(p, antlr_1.Java9FileParser, opts.sourceFilePath, javaPathExpressions_1.JavaImports, m => { if (m.$value.includes(".*")) { // Don't touch .* imports, we can't figure it out return; } const fqnChild = tree_path_1.evaluateScalar(m, "//typeName"); const simpleName = javaProjectUtils_1.classNameFromFqn(fqnChild.$value); // Look in the remainder of the file // TODO what about comments? const found = source.substring(m.$offset + m.$value.length).includes(simpleName); if (!found) { m.zap({}); } }); } return p; }); } exports.removeUnusedImports = removeUnusedImports; //# sourceMappingURL=imports.js.map