Skip to content

Commit

Permalink
feat(nx-spring-boot): add support for Kotlin DSL when using gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
tinesoft committed Dec 3, 2022
1 parent ad766f0 commit 31063fe
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 17 deletions.
35 changes: 34 additions & 1 deletion e2e/nx-spring-boot-e2e/tests/nx-spring-boot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,39 @@ describe('nx-spring-boot e2e', () => {
}, 200000);
});

describe('--buildSystem=gradle-project-kotlin', () => {
it.each`
projectType
${'application'}
${'library'}
`(`should create a gradle spring-boot '$projectType'`, async ({ projectType }) => {
const prjName = uniq('nx-spring-boot');
const prjDir = projectType === 'application' ? 'apps' : 'libs';

await runNxCommandAsync(
`generate @nxrocks/nx-spring-boot:new ${prjName} --projectType ${projectType} --buildSystem gradle-project-kotlin`
);

const resultBuild= await runNxCommandAsync(`build ${prjName}`);
expect(resultBuild.stdout).toContain(`Executing command: ${isWin ? 'gradlew.bat' : './gradlew'} build`)

expect(() =>
checkFilesExist(`${prjDir}/${prjName}/gradlew`,`${prjDir}/${prjName}/build.gradle.kts`, `${prjDir}/${prjName}/HELP.md`)
).not.toThrow();

// make sure the build wrapper file is executable (*nix only)
if(!isWin) {
const execPermission = '755';
expect(
lstatSync(
tmpProjPath(`${prjDir}/${prjName}/gradlew`)
).mode & octal(execPermission)
).toEqual(octal(execPermission));
}

}, 200000);
});

