Search in sources :

Example 1 with TypeChecker

use of org.eclipse.ceylon.compiler.typechecker.TypeChecker in project ceylon by eclipse.

the class MainForLanguage method main.

/**
 * Files that are not under a proper module structure are
 * placed under a <nomodule> module.
 */
public static void main(String[] args) throws Exception {
    ClosableVirtualFile latestZippedLanguageSourceFile = MainHelper.getLatestZippedLanguageSourceFile();
    RepositoryManager repositoryManager = CeylonUtils.repoManager().systemRepo("../dist/dist/repo").logger(new LeakingLogger()).buildManager();
    TypeChecker typeChecker = new TypeCheckerBuilder().verbose(false).addSrcDirectory(latestZippedLanguageSourceFile).setRepositoryManager(repositoryManager).getTypeChecker();
    typeChecker.process();
    latestZippedLanguageSourceFile.close();
    if (typeChecker.getErrors() > 0) {
        System.exit(1);
    }
}
Also used : ClosableVirtualFile(org.eclipse.ceylon.compiler.typechecker.io.ClosableVirtualFile) TypeChecker(org.eclipse.ceylon.compiler.typechecker.TypeChecker) LeakingLogger(org.eclipse.ceylon.compiler.typechecker.io.cmr.impl.LeakingLogger) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) TypeCheckerBuilder(org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder)

Example 2 with TypeChecker

use of org.eclipse.ceylon.compiler.typechecker.TypeChecker in project ceylon by eclipse.

the class MainForJsTest method main.

public static void main(String[] args) throws Exception {
    String distRepo = "../dist/dist/repo";
    final Options opts = new Options().addRepo(distRepo).outRepo("build/test/proto");
    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 if (dir.startsWith("c:")) {
            opts.cwd(d);
        } else {
            tcb.addSrcDirectory(d);
            opts.addSrcDir(d);
        }
    }
    final RepositoryManager repoman = CeylonUtils.repoManager().cwd(opts.getCwd()).systemRepo(opts.getSystemRepo()).userRepos(opts.getRepos()).outRepo(opts.getOutRepo()).buildManager();
    tcb.setRepositoryManager(repoman);
    final TypeChecker typeChecker = tcb.getTypeChecker();
    for (File x : excludes) {
        String ap = x.getPath();
        // Fix for Windows
        if ('/' != File.separatorChar) {
            ap = ap.replace(File.separatorChar, '/');
        }
        for (PhasedUnit pu : typeChecker.getPhasedUnits().getPhasedUnits()) {
            if (pu.getUnit().getFullPath().startsWith(ap)) {
                typeChecker.getPhasedUnits().removePhasedUnitForRelativePath(pu.getPathRelativeToSrcDir());
            }
        }
    }
    TypeCache.doWithoutCaching(new Runnable() {

        @Override
        public void run() {
            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);
    ArrayList<File> sources = new ArrayList<>();
    for (String dir : args) {
        // The arg is src/test/ceylon for example
        addFilesToList(new File(dir), sources);
    }
    Collections.sort(sources);
    jsc.setSourceFiles(sources);
    jsc.setResourceFiles(resfiles);
    PrintWriter writer = new PrintWriter(System.out);
    if (!jsc.generate()) {
        jsc.printErrorsAndCount(writer);
    }
}
Also used : Options(org.eclipse.ceylon.compiler.js.util.Options) JsModuleManagerFactory(org.eclipse.ceylon.compiler.js.loader.JsModuleManagerFactory) TypeChecker(org.eclipse.ceylon.compiler.typechecker.TypeChecker) ArrayList(java.util.ArrayList) TypeCheckerBuilder(org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) File(java.io.File) JsCompiler(org.eclipse.ceylon.compiler.js.JsCompiler) PrintWriter(java.io.PrintWriter)

Example 3 with TypeChecker

use of org.eclipse.ceylon.compiler.typechecker.TypeChecker in project ceylon by eclipse.

the class ModuleComplianceVisitor method main.

public static void main(String[] args) throws IOException {
    System.out.println("Finding JS language module...");
    File jsmod = findModule("build/runtime", "js");
    if (jsmod == null && System.getenv("ceylon.repo") != null) {
        jsmod = findModule(System.getenv("ceylon.repo"), "js");
    }
    if (jsmod == null && System.getProperty("user.home") != null) {
        jsmod = findModule(System.getProperty("user.home"), "js");
    }
    if (jsmod == null) {
        throw new IllegalStateException("Cannot find JS language module");
    }
    System.out.println("Finding Ceylon Language Module");
    File mod = findModule("../language/build/dist", "src");
    if (mod == null && System.getenv("ceylon.repo") != null) {
        mod = findModule(System.getenv("ceylon.repo"), "src");
    }
    if (mod == null && System.getProperty("user.home") != null) {
        mod = findModule(System.getProperty("user.home"), "src");
    }
    if (mod == null) {
        throw new IllegalStateException("Cannot find JVM language module");
    }
    System.out.println("Reading JS language module into memory...");
    FileReader reader = new FileReader(jsmod);
    char[] buf = new char[16384];
    StringWriter writer = new StringWriter();
    while (reader.ready()) {
        reader.read(buf);
        writer.write(buf);
    }
    reader.close();
    ModuleComplianceVisitor v = new ModuleComplianceVisitor(writer.toString());
    System.out.println("Checking Ceylon vs JS modules...");
    VFS vfs = new VFS();
    TypeChecker tc = new TypeCheckerBuilder().verbose(false).addSrcDirectory(vfs.getFromZipFile(mod)).getTypeChecker();
    tc.process();
    for (PhasedUnit pu : tc.getPhasedUnits().getPhasedUnits()) {
        pu.getCompilationUnit().visit(v);
    }
}
Also used : VFS(org.eclipse.ceylon.compiler.typechecker.io.VFS) StringWriter(java.io.StringWriter) TypeChecker(org.eclipse.ceylon.compiler.typechecker.TypeChecker) FileReader(java.io.FileReader) TypeCheckerBuilder(org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder) File(java.io.File) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)

