-
-
Notifications
You must be signed in to change notification settings - Fork 970
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
Open
sanjana2505006
wants to merge
4
commits into
apache:7.0.x
Choose a base branch
from
sanjana2505006:feat/intellij-formatter-15535
base: 7.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ae7d7d1
Add IntelliJ formatting task and optional hook
sanjana2505006 ed806f4
Merge branch '7.0.x' into feat/intellij-formatter-15535
sanjana2505006 4afb1a2
Merge branch '7.0.x' into feat/intellij-formatter-15535
sanjana2505006 8da88d3
Merge branch '7.0.x' into feat/intellij-formatter-15535
sanjana2505006 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsFormatPlugin.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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') | ||
| 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() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.