|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.grails.buildsrc |
| 20 | + |
| 21 | +import groovy.transform.CompileStatic |
| 22 | +import org.gradle.api.Plugin |
| 23 | +import org.gradle.api.Project |
| 24 | +import org.gradle.api.tasks.Copy |
| 25 | +import org.gradle.process.ExecSpec |
| 26 | +import org.gradle.process.ExecOperations |
| 27 | +import javax.inject.Inject |
| 28 | +import org.apache.tools.ant.taskdefs.condition.Os |
| 29 | + |
| 30 | +@CompileStatic |
| 31 | +class GrailsFormatPlugin implements Plugin<Project> { |
| 32 | + |
| 33 | + @Override |
| 34 | + void apply(Project project) { |
| 35 | + registerGitHooks(project) |
| 36 | + registerFormattingTasks(project) |
| 37 | + } |
| 38 | + |
| 39 | + private static void registerGitHooks(Project project) { |
| 40 | + if (project == project.rootProject) { |
| 41 | + project.tasks.register('installGitHooks', Copy) { |
| 42 | + it.group = 'verification' |
| 43 | + it.description = 'Installs the git pre-commit hook for automatic code formatting' |
| 44 | + it.from(project.rootProject.layout.projectDirectory.file('etc/hooks/pre-commit')) |
| 45 | + it.into(project.rootProject.layout.projectDirectory.dir('.git/hooks')) |
| 46 | + it.fileMode = 0755 |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private static void registerFormattingTasks(Project project) { |
| 52 | + ExecOperationsSupport execSupport = project.objects.newInstance(ExecOperationsSupport) |
| 53 | + def ideaExecProvider = project.providers.gradleProperty('idea.exec') |
| 54 | + .orElse(Os.isFamily(Os.FAMILY_WINDOWS) ? 'format.bat' : 'idea') |
| 55 | + def formatFilesProvider = project.providers.gradleProperty('formatFiles') |
| 56 | + def rootProjectDir = project.rootProject.projectDir |
| 57 | + def projectDir = project.projectDir |
| 58 | + |
| 59 | + project.tasks.register('formatCode') { task -> |
| 60 | + task.group = 'verification' |
| 61 | + task.description = 'Formats Java and Groovy source files using the IntelliJ command line formatter' |
| 62 | + |
| 63 | + task.doLast { |
| 64 | + String ideaExec = ideaExecProvider.get() |
| 65 | + def filesToFormat = formatFilesProvider.getOrNull() |
| 66 | + def settingsFile = new File(rootProjectDir, '.idea/codeStyles/Project.xml') |
| 67 | + |
| 68 | + if (!settingsFile.exists()) { |
| 69 | + throw new RuntimeException("IntelliJ code style settings not found at ${settingsFile.absolutePath}") |
| 70 | + } |
| 71 | + |
| 72 | + try { |
| 73 | + execSupport.execOperations.exec { ExecSpec exec -> |
| 74 | + exec.commandLine ideaExec |
| 75 | + if (ideaExec == 'idea') { |
| 76 | + exec.args 'format' |
| 77 | + } |
| 78 | + exec.args '-s', settingsFile.absolutePath |
| 79 | + exec.args '-mask', '*.java,*.groovy' |
| 80 | + exec.args '-r' |
| 81 | + if (filesToFormat) { |
| 82 | + exec.args((filesToFormat.toString()).split(',')) |
| 83 | + } else { |
| 84 | + exec.args projectDir.absolutePath |
| 85 | + } |
| 86 | + } |
| 87 | + } catch (Exception e) { |
| 88 | + task.logger.error("IntelliJ formatter failed to execute.") |
| 89 | + task.logger.error("Please ensure IntelliJ command line tools are installed and available on your PATH.") |
| 90 | + task.logger.error("See: https://www.jetbrains.com/help/idea/working-with-the-ide-features-from-command-line.html") |
| 91 | + throw new RuntimeException("IntelliJ formatter failed. See logs for details.", e) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +interface ExecOperationsSupport { |
| 99 | + @Inject |
| 100 | + ExecOperations getExecOperations() |
| 101 | +} |
0 commit comments