View File | Revision Log | Show Annotations | Download File | View Changeset
/hippo/hippo-cms7/cms/trunk/tools/eclipse/monkey/prepareProjects.gm
Revision: 26991
Committed: Thu Feb 17 08:25:51 2011 UTC (11 years, 9 months ago) by fvlankvelt
File size: 7199 byte(s)
Log Message:
CMS7-16: clean up perspective projects when preparing them to be run from eclipse

File Contents

# Content
1 /*
2 * Menu: Prepare Hippo ECM projects
3 * Script-Path: /hippo-cms-tools-eclipse/monkey/prepareProjects.gm
4 * Kudos: Wander Grevink (onehippo.com)
5 * License: Apache 2.0
6 * Job: WorkspaceJob
7 * Exec-Mode: Foreground
8 * DOM: http://groovy-monkey.sourceforge.net/update/plugins/net.sf.groovyMonkey.dom
9 */
10 import org.eclipse.jdt.internal.core.SetClasspathOperation
11 import org.eclipse.jdt.core.IClasspathEntry
12 import org.eclipse.jdt.internal.core.ClasspathEntry
13 import org.eclipse.jdt.core.IPackageFragmentRoot
14 import org.eclipse.jdt.core.IClasspathAttribute
15 import org.eclipse.jdt.core.JavaCore
16 import org.eclipse.core.internal.resources.Workspace
17
18 out.clear()
19 println "$metadata.menuName started\n"
20
21 addProjectDependency('hippo-quickstart-war', 'hippo-repository-application')
22 addProjectDependency('hippo-quickstart-war', 'hippo-cms-application')
23 addProjectDependency('hippo-package-war', 'hippo-repository-application')
24 addProjectDependency('hippo-package-war', 'hippo-cms-application')
25
26 cleanupSharedResources()
27 cleanupTestResourcesInDependencies('hippo-quickstart-war')
28 cleanupTestResourcesInDependencies('hippo-repository-application')
29 cleanupTestResourcesInDependencies('hippo-cms-application')
30 cleanupTestResourcesInDependencies('hippo-cms-browse')
31 cleanupTestResourcesInDependencies('hippo-cms-edit')
32 cleanupTestResourcesInDependencies('hippo-cms-dashboard')
33
34 convertResourcesToAny('hippo-repository-upgrade', 'src/main/resources')
35
36 println "\n$metadata.menuName finished"
37
38 /////////////
39
40 def cleanupTestResourcesInDependencies(projectName) {
41 removeClasspathEntry(projectName, 'src/test/java')
42 removeClasspathEntry(projectName, 'src/test/resources')
43 def project = workspace.root.getProject(projectName)
44 if (project.isOpen()) {
45 def javaProject = project.getNature('org.eclipse.jdt.core.javanature')
46 def classpath = javaProject.rawClasspath
47 classpath.each() {
48 if (it.entryKind == IClasspathEntry.CPE_PROJECT) {
49 def name = it.path.lastSegment()
50 removeProjectDependency(name, 'hippo-cms-engine-test')
51 removeProjectDependency(name, 'hippo-cms-builtin-test')
52 removeClasspathEntry(name, 'src/test/java')
53 removeClasspathEntry(name, 'src/test/resources')
54 }
55 }
56 }
57 }
58
59 def cleanupSharedResources() {
60 workspace.root.projects.each() {
61 if (it.isOpen() && it.hasNature('org.eclipse.jdt.core.javanature')) {
62 removeClasspathEntry(it.name, 'target/maven-shared-archive-resources')
63 }
64 }
65 }
66
67 def removeClasspathEntry(projectName, pathPattern) {
68 def project = workspace.root.getProject(projectName)
69 if (project.isOpen()) {
70 def javaProject = project.getNature('org.eclipse.jdt.core.javanature')
71 def classpath = javaProject.getRawClasspath()
72 def toBeRemoved = null
73 classpath.each() {
74 if (it.getPath().toString().contains(pathPattern)) {
75 toBeRemoved = it
76 }
77 }
78 if (toBeRemoved != null) {
79 println " Removing classpath entry $pathPattern for $projectName"
80 def classpathSet = new LinkedHashSet(Arrays.asList(classpath))
81 classpathSet.remove(toBeRemoved)
82 classpath = (IClasspathEntry[])classpathSet.toArray()
83 def op = new SetClasspathOperation(javaProject, classpath, javaProject.getOutputLocation(), true)
84 op.runOperation(monitor)
85 }
86 }
87 }
88
89
90 def addProjectDependency(fromName, toName) {
91 def from = workspace.root.getProject(fromName)
92 def to = workspace.root.getProject(toName)
93 if (from.isOpen() && to.isOpen()) {
94 def fromProject = from.getNature('org.eclipse.jdt.core.javanature')
95 def classpath = fromProject.getRawClasspath()
96 def classpathSet = new LinkedHashSet(Arrays.asList(classpath))
97 def dependency = new ClasspathEntry(
98 IPackageFragmentRoot.K_SOURCE, // indicates this classpath entry contains source files.
99 IClasspathEntry.CPE_PROJECT, // indicates a required project.
100 to.getFullPath(), // target project
101 ClasspathEntry.INCLUDE_ALL, // inclusion patterns
102 ClasspathEntry.EXCLUDE_NONE, // exclusion patterns
103 null, // source attachment
104 null, // source attachment root
105 null, // specific output folder
106 false, // isExported
107 null, // accessRules
108 true, // combineAccessRules
109 (IClasspathAttribute[])[] // extraAttributes
110 );
111 classpathSet.add(dependency)
112
113 if (classpathSet.size() > classpath.length) {
114 println " Adding project dependency from $fromName to $toName"
115 classpath = (IClasspathEntry[])classpathSet.toArray()
116 def op = new SetClasspathOperation(fromProject, classpath, fromProject.getOutputLocation(), true)
117 op.runOperation(monitor)
118 }
119 }
120 }
121
122 def removeProjectDependency(fromName, toName) {
123 def from = workspace.root.getProject(fromName)
124 def to = workspace.root.getProject(toName)
125 if (from.isOpen() && to.isOpen()) {
126 def fromProject = from.getNature('org.eclipse.jdt.core.javanature')
127 def classpath = fromProject.getRawClasspath()
128 def path = to.getFullPath()
129 def toBeRemoved = null
130 classpath.each() {
131 if (it.getPath().toString().contains(toName)) {
132 toBeRemoved = it
133 }
134 }
135 if (toBeRemoved != null) {
136 println " Removing project dependency from $fromName to $toName"
137 def classpathSet = new LinkedHashSet(Arrays.asList(classpath))
138 classpathSet.remove(toBeRemoved)
139 classpath = (IClasspathEntry[])classpathSet.toArray()
140 def op = new SetClasspathOperation(fromProject, classpath, fromProject.getOutputLocation(), true)
141 op.runOperation(monitor)
142 }
143 }
144 }
145
146 def convertResourcesToAny(fromName, source) {
147 def from = workspace.root.getProject(fromName)
148 if (from.isOpen()) {
149 def fromProject = from.getNature('org.eclipse.jdt.core.javanature')
150 def classpath = fromProject.getRawClasspath()
151 def toBeRemoved = null
152 classpath.each() {
153 if (it.entryKind == IClasspathEntry.CPE_SOURCE) {
154 if (it.path.toString().contains(source)) {
155 toBeRemoved = it
156 }
157 }
158 }
159 if (toBeRemoved != null) {
160 println " Converting resourses to use any in $fromName"
161 def classpathSet = new LinkedHashSet(Arrays.asList(classpath))
162 classpathSet.remove(toBeRemoved)
163
164 def dependency = JavaCore.newSourceEntry(from.fullPath.append(source))
165 classpathSet.add(dependency)
166
167 classpath = (IClasspathEntry[])classpathSet.toArray()
168 def op = new SetClasspathOperation(fromProject, classpath, fromProject.getOutputLocation(), true)
169 op.runOperation(monitor)
170 }
171 }
172 }
173
174
175