@jnxplus/nx-gradle
Version:
[](https://badge.fury.io/js/@jnxplus%2Fnx-gradle)
154 lines • 6.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProjectPath = getProjectPath;
exports.getProjectPathFromProjectRoot = getProjectPathFromProjectRoot;
exports.getQuarkusVersion = getQuarkusVersion;
exports.getRootProjectName = getRootProjectName;
exports.getPlugin = getPlugin;
exports.getGradleRootDirectory = getGradleRootDirectory;
exports.getExecutable = getExecutable;
exports.getDsl = getDsl;
exports.addProjectToGradleSetting = addProjectToGradleSetting;
exports.addLibraryToProjects = addLibraryToProjects;
exports.getVersionManagement = getVersionManagement;
exports.findQuarkusVersion = findQuarkusVersion;
const common_1 = require("@jnxplus/common");
const devkit_1 = require("@nx/devkit");
const fs = require("fs");
const path = require("path");
function getProjectPath(context, gradleRootDirectoryAbsolutePath) {
const projectRoot = (0, common_1.getProjectRoot)(context);
return getProjectPathFromProjectRoot(projectRoot, gradleRootDirectoryAbsolutePath);
}
function getProjectPathFromProjectRoot(projectRoot, gradleRootDirectoryAbsolutePath) {
const projectPathSlash = (0, devkit_1.normalizePath)(path.relative(gradleRootDirectoryAbsolutePath, path.join(devkit_1.workspaceRoot, projectRoot)));
const projectPath = projectPathSlash.replace(/\//g, ':');
return `:${projectPath}`;
}
function getQuarkusVersion(gradlePropertiesContent) {
var _a;
const regexp = /quarkusVersion=(.*)/g;
const matches = ((_a = gradlePropertiesContent.match(regexp)) !== null && _a !== void 0 ? _a : []).map((e) => e.replace(regexp, '$1'));
return matches[0];
}
function getRootProjectName(settingsGradleContent) {
var _a;
const regexp = /rootProject.name\s*=\s*['"](.*)['"]/g;
const matches = ((_a = settingsGradleContent.match(regexp)) !== null && _a !== void 0 ? _a : []).map((e) => e.replace(regexp, '$1'));
return matches[0];
}
function getPlugin() {
var _a;
const nxJsonPath = path.join(devkit_1.workspaceRoot, 'nx.json');
const nxJson = (0, devkit_1.readJsonFile)(nxJsonPath);
const plugin = ((_a = nxJson === null || nxJson === void 0 ? void 0 : nxJson.plugins) !== null && _a !== void 0 ? _a : []).find((p) => typeof p === 'string'
? p === '@jnxplus/nx-gradle'
: p.plugin === '@jnxplus/nx-gradle');
return plugin;
}
function getGradleRootDirectory() {
const plugin = getPlugin();
if (typeof plugin === 'string') {
return '';
}
const options = plugin === null || plugin === void 0 ? void 0 : plugin.options;
if (typeof options === 'object' &&
options &&
'gradleRootDirectory' in options &&
typeof options.gradleRootDirectory === 'string') {
return options.gradleRootDirectory;
}
return '';
}
function getExecutable() {
let executable = '';
if (process.env['NX_SKIP_GRADLE_WRAPPER'] === 'true') {
executable = 'gradle';
}
else {
const isWrapperExists = isWrapperExistsFunction();
if (isWrapperExists) {
executable = process.platform.startsWith('win')
? 'gradlew.bat'
: './gradlew';
}
else {
executable = 'gradle';
}
}
if (process.env['NX_GRADLE_CLI_OPTS']) {
executable += ` ${process.env['NX_GRADLE_CLI_OPTS']}`;
}
return executable;
}
function isWrapperExistsFunction() {
const gradleRootDirectory = getGradleRootDirectory();
const gradlePath = path.join(devkit_1.workspaceRoot, gradleRootDirectory, 'gradlew');
return fs.existsSync(gradlePath);
}
function getDsl(tree, gradleRootDirectory) {
const filePath = (0, devkit_1.joinPathFragments)(gradleRootDirectory, 'settings.gradle');
if (tree.exists(filePath)) {
return 'groovy';
}
return 'kotlin';
}
function addProjectToGradleSetting(tree, options) {
var _a, _b;
const filePath = (0, devkit_1.joinPathFragments)(options.gradleRootDirectory, 'settings.gradle');
const ktsFilePath = (0, devkit_1.joinPathFragments)(options.gradleRootDirectory, 'settings.gradle.kts');
const regex = /.*rootProject\.name.*/;
const projectPath = getProjectPathFromProjectRoot(options.projectRoot, options.gradleRootDirectory);
if (tree.exists(filePath)) {
const settingsContent = (_a = tree.read(filePath, 'utf-8')) !== null && _a !== void 0 ? _a : '';
const newSettingsContent = settingsContent.replace(regex, `$&\ninclude('${projectPath}')`);
tree.write(filePath, newSettingsContent);
}
if (tree.exists(ktsFilePath)) {
const settingsContent = (_b = tree.read(ktsFilePath, 'utf-8')) !== null && _b !== void 0 ? _b : '';
const newSettingsContent = settingsContent.replace(regex, `$&\ninclude("${projectPath}")`);
tree.write(ktsFilePath, newSettingsContent);
}
}
function addLibraryToProjects(tree, options) {
var _a, _b;
const regex = /dependencies\s*{/;
const projectPath = getProjectPathFromProjectRoot(options.projectRoot, options.gradleRootDirectory);
for (const projectName of options.parsedProjects) {
const projectRoot = (0, devkit_1.readProjectConfiguration)(tree, projectName).root;
const filePath = path.join(projectRoot, 'build.gradle');
const ktsPath = path.join(projectRoot, 'build.gradle.kts');
if (tree.exists(filePath)) {
const buildGradleContent = (_a = tree.read(filePath, 'utf-8')) !== null && _a !== void 0 ? _a : '';
const newBuildGradleContent = buildGradleContent.replace(regex, `$&\n\timplementation project(':${projectPath}')`);
tree.write(filePath, newBuildGradleContent);
}
if (tree.exists(ktsPath)) {
const buildGradleContent = (_b = tree.read(ktsPath, 'utf-8')) !== null && _b !== void 0 ? _b : '';
const newBuildGradleContent = buildGradleContent.replace(regex, `$&\n\timplementation(project(":${projectPath}"))`);
tree.write(ktsPath, newBuildGradleContent);
}
}
}
function getVersionManagement(tree, gradleRootDirectory) {
const filePath = (0, devkit_1.joinPathFragments)(gradleRootDirectory, 'gradle', 'libs.versions.toml');
if (tree.exists(filePath)) {
return 'version-catalog';
}
return 'properties';
}
function findQuarkusVersion(framework, gradleRootDirectory, versionManagement) {
let qVersion = '';
if (framework === 'quarkus') {
if (versionManagement === 'properties') {
const gradlePropertiesPath = path.join(devkit_1.workspaceRoot, gradleRootDirectory, 'gradle.properties');
const gradlePropertiesContent = fs.readFileSync(gradlePropertiesPath, 'utf-8');
qVersion = getQuarkusVersion(gradlePropertiesContent);
}
if (!qVersion) {
qVersion = common_1.quarkusVersion;
}
}
return qVersion;
}
//# sourceMappingURL=index.js.map