@atomist/sdm-pack-spring
Version:
Atomist software delivery machine extension pack for Spring and Spring Boot applications
124 lines • 6.15 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 antlr_1 = require("@atomist/antlr");
const automation_client_1 = require("@atomist/automation-client");
const tree_path_1 = require("@atomist/tree-path");
const path = require("path");
const JavaProjectStructure_1 = require("../../java/JavaProjectStructure");
const javaProjectUtils_1 = require("../../java/javaProjectUtils");
const packageInfo_1 = require("../../java/query/packageInfo");
/**
* Path expression for a class name annotated with Spring Boot.
* Uses Java formal grammar.
* @type {string}
*/
exports.SpringBootAppClassInJava = `//normalClassDeclaration
[//annotation[@value='@SpringBootApplication']]
/identifier`;
/**
* Path expression for a class name annotated with Spring Boot.
* Uses Kotlin formal grammar.
* @type {string}
*/
exports.SpringBootAppClassInKotlin = `//classDeclaration
[//annotation[@value='@SpringBootApplication']]
//Identifier`;
/**
* Represents the structure of a Spring Boot project,
* which can be inferred from its contents. Covers application class
* and starters.
*/
class SpringBootProjectStructure {
/**
* @param applicationPackage The package with the Spring Boot application class in it.
* @param applicationClass Name of the application class within the given package
* @param appClassFile path to the file containing the @SpringBootApplication annotation
*/
constructor(applicationPackage, applicationClass, appClassFile) {
this.applicationPackage = applicationPackage;
this.applicationClass = applicationClass;
this.appClassFile = appClassFile;
/**
* The stem of the application class. Strip "Application" if present.
*/
this.applicationClassStem = this.applicationClass.replace(/Application$/, "");
}
/**
* Infer the Spring project structure of the given project, if found
* @param {ProjectAsync} p
* @return {Promise<SpringBootProjectStructure>}
*/
static inferFromJavaSource(p, globOptions = javaProjectUtils_1.JavaSourceFiles) {
return __awaiter(this, void 0, void 0, function* () {
return this.inferFromSourceWithJavaLikeImports(p, antlr_1.Java9FileParser, globOptions, exports.SpringBootAppClassInJava);
});
}
static inferFromKotlinSource(p, globOptions = javaProjectUtils_1.KotlinSourceFiles) {
return __awaiter(this, void 0, void 0, function* () {
return this.inferFromSourceWithJavaLikeImports(p, antlr_1.KotlinFileParser, globOptions, exports.SpringBootAppClassInKotlin);
});
}
static inferFromJavaOrKotlinSource(p) {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.inferFromJavaSource(p)) || this.inferFromKotlinSource(p);
});
}
static inferFromSourceWithJavaLikeImports(p, parserOrRegistry, globOptions, pathExpression) {
return __awaiter(this, void 0, void 0, function* () {
const fileHits = yield automation_client_1.astUtils.findFileMatches(p, parserOrRegistry, globOptions, pathExpression);
if (fileHits.length === 0) {
return undefined;
}
if (fileHits.length > 1) {
const msg = `Found more than one Spring Boot application annotation in files: ` +
fileHits.map(f => path.join(f.file.path, f.file.name)).join(",");
automation_client_1.logger.warn(msg);
throw new Error(msg);
}
const fh = fileHits[0];
// It's in the default package if no match found
const packageName = {
name: fh.file.extension === "java" ?
// TODO using package workaround for Antlr bug
((yield packageInfo_1.packageInfo(p, fh.file.path)) || { fqn: "" }).fqn :
tree_path_1.evaluateScalarValue(fh.fileNode, JavaProjectStructure_1.KotlinPackage) ||
"",
};
const appClass = fh.matches[0].$value;
if (packageName && appClass) {
automation_client_1.logger.debug("Successful Spring Boot inference on %j: packageName '%s', '%s'", p.id, packageName.name, appClass);
return new SpringBootProjectStructure(packageName.name, appClass, fh.file);
}
else {
automation_client_1.logger.debug("Unsuccessful Spring Boot inference on %j: packageName '%j', '%s'", p.id, packageName, appClass);
return undefined;
}
});
}
}
exports.SpringBootProjectStructure = SpringBootProjectStructure;
//# sourceMappingURL=SpringBootProjectStructure.js.map