1 | /* Copyright 2006-2009 the original author or authors. |
---|
2 | * |
---|
3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | * you may not use this file except in compliance with the License. |
---|
5 | * You may obtain a copy of the License at |
---|
6 | * |
---|
7 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | * |
---|
9 | * Unless required by applicable law or agreed to in writing, software |
---|
10 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | * See the License for the specific language governing permissions and |
---|
13 | * limitations under the License. |
---|
14 | */ |
---|
15 | |
---|
16 | /** |
---|
17 | * Shared methods/closures and initialization. |
---|
18 | * |
---|
19 | * @author <a href='mailto:beckwithb@studentsonly.com'>Burt Beckwith</a> |
---|
20 | */ |
---|
21 | |
---|
22 | import groovy.text.SimpleTemplateEngine |
---|
23 | |
---|
24 | grailsHome = Ant.project.properties.'environment.GRAILS_HOME' |
---|
25 | includeTargets << new File("$grailsHome/scripts/Init.groovy") |
---|
26 | |
---|
27 | personClassName = 'Person' |
---|
28 | personClassPackage = '' |
---|
29 | authorityClassName = 'Authority' |
---|
30 | authorityClassPackage = '' |
---|
31 | requestmapClassName = 'Requestmap' |
---|
32 | requestmapClassPackage = '' |
---|
33 | templateDir = "$acegiPluginDir/src/templates" |
---|
34 | appDir = "$basedir/grails-app" |
---|
35 | |
---|
36 | overwrite = true |
---|
37 | |
---|
38 | generateFile = { String templatePath, String outputPath -> |
---|
39 | |
---|
40 | File templateFile = new File(templatePath) |
---|
41 | if (!templateFile.exists()) { |
---|
42 | println "$templatePath doesn't exist" |
---|
43 | return |
---|
44 | } |
---|
45 | |
---|
46 | File outFile = new File(outputPath) |
---|
47 | if (outFile.exists() && !overwrite) { |
---|
48 | println "file *not* generated: $outFile.absolutePath" |
---|
49 | return |
---|
50 | } |
---|
51 | |
---|
52 | // in case it's in a package, create dirs |
---|
53 | Ant.mkdir dir: outFile.parentFile |
---|
54 | |
---|
55 | def binding = [personClassName: personClassName, |
---|
56 | personClassPackage: '', |
---|
57 | personClass: personClassName, personClassImport: '', |
---|
58 | authorityClassName: authorityClassName, |
---|
59 | authorityClassPackage: '', |
---|
60 | authorityClass: authorityClassName, authorityClassImport: '', |
---|
61 | requestmapClassName: requestmapClassName, |
---|
62 | requestmapClassPackage: '', |
---|
63 | requestmapClass: requestmapClassName, requestmapClassImport: ''] |
---|
64 | |
---|
65 | if (personClassPackage) { |
---|
66 | binding.personClass = "${personClassPackage}.$personClassName" |
---|
67 | binding.personClassImport = "import $binding.personClass" |
---|
68 | binding.personClassPackage = "package $personClassPackage" |
---|
69 | } |
---|
70 | if (authorityClassPackage) { |
---|
71 | binding.authorityClass = "${authorityClassPackage}.$authorityClassName" |
---|
72 | binding.authorityClassImport = "import $binding.authorityClass" |
---|
73 | binding.authorityClassPackage = "package $authorityClassPackage" |
---|
74 | } |
---|
75 | if (requestmapClassPackage) { |
---|
76 | binding.requestmapClass = "${requestmapClassPackage}.$requestmapClassName" |
---|
77 | binding.requestmapClassImport = "import $binding.requestmapClass" |
---|
78 | binding.requestmapClassPackage = "package $requestmapClassPackage" |
---|
79 | } |
---|
80 | |
---|
81 | outFile.withWriter { writer -> |
---|
82 | def template = new SimpleTemplateEngine().createTemplate(templateFile.text) |
---|
83 | template.make(binding).writeTo(writer) |
---|
84 | } |
---|
85 | |
---|
86 | println "file generated at $outFile.absolutePath" |
---|
87 | } |
---|
88 | |
---|
89 | copyFile = { String from, String to -> |
---|
90 | Ant.copy(file: from, tofile: to, overwrite: overwrite) |
---|
91 | } |
---|
92 | |
---|
93 | loadConfig = { |
---|
94 | GroovyClassLoader loader = new GroovyClassLoader(getClass().getClassLoader()) |
---|
95 | Class clazz = loader.parseClass(new File("$basedir/grails-app/conf/SecurityConfig.groovy")) |
---|
96 | def securityConfig = new ConfigSlurper().parse(clazz) |
---|
97 | splitPersonClassName securityConfig.security.loginUserDomainClass |
---|
98 | splitAuthorityClassName securityConfig.security.authorityDomainClass |
---|
99 | splitRequestmapClassName securityConfig.security.requestMapClass |
---|
100 | println "Login user domain class: $securityConfig.security.loginUserDomainClass" |
---|
101 | println "Authority domain class: $securityConfig.security.authorityDomainClass" |
---|
102 | println "Request Map domain class: $securityConfig.security.requestMapClass" |
---|
103 | } |
---|
104 | |
---|
105 | splitClassName = { name -> |
---|
106 | int index = name.lastIndexOf('.') |
---|
107 | return index == -1 ? [name, ''] : [name.substring(index + 1), name.substring(0, index)] |
---|
108 | } |
---|
109 | |
---|
110 | splitPersonClassName = { name -> |
---|
111 | def packageAndClass = splitClassName(name) |
---|
112 | personClassName = packageAndClass[0] |
---|
113 | personClassPackage = packageAndClass[1] |
---|
114 | } |
---|
115 | |
---|
116 | splitAuthorityClassName = { name -> |
---|
117 | def packageAndClass = splitClassName(name) |
---|
118 | authorityClassName = packageAndClass[0] |
---|
119 | authorityClassPackage = packageAndClass[1] |
---|
120 | } |
---|
121 | |
---|
122 | splitRequestmapClassName = { name -> |
---|
123 | def packageAndClass = splitClassName(name) |
---|
124 | requestmapClassName = packageAndClass[0] |
---|
125 | requestmapClassPackage = packageAndClass[1] |
---|
126 | } |
---|