import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;

import com.redhat.ceylon.cmr.api.RepositoryManager;
import com.redhat.ceylon.cmr.ceylon.CeylonUtils;
import com.redhat.ceylon.compiler.Options;
import com.redhat.ceylon.compiler.js.JsCompiler;
import com.redhat.ceylon.compiler.loader.JsModuleManagerFactory;
import com.redhat.ceylon.compiler.typechecker.TypeChecker;
import com.redhat.ceylon.compiler.typechecker.TypeCheckerBuilder;
import com.redhat.ceylon.compiler.typechecker.context.PhasedUnit;

/**
 * Some hack before a proper unit test harness is put in place
 *
 * @author Emmanuel Bernard <emmanuel@hibernate.org>
 */
public class MainForJsTest {
    
    public static void main(String[] args) throws Exception {
        final Options opts = new Options().addRepo("build/runtime").outRepo("build/test/proto");
        final RepositoryManager repoman = CeylonUtils.repoManager()
                .cwd(opts.getCwd())
                .systemRepo(opts.getSystemRepo())
                .userRepos(opts.getRepos())
                .outRepo(opts.getOutRepo())
                .buildManager();
        System.out.println("Typechecking Ceylon test code...");
        JsModuleManagerFactory.setVerbose(true);
        TypeCheckerBuilder tcb = new TypeCheckerBuilder().verbose(false)
            .moduleManagerFactory(new JsModuleManagerFactory(null))
            .usageWarnings(false);
        final ArrayList<File> excludes = new ArrayList<>();
        final ArrayList<File> resfiles = new ArrayList<>();
        final ArrayList<File> resdirs = new ArrayList<>();
        for (String dir : args) {
            final File d = new File(dir.charAt(1)==':'?dir.substring(2):dir);
            if (dir.startsWith("X:")) {
                excludes.add(d);
            } else if (dir.startsWith("R:")) {
                resdirs.add(d);
            } else if (dir.startsWith("r:")) {
                resfiles.add(d);
            } else {
                tcb.addSrcDirectory(d);
            }
        }
        tcb.setRepositoryManager(repoman);
        TypeChecker typeChecker = tcb.getTypeChecker();
        for (File x : excludes) {
            final String ap = x.getPath();
            for (PhasedUnit pu : typeChecker.getPhasedUnits().getPhasedUnits()) {
                if (pu.getUnit().getFullPath().startsWith(ap)) {
                    typeChecker.getPhasedUnits().removePhasedUnitForRelativePath(pu.getPathRelativeToSrcDir());
                }
            }
        }
        typeChecker.process();
        if (typeChecker.getErrors() > 0) {
            System.exit(1);
        }
        System.out.println("Compiling in prototype style");
        opts.resourceDirs(resdirs);
        JsCompiler jsc = new JsCompiler(typeChecker, opts).stopOnErrors(true);
        jsc.setResourceFiles(resfiles);
        PrintWriter writer = new PrintWriter(System.out);
        if (!jsc.generate()) {
            jsc.printErrorsAndCount(writer);
        }
        System.out.println("Compiling in lexical scope style");
        jsc = new JsCompiler(typeChecker, opts.optimize(false).outRepo("build/test/lexical")).stopOnErrors(false);
        jsc.setResourceFiles(resfiles);
        if (!jsc.generate()) {
            jsc.printErrors(writer);
        }
    }

}

@@ -1985,24 +1985,59 @@
 rectory(d);%0A
+                opts.addSrcDir(d);%0A
             

add source dirs to make resource tests work #389