Skip to content

Commit

Permalink
feat(core): detect gradle plugin for any settings.gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Sep 10, 2024
1 parent 9b3a960 commit 020b81b
Show file tree
Hide file tree
Showing 19 changed files with 723 additions and 166 deletions.
1 change: 1 addition & 0 deletions e2e/gradle/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src/utils/create-gradle-project';
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 @@ -92,33 +89,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`);
}
38 changes: 38 additions & 0 deletions e2e/gradle/src/utils/create-gradle-project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
e2eConsoleLogger,
isWindows,
runCommand,
tmpProjPath,
} from '@nx/e2e/utils';
import { execSync } from 'child_process';
import { resolve } from 'path';

export function createGradleProject(
projectName: string,
type: 'kotlin' | 'groovy' = 'kotlin',
cwd: string = tmpProjPath()
) {
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 gradleProject --no-incubating --split-project`,
{
cwd,
}
)
);
}
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
71 changes: 63 additions & 8 deletions e2e/nx/src/import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import {
updateJson,
updateFile,
e2eCwd,
readJson,
tmpProjPath,
} from '@nx/e2e/utils';
import { writeFileSync, mkdirSync, rmdirSync } from 'fs';
import { createGradleProject } from '@nx/e2e/gradle';
import { execSync } from 'node:child_process';
import { join } from 'path';

Expand Down Expand Up @@ -36,13 +39,13 @@ describe('Nx Import', () => {
}

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

it('should be able to import a vite app', () => {
mkdirSync(join(tempImportE2ERoot), { recursive: true });
const tempViteProjectName = 'created-vite-app';
execSync(
`npx create-vite@latest ${tempViteProjectName} --template react-ts`,
Expand Down Expand Up @@ -77,12 +80,12 @@ describe('Nx Import', () => {
);

checkFilesExist(
'projects/vite-app/.gitignore',
'projects/vite-app/package.json',
'projects/vite-app/index.html',
'projects/vite-app/vite.config.ts',
'projects/vite-app/src/main.tsx',
'projects/vite-app/src/App.tsx'
`${directory}/.gitignore`,
`${directory}/package.json`,
`${directory}/index.html`,
`${directory}/vite.config.ts`,
`${directory}/src/main.tsx`,
`${directory}/src/App.tsx`
);
runCLI(`vite:build created-vite-app`);
});
Expand Down Expand Up @@ -136,4 +139,56 @@ describe('Nx Import', () => {

checkFilesExist('packages/a/README.md', 'packages/b/README.md');
});

it('should be able to import a gradle app', () => {
const tempGradleProjectName = 'created-gradle-app';
const tempGraldeProjectPath = join(
tempImportE2ERoot,
tempGradleProjectName
);
try {
rmdirSync(tempGraldeProjectPath);
} catch {}
mkdirSync(tempGraldeProjectPath, { recursive: true });
createGradleProject(tempGradleProjectName, 'kotlin', tempGraldeProjectPath);
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';

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

checkFilesExist(
`${directory}/settings.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 app');
}).not.toThrow();
});
});
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
18 changes: 18 additions & 0 deletions packages/gradle/src/plugin/nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ describe('@nx/gradle/plugin', () => {
"gradle",
],
},
"options": {
"cwd": ".",
},
},
},
},
Expand Down Expand Up @@ -200,6 +203,9 @@ describe('@nx/gradle/plugin', () => {
"gradle",
],
},
"options": {
"cwd": ".",
},
},
},
},
Expand Down Expand Up @@ -310,6 +316,9 @@ describe('@nx/gradle/plugin', () => {
"gradle",
],
},
"options": {
"cwd": ".",
},
},
"test-ci": {
"cache": true,
Expand Down Expand Up @@ -379,6 +388,9 @@ describe('@nx/gradle/plugin', () => {
"gradle",
],
},
"options": {
"cwd": ".",
},
},
"test-ci--bTest": {
"cache": true,
Expand Down Expand Up @@ -406,6 +418,9 @@ describe('@nx/gradle/plugin', () => {
"gradle",
],
},
"options": {
"cwd": ".",
},
},
"test-ci--cTests": {
"cache": true,
Expand Down Expand Up @@ -433,6 +448,9 @@ describe('@nx/gradle/plugin', () => {
"gradle",
],
},
"options": {
"cwd": ".",
},
},
},
},
Expand Down
Loading

0 comments on commit 020b81b

Please sign in to comment.