Example 4 with TypeChecker

use of org.eclipse.ceylon.compiler.typechecker.TypeChecker in project ceylon by eclipse.

the class Stitcher method compileLanguageModule.

private static int compileLanguageModule(final String line, final JsOutput writer) throws IOException {
    File clsrcTmpDir = Files.createTempDirectory(tmpDir, "clsrc").toFile();
    final File tmpout = new File(clsrcTmpDir, Constants.DEFAULT_MODULE_DIR);
    FileUtil.mkdirs(tmpout);
    final Options opts = new Options().addRepo("build/runtime").comment(false).optimize(true).outRepo(tmpout.getAbsolutePath()).modulify(false).minify(true).suppressWarnings(EnumUtil.enumsFromStrings(Warning.class, Arrays.asList("unusedDeclaration", "ceylonNamespace", "unusedImport"))).addSrcDir(clSrcDir).addSrcDir(LANGMOD_JS_SRC);
    // Typecheck the whole language module
    if (langmodtc == null) {
        langmodtc = new TypeCheckerBuilder().addSrcDirectory(clSrcDir).addSrcDirectory(LANGMOD_JS_SRC).encoding("UTF-8");
        langmodtc.setRepositoryManager(CeylonUtils.repoManager().systemRepo(opts.getSystemRepo()).userRepos(opts.getRepos()).outRepo(opts.getOutRepo()).buildManager());
        langmodtc.usageWarnings(false);
    }
    final TypeChecker tc = langmodtc.getTypeChecker();
    tc.process(true);
    if (tc.getErrors() > 0) {
        return 1;
    }
    // Compile these files
    final List<File> includes = new ArrayList<File>();
    for (String filename : line.split(",")) {
        filename = filename.trim();
        final boolean isJsSrc = filename.endsWith(".js");
        final boolean isDir = filename.endsWith("/");
        final boolean exclude = filename.charAt(0) == '-';
        if (exclude) {
            filename = filename.substring(1);
        }
        final File src = ".".equals(filename) ? clSrcFileDir : new File(isJsSrc ? LANGMOD_JS_SRC : clSrcFileDir, isJsSrc || isDir ? filename : String.format("%s.ceylon", filename));
        if (!addFiles(includes, src, exclude)) {
            final File src2 = new File(clJsFileDir, isDir ? filename : String.format("%s.ceylon", filename));
            if (!addFiles(includes, src2, exclude)) {
                throw new IllegalArgumentException("Invalid Ceylon language module source " + src + " or " + src2);
            }
        }
    }
    if (includes.isEmpty()) {
        return 0;
    }
    // Compile only the files specified in the line
    // Set this before typechecking to share some decls that otherwise would be private
    JsCompiler jsc = new JsCompiler(tc, opts, true).stopOnErrors(false).setSourceFiles(includes);
    if (!jsc.generate()) {
        jsc.printErrorsAndCount(new OutputStreamWriter(System.out));
        return 1;
    } else {
        // We still call this here for any warning there might be
        jsc.printErrorsAndCount(new OutputStreamWriter(System.out));
    }
    if (names == null) {
        names = jsc.getNames();
    }
    File compsrc = new File(tmpout, "delete/me/delete-me.js");
    if (compsrc.exists() && compsrc.isFile() && compsrc.canRead()) {
        try {
            writer.outputFile(compsrc);
        } finally {
            compsrc.delete();
        }
    } else {
        System.out.println("Can't find generated js for language module in " + compsrc.getAbsolutePath());
        return 1;
    }
    return 0;
}
Also used : Options(org.eclipse.ceylon.compiler.js.util.Options) Warning(org.eclipse.ceylon.compiler.typechecker.analyzer.Warning) TypeChecker(org.eclipse.ceylon.compiler.typechecker.TypeChecker) ArrayList(java.util.ArrayList) OutputStreamWriter(java.io.OutputStreamWriter) TypeCheckerBuilder(org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder) File(java.io.File)

