X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.ui%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fui%2Fmodulebrowser%2FCreateModuleAction.java;fp=bundles%2Forg.simantics.scl.ui%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fui%2Fmodulebrowser%2FCreateModuleAction.java;h=1d70442c85d709f5162f06da8e7427ca8b252404;hb=0d50e7ddf3d00d7ceabf109848139531dafffd6a;hp=0000000000000000000000000000000000000000;hpb=56f843ece1f6c8cbe55a5dc3495d828a1163c2fe;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/CreateModuleAction.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/CreateModuleAction.java new file mode 100644 index 000000000..1d70442c8 --- /dev/null +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/CreateModuleAction.java @@ -0,0 +1,72 @@ +package org.simantics.scl.ui.modulebrowser; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.simantics.scl.ui.Activator; + +import gnu.trove.map.hash.THashMap; + +public class CreateModuleAction { + public static final String PREFIX = "reference:file:/"; + + public static final void createModule(Bundle bundle, String packageName, String moduleName) throws IOException { + String bundleLocation = bundle.getLocation(); + bundleLocation = bundleLocation.substring(PREFIX.length()); + + Path packagePath = Paths.get(bundleLocation).resolve("scl").resolve(packageName); + Files.createDirectories(packagePath); + Path modulePath = packagePath.resolve(moduleName + ".scl"); + if(Files.exists(modulePath)) + return; + Files.createFile(modulePath); + } + + public static THashMap findGoodBundles() { + BundleContext context = Activator.getInstance().getBundle().getBundleContext(); + THashMap result = new THashMap(); + for(Bundle bundle : context.getBundles()) { + String location = bundle.getLocation(); + if(location.endsWith(".jar") || !location.startsWith(PREFIX)) + continue; + location = location.substring(PREFIX.length()); + Path bundlePath = Paths.get(location); + if(!Files.isDirectory(bundlePath)) + continue; + result.put(bundle.getSymbolicName(), bundle); + } + return result; + } + + public static String findBestPlugin(THashMap bundles, String packageName) { + for(Bundle bundle : bundles.values()) { + String location = bundle.getLocation(); + location = location.substring(PREFIX.length()); + Path packagePath = Paths.get(location).resolve("scl").resolve(packageName); + if(Files.exists(packagePath)) + return bundle.getSymbolicName(); + } + int p = packageName.lastIndexOf('/'); + if(p == -1) + return ""; + else + return findBestPlugin(bundles, packageName.substring(0, p)); + } + + public static int packageMatchLength(Bundle bundle, String packageName) { + if(bundle.getEntry("scl") == null) + return 0; + packageName = "scl/" + packageName; + while(packageName.length() > 3) { + if(bundle.getEntry(packageName) != null) + return packageName.length(); + int p = packageName.lastIndexOf('/'); + packageName = packageName.substring(0, p); + } + return 3; + } +}