Search in sources :

Example 1 with Measurement

use of org.eclipse.n4js.smith.Measurement in project n4js by eclipse.

the class EcmaScriptSubGenerator method internalDoGenerate.

@Override
protected void internalDoGenerate(Resource resource, GeneratorOption[] options, IFileSystemAccess fsa) {
    if (!(resource instanceof N4JSResource)) {
        if (IN4JSProject.N4MF_MANIFEST.equals(resource.getURI().lastSegment())) {
            return;
        }
        throw new IllegalArgumentException("Given resource is not an N4JSResource. " + resource);
    }
    final N4JSResource resourceCasted = (N4JSResource) resource;
    if (resourceCasted.getModule().isStaticPolyfillModule()) {
        // do not transpile static polyfill modules (i.e. the fillers)
        return;
    }
    Measurement measurement = this.collector.getMeasurement(resource.getURI().toString());
    /*
		 * In addition to here, check for cancellation is done also on file-emit boundaries, see fsa.generateFile().
		 */
    CancelIndicator monitor = ciExtractor.extractCancelIndicator(fsa);
    // if the transpile-conditions are all met, then transpile:
    if (shouldBeCompiled(resource, monitor)) {
        final String compiledFileExtension = getCompiledFileExtension(resource);
        final String filename = getTargetFileName(resource, compiledFileExtension);
        final String sourceMapFileExtension = getCompiledFileSourceMapExtension(resource);
        final String sourceMapFileName = getTargetFileName(resource, sourceMapFileExtension);
        // used within the file-content to refer to sibling-file:
        final String simpleSourceMapFileName = new File(sourceMapFileName).toPath().getFileName().toString();
        final String simpleCompiledFileName = new File(filename).toPath().getFileName().toString();
        // the next two variables store the navigation-prefix to get to the sources
        final Path relativeNavigationToSrc = calculateNavigationFromOutputToSourcePath(fsa, getCompilerID(), resourceCasted);
        final Path explicitNavigationToSrc = Paths.get("/sources");
        // true use explicitNavigationToSrc | false use relativeNavigationToSrc
        boolean useExplicitSourceRef = true;
        boolean createSourceMap = true;
        if (filename != null) {
            final EObject root = rootElement(resource);
            if (root != null) {
                final Writer buffCode = new StringWriter();
                Optional<SourceMapInfo> optSourceMapData = Optional.absent();
                if (createSourceMap) {
                    SourceMapInfo sourceMapDataInstance = getTranspiler().new SourceMapInfo();
                    sourceMapDataInstance.sourceMapBuff = new StringWriter();
                    sourceMapDataInstance.simpleSourceMapFileName = simpleSourceMapFileName;
                    sourceMapDataInstance.simpleCompiledFileName = simpleCompiledFileName;
                    sourceMapDataInstance.isExplicitSourceRef = useExplicitSourceRef;
                    sourceMapDataInstance.explicitNavigationToSrc = explicitNavigationToSrc;
                    sourceMapDataInstance.n4jsFilePath = relativeNavigationToSrc.resolve(resourceCasted.getURI().lastSegment()).toString();
                    sourceMapDataInstance.sourceMapFileExtension = sourceMapFileExtension;
                    optSourceMapData = Optional.of(sourceMapDataInstance);
                }
                getTranspiler().transpile(resourceCasted, options, buffCode, optSourceMapData);
                fsa.generateFile(filename, COMPILER_ID, buffCode.toString());
                if (createSourceMap) {
                    fsa.generateFile(sourceMapFileName, COMPILER_ID, optSourceMapData.get().sourceMapBuff.toString());
                }
            }
        }
    }
    measurement.end();
}
Also used : Measurement(org.eclipse.n4js.smith.Measurement) Path(java.nio.file.Path) SourceMapInfo(org.eclipse.n4js.transpiler.AbstractTranspiler.SourceMapInfo) StringWriter(java.io.StringWriter) EObject(org.eclipse.emf.ecore.EObject) N4JSResource(org.eclipse.n4js.resource.N4JSResource) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) File(java.io.File) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

File (java.io.File)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Path (java.nio.file.Path)1 EObject (org.eclipse.emf.ecore.EObject)1 N4JSResource (org.eclipse.n4js.resource.N4JSResource)1 Measurement (org.eclipse.n4js.smith.Measurement)1 SourceMapInfo (org.eclipse.n4js.transpiler.AbstractTranspiler.SourceMapInfo)1 CancelIndicator (org.eclipse.xtext.util.CancelIndicator)1