jdaugherty commented on code in PR #15555:
URL: https://github.com/apache/grails-core/pull/15555#discussion_r3041333715
##########
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')
+ def settingsFile =
project.rootProject.file('.idea/codeStyles/Project.xml')
+
+ if (!settingsFile.exists()) {
+ throw new RuntimeException("IntelliJ code style settings
not found at ${settingsFile.absolutePath}")
+ }
+
+ project.exec { ExecSpec exec ->
Review Comment:
Take a look at the `publishAllToMavenLocal` task for how to do in this a
modern Gradle way without referencing `project` .
--
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]