@@ -25,16 +25,12 @@ import groovy.transform.CompileStatic
2525
2626import org.gradle.api.Plugin
2727import org.gradle.api.Project
28- import org.gradle.api.file.Directory
2928import org.gradle.api.plugins.quality.Checkstyle
3029import org.gradle.api.plugins.quality.CheckstyleExtension
3130import org.gradle.api.plugins.quality.CheckstylePlugin
3231import org.gradle.api.plugins.quality.CodeNarc
3332import org.gradle.api.plugins.quality.CodeNarcExtension
3433import org.gradle.api.plugins.quality.CodeNarcPlugin
35- import org.gradle.api.tasks.Copy
36- import org.gradle.process.ExecSpec
37- import org.apache.tools.ant.taskdefs.condition.Os
3834
3935@CompileStatic
4036class GrailsCodeStylePlugin implements Plugin<Project > {
@@ -52,18 +48,11 @@ class GrailsCodeStylePlugin implements Plugin<Project> {
5248 void apply (Project project ) {
5349 initExtension(project)
5450 configureCodeStyle(project)
55- registerFormattingTasks(project)
56- doNotApplyStylingToTests(project)
5751 }
5852
5953 private static void initExtension (Project project ) {
6054 def gce = project. extensions. create(' grailsCodeStyle' , GrailsCodeStyleExtension )
6155
62- // Unfortunately, the codenarc plugin is still using a non-lazy property.
63- // Rather than rewrite the plugin to use afterEvaluate,
64- // this plugin uses properties to override the configuration location by default
65-
66-
6756 gce. checkstyleDirectory. set(project. provider {
6857 def directory = project. hasProperty(CHECKSTYLE_DIR_PROPERTY ) ?
6958 project. rootProject. layout. projectDirectory. dir(project. property(CHECKSTYLE_DIR_PROPERTY ) as String ) :
@@ -111,25 +100,6 @@ class GrailsCodeStylePlugin implements Plugin<Project> {
111100 }
112101 }
113102
114- private static void doNotApplyStylingToTests (Project project ) {
115- if (project. tasks. names. contains(' checkstyleTest' )) {
116- project. tasks. named(' checkstyleTest' ) {
117- it. enabled = false // Do not check test sources at this time
118- }
119- }
120-
121- project. afterEvaluate {
122- // Do not check test sources at this time
123- [' codenarcIntegrationTest' , ' codenarcTest' ]. each { testTaskName ->
124- if (project. tasks. names. contains(testTaskName)) {
125- project. tasks. named(testTaskName) {
126- it. enabled = false
127- }
128- }
129- }
130- }
131- }
132-
133103 private static void configureCodeStyle (Project project ) {
134104 configureCheckstyle(project)
135105 configureCodenarc(project)
@@ -197,88 +167,4 @@ class GrailsCodeStylePlugin implements Plugin<Project> {
197167 )
198168 }
199169 }
200-
201- private static void registerFormattingTasks (Project project ) {
202- if (project == project. rootProject) {
203- project. tasks. register(' installGitHooks' , Copy ) {
204- it. group = ' verification'
205- it. description = ' Installs the git pre-commit hook for automatic code formatting'
206- it. from(project. rootProject. layout. projectDirectory. file(' etc/hooks/pre-commit' ))
207- it. into(project. rootProject. layout. projectDirectory. dir(' .git/hooks' ))
208- it. fileMode = 0755
209- }
210- }
211-
212- project. tasks. register(' formatCode' ) {
213- it. group = ' verification'
214- it. description = ' Formats Java and Groovy source files using the IntelliJ command line formatter'
215-
216- it. doLast {
217- String ideaHome = (project. findProperty(' idea.home' ) ?: System . getenv(' IDEA_HOME' )) as String
218- String executable = Os . isFamily(Os . FAMILY_WINDOWS ) ? ' format.bat' : ' format.sh'
219- File formatExec = null
220-
221- if (ideaHome) {
222- formatExec = new File (ideaHome, " bin/$executable " )
223- } else {
224- // Try common paths on macOS
225- if (Os . isFamily(Os . FAMILY_MAC )) {
226- def commonPaths = [
227- " /Applications/IntelliJ IDEA.app/Contents/bin/$executable " ,
228- " /Applications/IntelliJ IDEA CE.app/Contents/bin/$executable "
229- ]
230- for (path in commonPaths) {
231- File f = new File (path)
232- if (f. exists()) {
233- formatExec = f
234- break
235- }
236- }
237- }
238-
239- if (formatExec == null && ! Os . isFamily(Os . FAMILY_WINDOWS )) {
240- // On Linux/Mac, try to find 'idea' in PATH
241- try {
242- def out = new ByteArrayOutputStream ()
243- project. exec { ExecSpec exec ->
244- exec. commandLine ' which' , ' idea'
245- exec. standardOutput = out
246- exec. ignoreExitValue = true
247- }
248- def path = out. toString(). trim()
249- if (path) {
250- formatExec = new File (new File (path). parentFile, executable)
251- }
252- } catch (Exception ignored) { }
253- }
254- }
255-
256- if (formatExec == null || ! formatExec. exists()) {
257- project. logger. error(" IntelliJ formatter executable not found." )
258- project. logger. error(" Please set 'idea.home' property or IDEA_HOME environment variable to your IntelliJ installation directory." )
259- project. logger. error(" Example: ./gradlew formatCode -Pidea.home=/Applications/IntelliJ\\ IDEA.app/Contents" )
260- throw new RuntimeException (" IntelliJ formatter executable not found." )
261- }
262-
263- def filesToFormat = project. findProperty(' formatFiles' )
264- def settingsFile = project. rootProject. file(' .idea/codeStyles/Project.xml' )
265-
266- if (! settingsFile. exists()) {
267- throw new RuntimeException (" IntelliJ code style settings not found at ${ settingsFile.absolutePath} " )
268- }
269-
270- project. exec { ExecSpec exec ->
271- exec. commandLine formatExec. absolutePath
272- exec. args ' -s' , settingsFile. absolutePath
273- exec. args ' -mask' , ' *.java,*.groovy'
274- exec. args ' -r'
275- if (filesToFormat) {
276- exec. args((filesToFormat. toString()). split(' ,' ))
277- } else {
278- exec. args project. projectDir. absolutePath
279- }
280- }
281- }
282- }
283- }
284170}
0 commit comments