import groovy.xml.MarkupBuilder
ant.importBuild 'build.xml'
def excludes = [
".svn",
"ofm.tools.charting",
"ofm.tools.rankingtool"
]
ext {
zipName = "${buildDir}/OFMTools-${ant.properties['app.version']}.zip"
pluginHelpers = []
updateDir = "${buildDir}/Updates"
}
task wrapper(type: Wrapper) {
gradleVersion = '1.1'
}
task distZip () {
description = 'clean - build - dist'
inputs.dir "${projectDir}/plugins"
outputs.file project.ext.zipName
}
distZip << {
println '[ANT] clean'
clean.execute()
println '[ANT] build'
build.execute()
println '[ANT] dist'
dist.execute()
}
task generateUpdates(dependsOn: [ build, distZip ]) {
description = 'generates Update.xml'
inputs.dir "${projectDir}/plugins"
outputs.file "${buildDir}/updates.xml"
}
generateUpdates << {
File srcDir = new File("${projectDir}/plugins")
File destFile = new File("${buildDir}/updates.xml")
description = 'Generates xml for plugin-update-repository.'
println 'generating updates.xml'
File[] fileList = srcDir.listFiles()
List<File> filteredList = fileList.collect { File file ->
if(!excludes.contains(file.name)) {
return new File(file.absoluteFile.toString() + "/plugin.xml")
}
} as List
filteredList.removeAll {
it == null ? true:false;
}
StringWriter writer = new StringWriter()
MarkupBuilder xml = new groovy.xml.MarkupBuilder(writer)
xml.updates() { allPlugins2XML(xml, filteredList) }
destFile.write(writer.toString())
println 'File was created: ' + destFile.absoluteFile
}
task unzip(type: Copy, dependsOn:[generateUpdates, distZip]){
description = 'unzips .zip to get all plugin.zips'
def zipFile = file(project.ext.zipName)
def outputDir = file("${buildDir}/tmp")
inputs.file project.ext.zipName
outputs.dir outputDir
from zipTree(zipFile)
into outputDir
}
unzip << {
println '[GRADLE] starting unzipping'
}
task prepUpload(type: Copy, dependsOn:[unzip,generateUpdates,distZip]) {
description = 'prepares Upload Folder'
def updatesXml = "${buildDir}/updates.xml"
def outputDir = project.ext.updateDir
inputs.dir "${buildDir}/tmp/plugins"
inputs.file updatesXml
outputs.dir outputDir
from updatesXml
into outputDir
}
prepUpload << {
println '[GRADLE] Preparing Upload Folder'
File pluginDir = file("${buildDir}/tmp/plugins")
for(File f in pluginDir.listFiles()) {
File newFolder = createMissingPluginFolder(f)
copy{
from f
into newFolder
}
}
}
def createMissingPluginFolder(def file) {
def outputDirPlugins = new File("${project.ext.updateDir}/Plugins")
def matcher = file.name =~ /\w*\.\w*\.(\w*)-/
def shortName = matcher[0][1].replaceAll(/[^a-zA-Z]/,"")
println "[INFO] moving ${file.name} - short: ${shortName}"
def newFolder = new File("${outputDirPlugins}\\${shortName}")
if(!newFolder.mkdirs()) {
println "[WARNUNG] Folder ${newFolder.absoluteFile} couldn't be created"
}
return newFolder
}
task readPluginTest(type: Test) { description = 'test Plugin reading' }
readPluginTest << {
def plugin = readPluginInfo(file("${projectDir}/plugins/ofm.tools.awprechner/plugin.xml"))
assert plugin.name == 'ofm.tools.awprechner'
println "${it.name} successfull"
}
task parsePlugin2XmlTest(type: Test) { description = 'Test parsing of Plugin to xml' }
parsePlugin2XmlTest << {
StringWriter writer = new StringWriter()
MarkupBuilder xml = new groovy.xml.MarkupBuilder(writer)
xml.updates() {
pluginInfo2XML(xml, file("${projectDir}/plugins/ofm.tools.awprechner/plugin.xml"))
}
def updates = new XmlParser().parseText(writer.toString())
assert updates.plugin[0].@name == 'ofm.tools.awprechner'
println "${it.name} successfull"
}
def readPluginInfo ( File file ) {
def pluginXML = new XmlParser().parse(file)
def plugin = new PluginHelper(name: pluginXML.@id, version: pluginXML.@version )
pluginXML.requires.import.each { dependency ->
plugin.requires << addRequiredPlugin(dependency)
}
project.ext.pluginHelpers << plugin
return plugin
}
def readBasicPluginInfo(File file) {
def pluginXML = new XmlParser().parse(file)
return new PluginHelper(name: pluginXML.@id, version: pluginXML.@version )
}
def addRequiredPlugin(def dependency) {
def requiredPlugin = new PluginHelper()
requiredPlugin.name = dependency.attribute('plugin-id')
def version = dependency.attribute('plugin-version')
if(version == null) {
version = readBasicPluginInfo(file("${projectDir}/plugins/${requiredPlugin.name}/plugin.xml")).version
}
requiredPlugin.version = version
return requiredPlugin
}
def allPlugins2XML( MarkupBuilder builder, List<File> files) {
files.each { file ->
pluginInfo2XML(builder,file)
}
}
def pluginInfo2XML (MarkupBuilder builder, File file) {
println 'Parsing Plugin to updatesxml format: ' + file.absoluteFile.toString().replace(projectDir.absolutePath.toString(),"")
def myPlugin = readPluginInfo(file)
builder.plugin(name: myPlugin.name, version: myPlugin.version, link: myPlugin.updateUrl) {
dependencies2XML(builder, myPlugin.requires)
}
}
def dependencies2XML(builder, deps) {
deps.each { dep ->
builder.dependency(name: dep.name, version: dep.version)
}
}
class PluginHelper {
String name
String version
List requires = []
String getShortName() {
return name.split(/\./)[2]
}
String getUpdateUrl() {
"http://sourceforge.net/projects/ofmtools/files/OFMTools/Updates/Plugins/${shortName}/${name}-${version}.zip"
}
}