Search in sources :

Example 1 with NpmDescriptorGenerator

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

the class JsCompiler method finish.

/**
 * Closes all output writers and puts resulting artifacts in the output repo.
 */
protected int finish() throws IOException {
    int result = 0;
    String outDir = CeylonUtils.resolveRepoUrl(opts.getOutRepo());
    if (!isURL(outDir)) {
        File root = new File(outDir);
        if (root.exists()) {
            if (!(root.isDirectory() && root.canWrite())) {
                logger.error("Cannot write to " + root + ". Stop.");
                result = 1;
            }
        } else {
            if (!FileUtil.mkdirs(root)) {
                logger.error("Cannot create " + root + ". Stop.");
                result = 1;
            }
        }
    }
    for (Map.Entry<Module, JsOutput> entry : output.entrySet()) {
        JsOutput jsout = entry.getValue();
        if (!compilingLanguageModule) {
            jsout.publishUnsharedDeclarations(names);
        }
        if (opts.isModulify()) {
            jsout.closeWrapper();
        }
        String moduleName = entry.getKey().getNameAsString();
        String moduleVersion = entry.getKey().getVersion();
        if (opts.getDiagnosticListener() != null)
            opts.getDiagnosticListener().moduleCompiled(moduleName, moduleVersion);
        // Create the JS file
        final File jsart = jsout.close();
        final File modart = jsout.getModelFile();
        if (entry.getKey().isDefaultModule()) {
            logger.info("Created module " + moduleName);
        } else if (!compilingLanguageModule) {
            logger.info("Created module " + moduleName + "/" + moduleVersion);
        }
        if (compilingLanguageModule) {
            ArtifactContext artifact = new ArtifactContext(null, "delete", "me", ArtifactContext.JS);
            artifact.setForceOperation(true);
            outRepo.putArtifact(artifact, jsart);
        } else {
            final ArtifactContext artifact = new ArtifactContext(null, moduleName, moduleVersion, ArtifactContext.JS);
            artifact.setForceOperation(true);
            outRepo.putArtifact(artifact, jsart);
            final ArtifactContext martifact = new ArtifactContext(null, moduleName, moduleVersion, ArtifactContext.JS_MODEL);
            martifact.setForceOperation(true);
            outRepo.putArtifact(martifact, modart);
            // js file signature
            ShaSigner.signArtifact(outRepo, artifact, jsart, logger);
            ShaSigner.signArtifact(outRepo, martifact, modart, logger);
            // Create the src archive
            if (opts.isGenerateSourceArchive()) {
                ArtifactCreator sac = CeylonUtils.makeSourceArtifactCreator(outRepo, opts.getSrcDirs(), moduleName, moduleVersion, opts.hasVerboseFlag("cmr"), logger);
                sac.copy(FileUtil.filesToPathList(jsout.getSources()));
            }
            if (resFiles != null && !resFiles.isEmpty()) {
                ArtifactCreator sac = CeylonUtils.makeResourceArtifactCreator(outRepo, opts.getSrcDirs(), opts.getResourceDirs(), opts.getResourceRootName(), moduleName, moduleVersion, opts.hasVerboseFlag("cmr"), logger);
                sac.copy(FileUtil.filesToPathList(filterForModule(resFiles, opts.getResourceDirs(), moduleName)));
            }
            if (!entry.getKey().isDefaultModule()) {
                String npmdesc = new NpmDescriptorGenerator(entry.getKey(), opts.isGenerateSourceArchive(), resFiles != null && !resFiles.isEmpty()).generateDescriptor();
                File npmfile = File.createTempFile("npm", "json");
                try {
                    try (FileWriter fw = new FileWriter(npmfile)) {
                        fw.write(npmdesc);
                    }
                    final ArtifactContext npmArtifact = new ArtifactContext(null, moduleName, moduleVersion, ArtifactContext.NPM_DESCRIPTOR);
                    npmArtifact.setForceOperation(true);
                    outRepo.putArtifact(npmArtifact, npmfile);
                } finally {
                    npmfile.delete();
                }
            }
        }
        FileUtil.deleteQuietly(jsart);
        if (modart != null) {
            FileUtil.deleteQuietly(modart);
        }
    }
    return result;
}
Also used : JsOutput(org.eclipse.ceylon.compiler.js.util.JsOutput) ArtifactCreator(org.eclipse.ceylon.cmr.api.ArtifactCreator) FileWriter(java.io.FileWriter) ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) NpmDescriptorGenerator(org.eclipse.ceylon.compiler.js.util.NpmDescriptorGenerator) Module(org.eclipse.ceylon.model.typechecker.model.Module) VirtualFile(org.eclipse.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with NpmDescriptorGenerator

use of org.eclipse.ceylon.compiler.js.util.NpmDescriptorGenerator 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

File (java.io.File)2 FileWriter (java.io.FileWriter)2 JsOutput (org.eclipse.ceylon.compiler.js.util.JsOutput)2 NpmDescriptorGenerator (org.eclipse.ceylon.compiler.js.util.NpmDescriptorGenerator)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ArtifactContext (org.eclipse.ceylon.cmr.api.ArtifactContext)1 ArtifactCreator (org.eclipse.ceylon.cmr.api.ArtifactCreator)1 MetamodelVisitor (org.eclipse.ceylon.compiler.js.loader.MetamodelVisitor)1 JsJULLogger (org.eclipse.ceylon.compiler.js.util.JsJULLogger)1 TypeChecker (org.eclipse.ceylon.compiler.typechecker.TypeChecker)1 TypeCheckerBuilder (org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder)1 PhasedUnit (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)1 VirtualFile (org.eclipse.ceylon.compiler.typechecker.io.VirtualFile)1 Module (org.eclipse.ceylon.model.typechecker.model.Module)1