"use strict";
/*
 * Copyright © 2020 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.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpringBootProjectStructure = exports.SpringBootAppClassInKotlin = exports.SpringBootAppClassInJava = void 0;
const antlr_1 = require("@atomist/antlr");
const astUtils_1 = require("@atomist/automation-client/lib/tree/ast/astUtils");
const logger_1 = require("@atomist/automation-client/lib/util/logger");
const tree_path_1 = require("@atomist/tree-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 any number of structures, looking at both Java and Kotlin
     */
    static async inferFromJavaOrKotlin(p) {
        return (await this.inferFromSourceWithJavaLikeImports(p, antlr_1.Java9FileParser, javaProjectUtils_1.JavaSourceFiles, exports.SpringBootAppClassInJava)).concat(await this.inferFromSourceWithJavaLikeImports(p, antlr_1.KotlinFileParser, javaProjectUtils_1.KotlinSourceFiles, exports.SpringBootAppClassInKotlin));
    }
    static async inferFromSourceWithJavaLikeImports(p, parserOrRegistry, globOptions, pathExpression) {
        const fileHits = await astUtils_1.fileMatches(p, {
            parseWith: parserOrRegistry,
            globPatterns: globOptions,
            pathExpression,
        });
        const matches = [];
        for (const fh of fileHits) {
            // It's in the default package if no match found
            const packageName = {
                name: fh.file.extension === "java"
                    ? // TODO using package workaround for Antlr bug
                        ((await 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) {
                logger_1.logger.debug("Successful Spring Boot inference on %j: packageName '%s', '%s'", p.id, packageName.name, appClass);
                matches.push(new SpringBootProjectStructure(packageName.name, appClass, fh.file));
            }
            else {
                logger_1.logger.debug("Unsuccessful Spring Boot inference on %j: packageName '%j', '%s'", p.id, packageName, appClass);
            }
        }
        return matches;
    }
}
exports.SpringBootProjectStructure = SpringBootProjectStructure;
//# sourceMappingURL=SpringBootProjectStructure.js.map