package com.redhat.ceylon.compiler.loader;

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

import org.junit.BeforeClass;
import org.junit.Test;

import com.redhat.ceylon.cmr.api.RepositoryManager;
import com.redhat.ceylon.cmr.ceylon.CeylonUtils;
import com.redhat.ceylon.cmr.impl.JULLogger;
import com.redhat.ceylon.compiler.Options;
import com.redhat.ceylon.compiler.js.JsCompiler;
import com.redhat.ceylon.compiler.typechecker.TypeChecker;
import com.redhat.ceylon.compiler.typechecker.TypeCheckerBuilder;

public class TestModuleManager {

    private static TypeChecker tc;
    private static RepositoryManager repoman;
    private static Options options;

    @BeforeClass
    public static void setup() throws IOException {
        new java.io.File("build/test/test_modules").mkdirs();
        ArrayList<String> args = new ArrayList<String>();
        args.addAll(Arrays.asList("src/test/resources/loader/pass1/m1/test.ceylon",
                "-rep", "build/test/test_modules", "-out", "build/test/test_modules",
                "-src", "src/test/resources/loader/pass1"));
        options = Options.parse(args);
        repoman = CeylonUtils.makeRepositoryManager(
                options.getRepos(), options.getOutDir(), new JULLogger());
        //Create a typechecker to compile the test module
        TypeCheckerBuilder tcb = new TypeCheckerBuilder().usageWarnings(false);
        tcb.moduleManagerFactory(new JsModuleManagerFactory());
        tcb.addSrcDirectory(new java.io.File("src/test/resources/loader/pass1"));
        tcb.setRepositoryManager(repoman);
        tc = tcb.getTypeChecker();
        tc.process();
        JsCompiler compiler = new JsCompiler(tc, options);
        compiler.stopOnErrors(false);
        compiler.generate();
    }

    @Test
    public void test() {
        final RepositoryManager repoman = CeylonUtils.makeRepositoryManager(
                options.getRepos(), options.getOutDir(), new JULLogger());
        TypeCheckerBuilder tcb = new TypeCheckerBuilder().usageWarnings(false);
        tcb.moduleManagerFactory(new JsModuleManagerFactory());
        tcb.addSrcDirectory(new java.io.File("src/test/resources/loader/pass2"));
        tcb.setRepositoryManager(repoman);
        tc = tcb.getTypeChecker();
        tc.process();
    }

}

@@ -793,58 +793,846 @@
     
-new java.io.File(%22build/test/test_modules%22).mkdirs
+//Copy language module to destination%0A        java.io.File srclangmod = new java.io.File(String.format(%0A                %22build/runtime/ceylon/language/%25s/ceylon.language-%25%3Cs.js%22, JsCompiler.VERSION));%0A        java.io.File dstlangmod = new java.io.File(%22build/test/test_modules/ceylon/language/%22 + JsCompiler.VERSION);%0A        System.out.printf(%22Copying %25s to %25s%25n%22, srclangmod, dstlangmod);%0A        dstlangmod.mkdirs();%0A        java.io.BufferedReader lmreader = new java.io.BufferedReader(new java.io.FileReader(srclangmod));%0A        java.io.Writer lmwriter = new java.io.FileWriter(new java.io.File(dstlangmod, srclangmod.getName()));%0A        String line;%0A        while ((line = lmreader.readLine()) != null) %7B%0A            lmwriter.write(line);%0A            lmwriter.write('%5Cn');%0A        %7D%0A        lmreader.close();%0A        lmwriter.close
 ();%0A
@@ -2145,16 +2145,64 @@
  module%0A
+        System.out.println(%22Compiling pass 1%22);%0A
         
@@ -2273,32 +2273,34 @@
 false);%0A        
+//
 tcb.moduleManage
@@ -2685,24 +2685,85 @@
 id test() %7B%0A
+        System.out.println(%22Pass 2: Loading model from JS%22);%0A
         fina
@@ -2894,32 +2894,32 @@
 w JULLogger());%0A
-
         TypeChec
@@ -2973,32 +2973,49 @@
 Warnings(false);
+//.verbose(true);
 %0A        tcb.mod

Copy language module from runtime to test dir