~gradle-plugins/gradle-templates/trunk

« back to all changes in this revision

Viewing changes to src/main/groovy/templates/GroovyTemplatesPlugin.groovy

  • Committer: Eric Berry
  • Date: 2011-08-04 00:49:30 UTC
  • Revision ID: elberry@gmail.com-20110804004930-hi55con4e6tt6c8f
renaming scala templates to match the others. Also implementing blueprints for groupId and version, as well as -P properties.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
package templates
2
2
 
 
3
import org.gradle.api.Plugin
3
4
import org.gradle.api.Project
4
 
import org.gradle.api.Plugin
5
5
 
6
6
class GroovyTemplatesPlugin implements Plugin<Project> {
7
 
   void createBase(String path = System.getProperty("user.dir")) {
8
 
      ProjectTemplate.fromRoot(path) {
9
 
         "src" {
10
 
            "main" {
11
 
               "groovy" {}
12
 
               "resources" {}
13
 
            }
14
 
            "test" {
15
 
               "groovy" {}
16
 
               "resources" {}
17
 
            }
18
 
         }
19
 
         "LICENSE.txt" "// Your License Goes here"
20
 
      }
21
 
   }
22
 
 
23
 
   static String findMainGroovyDir(Project project) {
24
 
      File rootDir = project.projectDir
25
 
      def mainSrcDir = project.sourceSets?.main?.groovy?.srcDirs*.path
26
 
      mainSrcDir = mainSrcDir?.first()
27
 
      mainSrcDir = mainSrcDir?.minus(rootDir.path)
28
 
      return mainSrcDir
29
 
   }
30
 
 
31
 
