Search in sources :

Example 11 with N4JSProjectConfigSnapshot

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

the class EcmaScriptSubGenerator method makeGeneratedFileExecutable.

/**
 * Makes the generated output file executable by adding the corresponding POSIX file permissions.
 */
protected void makeGeneratedFileExecutable(N4JSResource resource, String outputFileName) {
    N4JSProjectConfigSnapshot project = workspaceAccess.findProjectContaining(resource);
    if (project == null) {
        return;
    }
    Path projectLocation = project.getPathAsFileURI().toFileSystemPath();
    String outputPath = project.getOutputPath();
    Path outputFilePath = projectLocation.resolve((outputPath + "/" + outputFileName).replace("/", File.separator));
    if (!Files.isRegularFile(outputFilePath)) {
        return;
    }
    try {
        Set<PosixFilePermission> permsOld = Files.getPosixFilePermissions(outputFilePath);
        Set<PosixFilePermission> permsNew = new HashSet<>(permsOld);
        if (permsOld.contains(PosixFilePermission.OWNER_READ)) {
            permsNew.add(PosixFilePermission.OWNER_EXECUTE);
        }
        if (permsOld.contains(PosixFilePermission.GROUP_READ)) {
            permsNew.add(PosixFilePermission.GROUP_EXECUTE);
        }
        if (permsOld.contains(PosixFilePermission.OTHERS_READ)) {
            permsNew.add(PosixFilePermission.OTHERS_EXECUTE);
        }
        if (!permsNew.equals(permsOld)) {
            Files.setPosixFilePermissions(outputFilePath, permsNew);
        }
    } catch (Exception e) {
        LOGGER.warn("unable to get/set POSIX file permissions of output file \"" + outputFilePath + "\": " + e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) HashSet(java.util.HashSet)

Example 12 with N4JSProjectConfigSnapshot

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

the class XpectN4JSES5TranspilerHelper method compileImplementationOfN4JSDFile.

private void compileImplementationOfN4JSDFile(final Path projectFolder, StringBuilder errorResult, Resource dep, GeneratorOption[] options, boolean replaceQuotes) {
    Script script = (Script) dep.getContents().get(0);
    if (AnnotationDefinition.PROVIDED_BY_RUNTIME.hasAnnotation(script)) {
        return;
    }
    Pair<N4JSProjectConfigSnapshot, N4JSSourceFolderSnapshot> pair = workspaceAccess.findProjectAndSourceFolderContaining(dep, dep.getURI());
    N4JSProjectConfigSnapshot project = pair.getKey();
    N4JSSourceFolderSnapshot source = pair.getValue();
    if (project != null && source != null) {
        for (N4JSSourceFolderSnapshot c : project.getSourceFolders()) {
            if (c.isExternal()) {
                String sourceRelativePath = dep.getURI().toString().replace(source.getPathAsFileURI().toString(), "");
                String[] potentialExternalSourceRelativeURISegments = null;
                String potentialExternalSourceRelativePath = sourceRelativePath.replace("." + N4JSGlobals.N4JSD_FILE_EXTENSION, "." + N4JSGlobals.JS_FILE_EXTENSION);
                potentialExternalSourceRelativeURISegments = URI.createURI(potentialExternalSourceRelativePath).segments();
                if (potentialExternalSourceRelativeURISegments != null) {
                    URI potentialExternalSourceURI = c.getPathAsFileURI().appendSegments(potentialExternalSourceRelativeURISegments).toURI();
                    try {
                        Resource externalDep = dep.getResourceSet().getResource(potentialExternalSourceURI, true);
                        script = (Script) externalDep.getContents().get(0);
                        if (xpectGenerator.isCompilable(externalDep, errorResult)) {
                            createTempJsFileWithScript(projectFolder, script, options, replaceQuotes);
                        }
                    } catch (Exception e) {
                        throw new RuntimeException("Couldn't load " + potentialExternalSourceURI + ".", e);
                    }
                }
            }
        }
    }
}
Also used : Script(org.eclipse.n4js.n4JS.Script) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) N4JSSourceFolderSnapshot(org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot) XtextResource(org.eclipse.xtext.resource.XtextResource) N4JSResource(org.eclipse.n4js.resource.N4JSResource) Resource(org.eclipse.emf.ecore.resource.Resource) URI(org.eclipse.emf.common.util.URI) IOException(java.io.IOException)

Example 13 with N4JSProjectConfigSnapshot

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

the class N4JSResourceDescriptionManager method hasDependencyTo.

/**
 * Returns true iff the project containing the 'fromUri' has a direct dependency to the project containing the
 * 'toUri'.
 */
private boolean hasDependencyTo(URI fromUri, URI toUri, N4JSWorkspaceConfigSnapshot workspaceConfig) {
    final N4JSProjectConfigSnapshot fromProject = workspaceConfig.findProjectContaining(fromUri);
    final N4JSProjectConfigSnapshot toProject = workspaceConfig.findProjectContaining(toUri);
    if (null != fromProject && null != toProject) {
        if (Objects.equals(fromProject, toProject)) {
            return true;
        }
        String toProjectName = toProject.getName();
        Iterable<String> fromProjectDependencyNames = getDependenciesForIsAffected(fromProject);
        for (String fromProjectDependencyName : fromProjectDependencyNames) {
            if (Objects.equals(fromProjectDependencyName, toProjectName)) {
                return true;
            }
        }
    }
    return false;
}
Also used : N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot)

