<commit_before>/*
 * Copyright 2004-2005 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Gant script that handles the creation of Grails plugins
 * 
 * @author <NAME>
 *
 * @since 0.5.5
 */
import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU  
import groovy.xml.DOMBuilder
import groovy.xml.dom.DOMCategory
import org.apache.xml.serialize.XMLSerializer
import org.apache.xml.serialize.OutputFormat

DEFAULT_PLUGIN_DIST = "http://plugins.grails.org"
BINARY_PLUGIN_DIST = "http://plugins.grails.org/dist"

Ant.property(environment:"env")   
grailsHome = Ant.antProject.properties."env.GRAILS_HOME"    

includeTargets << new File ( "${grailsHome}/scripts/Init.groovy" )

pluginsHome = "${userHome}/.grails/plugins/"
Ant.mkdir(dir:"${pluginsHome}")
pluginsListFile = new File("${pluginsHome}/plugins-list.xml")


def recreateCache = false
if( !pluginsListFile.exists() ) {
    recreateCache = true
}
try {
    document = DOMBuilder.parse(new FileReader(pluginsListFile))
} catch( Exception e ) {
    recreateCache = true
}
if( recreateCache ) {
    event("StatusUpdate", ["Plugins list cache does not exist or is broken, recreating"])
    document = DOMBuilder.newInstance().createDocument()
    def root = document.createElement('plugins')
    root.setAttribute('revision','0')
    document.appendChild(root)
}

pluginsList = document.documentElement
builder = new DOMBuilder(document)

def indentingOutputFormat = new OutputFormat("XML","UTF-8",true)

def writePluginsFile = {
    XMLSerializer serializer = new XMLSerializer( new FileWriter(pluginsListFile), indentingOutputFormat );
    serializer.serialize(document)
}

def parseRemoteXML = { url ->
    DOMBuilder.parse(new URL(url).openStream().newReader())
}

task ( "default" : "Lists plug-ins that are hosted by the Grails server") {
    listPlugins()
}

task(listPlugins:"Implementation task") {
    depends(updatePluginsList)
    println '''
Plug-ins available in the Grails repository are listed below:
-------------------------------------------------------------
'''
    def spacesFormatter = "                    "
    def plugins = []
    use(DOMCategory) {
        pluginsList.'plugin'.each { plugin ->
            def pluginLine = plugin.'@name'
            def version
            def title = "No description available"
            if( plugin.'@latest-release' ) {
                version = plugin.'@latest-release'
                pluginLine += "${spacesFormatter[pluginLine.length()..-1]}<${version}>"
            } else if( plugin.'release'.size() > 0) {
                // determine latest release by comparing version names in lexicografic order
                version = plugin.'release'[0].'@version'
                plugin.'release'.each {
                    if( !"${it.'@version'}".endsWith("SNAPSHOT") && "${it.'@version'}" > version ) version = "${it.'@version'}"
                }
                pluginLine += "${spacesFormatter[pluginLine.length()..-1]}<${version} (?)>"
            } else {
                pluginLine += "${spacesFormatter[pluginLine.length()..-1]}<no releases>"
            }
            def release = plugin.'release'.find{ rel -> rel.'@version' == version }
            if( release?.'title' ) {
                title = release?.'title'.text()
            }
            pluginLine += "\t--\t${title}"
            plugins << pluginLine
        }
    }
    // Sort plugin descriptions
    plugins.sort()
    plugins.each { println it }
    println '''
To find more info about plugin type 'grails plugin-info [NAME]'

To install type 'grails install-plugin [NAME] [VERSION]'

For further info visit http://grails.org/Plugins
'''
}

def buildReleaseInfo = { root, pluginName, releasePath, releaseTag ->
    if( releaseTag == '..' || releaseTag == 'LATEST_RELEASE' ) return
    def releaseNode = root.'release'.find{ it.'@tag' == releaseTag }
    if( releaseNode ) {
        if( releaseTag != 'trunk' ) return
        else root.removeChild(releaseNode)
    }
    try {
        def properties = ['title','author','authorEmail','description','documentation']
        def releaseDescriptor = parseRemoteXML("${releasePath}/${releaseTag}/plugin.xml").documentElement
        def version = releaseDescriptor.'@version'
        def releaseContent = new URL("${releasePath}/${releaseTag}/").text
        // we don't want to proceed release if zip distribution for this release is not published
        if( releaseContent.indexOf( "grails-${pluginName}-${version}.zip" ) < 0 ) return
        releaseNode = builder.createNode('release',[tag:releaseTag,version:version,type:'svn'])
        root.appendChild(releaseNode)
        properties.each {
            if( releaseDescriptor."${it}") {
                releaseNode.appendChild(builder.createNode(it,releaseDescriptor."${it}".text()))
            }
        }
        releaseNode.appendChild(builder.createNode('file',"${releasePath}/${releaseTag}/grails-${pluginName}-${version}.zip"))
    } catch( Exception e ) {
        // no plugin release info available
    }
}

