package com.redhat.ceylon.eclipse.launching;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.launching.JavaLaunchDelegate;

import com.redhat.ceylon.compiler.typechecker.TypeChecker;
import com.redhat.ceylon.compiler.typechecker.io.ArtifactProvider;
import com.redhat.ceylon.compiler.typechecker.io.VirtualFile;
import com.redhat.ceylon.compiler.typechecker.io.impl.FileSystemVirtualFile;
import com.redhat.ceylon.compiler.util.RepositoryLister;
import com.redhat.ceylon.eclipse.imp.builder.CeylonBuilder;

public class CeylonLaunchDelegate extends JavaLaunchDelegate {

    private static String languageVersion = "0.1";
    private final static String languageCar = System.getProperty("user.home")+"/.ceylon/repo/ceylon/language/"+languageVersion +"/ceylon.language-"+languageVersion+".car";

    
    @Override
    public String[] getClasspath(ILaunchConfiguration configuration)
            throws CoreException {
        String[] javaClasspath = super.getClasspath(configuration);
        
        // Also add the car files of the output directory
        IJavaProject javaProject = getJavaProject(configuration);
        final List<String> classpathList = new ArrayList<String>(Arrays.asList(javaClasspath));

        TypeChecker typeChecker = CeylonBuilder.getProjectTypeChecker(javaProject.getProject());
        RepositoryLister archiveLister = new RepositoryLister();
        for (ArtifactProvider provider : typeChecker.getContext().getArtifactProviders()) {
            VirtualFile repository = provider.getHomeRepo();
            if (repository instanceof FileSystemVirtualFile) {
                archiveLister.list(((FileSystemVirtualFile)repository).getFile(), new RepositoryLister.Actions() {
                    @Override
                    public void doWithFile(File path) {
                        classpathList.add(path.getAbsolutePath());
                    }
                });
            }
            else {
                System.out.println("Ignoring non-filesystem repositories for launching classpath");
            }
        }
        File outputDirectory = CeylonBuilder.getOutputDirectory(javaProject);
        if (outputDirectory != null) {
            archiveLister.list(outputDirectory, new RepositoryLister.Actions() {
                @Override
                public void doWithFile(File path) {
                    classpathList.add(path.getAbsolutePath());
                }
            });
        }
                
        // Also add the language car
        classpathList.add(languageCar); 

        return classpathList.toArray(new String [classpathList.size()]);
    }
}

@@ -1582,24 +1582,679 @@
 ryLister();%0A
+        %0A        //first add modules in the current project%0A        //(we want them first in the classpath)%0A        File outputDirectory = CeylonBuilder.getOutputDirectory(javaProject);%0A        if (outputDirectory != null) %7B%0A            archiveLister.list(outputDirectory, new RepositoryLister.Actions() %7B%0A                @Override%0A                public void doWithFile(File path) %7B%0A                    classpathList.add(path.getAbsolutePath());%0A                %7D%0A            %7D);%0A        %7D%0A        %0A        //then add modules in the module repo%0A        //TODO: don't add modules with the same name as%0A        //      as a module belonging to the project%0A
         for 
@@ -2928,391 +2928,8 @@
   %7D%0A
-        File outputDirectory = CeylonBuilder.getOutputDirectory(javaProject);%0A        if (outputDirectory != null) %7B%0A            archiveLister.list(outputDirectory, new RepositoryLister.Actions() %7B%0A                @Override%0A                public void doWithFile(File path) %7B%0A                    classpathList.add(path.getAbsolutePath());%0A                %7D%0A            %7D);%0A        %7D%0A
     

Fix bug where module archive in repo took precedence over archive in bin