@atomist/sdm-pack-spring
Version:
Atomist software delivery machine extension pack for Spring and Spring Boot applications
99 lines • 4.44 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 antlr_1 = require("@atomist/antlr");
const automation_client_1 = require("@atomist/automation-client");
const _ = require("lodash");
const javaProjectUtils_1 = require("./javaProjectUtils");
const javaPathExpressions_1 = require("./query/javaPathExpressions");
/**
* Path expression using the Kotlin grammar for a Java package declaration
* @type {string}
*/
exports.KotlinPackage = "//packageHeader//identifier";
/**
* Represents Java project structure (nested packages following Java naming conventions)
* which can be inferred from project contents.
* Also works for Kotlin.
*/
class JavaProjectStructure {
/**
* @param applicationPackage The first Java package found in the project.
*/
constructor(applicationPackage) {
this.applicationPackage = applicationPackage;
}
/**
* Find root Java or Kotlin package
* @param {ProjectAsync} p
* @return {Promise<JavaProjectStructure>}
*/
static infer(p) {
return __awaiter(this, void 0, void 0, function* () {
// Treat Java and Kotlin as one
const packages = (yield automation_client_1.astUtils.gatherFromMatches(p, antlr_1.Java9FileParser, javaProjectUtils_1.JavaSourceFiles, javaPathExpressions_1.JavaPackage,
// TODO workaround for recursive ANTLR bug
m => m.$value.replace(/.*package (.*);/, "$1")))
.concat((yield automation_client_1.astUtils.findMatches(p, antlr_1.KotlinFileParser, javaProjectUtils_1.KotlinSourceFiles, exports.KotlinPackage)).map(m => m.$value));
const uniquePackages = _.uniq(packages);
if (uniquePackages.length === 0) {
return undefined;
}
if (uniquePackages.length === 1) {
const jps = new JavaProjectStructure(uniquePackages[0]);
automation_client_1.logger.debug("Successful JavaProjectStructure inference on %j: Sole package is %j", p.id, jps);
return jps;
}
const longestPrefix = sharedStart(uniquePackages);
if (!!longestPrefix) {
const jps = new JavaProjectStructure(longestPrefix.replace(/\.$/, ""));
automation_client_1.logger.debug("Successful JavaProjectStructure inference on %j: Shortest path is %j", p.id, jps);
return jps;
}
else {
automation_client_1.logger.debug("Unsuccessful JavaProjectStructure inference on %j", p.id);
return undefined;
}
});
}
}
exports.JavaProjectStructure = JavaProjectStructure;
// Taken from https://stackoverflow.com/questions/1916218/find-the-longest-common-starting-substring-in-a-set-of-strings
function sharedStart(array) {
const A = array.concat().sort();
if (!A) {
return "";
}
const a1 = A[0];
const a2 = A[A.length - 1];
const L = a1.length;
let i = 0;
while (i < L && a1.charAt(i) === a2.charAt(i)) {
i++;
}
return a1.substring(0, i);
}
//# sourceMappingURL=JavaProjectStructure.js.map