package org.strategoxt.imp.metatooling;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

import org.eclipse.ant.core.IAntPropertyValueProvider;
import org.eclipse.core.runtime.FileLocator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;

/**
 * Ant property provider that generates a classpath from all installed plugins, to be used for Java compilation in a
 * Spoofax ant build.
 */
public class PluginClasspathProvider implements IAntPropertyValueProvider {
    public String getAntPropertyValue(String antPropertyName) {
        final StringBuilder classpathBuilder = new StringBuilder();
        boolean first = true;
        final BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
        final Bundle[] bundles = bundleContext.getBundles();
        for (final Bundle bundle : bundles) {
            try {
                if (!first) {
                    classpathBuilder.append(File.pathSeparator);
                }
                first = false;
                
                final File file = FileLocator.getBundleFile(bundle);
                final String path = file.getAbsolutePath();
                if (path.endsWith(".jar")) {
                    /*
                     * An installed JAR plugin.
                     */
                    classpathBuilder.append(path);
                } else {
                    final File targetClasses = Paths.get(path, "target", "classes").toFile();
                    final File bin = Paths.get(path, "bin").toFile();
                    if (targetClasses.exists()) {
                        /*
                         * A plugin under development with all its classes in the target/classes directory.
                         */
                        classpathBuilder.append(targetClasses);
                    } else if (bin.exists()) {
                    	/*
                         * A plugin under development with all its classes in the bin directory.
                         */
                        classpathBuilder.append(bin);
                    } else {
                        /*
                         * An installed unpacked plugin. Class files are extracted in this directory.
                         */
                        classpathBuilder.append(path);
                    }
                }
            } catch (IOException e) {

            }
        }

        return classpathBuilder.toString();
    }
}

@@ -51,24 +51,55 @@
 va.io.File;%0A
+import java.io.FilenameFilter;%0A
 import java.
@@ -141,16 +141,45 @@
 e.Paths;
+%0Aimport java.util.Collection;
 %0A%0Aimport
@@ -389,16 +389,95 @@
 kUtil;%0A%0A
+import com.google.common.base.Joiner;%0Aimport com.google.common.collect.Lists;%0A%0A
 /**%0A * A
@@ -571,16 +571,19 @@
 for Java
+%0A *
  compila
@@ -591,19 +591,16 @@
 ion in a
-%0A *
  Spoofax
@@ -773,90 +773,59 @@
 nal 
-StringBuilder classpathBuilder = new StringBuilder();%0A        boolean first = true
+Collection%3CString%3E classpath = Lists.newArrayList()
 ;%0A  
@@ -999,17 +999,16 @@
      for
- 
 (final B
@@ -1055,169 +1055,8 @@
 y %7B%0A
-                if (!first) %7B%0A                    classpathBuilder.append(File.pathSeparator);%0A                %7D%0A                first = false;%0A                %0A
     
@@ -1198,17 +1198,16 @@
       if
- 
 (path.en
@@ -1249,32 +1249,9 @@
    /
-*%0A                     *
+/
  An 
@@ -1296,54 +1296,20 @@
     
- */%0A                    classpathBuilder.appen
+classpath.ad
 d(pa
@@ -1524,17 +1524,16 @@
       if
- 
 (targetC
@@ -1580,36 +1580,9 @@
    /
-*%0A                         *
+/
  A p
@@ -1687,73 +1687,53 @@
     
- */%0A                        classpathBuilder.append(targetClasses
+classpath.add(targetClasses.getAbsolutePath()
 );%0A 
@@ -1760,17 +1760,16 @@
  else if
- 
 (bin.exi
@@ -1801,38 +1801,14 @@
     
-%09/*%0A                         *
+    //
  A p
@@ -1902,99 +1902,245 @@
     
- */%0A                        classpathBuilder.append(bin);%0A                    %7D else %7B%0A
+classpath.add(bin.getAbsolutePath());%0A                    %7D else %7B%0A                        // An installed unpacked plugin. Class files are extracted in this directory.%0A                        classpath.add(path);%0A                
     
+%7D%0A%0A
     
@@ -2160,139 +2160,366 @@
    /
-*%0A                         * An installed unpacked plugin. Class files are extracted in this directory.%0A                         */
+/ Also include any nested jar files.%0A                    final File%5B%5D jarFiles = file.listFiles(new FilenameFilter() %7B%0A                        @Override public boolean accept(File dir, String name) %7B%0A                            return name.endsWith(%22.jar%22);%0A                        %7D%0A                    %7D);%0A                    for(File jarFile : jarFiles) %7B
 %0A   
@@ -2552,27 +2552,38 @@
 path
-Builder.append(p
+.add(jarFile.getAbsoluteP
 ath
+()
 );%0A 
@@ -2640,17 +2640,16 @@
  %7D catch
- 
 (IOExcep
@@ -2703,34 +2703,52 @@
 urn 
-classpathBuilder.toString(
+Joiner.on(File.pathSeparator).join(classpath
 );%0A 

Add support for nested JAR files for the plugin classpath.