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());
}
}
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);
}
}
}
}
}
}
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;
}
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();
}
}
}
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();
}
Aggregations