UNPKG

@atomist/sdm

Version:

Atomist Software Delivery Machine SDK

127 lines 5.59 kB
"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.inferStructureAndMovePackageTransform = exports.renameClass = exports.packageNameFromFqn = exports.classNameFromFqn = exports.packageToPath = exports.movePackage = exports.ResourcesDir = exports.KotlinSourceFiles = exports.JavaAndKotlinSource = exports.AllJavaAndKotlinFiles = exports.JavaTestFiles = exports.JavaSourceFiles = exports.AllJavaFiles = void 0; const projectUtils = require("@atomist/automation-client/lib/project/util/projectUtils"); const logger_1 = require("@atomist/automation-client/lib/util/logger"); 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"; exports.ResourcesDir = "**/src/main/resources"; /** * 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 */ async function movePackage(project, oldPackage, newPackage, globPattern = exports.AllJavaAndKotlinFiles) { const pathToReplace = packageToPath(oldPackage); const newPath = packageToPath(newPackage); logger_1.logger.debug("Replacing path '%s' with '%s', package '%s' with '%s'", pathToReplace, newPath, oldPackage, newPackage); await projectUtils.doWithFiles(project, globPattern, async (f) => { await f.replaceAll(oldPackage, newPackage); await 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) { logger_1.logger.debug("Replacing old class stem '%s' with '%s'", oldClass, newClass); return projectUtils.doWithFiles(project, exports.AllJavaAndKotlinFiles, async (f) => { if (f.name.includes(oldClass)) { await f.rename(f.name.replace(oldClass, newClass)); const oldClassRe = new RegExp("([( \t<])" + oldClass, "gm"); await 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>} */ async function inferStructureAndMovePackage(rootPackage, p) { const structure = await JavaProjectStructure_1.JavaProjectStructure.infer(p); return !!structure ? movePackage(p, structure.applicationPackage, rootPackage) : p; } const inferStructureAndMovePackageTransform = (p, c, params) => inferStructureAndMovePackage(params.rootPackage, p); exports.inferStructureAndMovePackageTransform = inferStructureAndMovePackageTransform; //# sourceMappingURL=javaProjectUtils.js.map