Skip to content

Commit

Permalink
feat(nx-spring-boot): add format-check executor to check code format
Browse files Browse the repository at this point in the history
You can also use the alias `check-format` to run the executor
  • Loading branch information
tinesoft committed Feb 14, 2022
1 parent f226eba commit 337fca8
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/nx-spring-boot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Here the list of available executors:
| `test` | `ignoreWrapper:boolean`, `args: string[]` | Tests the project using either `./mvnw\|mvn test` or `./gradlew\|gradle test` |
| `clean` | `ignoreWrapper:boolean`, `args: string[]` | Cleans the project using either `./mvnw\|mvn clean` or `./gradlew\|gradle clean` |
| `format` | `ignoreWrapper:boolean`, `args: string[]` | Format the project using [Spotless](https://github.com/diffplug/spotless) plugin for Maven or Gradle |
| `format-check` | `ignoreWrapper:boolean`, `args: string[]` | Check whether the project is well formatted using [Spotless](https://github.com/diffplug/spotless) plugin for Maven or Gradle |
| `build` | `ignoreWrapper:boolean`, `args: string[]` | Packages the project into an executable Jar using either `./mvnw\|mvn package` or `./gradlew\|gradle build` |
| `buildInfo`<sup>*</sup> | `ignoreWrapper:boolean`, | Generates a `build-info.properties` using either `./mvnw\|mvn spring-boot:build-info` or `./gradlew\|gradle bootBuildInfo` |
| `buildImage`<sup>*</sup> | `ignoreWrapper:boolean`, `args: string[]` | Generates an [OCI Image](https://github.com/opencontainers/image-spec) using either `./mvnw\|mvn spring-boot:build-image` or `./gradlew\|gradle bootBuildImage` |
Expand Down Expand Up @@ -207,6 +208,16 @@ nx clean your-boot-app
nx run your-boot-app:format
```

### Checking the format the project - ('format-check' Executor)

```
nx check-format your-boot-app
or
nx format-check your-boot-app
```

## Compatibility with Nx

Every Nx plugin relies on the underlying Nx Workspace/DevKit it runs on. This table provides the compatibility matrix between major versions of Nx workspace and this plugin.
Expand Down
10 changes: 10 additions & 0 deletions packages/nx-spring-boot/executors.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
"schema": "./src/executors/format/schema.json",
"description": "Executor to format the project's files using Spotless plugin"
},
"format-check": {
"implementation": "./src/executors/format-check/executor",
"schema": "./src/executors/format-check/schema.json",
"description": "Executor to check if project's files are well formatted using Spotless plugin"
},
"check-format": {
"implementation": "./src/executors/format-check/executor",
"schema": "./src/executors/format-check/schema.json",
"description": "Executor to check if project's files are well formatted using Spotless plugin (alias to 'format-check' executor)"
},
"buildImage": {
"implementation": "./src/executors/build-image/executor",
"schema": "./src/executors/build-image/schema.json",
Expand Down
2 changes: 2 additions & 0 deletions packages/nx-spring-boot/src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const GRADLE_BOOT_COMMAND_MAPPER : BuilderCommandAliasMapper = {
'clean': 'clean',
'build': 'build',
'format': 'spotlessApply',
'format-check': 'spotlessCheck',
'buildImage': 'bootBuildImage',
'buildInfo': 'bootBuildInfo'
}
Expand All @@ -18,6 +19,7 @@ export const MAVEN_BOOT_COMMAND_MAPPER: BuilderCommandAliasMapper = {
'clean': 'clean',
'build': 'package',
'format': 'spotless:apply',
'format-check': 'spotless:check',
'buildImage': 'spring-boot:build-image',
'buildInfo': 'spring-boot:build-info'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { logger } from '@nrwl/devkit';
import { mocked } from 'ts-jest/utils';

import { formatCheckExecutor } from './executor';
import { FormatCheckExecutorOptions } from './schema';
import { GRADLE_WRAPPER_EXECUTABLE, MAVEN_WRAPPER_EXECUTABLE, NX_SPRING_BOOT_PKG } from '@nxrocks/common';
import { expectExecutorCommandRanWith, mockExecutorContext } from '@nxrocks/common/testing';

//first, we mock
jest.mock('child_process');
jest.mock('@nrwl/workspace/src/utils/fileutils');

//then, we import
import * as fsUtility from '@nrwl/workspace/src/utils/fileutils';
import * as cp from 'child_process';

const mockContext = mockExecutorContext(NX_SPRING_BOOT_PKG, 'format-check');
const options: FormatCheckExecutorOptions = {
root: 'apps/bootapp'
};

describe('Format Check Executor', () => {

beforeEach(async () => {
jest.spyOn(logger, 'info');
jest.spyOn(cp, 'execSync');
});

afterEach(() => {
jest.resetAllMocks();
});

it.each`
ignoreWrapper | buildSystem | formatFile | execute
${true} | ${'maven'} | ${'pom.xml'} | ${'mvn spotless:check '}
${true} | ${'gradle'} | ${'build.gradle'} | ${'gradle spotlessCheck '}
${false} | ${'maven'} | ${'pom.xml'} | ${MAVEN_WRAPPER_EXECUTABLE + ' spotless:check '}
${false} | ${'gradle'} | ${'build.gradle'} | ${GRADLE_WRAPPER_EXECUTABLE + ' spotlessCheck '}
`('should execute a $buildSystem format check and ignoring wrapper : $ignoreWrapper', async ({ ignoreWrapper, formatFile, execute }) => {
mocked(fsUtility.fileExists).mockImplementation((filePath: string) => filePath.indexOf(formatFile) !== -1);

await formatCheckExecutor({ ...options, ignoreWrapper }, mockContext);

expectExecutorCommandRanWith(execute, mockContext, options);
});

});
17 changes: 17 additions & 0 deletions packages/nx-spring-boot/src/executors/format-check/executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ExecutorContext } from '@nrwl/devkit'
import * as path from 'path'
import { runBootPluginCommand } from '../../utils/boot-utils';
import { FormatCheckExecutorOptions } from './schema'

export async function formatCheckExecutor(options: FormatCheckExecutorOptions, context: ExecutorContext){
const root = path.resolve(context.root, options.root);
const result = runBootPluginCommand('format-check', options.args, { cwd : root, ignoreWrapper: options.ignoreWrapper});

if (!result.success) {
throw new Error();
}

return result;
}

export default formatCheckExecutor;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export interface FormatCheckExecutorOptions {
root: string;
ignoreWrapper?: boolean;
args?: string[];
}
24 changes: 24 additions & 0 deletions packages/nx-spring-boot/src/executors/format-check/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/schema",
"title": "Check Format executor",
"description": "",
"cli": "nx",
"type": "object",
"properties": {
"root": {
"description": "The project root",
"type": "string"
},
"ignoreWrapper": {
"description": "Whether or not to use the embedded wrapper (`mvnw`or `gradlew`) to perfom build operations",
"type": "boolean",
"default": false
},
"args": {
"description": "The argument to be passed to the underlying Spring Boot command",
"type": "array",
"default": []
}
},
"required": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('project generator', () => {
const project = readProjectConfiguration(tree, options.name);
expect(project.root).toBe(`${subDir}/${options.name}`);

const commands = ['build', 'format', 'test', 'clean']
const commands = ['build', 'format', 'format-check', 'test', 'clean']
const bootOnlyCommands = ['run', 'serve', 'buildImage', 'buildInfo'];

if (projectType === 'application') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function projectGenerator(tree: Tree, options: ProjectGeneratorOpti
const normalizedOptions = normalizeOptions(tree, options);

const targets = {};
const commands = ['build', 'test', 'clean', 'format'];
const commands = ['build', 'test', 'clean', 'format', 'format-check'];
const bootOnlyCommands = ['run', 'serve', 'buildImage', 'buildInfo'];

if (options.projectType === 'application') { //only 'application' projects should have 'boot' related commands
Expand Down

0 comments on commit 337fca8

Please sign in to comment.