Example 5 with TypeChecker

use of org.eclipse.ceylon.compiler.typechecker.TypeChecker in project ceylon by eclipse.

the class Stitcher method encodeModel.

private static int encodeModel(final File moduleFile) throws IOException {
    final String name = moduleFile.getName();
    final File file = new File(moduleFile.getParentFile(), name.substring(0, name.length() - 3) + ArtifactContext.JS_MODEL);
    System.out.println("Generating language module compile-time model in JSON...");
    TypeCheckerBuilder tcb = new TypeCheckerBuilder().usageWarnings(false);
    tcb.addSrcDirectory(clSrcDir);
    TypeChecker tc = tcb.getTypeChecker();
    tc.process(true);
    MetamodelVisitor mmg = null;
    final ErrorCollectingVisitor errVisitor = new ErrorCollectingVisitor(tc);
    for (PhasedUnit pu : tc.getPhasedUnits().getPhasedUnits()) {
        pu.getCompilationUnit().visit(errVisitor);
        if (errVisitor.getErrorCount() > 0) {
            errVisitor.printErrors(false, false);
            System.out.println("errors in the language module " + pu.getCompilationUnit().getLocation());
            return 1;
        }
        if (mmg == null) {
            mmg = new MetamodelVisitor(pu.getPackage().getModule());
        }
        pu.getCompilationUnit().visit(mmg);
    }
    mod = tc.getPhasedUnits().getPhasedUnits().get(0).getPackage().getModule();
    try (FileWriter writer = new FileWriter(file)) {
        JsCompiler.beginWrapper(writer);
        writer.write("ex$.$CCMM$=");
        ModelEncoder.encodeModel(mmg.getModel(), writer);
        writer.write(";\n");
        final JsOutput jsout = new JsOutput(mod, true) {

            @Override
            public Writer getWriter() throws IOException {
                return writer;
            }
        };
        jsout.outputFile(new File(LANGMOD_JS_SRC, "MODEL.js"));
        JsCompiler.endWrapper(writer);
    } finally {
        ShaSigner.sign(file, new JsJULLogger(), true);
    }
    final File npmFile = new File(moduleFile.getParentFile(), ArtifactContext.NPM_DESCRIPTOR);
    try (FileWriter writer = new FileWriter(npmFile)) {
        String npmdesc = new NpmDescriptorGenerator(mod, true, false).generateDescriptor();
        writer.write(npmdesc);
    }
    return 0;
}
Also used : JsOutput(org.eclipse.ceylon.compiler.js.util.JsOutput) JsJULLogger(org.eclipse.ceylon.compiler.js.util.JsJULLogger) TypeChecker(org.eclipse.ceylon.compiler.typechecker.TypeChecker) MetamodelVisitor(org.eclipse.ceylon.compiler.js.loader.MetamodelVisitor) FileWriter(java.io.FileWriter) NpmDescriptorGenerator(org.eclipse.ceylon.compiler.js.util.NpmDescriptorGenerator) TypeCheckerBuilder(org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder) File(java.io.File) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)

Aggregations

TypeChecker (org.eclipse.ceylon.compiler.typechecker.TypeChecker)15 TypeCheckerBuilder (org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder)14 File (java.io.File)11 Options (org.eclipse.ceylon.compiler.js.util.Options)8 RepositoryManager (org.eclipse.ceylon.cmr.api.RepositoryManager)7 JsCompiler (org.eclipse.ceylon.compiler.js.JsCompiler)6 JsModuleManagerFactory (org.eclipse.ceylon.compiler.js.loader.JsModuleManagerFactory)5 ArrayList (java.util.ArrayList)4 PhasedUnit (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)4 Test (org.junit.Test)4 ClosableVirtualFile (org.eclipse.ceylon.compiler.typechecker.io.ClosableVirtualFile)3 Module (org.eclipse.ceylon.model.typechecker.model.Module)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 LeakingLogger (org.eclipse.ceylon.compiler.typechecker.io.cmr.impl.LeakingLogger)2 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1