UNPKG

@jnxplus/nx-gradle

Version:

[![npm version](https://badge.fury.io/js/@jnxplus%2Fnx-gradle.svg)](https://badge.fury.io/js/@jnxplus%2Fnx-gradle)

183 lines 7.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.initGenerator = initGenerator; const tslib_1 = require("tslib"); const common_1 = require("@jnxplus/common"); const devkit_1 = require("@nx/devkit"); const path = require("path"); const libs_versions_toml_1 = require("../../utils/libs-versions-toml"); function normalizeOptions(tree, options) { const kotlinExtension = options.dsl === 'kotlin' ? '.kts' : ''; const generateRepositories = process.env['NODE_ENV'] === 'test'; return Object.assign(Object.assign({}, options), { kotlinExtension, springBootVersion: common_1.springBootVersion, springDependencyManagementVersion: common_1.springDependencyManagementVersion, quarkusVersion: common_1.quarkusVersion, micronautVersion: common_1.micronautVersion, kspVersion: common_1.kspVersion, shadowVersion: common_1.shadowVersion, kotlinVersion: common_1.kotlinVersion, jnxplusGradlePluginVersion: common_1.jnxplusGradlePluginVersion, generateRepositories }); } function addFiles(tree, options) { const templateOptions = Object.assign(Object.assign({}, options), { offsetFromRoot: (0, devkit_1.offsetFromRoot)(tree.root), template: '' }); if (!options.skipWrapper) { (0, devkit_1.generateFiles)(tree, path.join(__dirname, 'files', 'gradle', 'wrapper'), options.gradleRootDirectory, templateOptions); } (0, devkit_1.generateFiles)(tree, path.join(__dirname, 'files', 'gradle', 'config', options.preset), options.gradleRootDirectory, templateOptions); if (options.versionManagement === 'version-catalog') { (0, libs_versions_toml_1.addLibsVersionsToml)(tree, options.gradleRootDirectory, options.javaVersion, options.preset, 'java'); } } exports.default = initGenerator; function initGenerator(tree, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const nxGradleVersion = (0, common_1.getPluginVersion)(__dirname); devkit_1.logger.info(`Using version v${nxGradleVersion} of nx-gradle`); const normalizedOptions = normalizeOptions(tree, options); if (options.gradleRootDirectory) { const projectConfiguration = { root: normalizedOptions.gradleRootDirectory, targets: {}, }; (0, devkit_1.addProjectConfiguration)(tree, normalizedOptions.rootProjectName, projectConfiguration); } addFiles(tree, normalizedOptions); updateNxJson(tree, normalizedOptions); (0, common_1.updateNxJsonConfiguration)(tree); updateGitIgnore(tree, normalizedOptions); if (options.formatter === 'prettier') { addPrettierToPackageJson(tree); addOrUpdatePrettierRc(tree); addOrUpdatePrettierIgnore(tree); } addOrUpdateGitattributes(tree); if (!options.skipWrapper) { tree.changePermissions((0, devkit_1.joinPathFragments)(options.gradleRootDirectory, 'gradlew'), '755'); tree.changePermissions((0, devkit_1.joinPathFragments)(options.gradleRootDirectory, 'gradlew.bat'), '755'); } if (!options.skipFormat) { yield (0, devkit_1.formatFiles)(tree); } return () => { (0, devkit_1.installPackagesTask)(tree); }; }); } function updateNxJson(tree, options) { let pluginOptions = {}; if (options.gradleRootDirectory) { pluginOptions = Object.assign(Object.assign({}, pluginOptions), { gradleRootDirectory: options.gradleRootDirectory }); } if (options.buildTargetName && options.buildTargetName !== 'build') { pluginOptions = Object.assign(Object.assign({}, pluginOptions), { buildTargetName: options.buildTargetName }); } let plugin; if (pluginOptions) { plugin = { plugin: '@jnxplus/nx-gradle', options: pluginOptions, }; } else { plugin = '@jnxplus/nx-gradle'; } (0, devkit_1.updateJson)(tree, 'nx.json', (nxJson) => { var _a; nxJson.targetDefaults = Object.assign(Object.assign({}, nxJson.targetDefaults), { [options.buildTargetName]: { cache: true, dependsOn: [`^${options.buildTargetName}`], inputs: ['production', '^production'], } }); // if plugins is undefined, set it to an empty array nxJson.plugins = (_a = nxJson.plugins) !== null && _a !== void 0 ? _a : []; // add plugin nxJson.plugins.push(plugin); // return modified JSON object return nxJson; }); } function updateGitIgnore(tree, options) { var _a; const filePath = '.gitignore'; const contents = (_a = tree.read(filePath, 'utf-8')) !== null && _a !== void 0 ? _a : ''; const gradleIgnores = [ '\n', '\n# Gradle', '\n.gradle', '\nbuild/', '\n!**/src/main/**/build/', '\n!**/src/test/**/build/', ]; if (!options.skipWrapper) { gradleIgnores.push('\n!gradle/wrapper/gradle-wrapper.jar'); } const newContents = contents.concat(gradleIgnores.join('')); tree.write(filePath, newContents); } function addPrettierToPackageJson(tree) { (0, devkit_1.addDependenciesToPackageJson)(tree, {}, { prettier: common_1.prettierVersion, 'prettier-plugin-java': common_1.prettierPluginJavaVersion, }); } function addOrUpdatePrettierRc(tree) { const prettierRcPath = '.prettierrc'; if (tree.exists(prettierRcPath)) { (0, devkit_1.updateJson)(tree, prettierRcPath, (prettierRcJson) => { var _a; prettierRcJson.plugins = (_a = prettierRcJson.plugins) !== null && _a !== void 0 ? _a : []; if (!prettierRcJson.plugins.includes('prettier-plugin-java')) { prettierRcJson.plugins.push('prettier-plugin-java'); } // return modified JSON object return prettierRcJson; }); } else if (common_1.prettierrcNameOptions.every((name) => !tree.exists(name))) { (0, devkit_1.writeJson)(tree, prettierRcPath, { plugins: ['prettier-plugin-java'], }); } else { devkit_1.logger.warn('Please add prettier-plugin-java plugin to your prettier config file'); } } function addOrUpdatePrettierIgnore(tree) { var _a; const prettierIgnorePath = '.prettierignore'; const gradlePrettierIgnores = ['# Gradle build', '\nbuild/']; if (tree.exists(prettierIgnorePath)) { const prettierIgnoreOldContent = (_a = tree.read(prettierIgnorePath, 'utf-8')) !== null && _a !== void 0 ? _a : ''; gradlePrettierIgnores.unshift('\n\n'); const prettierIgnoreContent = prettierIgnoreOldContent.concat(gradlePrettierIgnores.join('')); tree.write(prettierIgnorePath, prettierIgnoreContent); } else { tree.write(prettierIgnorePath, gradlePrettierIgnores.join('')); } } function addOrUpdateGitattributes(tree) { var _a; const gitattributesPath = '.gitattributes'; const attributes = [ '#', '\n# https://help.github.com/articles/dealing-with-line-endings/', '\n#', '\n# Linux start script should use lf', '\ngradlew text eol=lf', '\n# Windows script files should use crlf', '\n*.bat text eol=crlf', ]; if (tree.exists(gitattributesPath)) { const gitattributesOldContent = (_a = tree.read(gitattributesPath, 'utf-8')) !== null && _a !== void 0 ? _a : ''; attributes.unshift('\n\n'); const gitattributesContent = gitattributesOldContent.concat(attributes.join('')); tree.write(gitattributesPath, gitattributesContent); } else { tree.write(gitattributesPath, attributes.join('')); } } //# sourceMappingURL=generator.js.map