OCIPlugin.groovy
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2019-2022 Andres Almiray.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.kordamp.gradle.plugin.oci
19 
20 import groovy.transform.CompileDynamic
21 import groovy.transform.CompileStatic
22 import org.gradle.api.Action
23 import org.gradle.api.Project
24 import org.gradle.api.Task
25 import org.kordamp.gradle.util.StringUtils
26 import org.kordamp.gradle.plugin.AbstractKordampPlugin
27 import org.kordamp.gradle.plugin.oci.tasks.interfaces.OCITask
28 import org.kordamp.jipsy.util.TypeLoader
29 
30 /**
31  @author Andres Almiray
32  @since 0.1.0
33  */
34 @CompileStatic
35 class OCIPlugin extends AbstractKordampPlugin {
36     static {
37         System.setProperty('sun.net.http.allowRestrictedHeaders', 'true')
38     }
39 
40     Project project
41 
42     OCIPlugin() {
43         super('org.kordamp.gradle.oci')
44     }
45 
46     void apply(Project project) {
47         Banner.display(project)
48         this.project = project
49         configureProject(project)
50     }
51 
52     static void applyIfMissing(Project project) {
53         if (!project.plugins.findPlugin(OCIPlugin)) {
54             project.plugins.apply(OCIPlugin)
55         }
56     }
57 
58     private void configureProject(Project project) {
59         if (hasBeenVisited(project)) {
60             return
61         }
62         setVisited(project, true)
63 
64         TypeLoader.load(this.class.classLoader, OCITask, new TypeLoader.LineProcessor() {
65             @Override
66             @CompileDynamic
67             void process(ClassLoader classLoader, Class<?> clazz, String line) {
68                 Class<? extends Task> taskType = classLoader.loadClass(line.trim()true)
69                 String taskName = StringUtils.getPropertyName(taskType.simpleName - 'Task')
70                 String group = taskType.package.name.split('\\.')[-1]
71 
72                 project.tasks.register(taskName, taskType,
73                     new Action<Task>() {
74                         @Override
75                         void execute(Task t) {
76                             t.group = 'OC' + group.capitalize()
77                             t.description = taskType.TASK_DESCRIPTION
78                         }
79                     })
80             }
81         })
82     }
83 }