Skip to content

Commit

Permalink
feat(core): able to import gradle project (#27645)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
xiongemi authored Sep 13, 2024
1 parent 5bbaffb commit 61b3503
Show file tree
Hide file tree
Showing 17 changed files with 876 additions and 164 deletions.
175 changes: 175 additions & 0 deletions e2e/gradle/src/gradle-import.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import {
checkFilesExist,
cleanupProject,
getSelectedPackageManager,
newProject,
runCLI,
updateJson,
updateFile,
e2eCwd,
readJson,
} from '@nx/e2e/utils';
import { mkdirSync, rmdirSync, writeFileSync } from 'fs';
import { execSync } from 'node:child_process';
import { join } from 'path';
import { createGradleProject } from './utils/create-gradle-project';
import { createFileSync } from 'fs-extra';

describe('Nx Import Gradle', () => {
let proj: string;
const tempImportE2ERoot = join(e2eCwd, 'nx-import');
beforeAll(() => {
proj = newProject({
packages: ['@nx/js'],
unsetProjectNameAndRootFormat: false,
});

if (getSelectedPackageManager() === 'pnpm') {
updateFile(
'pnpm-workspace.yaml',
`packages:
- 'projects/*'
`
);
} else {
updateJson('package.json', (json) => {
json.workspaces = ['projects/*'];
return json;
});
}

try {
rmdirSync(tempImportE2ERoot);
} catch {}
mkdirSync(tempImportE2ERoot, { recursive: true });
});
afterAll(() => cleanupProject());

it('should be able to import a kotlin gradle app', () => {
const tempGradleProjectName = 'created-gradle-app-kotlin';
const tempGraldeProjectPath = join(
tempImportE2ERoot,
tempGradleProjectName
);
try {
rmdirSync(tempGraldeProjectPath);
} catch {}
mkdirSync(tempGraldeProjectPath, { recursive: true });
createGradleProject(
tempGradleProjectName,
'kotlin',
tempGraldeProjectPath,
'gradleProjectKotlin',
'kotlin-'
);
// Add project.json files to the gradle project to avoid duplicate project names
createFileSync(join(tempGraldeProjectPath, 'project.json'));
writeFileSync(
join(tempGraldeProjectPath, 'project.json'),
`{"name": "${tempGradleProjectName}"}`
);

execSync(`git init`, {
cwd: tempGraldeProjectPath,
});
execSync(`git add .`, {
cwd: tempGraldeProjectPath,
});
execSync(`git commit -am "initial commit"`, {
cwd: tempGraldeProjectPath,
});
execSync(`git checkout -b main`, {
cwd: tempGraldeProjectPath,
});

const remote = tempGraldeProjectPath;
const ref = 'main';
const source = '.';
const directory = 'projects/gradle-app-kotlin';

runCLI(
`import ${remote} ${directory} --ref ${ref} --source ${source} --no-interactive`,
{
verbose: true,
}
);

checkFilesExist(
`${directory}/settings.gradle.kts`,
`${directory}/build.gradle.kts`,
`${directory}/gradlew`,
`${directory}/gradlew.bat`
);
const nxJson = readJson('nx.json');
const gradlePlugin = nxJson.plugins.find(
(plugin) => plugin.plugin === '@nx/gradle'
);
expect(gradlePlugin).toBeDefined();
expect(() => {
runCLI(`show projects`);
runCLI('build kotlin-app');
}).not.toThrow();
});

it('should be able to import a groovy gradle app', () => {
const tempGradleProjectName = 'created-gradle-app-groovy';
const tempGraldeProjectPath = join(
tempImportE2ERoot,
tempGradleProjectName
);
try {
rmdirSync(tempGraldeProjectPath);
} catch {}
mkdirSync(tempGraldeProjectPath, { recursive: true });
createGradleProject(
tempGradleProjectName,
'groovy',
tempGraldeProjectPath,
'gradleProjectGroovy',
'groovy-'
);
// Add project.json files to the gradle project to avoid duplicate project names
createFileSync(join(tempGraldeProjectPath, 'project.json'));
writeFileSync(
join(tempGraldeProjectPath, 'project.json'),
`{"name": "${tempGradleProjectName}"}`
);

execSync(`git init`, {
cwd: tempGraldeProjectPath,
});
execSync(`git add .`, {
cwd: tempGraldeProjectPath,
});
execSync(`git commit -am "initial commit"`, {
cwd: tempGraldeProjectPath,
});
execSync(`git checkout -b main`, {
cwd: tempGraldeProjectPath,
});

const remote = tempGraldeProjectPath;
const ref = 'main';
const source = '.';
const directory = 'projects/gradle-app-groovy';

runCLI(
`import ${remote} ${directory} --ref ${ref} --source ${source} --no-interactive`,
{
verbose: true,
}
);
runCLI(`g @nx/gradle:init --no-interactive`);

checkFilesExist(
`${directory}/build.gradle`,
`${directory}/settings.gradle`,
`${directory}/gradlew`,
`${directory}/gradlew.bat`
);
expect(() => {
runCLI(`show projects`);
runCLI('build groovy-app');
}).not.toThrow();
});
});
39 changes: 3 additions & 36 deletions e2e/gradle/src/gradle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ import {
checkFilesExist,
cleanupProject,
createFile,
e2eConsoleLogger,
isWindows,
newProject,
runCLI,
runCommand,
tmpProjPath,
uniq,
updateFile,
} from '@nx/e2e/utils';
import { execSync } from 'child_process';
import { resolve } from 'path';

import { createGradleProject } from './utils/create-gradle-project';

describe('Gradle', () => {
describe.each([{ type: 'kotlin' }, { type: 'groovy' }])(
Expand All @@ -22,6 +18,7 @@ describe('Gradle', () => {
beforeAll(() => {
newProject();
createGradleProject(gradleProjectName, type);
runCLI(`add @nx/gradle`);
});
afterAll(() => cleanupProject());

Expand Down Expand Up @@ -101,33 +98,3 @@ dependencies {
}
);
});

function createGradleProject(
projectName: string,
type: 'kotlin' | 'groovy' = 'kotlin'
) {
e2eConsoleLogger(`Using java version: ${execSync('java -version')}`);
const gradleCommand = isWindows()
? resolve(`${__dirname}/../gradlew.bat`)
: resolve(`${__dirname}/../gradlew`);
e2eConsoleLogger(
'Using gradle version: ' +
execSync(`${gradleCommand} --version`, {
cwd: tmpProjPath(),
})
);
e2eConsoleLogger(
execSync(`${gradleCommand} help --task :init`, {
cwd: tmpProjPath(),
}).toString()
);
e2eConsoleLogger(
runCommand(
`${gradleCommand} init --type ${type}-application --dsl ${type} --project-name ${projectName} --package gradleProject --no-incubating --split-project`,
{
cwd: tmpProjPath(),
}
)
);
runCLI(`add @nx/gradle`);
}
59 changes: 59 additions & 0 deletions e2e/gradle/src/utils/create-gradle-project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
e2eConsoleLogger,
isWindows,
runCommand,
tmpProjPath,
} from '@nx/e2e/utils';
import { execSync } from 'child_process';
import { createFileSync, writeFileSync } from 'fs-extra';
import { join, resolve } from 'path';

export function createGradleProject(
projectName: string,
type: 'kotlin' | 'groovy' = 'kotlin',
cwd: string = tmpProjPath(),
packageName: string = 'gradleProject',
addProjectJsonNamePrefix: string = ''
) {
e2eConsoleLogger(`Using java version: ${execSync('java -version')}`);
const gradleCommand = isWindows()
? resolve(`${__dirname}/../../gradlew.bat`)
: resolve(`${__dirname}/../../gradlew`);
e2eConsoleLogger(
'Using gradle version: ' +
execSync(`${gradleCommand} --version`, {
cwd,
})
);
e2eConsoleLogger(
execSync(`${gradleCommand} help --task :init`, {
cwd,
}).toString()
);
e2eConsoleLogger(
runCommand(
`${gradleCommand} init --type ${type}-application --dsl ${type} --project-name ${projectName} --package ${packageName} --no-incubating --split-project`,
{
cwd,
}
)
);

if (addProjectJsonNamePrefix) {
createFileSync(join(cwd, 'app/project.json'));
writeFileSync(
join(cwd, 'app/project.json'),
`{"name": "${addProjectJsonNamePrefix}app"}`
);
createFileSync(join(cwd, 'list/project.json'));
writeFileSync(
join(cwd, 'list/project.json'),
`{"name": "${addProjectJsonNamePrefix}list"}`
);
createFileSync(join(cwd, 'utilities/project.json'));
writeFileSync(
join(cwd, 'utilities/project.json'),
`{"name": "${addProjectJsonNamePrefix}utilities"}`
);
}
}
2 changes: 1 addition & 1 deletion e2e/nx/project.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "e2e-nx",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "e2e/nx-misc",
"sourceRoot": "e2e/nx",
"projectType": "application",
"implicitDependencies": ["nx", "js"],
"// targets": "to see all targets run: nx show project e2e-nx --web",
Expand Down
8 changes: 0 additions & 8 deletions packages/gradle/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
Tree,
updateNxJson,
} from '@nx/devkit';
import { execSync } from 'child_process';
import { nxVersion } from '../../utils/versions';
import { InitGeneratorSchema } from './schema';
import { hasGradlePlugin } from '../../utils/has-gradle-plugin';
Expand All @@ -18,13 +17,6 @@ import { dirname, join, basename } from 'path';
export async function initGenerator(tree: Tree, options: InitGeneratorSchema) {
const tasks: GeneratorCallback[] = [];

if (!tree.exists('settings.gradle') && !tree.exists('settings.gradle.kts')) {
logger.warn(`Could not find 'settings.gradle' or 'settings.gradle.kts' file in your gradle workspace.
A Gradle build should contain a 'settings.gradle' or 'settings.gradle.kts' file in its root directory. It may also contain a 'build.gradle' or 'build.gradle.kts' file.
Running 'gradle init':`);
execSync('gradle init', { stdio: 'inherit' });
}

if (!options.skipPackageJson && tree.exists('package.json')) {
tasks.push(
addDependenciesToPackageJson(
Expand Down
8 changes: 3 additions & 5 deletions packages/gradle/src/plugin/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import {
import { readFileSync } from 'node:fs';
import { basename, dirname } from 'node:path';

import {
GRADLE_BUILD_FILES,
getCurrentGradleReport,
newLineSeparator,
} from '../utils/get-gradle-report';
import { getCurrentGradleReport } from '../utils/get-gradle-report';
import { GRADLE_BUILD_FILES } from '../utils/split-config-files';
import { newLineSeparator } from '../utils/get-project-report-lines';

export const createDependencies: CreateDependencies = async (
_,
Expand Down
Loading

0 comments on commit 61b3503

Please sign in to comment.