Skip to content

Commit 58351ac

Browse files
rvowlesgraemerocher
authored andcommitted
GRAILS-9333 - ensures that files excluded in the pluginExcludes do not make it into a binary plugin
1 parent eea8d3a commit 58351ac

File tree

1 file changed

+80
-10
lines changed

1 file changed

+80
-10
lines changed

grails-core/src/main/groovy/org/codehaus/groovy/grails/plugins/publishing/PluginPackager.groovy

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import org.codehaus.groovy.grails.plugins.GrailsPluginInfo
2323
import org.springframework.core.io.FileSystemResource
2424
import org.springframework.core.io.Resource
2525
import grails.util.BuildSettings
26+
import org.springframework.util.AntPathMatcher
2627

2728
/**
2829
* Packages a plugin in source or binary form.
@@ -68,6 +69,7 @@ class PluginPackager {
6869
private AntBuilder ant
6970
private File resourcesDir
7071
private BuildSettings buildSettings
72+
private AntPathMatcher antPathMatcher = new AntPathMatcher()
7173

7274
List<File> jarFiles = []
7375
boolean hasApplicationDependencies
@@ -180,6 +182,59 @@ class PluginPackager {
180182
return pluginZip
181183
}
182184

185+
private boolean matchesExcludes(excludes, path) {
186+
for(String exclude : excludes) {
187+
if (antPathMatcher.match(exclude, path)) {
188+
return true
189+
}
190+
}
191+
192+
return false
193+
}
194+
195+
private void extraExcludesProcessDirectory(File dir, int stripLength, int fullStripLength, excludes, List<String> ejectedFiles) {
196+
dir.listFiles().each { File f ->
197+
if (f.isFile()) {
198+
if (matchesExcludes(excludes, f.absolutePath.substring(stripLength+1))) {
199+
ejectedFiles << f.absolutePath.substring(fullStripLength+1)
200+
}
201+
} else if (f.isDirectory() && !f.name.startsWith(".")) {
202+
extraExcludesProcessDirectory(f, stripLength, fullStripLength, excludes, ejectedFiles)
203+
}
204+
}
205+
}
206+
207+
/**
208+
* We have to go through and build up a list of exclusions because the plugin excludes are based from to "basedir" and all of the excludes
209+
* when copying are based from the directory we are copying from (e.g. APP/web-app will not match web-app/** because the exclude doesn't include
210+
* web-app when determining that pattern). This makes all plugin excludes worthless when copy files unless we go through and manually match them
211+
* ourselves and return a list of specifically excluded files.
212+
*
213+
* @param basedir - the Grails Applications base directory
214+
* @param subdir - the sub directory we are copying from so we can prefix this for our pattern matching
215+
* @param excludes - the excludes from pluginExcludes
216+
* @param base - the ones that the Ant copy wants to include no matter what (originally included as specific exclude: lines)
217+
* @return returns a list of files to exclude relative to basedir/subdir
218+
*/
219+
private List<String> extraExcludes(File basedir, String subdir, excludes, List<String> base = []) {
220+
if (!excludes) return base
221+
222+
List<String> ejectedFiles = []
223+
224+
File dir = new File(basedir, subdir)
225+
226+
if (dir.exists()) {
227+
int stripLength = basedir.absolutePath.size() // e.g. [/blah/blah/]web-dir/ - preserve web-dir so we can match it
228+
int fullStripLength = stripLength + 1 + subdir.size() // e.g. /blah/blah/[web-dir/]
229+
230+
extraExcludesProcessDirectory(dir, stripLength, fullStripLength, excludes, ejectedFiles)
231+
}
232+
233+
ejectedFiles.addAll(base)
234+
235+
return ejectedFiles
236+
}
237+
183238
String packageBinary(String pluginName, File classesDir, File targetDir) {
184239
def pluginProps = pluginInfo
185240
ant.taskdef (name: 'gspc', classname : 'org.codehaus.groovy.grails.web.pages.GroovyPageCompilerTask')
@@ -219,30 +274,45 @@ class PluginPackager {
219274
mkdir(dir:"${metaInf}/grails-app/i18n")
220275
if (new File("${resourcesDir}/grails-app/i18n").exists()) {
221276
copy(todir:"${metaInf}/grails-app/i18n", includeEmptyDirs:false,failonerror:false) {
222-
fileset(dir:"${resourcesDir}/grails-app/i18n")
277+
fileset(dir:"${resourcesDir}/grails-app/i18n") {
278+
extraExcludes(basedir, "grails-app/i18n", pluginProps?.pluginExcludes).each {
279+
exclude name: it
280+
}
281+
}
223282
}
224283
}
284+
285+
// the excludes get more difficult now, as they are from the root of the project and these
286+
// excludes would be from the fileset's dir
287+
225288
mkdir(dir:"${metaInf}/static")
226289
copy(todir:"${metaInf}/static", includeEmptyDirs:false, failonerror:false) {
227290
fileset(dir:"${basedir}/web-app") {
228-
exclude name:"plugins/**"
229-
exclude name:"**/WEB-INF/**"
230-
exclude name:"**/META-INF/**"
231-
exclude name:"**/*.gsp"
232-
exclude name:"**/*.jsp"
291+
extraExcludes(basedir, "web-app",
292+
pluginProps?.pluginExcludes, ["plugins/**", "**/WEB-INF/**", "**/META-INF/**",
293+
"**/*.gsp", "**/*.jsp"]).each {
294+
exclude name: it
295+
}
296+
233297
}
234298
}
235299
mkdir(dir:"${metaInf}/scripts")
236300
copy(todir:"${metaInf}/scripts") {
237-
fileset(dir:"${basedir}/scripts",
238-
excludes:"_Install.groovy,_Uninstall.groovy,_Upgrade.groovy")
301+
fileset(dir:"${basedir}/scripts") {
302+
extraExcludes(basedir, "scripts", pluginProps?.pluginExcludes, ["_Install.groovy","_Uninstall.groovy","_Upgrade.groovy"]).each {
303+
exclude name: it
304+
}
305+
}
239306
}
240307
mkdir(dir:"${classesDir}/src")
241308
copy(todir:"${classesDir}/src") {
242-
fileset(dir:"${basedir}/src", excludes:"groovy/**,java/**")
309+
fileset(dir:"${basedir}/src") {
310+
extraExcludes(basedir, "src", pluginProps?.pluginExcludes, ["groovy/**","java/**"]).each {
311+
exclude name: it
312+
}
313+
}
243314
}
244315

245-
246316
jar(destfile:destinationFile) {
247317
fileset(dir:classesDir, excludes:excludeList.join(','))
248318
manifest {

0 commit comments

Comments
 (0)