   void apply(Project project) {
32
 
      project.task("createGroovyClass", group: TemplatesPlugin.group, description: "Creates a new Groovy class in the current project.") << {
33
 
         
34
 
         def mainSrcDir = null
35
 
         try {
36
 
            // get main groovy dir, and check to see if Groovy plugin is installed.
37
 
            mainSrcDir = findMainGroovyDir(project)
38
 
         } catch (Exception e) {
39
 
            throw new IllegalStateException("It seems that the Groovy plugin is not installed, I cannot determine the main groovy source directory.", e)
40
 
         }
41
 
 
42
 
         def fullClassName = TemplatesPlugin.prompt("Class name (com.example.MyClass)")
43
 
         if (fullClassName) {
44
 
            def classParts = JavaTemplatesPlugin.getClassParts(fullClassName)
45
 
            ProjectTemplate.fromUserDir {
46
 
               "${mainSrcDir}" {
47
 
                  "${classParts.classPackagePath}" {
48
 
                     "${classParts.className}.groovy" template: "/templates/groovy/groovy-class.tmpl",
49
 
                           className: classParts.className,
50
 
                           classPackage: classParts.classPackage
51
 
                  }
52
 
               }
53
 
            }
54
 
         } else {
55
 
            println "No class name provided."
56
 
         }
57
 
      }
58
 
      project.task("createGroovyProject", group: TemplatesPlugin.group,
59
 
            description: "Creates a new Gradle Groovy project in a new directory named after your project.") << {
60
 
         def projectName = TemplatesPlugin.prompt("Project Name:")
61
 
         if (projectName) {
62
 
            createBase(projectName)
63
 
            ProjectTemplate.fromRoot(projectName) {
64
 
               "build.gradle" template: "/templates/groovy/build.gradle.tmpl"
65
 
            }
66
 
         } else {
67
 
            println "No project name provided."
68
 
         }
69
 
      }
70
 
      project.task("initGroovyProject", group: TemplatesPlugin.group,
71
 
            description: "Initializes a new Gradle Groovy project in the current directory.") << {
72
 
         createBase()
73
 
         TemplatesPlugin.prependPlugin "groovy", new File("build.gradle")
74
 
      }
75
 
 
76
 
   }
 
7
        void createBase(String path = System.getProperty('user.dir')) {
 
8
                ProjectTemplate.fromRoot(path) {
 
9
                        'src' {
 
10
                                'main' {
 
11
                                        'groovy' {}
 
12
                                        'resources' {}
 
13
                                }
 
14
                                'test' {
 
15
                                        "groovy" {}
 
16
                                        "resources" {}
 
17
                                }
 
18
                        }
 
19
                        'LICENSE.txt' '// Your License Goes here'
 
20
                }
 
21
        }
 
22
 
 
23
        static String findMainGroovyDir(Project project) {
 
24
                File rootDir = project.projectDir
 
25
                def mainSrcDir = project.sourceSets?.main?.groovy?.srcDirs*.path
 
26
                mainSrcDir = mainSrcDir?.first()
 
27
                mainSrcDir = mainSrcDir?.minus(rootDir.path)
 
28
                return mainSrcDir
 
29
        }
 
30
 
 
31
        void apply(Project project) {
 
32
                def props = project.properties
 
33
 
 
34
                project.task('createGroovyClass', group: TemplatesPlugin.group, description: 'Creates a new Groovy class in the current project.') << {
 
35
 
 
36
                        def mainSrcDir = null
 
37
                        try {
 
38
                                // get main groovy dir, and check to see if Groovy plugin is installed.
 
39
                                mainSrcDir = findMainGroovyDir(project)
 
40
                        } catch (Exception e) {
 
41
                                throw new IllegalStateException('It seems that the Groovy plugin is not installed, I cannot determine the main groovy source directory.', e)
 
42
                        }
 
43
 
 
44
                        def fullClassName = props['newClassName'] ?: TemplatesPlugin.prompt('Class name (com.example.MyClass)')
 
45
 
 
46
                        if (fullClassName) {
 
47
                                def classParts = JavaTemplatesPlugin.getClassParts(fullClassName)
 
48
                                ProjectTemplate.fromUserDir {
 
49
                                        "${mainSrcDir}" {
 
50
                                                "${classParts.classPackagePath}" {
 
51
                                                        "${classParts.className}.groovy" template: '/templates/groovy/groovy-class.tmpl',
 
52
                                                                        className: classParts.className,
 
53
                                                                        classPackage: classParts.classPackage
 
54
                                                }
 
55
                                        }
 
56
                                }
 
57
                        } else {
 
58
                                println 'No class name provided.'
 
59
                        }
 
60
                }
 
61
                project.task('createGroovyProject', group: TemplatesPlugin.group,
 
62
                                description: 'Creates a new Gradle Groovy project in a new directory named after your project.') << {
 
63
 
 
64
                        def projectName = props['newProjectName'] ?: TemplatesPlugin.prompt('Project Name:')
 
65
                        if (projectName) {
 
66
                                String projectGroup = props['projectGroup'] ?: TemplatesPlugin.prompt('Group:', projectName.toLowerCase())
 
67
                                String projectVersion = props['projectVersion'] ?: TemplatesPlugin.prompt('Version:', '1.0')
 
68
                                createBase(projectName)
 
69
                                ProjectTemplate.fromRoot(projectName) {
 
70
                                        'build.gradle' template: '/templates/groovy/build.gradle.tmpl', projectGroup: projectGroup
 
71
                                        'gradle.properties' content: "version=$projectVersion", append: true
 
72
                                }
 
73
                        } else {
 
74
                                println 'No project name provided.'
 
75
                        }
 
76
                }
 
77
                project.task('initGroovyProject', group: TemplatesPlugin.group,
 
78
                                description: 'Initializes a new Gradle Groovy project in the current directory.') << {
 
79
                        createBase()
 
80
                        def buildFile = new File('build.gradle')
 
81
                        buildFile.exists() ?: buildFile.createNewFile()
 
82
                        TemplatesPlugin.prependPlugin 'groovy', buildFile
 
83
                }
 
84
 
 
85
        }
77
86
}
 
 
b'\\ No newline at end of file'