jdaugherty commented on code in PR #15555:
URL: https://github.com/apache/grails-core/pull/15555#discussion_r3041329677


##########
build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStylePlugin.groovy:
##########
@@ -191,4 +197,88 @@ class GrailsCodeStylePlugin implements Plugin<Project> {
             )
         }
     }
+
+    private static void registerFormattingTasks(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
+            }
+        }
+
+        project.tasks.register('formatCode') {
+            it.group = 'verification'
+            it.description = 'Formats Java and Groovy source files using the 
IntelliJ command line formatter'
+
+            it.doLast {
+                String ideaHome = (project.findProperty('idea.home') ?: 
System.getenv('IDEA_HOME')) as String
+                String executable = Os.isFamily(Os.FAMILY_WINDOWS) ? 
'format.bat' : 'format.sh'
+                File formatExec = null
+
+                if (ideaHome) {
+                    formatExec = new File(ideaHome, "bin/$executable")
+                } else {
+                    // Try common paths on macOS
+                    if (Os.isFamily(Os.FAMILY_MAC)) {
+                        def commonPaths = [
+                                "/Applications/IntelliJ 
IDEA.app/Contents/bin/$executable",
+                                "/Applications/IntelliJ IDEA 
CE.app/Contents/bin/$executable"
+                        ]
+                        for (path in commonPaths) {
+                            File f = new File(path)
+                            if (f.exists()) {
+                                formatExec = f
+                                break
+                            }
+                        }
+                    }
+
+                    if (formatExec == null && !Os.isFamily(Os.FAMILY_WINDOWS)) 
{
+                        // On Linux/Mac, try to find 'idea' in PATH
+                        try {
+                            def out = new ByteArrayOutputStream()
+                            project.exec { ExecSpec exec ->
+                                exec.commandLine 'which', 'idea'
+                                exec.standardOutput = out
+                                exec.ignoreExitValue = true
+                            }
+                            def path = out.toString().trim()
+                            if (path) {
+                                formatExec = new File(new 
File(path).parentFile, executable)
+                            }
+                        } catch (Exception ignored) { }
+                    }
+                }
+
+                if (formatExec == null || !formatExec.exists()) {
+                    project.logger.error("IntelliJ formatter executable not 
found.")
+                    project.logger.error("Please set 'idea.home' property or 
IDEA_HOME environment variable to your IntelliJ installation directory.")
+                    project.logger.error("Example: ./gradlew formatCode 
-Pidea.home=/Applications/IntelliJ\\ IDEA.app/Contents")
+                    throw new RuntimeException("IntelliJ formatter executable 
not found.")
+                }
+
+                def filesToFormat = project.findProperty('formatFiles')

Review Comment:
   I hadn't considered the useful of having a format task added to the 
subprojects.  I still think we should split the format tasks out from the code 
style though - otherwise you're trying to apply styling to the root project 
which we don't want to do.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to