@atomist/sdm-pack-spring
Version:
Atomist software delivery machine extension pack for Spring and Spring Boot applications
138 lines • 6.02 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 automation_client_1 = require("@atomist/automation-client");
const fs = require("fs");
const _ = require("lodash");
const path = require("path");
const JavaProjectStructure_1 = require("./JavaProjectStructure");
exports.AllJavaFiles = "**/*.java";
exports.JavaSourceFiles = "**/src/main/java/**/*.java";
exports.JavaTestFiles = "**/src/main/test/**/*.java";
exports.AllJavaAndKotlinFiles = "**/{*.java,*.kt}";
exports.JavaAndKotlinSource = "**/src/main/**/{*.java,*.kt}";
exports.KotlinSourceFiles = "**/src/main/kotlin/**/*.kt";
/**
* Move files from one package to another. Defaults to
* working on all Java and Kotlin source. Will work for Scala
* if you pass in the appropriate glob pattern to select the files you want.
*
* @param project project whose files should be moved
* @param oldPackage name of package to move from
* @param newPackage name of package to move to
* @param globPattern glob to select files. Defaults to all Java files in the project
*/
function movePackage(project, oldPackage, newPackage, globPattern = exports.AllJavaAndKotlinFiles) {
return __awaiter(this, void 0, void 0, function* () {
const pathToReplace = packageToPath(oldPackage);
const newPath = packageToPath(newPackage);
automation_client_1.logger.debug("Replacing path '%s' with '%s', package '%s' with '%s'", pathToReplace, newPath, oldPackage, newPackage);
yield automation_client_1.projectUtils.doWithFiles(project, globPattern, (f) => __awaiter(this, void 0, void 0, function* () {
yield f.replaceAll(oldPackage, newPackage);
yield f.setPath(f.path.replace(pathToReplace, newPath));
}));
const projectDir = project.baseDir;
if (projectDir) {
cleanEmptyFoldersRecursively(projectDir);
}
return project;
});
}
exports.movePackage = movePackage;
function cleanEmptyFoldersRecursively(folder) {
const isDir = fs.statSync(folder).isDirectory();
if (!isDir) {
return;
}
let files = fs.readdirSync(folder);
if (files.length > 0) {
files.forEach(file => {
const fullPath = path.join(folder, file);
cleanEmptyFoldersRecursively(fullPath);
});
// re-evaluate files; after deleting subfolder
// we may have parent folder empty now
files = fs.readdirSync(folder);
}
if (files.length === 0) {
fs.rmdirSync(folder);
return;
}
}
/**
* Convert a Java package (with dots) to a source path
* @param pkg package
* @return {string}
*/
function packageToPath(pkg) {
return pkg.replace(/\./g, "/");
}
exports.packageToPath = packageToPath;
function classNameFromFqn(fqn) {
return _.last(fqn.split("."));
}
exports.classNameFromFqn = classNameFromFqn;
function packageNameFromFqn(fqn) {
return _.dropRight(fqn.split(".")).join(".");
}
exports.packageNameFromFqn = packageNameFromFqn;
/**
* Rename all instances of a Java or Kotlin class. This method is somewhat
* surgical when replacing appearances in Java code but brutal when
* replacing appearances elsewhere.
* Renames class stem (start of class name), not just a whole class name
* Does not change filename, which is necessary in Java.
* @param project project whose Java classes should be renamed
* @param oldClass name of class to move from
* @param newClass name of class to move to
*/
function renameClass(project, oldClass, newClass) {
automation_client_1.logger.debug("Replacing old class stem '%s' with '%s'", oldClass, newClass);
return automation_client_1.projectUtils.doWithFiles(project, exports.AllJavaAndKotlinFiles, (f) => __awaiter(this, void 0, void 0, function* () {
if (f.name.includes(oldClass)) {
yield f.rename(f.name.replace(oldClass, newClass));
const oldClassRe = new RegExp("([\( \t<])" + oldClass, "gm");
yield f.replace(oldClassRe, `$1${newClass}`);
}
}));
}
exports.renameClass = renameClass;
/**
* Infer the root package and move it to the new root package
* @param {string} rootPackage new root package
* @param {Project} p
* @return {Promise<Project>}
*/
function inferStructureAndMovePackage(rootPackage, p) {
return __awaiter(this, void 0, void 0, function* () {
const structure = yield JavaProjectStructure_1.JavaProjectStructure.infer(p);
return !!structure ?
movePackage(p, structure.applicationPackage, rootPackage) :
p;
});
}
exports.inferStructureAndMovePackageTransform = (p, c, params) => inferStructureAndMovePackage(params.rootPackage, p);
//# sourceMappingURL=javaProjectUtils.js.map