Search in sources :

Example 26 with N4JSProjectConfigSnapshot

use of org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot in project n4js by eclipse.

the class DtsUtils method isDtsExportableReference.

/**
 * Tells whether references (e.g. imports, type references) to the given resource and its contents can be exported
 * to .d.ts. Iff this returns <code>false</code>, the .d.ts export will be cut off at this reference.
 */
public static boolean isDtsExportableReference(Resource resource, TranspilerState state) {
    if (resource == null) {
        return false;
    }
    if (N4Scheme.isResourceWithN4Scheme(resource)) {
        return true;
    }
    N4JSProjectConfigSnapshot project = state.workspaceAccess.findProjectContaining(resource);
    ProjectDescription pd = project != null ? project.getProjectDescription() : null;
    return pd != null && pd.hasN4JSNature() && (N4JSLanguageUtils.isDtsGenerationActive(pd) || isDefinition(pd) || isRuntime(pd));
}
Also used : N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription)

Example 27 with N4JSProjectConfigSnapshot

use of org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot in project n4js by eclipse.

the class EcmaScriptSubGenerator method internalDoGenerate.

@Override
protected void internalDoGenerate(N4JSWorkspaceConfigSnapshot ws, Resource resource, GeneratorOption[] options, IFileSystemAccess fsa) {
    if (!(resource instanceof N4JSResource)) {
        if (N4JSGlobals.PACKAGE_JSON.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;
    }
    /*
		 * 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(ws, fsa, getCompilerID(), resourceCasted);
        final N4JSProjectConfigSnapshot project = ws.findProjectContaining(resource.getURI());
        boolean createSourceMap = project.getProjectDescription().isGeneratorEnabledSourceMaps();
        if (filename != null) {
            final EObject root = rootElement(resource);
            if (root != null) {
                final StringWriter 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 = false;
                    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 (resourceCasted.getScript().getHashbang() != null) {
                    makeGeneratedFileExecutable(resourceCasted, filename);
                }
                if (createSourceMap) {
                    fsa.generateFile(sourceMapFileName, COMPILER_ID, optSourceMapData.get().sourceMapBuff.toString());
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) SourceMapInfo(org.eclipse.n4js.transpiler.AbstractTranspiler.SourceMapInfo) StringWriter(java.io.StringWriter) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) EObject(org.eclipse.emf.ecore.EObject) N4JSResource(org.eclipse.n4js.resource.N4JSResource) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) File(java.io.File)

Example 28 with N4JSProjectConfigSnapshot

use of org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot in project n4js by eclipse.

the class PreparationStep method prepare.

/**
 * Creates and initializes the transpiler state. In particular, this will create the intermediate model as a copy of
 * the original AST with some modifications (esp. rewiring of cross-references to ensure intermediate model is
 * self-contained).
 *
 * @param options
 *            the {@link GeneratorOption generator options} to use during generation.
 */
public TranspilerState prepare(Script script, GeneratorOption[] options) {
    final N4JSResource resource = (N4JSResource) script.eResource();
    final N4JSProjectConfigSnapshot project = workspaceAccess.findProjectContaining(resource);
    final ContainerTypesHelper.MemberCollector memberCollector = containerTypesHelper.fromContext(resource);
    final Tracer tracer = new Tracer();
    final InformationRegistry info = new InformationRegistry();
    final STECache steCache = createIM(script, tracer, info);
    final IResourceDescriptions index = resourceDescriptionsProvider.getResourceDescriptions(resource);
    return new TranspilerState(resource, project, options, memberCollector, steCache.im, steCache, tracer, info, index, workspaceAccess);
}
Also used : ContainerTypesHelper(org.eclipse.n4js.utils.ContainerTypesHelper) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) STECache(org.eclipse.n4js.transpiler.TranspilerState.STECache) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) N4JSResource(org.eclipse.n4js.resource.N4JSResource)

Example 29 with N4JSProjectConfigSnapshot

use of org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot in project n4js by eclipse.

the class XpectN4JSES5TranspilerHelper method getCompiledFileBasePath.

private String getCompiledFileBasePath(final Script script) {
    String path = N4JSLanguageConstants.DEFAULT_PROJECT_OUTPUT;
    N4JSProjectConfigSnapshot project = workspaceAccess.findProjectContaining(script);
    if (project != null) {
        path = AbstractSubGenerator.calculateProjectBasedOutputDirectory(project, false);
    }
    return path;
}
Also used : N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot)

Example 30 with N4JSProjectConfigSnapshot

use of org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot in project n4js by eclipse.

the class AbstractTypeVisibilityChecker method isProjectVisible.

private boolean isProjectVisible(Resource contextResource, final IEObjectDescription element) {
    if (contextResource != null) {
        final TModule contextModule = N4JSResource.getModule(contextResource);
        if (contextModule == null) {
            return false;
        }
        N4JSProjectConfigSnapshot project = this.workspaceAccess.findProjectByNestedLocation(contextResource, element.getEObjectURI());
        if (project == null) {
            return true;
        }
        return Strings.equal(contextModule.getProjectID(), project.getName()) && Strings.equal(contextModule.getVendorID(), project.getVendorId()) || isTestedProjectOf(contextModule, project);
    }
    return false;
}
Also used : N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) TModule(org.eclipse.n4js.ts.types.TModule)

Aggregations

N4JSProjectConfigSnapshot (org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot)50 URI (org.eclipse.emf.common.util.URI)15 N4JSPackageName (org.eclipse.n4js.workspace.utils.N4JSPackageName)9 N4JSSourceFolderSnapshot (org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot)8 Path (java.nio.file.Path)7 Resource (org.eclipse.emf.ecore.resource.Resource)7 N4JSWorkspaceConfigSnapshot (org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot)7 FileURI (org.eclipse.n4js.workspace.locations.FileURI)7 ArrayList (java.util.ArrayList)6 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)6 File (java.io.File)5 HashMap (java.util.HashMap)5 N4JSResource (org.eclipse.n4js.resource.N4JSResource)5 LinkedHashMap (java.util.LinkedHashMap)4 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)4 ProjectDescription (org.eclipse.n4js.packagejson.projectDescription.ProjectDescription)4 TModule (org.eclipse.n4js.ts.types.TModule)4 QualifiedName (org.eclipse.xtext.naming.QualifiedName)4 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)4 IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)4