-
-
Notifications
You must be signed in to change notification settings - Fork 968
Add Gradle tasks and Git hooks for IntelliJ-based code formatting (#15535) #15555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.0.x
Are you sure you want to change the base?
Changes from all commits
ae7d7d1
ed806f4
4afb1a2
8da88d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,10 @@ gradlePlugin { | |
| id = 'org.apache.grails.gradle.grails-code-style' | ||
| implementationClass = 'org.apache.grails.buildsrc.GrailsCodeStylePlugin' | ||
| } | ||
| register('grailsFormat') { | ||
| id = 'org.apache.grails.gradle.grails-format' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have id: |
||
| implementationClass = 'org.apache.grails.buildsrc.GrailsFormatPlugin' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have: |
||
| } | ||
| register('groovydocEnhancer') { | ||
| id = 'org.apache.grails.buildsrc.groovydoc-enhancer' | ||
| implementationClass = 'org.apache.grails.buildsrc.GroovydocEnhancerPlugin' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.grails.buildsrc | ||
|
|
||
| import groovy.transform.CompileStatic | ||
| import org.gradle.api.Plugin | ||
| import org.gradle.api.Project | ||
| import org.gradle.api.tasks.Copy | ||
| import org.gradle.process.ExecSpec | ||
| import org.gradle.process.ExecOperations | ||
| import javax.inject.Inject | ||
| import org.apache.tools.ant.taskdefs.condition.Os | ||
|
|
||
| @CompileStatic | ||
| class GrailsFormatPlugin implements Plugin<Project> { | ||
|
|
||
| @Override | ||
| void apply(Project project) { | ||
| registerGitHooks(project) | ||
| registerFormattingTasks(project) | ||
| } | ||
|
|
||
| private static void registerGitHooks(Project project) { | ||
| if (project == project.rootProject) { | ||
| project.tasks.register('installGitHooks', Copy) { | ||
| it.group = 'verification' | ||
| it.description = 'Installs the git pre-commit hook for automatic code formatting' | ||
| it.from(project.rootProject.layout.projectDirectory.file('etc/hooks/pre-commit')) | ||
| it.into(project.rootProject.layout.projectDirectory.dir('.git/hooks')) | ||
| it.fileMode = 0755 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void registerFormattingTasks(Project project) { | ||
| ExecOperationsSupport execSupport = project.objects.newInstance(ExecOperationsSupport) | ||
| def ideaExecProvider = project.providers.gradleProperty('idea.exec') | ||
| .orElse(Os.isFamily(Os.FAMILY_WINDOWS) ? 'format.bat' : 'idea') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
| def formatFilesProvider = project.providers.gradleProperty('formatFiles') | ||
| def rootProjectDir = project.rootProject.projectDir | ||
| def projectDir = project.projectDir | ||
|
|
||
| project.tasks.register('formatCode') { task -> | ||
| task.group = 'verification' | ||
| task.description = 'Formats Java and Groovy source files using the IntelliJ command line formatter' | ||
|
|
||
| task.doLast { | ||
| String ideaExec = ideaExecProvider.get() | ||
| def filesToFormat = formatFilesProvider.getOrNull() | ||
| def settingsFile = new File(rootProjectDir, '.idea/codeStyles/Project.xml') | ||
|
|
||
| if (!settingsFile.exists()) { | ||
| throw new RuntimeException("IntelliJ code style settings not found at ${settingsFile.absolutePath}") | ||
| } | ||
|
|
||
| try { | ||
| execSupport.execOperations.exec { ExecSpec exec -> | ||
| exec.commandLine ideaExec | ||
| if (ideaExec == 'idea') { | ||
| exec.args 'format' | ||
| } | ||
| exec.args '-s', settingsFile.absolutePath | ||
| exec.args '-mask', '*.java,*.groovy' | ||
| exec.args '-r' | ||
| if (filesToFormat) { | ||
| exec.args((filesToFormat.toString()).split(',')) | ||
| } else { | ||
| exec.args projectDir.absolutePath | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| task.logger.error("IntelliJ formatter failed to execute.") | ||
| task.logger.error("Please ensure IntelliJ command line tools are installed and available on your PATH.") | ||
| task.logger.error("See: https://www.jetbrains.com/help/idea/working-with-the-ide-features-from-command-line.html") | ||
| throw new RuntimeException("IntelliJ formatter failed. See logs for details.", e) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| interface ExecOperationsSupport { | ||
| @Inject | ||
| ExecOperations getExecOperations() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #!/bin/bash | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| # | ||
|
|
||
| set -e | ||
|
|
||
| # Get staged files that are Groovy or Java | ||
| STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.(groovy|java)$' || true) | ||
|
|
||
| if [ -n "$STAGED_FILES" ]; then | ||
| echo "Formatting staged Groovy/Java files using IntelliJ formatter..." | ||
|
|
||
| # Convert newline-separated list to comma-separated for Gradle property | ||
| FILES_COMMAS=$(echo "$STAGED_FILES" | tr '\n' ',' | sed 's/,$//') | ||
|
|
||
| # Run the Gradle formatting task | ||
| ./gradlew :formatCode -PformatFiles="$FILES_COMMAS" | ||
|
|
||
| # Re-stage the files in case they were modified by the formatter | ||
| for FILE in $STAGED_FILES; do | ||
| if [ -f "$FILE" ]; then | ||
| git add "$FILE" | ||
| fi | ||
| done | ||
| fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we name this:
grailsIJFormatter