(refs #7362) Creation of new SCL modules in SCL module browser
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / modulebrowser / CreateModuleAction.java
1 package org.simantics.scl.ui.modulebrowser;
2
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.nio.file.Paths;
7
8 import org.osgi.framework.Bundle;
9 import org.osgi.framework.BundleContext;
10 import org.simantics.scl.ui.Activator;
11
12 import gnu.trove.map.hash.THashMap;
13
14 public class CreateModuleAction {
15     public static final String PREFIX = "reference:file:/";
16     
17     public static final void createModule(Bundle bundle, String packageName, String moduleName) throws IOException {
18         String bundleLocation = bundle.getLocation();
19         bundleLocation = bundleLocation.substring(PREFIX.length());
20         
21         Path packagePath = Paths.get(bundleLocation).resolve("scl").resolve(packageName);
22         Files.createDirectories(packagePath);
23         Path modulePath = packagePath.resolve(moduleName + ".scl");
24         if(Files.exists(modulePath))
25             return;
26         Files.createFile(modulePath);
27     }
28     
29     public static THashMap<String,Bundle> findGoodBundles() {
30         BundleContext context = Activator.getInstance().getBundle().getBundleContext();
31         THashMap<String,Bundle> result = new THashMap<String,Bundle>();
32         for(Bundle bundle : context.getBundles()) {
33             String location = bundle.getLocation();
34             if(location.endsWith(".jar") || !location.startsWith(PREFIX))
35                 continue;
36             location = location.substring(PREFIX.length());
37             Path bundlePath = Paths.get(location);
38             if(!Files.isDirectory(bundlePath))
39                 continue;
40             result.put(bundle.getSymbolicName(), bundle);
41         }
42         return result;
43     }
44  
45     public static String findBestPlugin(THashMap<String,Bundle> bundles, String packageName) {
46         for(Bundle bundle : bundles.values()) {
47             String location = bundle.getLocation();
48             location = location.substring(PREFIX.length());
49             Path packagePath = Paths.get(location).resolve("scl").resolve(packageName);
50             if(Files.exists(packagePath))
51                 return bundle.getSymbolicName();
52         }
53         int p = packageName.lastIndexOf('/');
54         if(p == -1)
55             return "";
56         else
57             return findBestPlugin(bundles, packageName.substring(0, p));
58     }
59
60     public static int packageMatchLength(Bundle bundle, String packageName) {
61         if(bundle.getEntry("scl") == null)
62             return 0;
63         packageName = "scl/" + packageName;
64         while(packageName.length() > 3) {
65             if(bundle.getEntry(packageName) != null)
66                 return packageName.length();
67             int p = packageName.lastIndexOf('/');
68             packageName = packageName.substring(0, p);
69         }
70         return 3;
71     }
72 }