Search in sources :

Example 6 with JsOutput

use of org.eclipse.ceylon.compiler.js.util.JsOutput in project ceylon by eclipse.

the class Stitcher method main.

public static void main(String[] args) throws IOException {
    if (args.length < 2) {
        System.err.println("This program requires 2 arguments to run:");
        System.err.println("1. The path to the master file (the one with the list of files to compile)");
        System.err.println("2. The path of the resulting JS file");
        System.exit(1);
        return;
    }
    // Force coloring of output if not already set
    String useColors = System.getProperty(Constants.PROP_CEYLON_TERM_COLORS, "yes");
    System.setProperty(Constants.PROP_CEYLON_TERM_COLORS, useColors);
    int exitCode = 0;
    tmpDir = Files.createTempDirectory("ceylon-jsstitcher-");
    try {
        File infile = new File(args[0]);
        if (infile.exists() && infile.isFile() && infile.canRead()) {
            File outfile = new File(args[1]);
            if (!outfile.getParentFile().exists()) {
                FileUtil.mkdirs(outfile);
            }
            exitCode = encodeModel(outfile);
            if (exitCode == 0) {
                final int p0 = args[1].indexOf(".language-");
                final String version = args[1].substring(p0 + 10, args[1].length() - 3);
                try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8")) {
                    final JsOutput jsout = new JsOutput(mod, true) {

                        @Override
                        public Writer getWriter() throws IOException {
                            return writer;
                        }
                    };
                    // CommonJS wrapper
                    JsCompiler.beginWrapper(writer);
                    JsCompiler.requireWrapper(writer, mod);
                    // Model
                    jsout.out("var _CTM$;function $CCMM$(){if (_CTM$===undefined)_CTM$=require('", "ceylon/language/", version, "/ceylon.language-", version, "-model", "').$CCMM$;return _CTM$;}\nex$.$CCMM$=$CCMM$;");
                    // Compile all the listed files
                    exitCode = stitch(infile, jsout);
                    // Unshared declarations
                    if (names != null) {
                        jsout.publishUnsharedDeclarations(names);
                    }
                    // Close the commonJS wrapper
                    JsCompiler.endWrapper(writer);
                } finally {
                    ShaSigner.sign(outfile, new JsJULLogger(), true);
                }
            }
        } else {
            System.err.println("Input file is invalid: " + infile);
            exitCode = 2;
        }
    } finally {
        FileUtil.deleteQuietly(tmpDir.toFile());
    }
    if (exitCode != 0) {
        System.exit(exitCode);
    }
}
Also used : JsOutput(org.eclipse.ceylon.compiler.js.util.JsOutput) JsJULLogger(org.eclipse.ceylon.compiler.js.util.JsJULLogger) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Aggregations

JsOutput (org.eclipse.ceylon.compiler.js.util.JsOutput)6 File (java.io.File)4 FileWriter (java.io.FileWriter)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ArtifactContext (org.eclipse.ceylon.cmr.api.ArtifactContext)2 JsJULLogger (org.eclipse.ceylon.compiler.js.util.JsJULLogger)2 NpmDescriptorGenerator (org.eclipse.ceylon.compiler.js.util.NpmDescriptorGenerator)2 MissingNativeVisitor (org.eclipse.ceylon.compiler.typechecker.analyzer.MissingNativeVisitor)2 PhasedUnit (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)2 VirtualFile (org.eclipse.ceylon.compiler.typechecker.io.VirtualFile)2 Module (org.eclipse.ceylon.model.typechecker.model.Module)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1