def buildPluginInfo = {root, pluginName ->
    use( DOMCategory ) {
        def pluginNode = root.'plugin'.find { it.'@name' == pluginName }
        if( !pluginNode ) {
            pluginNode = builder.'plugin'(name:pluginName)
            root.appendChild(pluginNode)
        }
        def latestVersion = null
        try {
            latestVersion = parseRemoteXML("${DEFAULT_PLUGIN_DIST}/grails-${pluginName}/tags/LATEST_RELEASE/plugin.xml").documentElement.'@version'
        } catch( Exception e ) {
            // latest release version is not available
        }
        if( latestVersion ) pluginNode.setAttribute('latest-release',latestVersion as String)

        // proceed tagged releases
        try {
            def releaseTagsList = new URL("${DEFAULT_PLUGIN_DIST}/grails-${pluginName}/tags/").text
            releaseTagsList.eachMatch( /<li><a href="(.+?)">/ ) {
                def releaseTag = it[1][0..-2]
                buildReleaseInfo(pluginNode, pluginName, "${DEFAULT_PLUGIN_DIST}/grails-${pluginName}/tags", releaseTag)
            }
        } catch( Exception e ) {
            // no plugin release info available
        }

        // proceed trunk release
        try {
            buildReleaseInfo(pluginNode, pluginName,"${DEFAULT_PLUGIN_DIST}/grails-${pluginName}", "trunk" )
        } catch( Exception e ) {
            // no plugin release info available
        }
    }
}