Example 14 with N4JSProjectConfigSnapshot

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

the class N4JSResource method clearUnnecessaryFunctionBodies.

/**
 * Performance tweak. This method clears all bodies of {@link FunctionDefinition}s that:
 * <ul>
 * <li/>are in external projects (i.e. a dependency in a node_modules folder), and
 * <li/>declare a return type (otherwise, the poor man's return type inference wouldn't work anymore)
 * </ul>
 */
private void clearUnnecessaryFunctionBodies(EObject newRootAstElement) {
    if (!optionClearFunctionBodies) {
        return;
    }
    if (Objects.equals(N4JSGlobals.N4JSD_FILE_EXTENSION, URIUtils.fileExtension(getURI()))) {
        // There are no function bodies in n4jsd files.
        return;
    }
    N4JSProjectConfigSnapshot project = workspaceAccess.findProjectContaining(this);
    if (project == null || !project.isExternal()) {
        return;
    }
    List<FunctionDefinition> allFunDefs = EcoreUtil2.getAllContentsOfType(newRootAstElement, FunctionDefinition.class);
    for (FunctionDefinition funDef : allFunDefs) {
        if (funDef.getDeclaredReturnTypeRefInAST() != null && funDef.getBody() != null) {
            funDef.getBody().getStatements().clear();
        }
    }
}
Also used : N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) FunctionDefinition(org.eclipse.n4js.n4JS.FunctionDefinition)

Example 15 with N4JSProjectConfigSnapshot

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

the class ProjectImportEnablingScope method getElementsWithDesiredProjectName.

/**
 * This method asks {@link #delegate} for elements matching provided <code>moduleSpecifier</code>. Returned results
 * are filtered by expected {@link N4JSPackageName}.
 */
private Collection<IEObjectDescription> getElementsWithDesiredProjectName(QualifiedName moduleSpecifier, N4JSPackageName projectName) {
    final Iterable<IEObjectDescription> moduleSpecifierMatchesWithPossibleDuplicates = delegate.getElements(moduleSpecifier);
    // delegate may return multiple entries since it allows duplication (normal 'shadowing' of scopes is not
    // applied). We filter duplicates by uniqueness of target EObject URI.
    final Map<String, IEObjectDescription> result = new HashMap<>();
    for (IEObjectDescription desc : moduleSpecifierMatchesWithPossibleDuplicates) {
        final N4JSProjectConfigSnapshot containingProject = workspaceConfigSnapshot.findProjectContaining(desc.getEObjectURI());
        if (containingProject != null && projectName.equals(containingProject.getN4JSPackageName())) {
            result.put(desc.getEObjectURI().toString(), desc);
        }
    }
    return result.values();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

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