describe('--buildSystem=gradle-project and --language=kotlin', () => {
it.each`
projectType
Expand All @@ -150,7 +183,7 @@ describe('nx-spring-boot e2e', () => {
expect(resultBuild.stdout).toContain(`Executing command: ${isWin ? 'gradlew.bat' : './gradlew'} build`)

expect(() =>
checkFilesExist(`${prjDir}/${prjName}/gradlew`,`${prjDir}/${prjName}/build.gradle.kts`, `${prjDir}/${prjName}/HELP.md`)
checkFilesExist(`${prjDir}/${prjName}/gradlew`,`${prjDir}/${prjName}/build.gradle`, `${prjDir}/${prjName}/HELP.md`)
).not.toThrow();

// make sure the build wrapper file is executable (*nix only)
Expand Down
27 changes: 26 additions & 1 deletion packages/nx-spring-boot/src/generators/project/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,29 @@ test {
useJUnitPlatform()
}`;

const BUILD_GRADLE_KTS =
`plugins {
java
id("org.springframework.boot") version "3.0.0"
id("io.spring.dependency-management") version "1.1.0"
}
group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
tasks.withType<Test> {
useJUnitPlatform()
}`;

describe('project generator', () => {
let tree: Tree;
Expand Down Expand Up @@ -112,8 +135,10 @@ describe('project generator', () => {
projectType | buildSystem | buildFile | buildFileContent | wrapperName
${'application'} | ${'maven-project'} | ${'pom.xml'} | ${POM_XML} | ${'mvnw'}
${'application'} | ${'gradle-project'} | ${'build.gradle'} | ${BUILD_GRADLE} | ${'gradlew'}
${'application'} | ${'gradle-project-kotlin'} | ${'build.gradle.kts'} | ${BUILD_GRADLE_KTS} | ${'gradlew'}
${'library'} | ${'maven-project'} | ${'pom.xml'} | ${POM_XML} | ${'mvnw'}
${'library'} | ${'gradle-project'} | ${'build.gradle'} | ${BUILD_GRADLE} | ${'gradlew'}
${'library'} | ${'gradle-project-kotlin'} | ${'build.gradle.kts'} | ${BUILD_GRADLE_KTS} | ${'gradlew'}
`(`should download a spring boot '$projectType' build with $buildSystem`, async ({ projectType, buildSystem, buildFile, buildFileContent, wrapperName }) => {

const rootDir = projectType === 'application' ? 'apps' : 'libs';
Expand Down Expand Up @@ -144,7 +169,7 @@ describe('project generator', () => {
if (projectType === 'library') {
expect(logger.debug).toHaveBeenNthCalledWith(1,`Disabling 'spring-boot' gradle plugin on a library project...`);
} else {
expect(logger.debug).toHaveBeenNthCalledWith(1,`Adding 'buildInfo' task to the build.gradle file...`);
expect(logger.debug).toHaveBeenNthCalledWith(1,`Adding 'buildInfo' task to the ${buildFile} file...`);
}
expect(logger.debug).toHaveBeenNthCalledWith(2,`Adding 'maven-publish' plugin...`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import { stripIndent } from '@nxrocks/common';
import { NormalizedSchema } from '../schema';

export function addBuilInfoTask(tree: Tree, options: NormalizedSchema) {
if (options.projectType === 'application' && options.buildSystem === 'gradle-project') {
logger.debug(`Adding 'buildInfo' task to the build.gradle file...`);
if (options.projectType === 'application' && (options.buildSystem === 'gradle-project' || options.buildSystem === 'gradle-project-kotlin')) {

const ext = options.buildSystem === 'gradle-project-kotlin' ? '.kts' : ''
logger.debug(`Adding 'buildInfo' task to the build.gradle${ext} file...`);

const buildInfoTask =
stripIndent`
springBoot {
buildInfo()
}
`;
const ext = options.language === 'kotlin' ? '.kts' : ''
const buildGradlePath = `${options.projectRoot}/build.gradle${ext}`;
const content = tree.read(buildGradlePath, 'utf-8') + buildInfoTask;
tree.write(buildGradlePath, content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { NormalizedSchema } from '../schema';

export function addFormattingWithSpotless(tree: Tree, options: NormalizedSchema) {

if (options.buildSystem === 'gradle-project') {
addSpotlessGradlePlugin(tree, options.projectRoot, options.language, +(options.javaVersion));
if (options.buildSystem === 'maven-project') {
addSpotlessMavenPlugin(tree, options.projectRoot, options.language, +(options.javaVersion));
}
else {
addSpotlessMavenPlugin(tree, options.projectRoot, options.language, +(options.javaVersion));
addSpotlessGradlePlugin(tree, options.projectRoot, options.language, +(options.javaVersion),null, options.buildSystem === 'gradle-project-kotlin');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { addGradlePlugin, stripIndent } from '@nxrocks/common';
import { NormalizedSchema } from '../schema';

export function addMavenPublishPlugin(tree: Tree, options: NormalizedSchema) {
if (options.buildSystem === 'gradle-project') {
if (options.buildSystem === 'gradle-project' || options.buildSystem === 'gradle-project-kotlin') {
logger.debug(`Adding 'maven-publish' plugin...`);

addGradlePlugin(tree, options.projectRoot, options.language, 'maven-publish');
addGradlePlugin(tree, options.projectRoot, options.language, 'maven-publish', undefined, options.buildSystem === 'gradle-project-kotlin');

const artifactSource = options.projectType === 'application' ? 'bootJar' : 'jar';
const publishing = options.language === 'kotlin' ?
const publishing = options.buildSystem === 'gradle-project-kotlin' ?
stripIndent`
publishing {
publications {
Expand All @@ -32,7 +32,7 @@ export function addMavenPublishPlugin(tree: Tree, options: NormalizedSchema) {
}
}
`;
const ext = options.language === 'kotlin' ? '.kts' : '';
const ext = options.buildSystem === 'gradle-project-kotlin'? '.kts' : '';
const buildGradlePath = `${options.projectRoot}/build.gradle${ext}`;
const content = tree.read(buildGradlePath, 'utf-8') + '\n' + publishing;
tree.write(buildGradlePath, content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { disableGradlePlugin, stripIndent } from '@nxrocks/common';
import { NormalizedSchema } from '../schema';

export function disableBootGradlePlugin(tree: Tree, options: NormalizedSchema) {
if (options.projectType === 'library' && options.buildSystem === 'gradle-project') {
if (options.projectType === 'library' && (options.buildSystem === 'gradle-project' || options.buildSystem === 'gradle-project-kotlin')) {
logger.debug(`Disabling 'spring-boot' gradle plugin on a library project...`);

const disabled = disableGradlePlugin(tree, options.projectRoot, options.language, 'org.springframework.boot');
if(disabled) {

const dependencyManagement = options.language === 'kotlin' ?
const dependencyManagement = options.buildSystem === 'gradle-project-kotlin' ?
stripIndent`
dependencyManagement {
imports {
Expand All @@ -29,7 +29,7 @@ export function disableBootGradlePlugin(tree: Tree, options: NormalizedSchema) {
}
`;

const ext = options.language === 'kotlin' ? '.kts' : ''
const ext = options.buildSystem === 'gradle-project-kotlin' ? '.kts' : ''
const buildGradlePath = `${options.projectRoot}/build.gradle${ext}`;
let content = tree.read(buildGradlePath).toString();

Expand Down
2 changes: 1 addition & 1 deletion packages/nx-spring-boot/src/generators/project/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface ProjectGeneratorOptions {
springInitializerUrl?: string;
proxyUrl?: string;

buildSystem: 'maven-project' | 'gradle-project';
buildSystem: 'maven-project' | 'gradle-project' | 'gradle-project-kotlin';
language?: 'java' | 'kotlin' | 'groovy';
bootVersion?: string;
groupId?: string;
Expand Down
6 changes: 5 additions & 1 deletion packages/nx-spring-boot/src/generators/project/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"alias": "type",
"default": "maven-project",
"type": "string",
"enum": ["maven-project", "gradle-project"],
"enum": ["maven-project", "gradle-project", "gradle-project-kotlin"],
"x-prompt": {
"message": "Which build system would you like to use?",
"type": "list",
Expand All @@ -52,6 +52,10 @@
{
"value": "gradle-project",
"label": "Gradle"
},
{
"value": "gradle-project-kotlin",
"label": "Gradle - Kotlin"
}
]
}
Expand Down

0 comments on commit 31063fe

Please sign in to comment.