diff --git a/packages/nx-gradle/src/executors/run-task/executor.ts b/packages/nx-gradle/src/executors/run-task/executor.ts index 615ed4bf9..1b74076b0 100644 --- a/packages/nx-gradle/src/executors/run-task/executor.ts +++ b/packages/nx-gradle/src/executors/run-task/executor.ts @@ -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 = ''; @@ -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 }; diff --git a/packages/nx-gradle/src/utils/index.ts b/packages/nx-gradle/src/utils/index.ts index 8e3f8240c..dd0361ebe 100644 --- a/packages/nx-gradle/src/utils/index.ts +++ b/packages/nx-gradle/src/utils/index.ts @@ -9,6 +9,7 @@ import { NxJsonConfiguration, Tree, joinPathFragments, + normalizePath, readJsonFile, readProjectConfiguration, workspaceRoot, @@ -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) { diff --git a/packages/nx-gradle/src/utils/regexp.spec.ts b/packages/nx-gradle/src/utils/regexp.spec.ts index 2c24c0c32..06c29d08b 100644 --- a/packages/nx-gradle/src/utils/regexp.spec.ts +++ b/packages/nx-gradle/src/utils/regexp.spec.ts @@ -1,4 +1,4 @@ -import { getProjectPathFromProjectRoot, getRootProjectName } from '.'; +import { getRootProjectName } from '.'; describe('regexp', () => { it('should get name from settings.gradle', () => { @@ -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'); - }); }); diff --git a/testing-projects/jnxplus-e2e/tests/nx-gradle/spring-boot-kt.spec.ts b/testing-projects/jnxplus-e2e/tests/nx-gradle/spring-boot-kt.spec.ts index 223b4d915..623b7dd45 100644 --- a/testing-projects/jnxplus-e2e/tests/nx-gradle/spring-boot-kt.spec.ts +++ b/testing-projects/jnxplus-e2e/tests/nx-gradle/spring-boot-kt.spec.ts @@ -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); diff --git a/testing-projects/jnxplus-smoke/tests/nx-gradle/quarkus.spec.ts b/testing-projects/jnxplus-smoke/tests/nx-gradle/quarkus.spec.ts index 493288b97..aebab5d1f 100644 --- a/testing-projects/jnxplus-smoke/tests/nx-gradle/quarkus.spec.ts +++ b/testing-projects/jnxplus-smoke/tests/nx-gradle/quarkus.spec.ts @@ -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(), );