View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.pom;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.commons.io.FileUtils;
27  import org.apache.commons.lang3.StringUtils;
28  import org.kuali.common.util.KeyValue;
29  import org.kuali.common.util.LocationUtils;
30  import org.kuali.common.util.SimpleScanner;
31  
32  public class MobilityPomScrubber {
33  
34  	public static void main(String[] args) {
35  		try {
36  			File basedir = new File("/Users/jcaddel/ws/mobility-devops");
37  			SimpleScanner ss = new SimpleScanner(basedir, "**/pom.xml", "**/target/**");
38  			List<File> files = ss.getFiles();
39  			List<String> paths = new ArrayList<String>();
40  			for (File file : files) {
41  				String path = LocationUtils.getCanonicalPath(file);
42  				paths.add(path);
43  			}
44  			Collections.sort(paths);
45  			for (String path : paths) {
46  				scrub(path);
47  			}
48  			for (String path : paths) {
49  				scrub2(path);
50  			}
51  			for (String path : paths) {
52  				scrub3(path);
53  			}
54  		} catch (Exception e) {
55  			e.printStackTrace();
56  		}
57  	}
58  
59  	protected static Pom getPom(List<String> lines) {
60  		Pom pom = new Pom();
61  		for (int i = 0; i < lines.size(); i++) {
62  			String line = lines.get(i);
63  			if (StringUtils.startsWith(line, "    <artifactId>")) {
64  				pom.setArtifactId(new Line(i, line));
65  			} else if (StringUtils.startsWith(line, "    <version>")) {
66  				pom.setVersion(new Line(i, line));
67  			} else if (StringUtils.startsWith(line, "    <packaging>")) {
68  				pom.setPackaging(new Line(i, line));
69  			} else if (StringUtils.startsWith(line, "    <name>")) {
70  				pom.setName(new Line(i, line));
71  			} else if (StringUtils.startsWith(line, "    <description>")) {
72  				pom.setDescription(new Line(i, line));
73  			} else if (StringUtils.startsWith(line, "    </parent>")) {
74  				pom.setEndParentTag(new Line(i, line));
75  			}
76  		}
77  		return pom;
78  	}
79  
80  	protected static void scrub3(String path) throws IOException {
81  		List<String> lines = LocationUtils.readLines(path);
82  		Pom pom = getPom(lines);
83  		if (reArrange(pom, lines)) {
84  			System.out.println(path);
85  			FileUtils.writeLines(new File(path), lines);
86  		}
87  	}
88  
89  	protected static boolean reArrange(Pom pom, List<String> lines) {
90  		if (pom.getEndParentTag() == null) {
91  			return false;
92  		}
93  
94  		boolean reArranged = false;
95  
96  		// Setup indexes
97  		int endParentIndex = pom.getEndParentTag().getIndex();
98  		int addIndex = endParentIndex + 1;
99  
100 		// Add stuff
101 		if (pom.getArtifactId() != null && pom.getArtifactId().getIndex() < endParentIndex) {
102 			lines.add(addIndex, pom.getArtifactId().getContent());
103 			addIndex++;
104 			reArranged = true;
105 		}
106 		if (pom.getVersion() != null && pom.getVersion().getIndex() < endParentIndex) {
107 			lines.add(addIndex, pom.getVersion().getContent());
108 			addIndex++;
109 			reArranged = true;
110 		}
111 		if (pom.getPackaging() != null && pom.getPackaging().getIndex() < endParentIndex) {
112 			lines.add(addIndex, pom.getPackaging().getContent());
113 			addIndex++;
114 			reArranged = true;
115 		}
116 		if (pom.getName() != null && pom.getName().getIndex() < endParentIndex) {
117 			lines.add(addIndex, pom.getName().getContent());
118 			addIndex++;
119 			reArranged = true;
120 		}
121 		if (pom.getDescription() != null && pom.getDescription().getIndex() < endParentIndex) {
122 			lines.add(addIndex, pom.getDescription().getContent());
123 			addIndex++;
124 			reArranged = true;
125 		}
126 
127 		// Delete stuff
128 		if (pom.getArtifactId() != null && pom.getArtifactId().getIndex() < endParentIndex) {
129 			lines.set(pom.getArtifactId().getIndex(), "");
130 		}
131 		if (pom.getVersion() != null && pom.getVersion().getIndex() < endParentIndex) {
132 			lines.set(pom.getVersion().getIndex(), "");
133 		}
134 		if (pom.getPackaging() != null && pom.getPackaging().getIndex() < endParentIndex) {
135 			lines.set(pom.getPackaging().getIndex(), "");
136 		}
137 		if (pom.getName() != null && pom.getName().getIndex() < endParentIndex) {
138 			lines.set(pom.getName().getIndex(), "");
139 		}
140 		if (pom.getDescription() != null && pom.getDescription().getIndex() < endParentIndex) {
141 			lines.set(pom.getDescription().getIndex(), "");
142 		}
143 
144 		Iterator<String> itr = lines.iterator();
145 		while (itr.hasNext()) {
146 			String line = itr.next();
147 			if (StringUtils.isBlank(line)) {
148 				itr.remove();
149 			}
150 		}
151 		return reArranged;
152 	}
153 
154 	protected static void scrub2(String path) throws IOException {
155 		List<String> lines = LocationUtils.readLines(path);
156 		for (int i = 0; i < lines.size(); i++) {
157 			String line = lines.get(i);
158 			if (StringUtils.contains(line, "<parent>")) {
159 				if (reArrange(lines, i)) {
160 					System.out.println(path);
161 					FileUtils.writeLines(new File(path), lines);
162 				}
163 			}
164 		}
165 	}
166 
167 	protected static boolean reArrange(List<String> lines, int parentIndex) {
168 		String artifactId = lines.get(parentIndex + 1);
169 		String groupId = lines.get(parentIndex + 2);
170 
171 		if (!StringUtils.contains(artifactId, "<artifactId>")) {
172 			return false;
173 		}
174 
175 		if (!StringUtils.contains(groupId, "<groupId>")) {
176 			return false;
177 		}
178 
179 		lines.set(parentIndex + 1, groupId);
180 		lines.set(parentIndex + 2, artifactId);
181 		return true;
182 
183 	}
184 
185 	protected static void scrub(String path) throws IOException {
186 		String original = LocationUtils.toString(path);
187 		String scrubbed = getScrubbedContent(original);
188 		if (!StringUtils.equals(original, scrubbed)) {
189 			System.out.println(path);
190 			FileUtils.write(new File(path), scrubbed);
191 		}
192 	}
193 
194 	protected static String getScrubbedContent(String content) {
195 
196 		String compile = "<scope>compile</scope>\n";
197 		String jar = "<packaging>jar</packaging>\n";
198 
199 		List<String> removes = Arrays.asList(compile, jar);
200 		for (String remove : removes) {
201 			content = StringUtils.remove(content, remove);
202 		}
203 
204 		List<KeyValue> replaces = getKeyValues(getModules(), getSubModules());
205 		for (KeyValue replace : replaces) {
206 			content = StringUtils.replace(content, replace.getKey(), replace.getValue());
207 		}
208 		return content;
209 	}
210 
211 	protected static List<String> getModules() {
212 		List<String> modules = new ArrayList<String>();
213 		modules.add("academics");
214 		modules.add("admin");
215 		modules.add("alerts");
216 		modules.add("bus");
217 		modules.add("calendars");
218 		modules.add("computerlabs");
219 		modules.add("conference");
220 		modules.add("configparams");
221 		modules.add("configparams");
222 		modules.add("database");
223 		modules.add("dining");
224 		modules.add("emergencyinfo");
225 		modules.add("events");
226 		modules.add("feedback");
227 		modules.add("grades");
228 		modules.add("maps");
229 		modules.add("mdot-webapp");
230 		modules.add("news");
231 		modules.add("people");
232 		modules.add("push");
233 		modules.add("reporting");
234 		modules.add("security.authentication");
235 		modules.add("security.authorization");
236 		modules.add("shared");
237 		modules.add("tours");
238 		modules.add("user");
239 		modules.add("util.webservice.adapter");
240 		modules.add("weather");
241 		return modules;
242 	}
243 
244 	protected static List<String> getSubModules() {
245 		return Arrays.asList("api", "config", "impl", "ui", "web", "webapp");
246 	}
247 
248 	protected static List<KeyValue> getKeyValues(List<String> modules, List<String> submodules) {
249 		List<KeyValue> list = new ArrayList<KeyValue>();
250 		for (String artifactId : modules) {
251 			KeyValue kv = new KeyValue("<artifactId>" + artifactId + "</artifactId>", "<artifactId>kme-" + artifactId + "</artifactId>");
252 			list.add(kv);
253 			for (String submodule : submodules) {
254 				String original = "<artifactId>" + artifactId + "-" + submodule + "</artifactId>";
255 				String scrubbed = "<artifactId>kme-" + artifactId + "-" + submodule + "</artifactId>";
256 				KeyValue kvv = new KeyValue(original, scrubbed);
257 				list.add(kvv);
258 			}
259 		}
260 		return list;
261 	}
262 
263 }