def buildBinaryPluginInfo = {root, pluginName ->
    // split plugin name in form of 'plugin-name-0.1' to name ('plugin-name') and version ('0.1')
    def matcher = (pluginName =~ /^([^\d]+)-(.++)/)
    // convert to new plugin naming convention (MyPlugin -> my-plugin)<commit_msg>- changed to not proceed plugin's trunk/ if it's version not ends with 'SNAPSHOT' - changed to not set latest-release if corresponding release is broken (zip is not published, for example)<commit_after>/*
 * Copyright 2004-2005 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Gant script that handles the creation of Grails plugins
 * 
 * @author <NAME>
 *
 * @since 0.5.5
 */
import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU  
import groovy.xml.DOMBuilder
import groovy.xml.dom.DOMCategory
import org.apache.xml.serialize.XMLSerializer
import org.apache.xml.serialize.OutputFormat

DEFAULT_PLUGIN_DIST = "http://plugins.grails.org"
BINARY_PLUGIN_DIST = "http://plugins.grails.org/dist"

Ant.property(environment:"env")   
grailsHome = Ant.antProject.properties."env.GRAILS_HOME"    

includeTargets << new File ( "${grailsHome}/scripts/Init.groovy" )

pluginsHome = "${userHome}/.grails/plugins/"
Ant.mkdir(dir:"${pluginsHome}")
pluginsListFile = new File("${pluginsHome}/plugins-list.xml")


def recreateCache = false
if( !pluginsListFile.exists() ) {
    recreateCache = true
}
try {
    document = DOMBuilder.parse(new FileReader(pluginsListFile))
} catch( Exception e ) {
    recreateCache = true
}
if( recreateCache ) {
    event("StatusUpdate", ["Plugins list cache does not exist or is broken, recreating"])
    document = DOMBuilder.newInstance().createDocument()
    def root = document.createElement('plugins')
    root.setAttribute('revision','0')
    document.appendChild(root)
}

pluginsList = document.documentElement
builder = new DOMBuilder(document)

def indentingOutputFormat = new OutputFormat("XML","UTF-8",true)

def writePluginsFile = {
    XMLSerializer serializer = new XMLSerializer( new FileWriter(pluginsListFile), indentingOutputFormat );
    serializer.serialize(document)
}

def parseRemoteXML = { url ->
    DOMBuilder.parse(new URL(url).openStream().newReader())
}

task ( "default" : "Lists plug-ins that are hosted by the Grails server") {
    listPlugins()
}

task(listPlugins:"Implementation task") {
    depends(updatePluginsList)
    println '''
Plug-ins available in the Grails repository are listed below:
-------------------------------------------------------------
'''
    def spacesFormatter = "                    "
    def plugins = []
    use(DOMCategory) {
        pluginsList.'plugin'.each { plugin ->
            def pluginLine = plugin.'@name'
            def version
            def title = "No description available"
            if( plugin.'@latest-release' ) {
                version = plugin.'@latest-release'
                pluginLine += "${spacesFormatter[pluginLine.length()..-1]}<${version}>\t"
            } else if( plugin.'release'.size() > 0) {
                // determine latest release by comparing version names in lexicografic order
                version = plugin.'release'[0].'@version'
                plugin.'release'.each {
                    if( !"${it.'@version'}".endsWith("SNAPSHOT") && "${it.'@version'}" > version ) version = "${it.'@version'}"
                }
                pluginLine += "${spacesFormatter[pluginLine.length()..-1]}<${version} (?)>\t"
            } else {
                pluginLine += "${spacesFormatter[pluginLine.length()..-1]}<no releases>"
            }
            def release = plugin.'release'.find{ rel -> rel.'@version' == version }
            if( release?.'title' ) {
                title = release?.'title'.text()
            }
            pluginLine += "\t--\t${title}"
            plugins << pluginLine
        }
    }
    // Sort plugin descriptions
    plugins.sort()
    plugins.each { println it }
    println '''
To find more info about plugin type 'grails plugin-info [NAME]'

To install type 'grails install-plugin [NAME] [VERSION]'

For further info visit http://grails.org/Plugins
'''
}

def buildReleaseInfo = { root, pluginName, releasePath, releaseTag ->
    if( releaseTag == '..' || releaseTag == 'LATEST_RELEASE' ) return
    def releaseNode = root.'release'.find{ it.'@tag' == releaseTag }
    if( releaseNode ) {
        if( releaseTag != 'trunk' ) return
        else root.removeChild(releaseNode)
    }
    try {
        def properties = ['title','author','authorEmail','description','documentation']
        def releaseDescriptor = parseRemoteXML("${releasePath}/${releaseTag}/plugin.xml").documentElement
        def version = releaseDescriptor.'@version'
        if( releaseTag == 'trunk' && !(version.endsWith('SNAPSHOT'))) return
        def releaseContent = new URL("${releasePath}/${releaseTag}/").text
        // we don't want to proceed release if zip distribution for this release is not published
        if( releaseContent.indexOf( "grails-${pluginName}-${version}.zip" ) < 0 ) return
        releaseNode = builder.createNode('release',[tag:releaseTag,version:version,type:'svn'])
        root.appendChild(releaseNode)
        properties.each {
            if( releaseDescriptor."${it}") {
                releaseNode.appendChild(builder.createNode(it,releaseDescriptor."${it}".text()))
            }
        }
        releaseNode.appendChild(builder.createNode('file',"${releasePath}/${releaseTag}/grails-${pluginName}-${version}.zip"))
    } catch( Exception e ) {
        // no plugin release info available
    }
}

def buildPluginInfo = {root, pluginName ->
    use( DOMCategory ) {
        def pluginNode = root.'plugin'.find { it.'@name' == pluginName }
        if( !pluginNode ) {
            pluginNode = builder.'plugin'(name:pluginName)
            root.appendChild(pluginNode)
        }

        // proceed tagged releases
        try {
            def releaseTagsList = new URL("${DEFAULT_PLUGIN_DIST}/grails-${pluginName}/tags/").text
            releaseTagsList.eachMatch( /<li><a href="(.+?)">/ ) {
                def releaseTag = it[1][0..-2]
                buildReleaseInfo(pluginNode, pluginName, "${DEFAULT_PLUGIN_DIST}/grails-${pluginName}/tags", releaseTag)
            }
        } catch( Exception e ) {
            // no plugin release info available
        }

        // proceed trunk release
        try {
            buildReleaseInfo(pluginNode, pluginName,"${DEFAULT_PLUGIN_DIST}/grails-${pluginName}", "trunk" )
        } catch( Exception e ) {
            // no plugin release info available
        }

        def latestVersion = null
        try {
            latestVersion = parseRemoteXML("${DEFAULT_PLUGIN_DIST}/grails-${pluginName}/tags/LATEST_RELEASE/plugin.xml").documentElement.'@version'
        } catch( Exception e ) {
            // latest release version is not available
        }
        if( latestVersion && pluginNode.'release'.find{ it.'@version' == latestVersion }) pluginNode.setAttribute('latest-release',latestVersion as String)
    }
}

def buildBinaryPluginInfo = {root, pluginName ->
    // split plugin name in form of 'plugin-name-0.1' to name ('plugin-name') and version ('0.1')
    def matcher = (pluginName =~ /^([^\d]+)-(.++)/)
    // convert to new plugin naming convention (MyPlugin -> my-plugin)
{'fraction_non_alphanumeric': 0.14014667294624233, 'fraction_numerical': 0.00403687008006459, 'mean_word_length': 2.7535353535353537, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '784682', 'n_tokens_mistral': 4126, 'n_tokens_neox': 4002, 'n_words': 1015}