Skip to content

Commit

Permalink
fix: better projectPath calculation (#833)
Browse files Browse the repository at this point in the history
* fix: fix when gradleRootDirectory is dot

* build: work in progress

* build: work in progress

* build: work in progress

---------

Co-authored-by: khalilou88 <[email protected]>
  • Loading branch information
khalilou88 and khalilou88 authored Jan 30, 2024
1 parent 0f8177a commit 86ccd15
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 41 deletions.
8 changes: 6 additions & 2 deletions packages/nx-gradle/src/executors/run-task/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ export default async function runExecutor(
logger.info(`Executor ran for ${targetName}: ${JSON.stringify(options)}`);

const gradleRootDirectory = getGradleRootDirectory();
const gradleRootDirectoryAbsolutePath = join(
workspaceRoot,
gradleRootDirectory,
);

let projectPath = '';
if (options.projectPath) {
projectPath = options.projectPath;
} else {
projectPath = getProjectPath(context, gradleRootDirectory);
projectPath = getProjectPath(context, gradleRootDirectoryAbsolutePath);
}

let task = '';
Expand All @@ -33,7 +37,7 @@ export default async function runExecutor(

const command = `${getExecutable()} ${projectPath}:${task}`;

const result = runCommand(command, join(workspaceRoot, gradleRootDirectory));
const result = runCommand(command, gradleRootDirectoryAbsolutePath);

if (!result.success) {
return { success: false };
Expand Down
42 changes: 15 additions & 27 deletions packages/nx-gradle/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
NxJsonConfiguration,
Tree,
joinPathFragments,
normalizePath,
readJsonFile,
readProjectConfiguration,
workspaceRoot,
Expand All @@ -19,42 +20,29 @@ import { join } from 'path';

export function getProjectPath(
context: ExecutorContext,
gradleRootDirectory: string,
gradleRootDirectoryAbsolutePath: string,
) {
const projectRoot = getProjectRoot(context);
return getProjectPathFromProjectRoot(projectRoot, gradleRootDirectory);
return getProjectPathFromProjectRoot(
projectRoot,
gradleRootDirectoryAbsolutePath,
);
}

export function getProjectPathFromProjectRoot(
projectRoot: string,
gradleRootDirectory: string,
gradleRootDirectoryAbsolutePath: string,
) {
//Remove first dot
let replacedString = projectRoot.replace(new RegExp('^\\.', 'g'), '');

//Remove /gradleRootDirectory if exists
if (gradleRootDirectory) {
replacedString = replacedString.replace(
new RegExp(`^\\/?${gradleRootDirectory}`, 'g'),
'',
);
}

replacedString = replacedString.replace(new RegExp('/', 'g'), ':');

if (!replacedString.startsWith(':')) {
replacedString = `:${replacedString}`;
}

return replacedString;
}
const projectPathSlash = normalizePath(
path.relative(
gradleRootDirectoryAbsolutePath,
path.join(workspaceRoot, projectRoot),
),
);

export function getProjectRootFromProjectPath(projectPath: string) {
if (projectPath.startsWith(':')) {
throw new Error(`Path ${projectPath} should not starts with two dots (:)`);
}
const projectPath = projectPathSlash.replace(new RegExp('/', 'g'), ':');

return projectPath.replace(/:/g, '/');
return `:${projectPath}`;
}

export function getQuarkusVersion(gradlePropertiesContent: string) {
Expand Down
11 changes: 1 addition & 10 deletions packages/nx-gradle/src/utils/regexp.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getProjectPathFromProjectRoot, getRootProjectName } from '.';
import { getRootProjectName } from '.';

describe('regexp', () => {
it('should get name from settings.gradle', () => {
Expand All @@ -20,13 +20,4 @@ describe('regexp', () => {

expect(getRootProjectName(settingsGradleKts)).toBe('basic-multiproject');
});

it('should get correct projectPath', () => {
expect(
getProjectPathFromProjectRoot(
'./nx-gradle/boot-gradle-app-4128904/test',
'nx-gradle',
),
).toBe(':boot-gradle-app-4128904:test');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('nx-gradle spring-boot kotlin dsl e2e', () => {
});

await runNxCommandAsync(
`generate @jnxplus/nx-gradle:init --dsl kotlin --rootProjectName ${rootProjectName} --preset none --versionManagement version-catalog`,
`generate @jnxplus/nx-gradle:init --gradleRootDirectory . --dsl kotlin --rootProjectName ${rootProjectName} --preset none --versionManagement version-catalog`,
);
}, 120000);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('nx-gradle quarkus smoke', () => {
execSync('npm i --save-dev @jnxplus/nx-gradle', execSyncOptions());

execSync(
'npx nx generate @jnxplus/nx-gradle:init --preset quarkus',
'npx nx generate @jnxplus/nx-gradle:init --gradleRootDirectory . --preset quarkus',
execSyncOptions(),
);

Expand Down

0 comments on commit 86ccd15

Please sign in to comment.