krkr
2/23/2012 - 5:11 PM

fix config with wtp/jboss/maven/gwt

fix config with wtp/jboss/maven/gwt

import static Constants.*

class Constants {
	static final wsName = "mandarine"

	static final serverPath= "c:\\\\xxxx\\\\yyyy\\\\server\\\\profil\\\\deploy";
	static final wsPath = "c:\\\\xxxx\\\\ws\\\\$wsName\\\\.metadata\\\\.plugins\\\\org.eclipse.core.resources\\\\.projects"
}

// WS status

def listProjects = []
new File(wsPath).eachFile { 
	open = !new File(it.getAbsolutePath() + File.separator + "1.tree").exists()
	listProjects.add(new Project(path: it, name: it.getName(), open: open))
}

def listWsProjectClosed = listProjects.findAll { !it.open }
def listWsProjectOpened = listProjects.findAll { it.open }

//def listEarWsProjectOpened = listProjects.findAll { it.open && it.name.contains("ear") }
//println "-- liste des projets EAR ouverts presents dans le workspace"
//listEarWsProjectOpened.each { p -> println "	" + p }

//def listEarWsProjectClosed = listProjects.findAll { !it.open && it.name.contains("ear") }
//println "-- liste des projets EAR fermes presents dans le workspace"
//listEarWsProjectClosed.each { p -> println "	" + p }

// Server status

def listEarDeployed = []
new File(serverPath).traverse(nameFilter:~/.*\.ear/) { if (it.isFile()) listEarDeployed << it }

//println "-- liste des ear non exploded deployes"
//listEarDeployed.each { p -> println "	" + p.getName() }

def listEarExplodedDeployed = []
new File(serverPath).traverse(nameFilter:~/.*\.ear/) { if (it.isDirectory()) listEarExplodedDeployed << it }

//println "-- liste des ear exploded deployes dans"
//listEarExplodedDeployed.each { p -> println "	" + p.getName() }

// Fix configuration

new File(".").traverse(nameFilter:~/.*-ear/) {
	fixEar(it)
}
new File(".").traverse(nameFilter:~/.*-application-ui/) {
	fixAppUi(it, listWsProjectOpened)
}

def fixEar(projectPath) {
	println ""
	println "-- fixEar " + projectPath
	artifactIdVersion = readPomArtifactIdVersion(projectPath)

	filename = projectPath.getAbsolutePath() + File.separator + ".settings" + File.separator + "org.eclipse.wst.common.component"
	File file = new File(filename)
	if (file.exists()) {
		xml = new XmlSlurper().parse(file)
		deployNameRead = xml."wb-module"."@deploy-name".text();

		if (artifactIdVersion.equals(deployNameRead)) {
			println "[OK] .. org.eclipse.wst.common.component"
		} else {
			println "[KO] .. org.eclipse.wst.common.component"
			println "$deployNameRead instead of $artifactIdVersion"

			xml."wb-module"."@deploy-name" = artifactIdVersion
			writeXml(file, xml)
		}
	} else {
		println "[WARN] org.eclipse.wst.common.component n'existe pas"
	}
}

def fixAppUi(projectPath, listWsProjectOpened) {
	println ""
	println "-- fixAppUi $projectPath"

	def sourceDependencies = []
	def pom = new File(projectPath.getAbsolutePath() + File.separator + "pom.xml").text
	def matcher = pom =~ '''(?x)
		<dependency>                            \\s*
			<groupId>[^<]+</groupId>          \\s*
			<artifactId>([^<]+)</artifactId>    \\s*
			<version>[^<]+</version>          \\s*
			<classifier>sources</classifier>    \\s*
			<scope>provided</scope>            (?s:.*?)
		</dependency>
	'''
	matcher.each { matched, artifactId -> sourceDependencies << artifactId }
	matcher = pom =~ '''(?x)
		<dependency>                            \\s*
			<groupId>[^<]+</groupId>          \\s*
			<artifactId>([^<]+)</artifactId>    \\s*
			<version>[^<]+</version>          \\s*
			<scope>provided</scope>             \\s*
			<classifier>sources</classifier>    (?s:.*?)
		</dependency>
	'''
	matcher.each { matched, artifactId -> sourceDependencies << artifactId }
	artifactIdVersion = readPomArtifactIdVersion(projectPath)

	projectPath.traverse(nameFilter:~/.*.\.launch/) {
		// fix -war url to generate m
		name = it.getName()
		println " --- fix $name"
		xml = new XmlSlurper().parse(it)
		xml.stringAttribute.findAll { it.@key == 'org.eclipse.jdt.launching.PROGRAM_ARGUMENTS' }.each { s ->
			warName = artifactIdVersion +".war"
			earName = warName.replace("ui", "ear").replace("war", "ear")
			url = warName.replace("-application-ui", "").replace(".war", "")
			progArgsRead = s.@value.text().replaceAll("-war \".*\"", "-war \"$serverPath\\\\$earName\\\\$warName\"")
			progArgsRead = s.@value.text().replaceAll("-startupUrl /.*/m ", "-startupUrl /$url/m ")
			s.@value = progArgsRead
		}

		writeXml(it, xml)

		// fix list 
		def sourceDeclaredInLauncher = []
		def pattern = ~/internalArchive=".*"/
		xml.listAttribute.findAll { it.@key == 'org.eclipse.jdt.launching.CLASSPATH' }.each { s ->
			s.listEntry.findAll { it.@value =~ /internalArchive/ }.each { l -> 
				sourceDeclaredInLauncher << l.@value.text().replaceAll(/.*internalArchive="\/(.*)\/src.*" p.*/, '$1') 
			}
		}

		//println " --- liste des dependances sources provided utiles a la compil gwt"
		//sourceDependencies.each { s -> println s }

		//println " --- liste des dependances sources declares dans le launcher gwt"
		//sourceDeclaredInLauncher.each { s -> println s }

		println " --- quelles dependances declarees dans le pom de l'ui ne sont pas referencees dans le launcher ?"
		sourceDependencies.findAll{ !sourceDeclaredInLauncher.contains(it) }.each { s -> println "		" + s }

		println " --- quelles dependances referencees dans le launcher n'ont pas de projets ouverts dans le workspace ?"
		sourceDeclaredInLauncher.findAll{ !listWsProjectOpened.collect { it.name }.contains(it) }.each { s -> println "		" + s }
	}
}

def String readPomArtifactIdVersion(directory)
{
	pomXml = new XmlSlurper().parse(directory.getAbsolutePath() + File.separator + "pom.xml")
	pomXml.artifactId.text() + "-" + pomXml.version.text()
}

def writeXml(file, xml)
{
	PrintWriter writer = new PrintWriter(file)
	writer.print(serializeXml(xml));
	writer.close()
}

def String serializeXml(groovy.util.slurpersupport.GPathResult xml){
    groovy.xml.XmlUtil.serialize(new groovy.xml.StreamingMarkupBuilder().bind {
        mkp.yield xml
      } )
}

class Project {
	File path;
	String name;
	boolean open;
}