OSDN Git Service

added robovm to setup-ui
[mikumikustudio/libgdx-mikumikustudio.git] / extensions / gdx-setup-ui / src / aurelienribon / gdxsetupui / Helper.java
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  * 
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 aurelienribon.gdxsetupui;
17
18 import aurelienribon.utils.XmlUtils;
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import javax.xml.xpath.XPathConstants;
24 import org.apache.commons.io.FilenameUtils;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.NodeList;
27 import org.xml.sax.SAXException;
28
29 /**
30  * @author Aurelien Ribon | http://www.aurelienribon.com/
31  */
32 public class Helper {
33         // -------------------------------------------------------------------------
34         // Project Configuration
35         // -------------------------------------------------------------------------
36
37         public static String getCorePrjName(BaseProjectConfiguration cfg) {
38                 return cfg.projectName + cfg.suffixCommon;
39         }
40
41         public static String getDesktopPrjName(BaseProjectConfiguration cfg) {
42                 return cfg.projectName + cfg.suffixDesktop;
43         }
44
45         public static String getAndroidPrjName(BaseProjectConfiguration cfg) {
46                 return cfg.projectName + cfg.suffixAndroid;
47         }
48
49         public static String getHtmlPrjName(BaseProjectConfiguration cfg) {
50                 return cfg.projectName + cfg.suffixHtml;
51         }
52
53         public static String getCorePrjPath(BaseProjectConfiguration cfg) {
54                 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixCommon + "/", true);
55         }
56
57         public static String getDesktopPrjPath(BaseProjectConfiguration cfg) {
58                 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixDesktop + "/", true);
59         }
60
61         public static String getAndroidPrjPath(BaseProjectConfiguration cfg) {
62                 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixAndroid + "/", true);
63         }
64
65         public static String getHtmlPrjPath(BaseProjectConfiguration cfg) {
66                 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixHtml + "/", true);
67         }
68         
69         public static String getIosPrjPath (ProjectUpdateConfiguration cfg) {
70                 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixIos + "/", true);
71         }
72         
73         public static String getRobovomPrjPath (ProjectUpdateConfiguration cfg) {
74                 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixRobovm + "/", true);
75         }
76
77         // -------------------------------------------------------------------------
78         // Classpath
79         // -------------------------------------------------------------------------
80
81         public static boolean isFileRuntimeJar(String filename) {
82                 if (!filename.endsWith(".jar")) return false;
83                 String name = FilenameUtils.getBaseName(filename);
84                 if (endsWith(name, "-source", "-sources", "-src")) return false;
85                 return true;
86         }
87
88         public static boolean endsWith(String str, String... ends) {
89                 for (String end : ends) if (str.endsWith(end)) return true;
90                 return false;
91         }
92
93         public static String getSource(List<String> files, String file) {
94                 String path = FilenameUtils.getFullPath(file);
95                 String name = FilenameUtils.getBaseName(file);
96                 String ext = FilenameUtils.getExtension(file);
97
98                 if (files.contains(path + name + "-source." + ext)) return path + name + "-source." + ext;
99                 if (files.contains(path + name + "-sources." + ext)) return path + name + "-sources." + ext;
100                 if (files.contains(path + name + "-src." + ext)) return path + name + "-src." + ext;
101                 return null;
102         }
103
104         public static List<ClasspathEntry> getCoreClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
105                 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
106
107                 for (String library : cfg.libraries) {
108                         LibraryDef def = libs.getDef(library);
109
110                         for (String file : def.libsCommon) {
111                                 if (!isFileRuntimeJar(file)) continue;
112                                 String source = getSource(def.libsCommon, file);
113                                 classpath.add(new ClasspathEntry("libs/", file, source, true));
114                         }
115                 }
116
117                 return classpath;
118         }
119
120         public static List<ClasspathEntry> getAndroidClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
121                 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
122                 if (!cfg.isAndroidIncluded) return classpath;
123
124                 String corePrjName = getCorePrjName(cfg);
125
126                 for (String library : cfg.libraries) {
127                         LibraryDef def = libs.getDef(library);
128
129                         for (String file : def.libsCommon) {
130                                 if (!isFileRuntimeJar(file)) continue;
131                                 String source = getSource(def.libsCommon, file);
132                                 classpath.add(new ClasspathEntry("/" + corePrjName + "/libs/", file, source, true));
133                         }
134
135                         for (String file : def.libsAndroid) {
136                                 if (!isFileRuntimeJar(file)) continue;
137                                 String source = getSource(def.libsAndroid, file);
138                                 classpath.add(new ClasspathEntry("libs/", file, source, true));
139                         }
140                 }
141
142                 return classpath;
143         }
144
145         public static List<ClasspathEntry> getDesktopClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
146                 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
147                 if (!cfg.isDesktopIncluded) return classpath;
148
149                 for (String library : cfg.libraries) {
150                         LibraryDef def = libs.getDef(library);
151
152                         for (String file : def.libsDesktop) {
153                                 if (!isFileRuntimeJar(file)) continue;
154                                 String source = getSource(def.libsDesktop, file);
155                                 classpath.add(new ClasspathEntry("libs/", file, source, false));
156                         }
157                 }
158
159                 return classpath;
160         }
161
162         public static List<ClasspathEntry> getHtmlClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
163                 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
164                 if (!cfg.isHtmlIncluded) return classpath;
165
166                 String corePrjName = getCorePrjName(cfg);
167
168                 for (String library : cfg.libraries) {
169                         LibraryDef def = libs.getDef(library);
170
171                         for (String file : def.libsCommon) {
172                                 if (!isFileRuntimeJar(file)) continue;
173                                 String source = getSource(def.libsCommon, file);
174                                 classpath.add(new ClasspathEntry("/" + corePrjName + "/libs/", file, source, false));
175                                 if (source != null) classpath.add(new ClasspathEntry("/" + corePrjName + "/libs/", source, null, false));
176                         }
177
178                         for (String file : def.libsHtml) {
179                                 if (!isFileRuntimeJar(file)) continue;
180                                 String source = getSource(def.libsHtml, file);
181                                 classpath.add(new ClasspathEntry("war/WEB-INF/lib/", file, source, false));
182                                 if (source != null) classpath.add(new ClasspathEntry("war/WEB-INF/lib/", source, null, false));
183                         }
184                 }
185
186                 return classpath;
187         }
188
189         public static List<GwtModule> getGwtModules(BaseProjectConfiguration cfg, LibraryManager libs) {
190                 List<GwtModule> gwtModules = new ArrayList<GwtModule>();
191                 if (!cfg.isHtmlIncluded) return gwtModules;
192
193                 for (String library : cfg.libraries) {
194                         LibraryDef def = libs.getDef(library);
195
196                         if (def.gwtModuleName != null) {
197                                 gwtModules.add(new GwtModule(def.gwtModuleName));
198                         }
199                 }
200
201                 return gwtModules;
202         }
203
204         public static List<ClasspathEntry> getClasspathEntries(File classpathFile) {
205                 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
206                 if (!classpathFile.isFile()) return classpath;
207
208                 try {
209                         Document doc = XmlUtils.createParser().parse(classpathFile);
210                         NodeList nodes = (NodeList) XmlUtils.xpath("classpath/classpathentry[@kind='lib' and @path]", doc, XPathConstants.NODESET);
211
212                         for (int i=0; i<nodes.getLength(); i++) {
213                                 String path = nodes.item(i).getAttributes().getNamedItem("path").getNodeValue();
214                                 String sourcepath = nodes.item(i).getAttributes().getNamedItem("sourcepath") != null
215                                         ? nodes.item(i).getAttributes().getNamedItem("sourcepath").getNodeValue() : null;
216                                 boolean exported = nodes.item(i).getAttributes().getNamedItem("exported") != null
217                                         ? nodes.item(i).getAttributes().getNamedItem("exported").getNodeValue().equalsIgnoreCase("true") : false;
218
219                                 classpath.add(new ClasspathEntry(path, sourcepath, exported));
220                         }
221
222                 } catch (SAXException ex) {
223                 } catch (IOException ex) {
224                 }
225
226                 return classpath;
227         }
228
229         public static List<GwtModule> getGwtModules(File modulesFile) {
230                 List<GwtModule> modules = new ArrayList<GwtModule>();
231                 if (!modulesFile.isFile()) return modules;
232
233                 try {
234                         Document doc = XmlUtils.createParser().parse(modulesFile);
235                         NodeList nodes = (NodeList) XmlUtils.xpath("module/inherits[@name]", doc, XPathConstants.NODESET);
236
237                         for (int i=0; i<nodes.getLength(); i++) {
238                                 String name = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
239                                 modules.add(new GwtModule(name));
240                         }
241
242                 } catch (SAXException ex) {
243                 } catch (IOException ex) {
244                 }
245
246                 return modules;
247         }
248
249         public static class ClasspathEntry implements Comparable<ClasspathEntry> {
250                 public String path;
251                 public String sourcepath;
252                 public boolean exported;
253                 public boolean overwritten = false;
254                 public boolean added = false;
255
256                 public ClasspathEntry(String path, String sourcepath, boolean exported) {
257                         this.path = path;
258                         this.sourcepath = sourcepath;
259                         this.exported = exported;
260                 }
261
262                 public ClasspathEntry(String preffix, String path, String sourcepath, boolean exported) {
263                         this.path = preffix + path;
264                         this.sourcepath = sourcepath != null ? preffix + sourcepath : null;
265                         this.exported = exported;
266                 }
267
268                 public void testOverwritten(List<ClasspathEntry> entries) {
269                         for (ClasspathEntry e : entries) if (e.path.equals(path)) {overwritten = true; return;}
270                 }
271
272                 public boolean testAdded(List<ClasspathEntry> entries) {
273                         for (ClasspathEntry e : entries) if (e.path.equals(path)) return false;
274                         added = true;
275                         return true;
276                 }
277
278                 @Override
279                 public int compareTo(ClasspathEntry o) {
280                         if (path.startsWith("/") && !o.path.startsWith("/")) return 1;
281                         if (!path.startsWith("/") && o.path.startsWith("/")) return -1;
282                         return path.compareTo(o.path);
283                 }
284         }
285
286         public static class GwtModule implements Comparable<GwtModule> {
287                 public String name;
288                 public boolean overwritten = false;
289                 public boolean added = false;
290
291                 public GwtModule(String name) {
292                         this.name = name;
293                 }
294
295                 public void testOverwritten(List<GwtModule> entries) {
296                         for (GwtModule e : entries) if (e.name.equals(name)) {overwritten = true; return;}
297                 }
298
299                 public boolean testAdded(List<GwtModule> entries) {
300                         for (GwtModule e : entries)
301                                 if (e.name.equals(name))
302                                         return false;
303                         added = true;
304                         return true;
305                 }
306
307                 @Override
308                 public int compareTo(GwtModule o) {
309                         return name.compareTo(o.name);
310                 }
311         }
312 }