Search in sources :

Example 6 with Project

use of org.eclipse.n4js.tests.codegen.Project in project n4js by eclipse.

the class TestWorkspaceManager method createSimpleProject.

private Project createSimpleProject(String projectName, Map<String, ? extends CharSequence> modulesContents, Multimap<String, String> dependencies, ProjectKind projectKind) {
    if (projectName.equals(N4JS_RUNTIME) && (modulesContents == null || modulesContents.isEmpty())) {
        return N4JS_RUNTIME_FAKE;
    }
    ProjectType prjType = projectName.equals(N4JS_RUNTIME) ? ProjectType.RUNTIME_ENVIRONMENT : projectType;
    Project project = new Project(projectName, VENDOR, VENDOR_NAME, prjType);
    CharSequence customSourceFolderName = modulesContents.get(CFG_SOURCE_FOLDER);
    Folder sourceFolder = project.createSourceFolder(customSourceFolderName != null ? customSourceFolderName.toString() : DEFAULT_SOURCE_FOLDER);
    for (String moduleName : modulesContents.keySet()) {
        String contents = modulesContents.get(moduleName).toString();
        if (moduleName.equals(CFG_DEPENDENCIES)) {
            String[] allDeps = contents.split(",");
            for (String dependency : allDeps) {
                String dependencyTrimmed = dependency.trim();
                dependencies.put(projectName, dependencyTrimmed);
                project.addProjectDependency(dependencyTrimmed);
            }
        } else if (moduleName.equals(CFG_MAIN_MODULE)) {
            project.setMainModule(contents);
        } else if (moduleName.equals(CFG_SOURCE_FOLDER)) {
        // ignore (already processed above)
        } else if (moduleName.equals(N4JSGlobals.PACKAGE_JSON)) {
            project.setProjectDescriptionContent(contents);
        } else if (moduleName.startsWith(CFG_NODE_MODULES)) {
            int length = CFG_NODE_MODULES.length();
            int endIdx = moduleName.indexOf(NAME_DIVIDER, length);
            String nmName = moduleName.substring(length, endIdx < 0 ? moduleName.length() : endIdx);
            if (moduleName.equals(CFG_NODE_MODULES + N4JS_RUNTIME)) {
                project.addNodeModuleProject(N4JS_RUNTIME_FAKE);
                project.addProjectDependency(N4JS_RUNTIME_FAKE.getName());
            } else if (moduleName.indexOf(CFG_SRC, length) > 0) {
                int indexOfSrc = moduleName.indexOf(CFG_SRC);
                String nmModuleName = moduleName.substring(indexOfSrc + CFG_SRC.length());
                Project nmProject = project.getNodeModuleProject(nmName);
                if (nmProject == null) {
                    nmProject = new Project(nmName, VENDOR, VENDOR + "_name", prjType);
                    nmProject.createSourceFolder(DEFAULT_SOURCE_FOLDER);
                    project.addNodeModuleProject(nmProject);
                    project.addProjectDependency(nmProject.getName());
                }
                Folder nmSourceFolder = nmProject.getSourceFolders().iterator().next();
                createAndAddModule(contents, nmModuleName, nmSourceFolder);
            } else if (moduleName.indexOf(CFG_DEPENDENCIES, length) > 0) {
                Project nmProject = project.getNodeModuleProject(nmName);
                String[] allDeps = contents.split(",");
                for (String dependency : allDeps) {
                    String dependencyTrimmed = dependency.trim();
                    dependencies.put(nmName, dependencyTrimmed);
                    nmProject.addProjectDependency(dependencyTrimmed);
                }
            } else if (moduleName.indexOf(CFG_NODE_MODULES, length) > 0) {
                Project nmProject = project.getNodeModuleProject(nmName);
                int startIdx = moduleName.indexOf(CFG_NODE_MODULES, length) + CFG_NODE_MODULES.length();
                String nmnmName = moduleName.substring(startIdx);
                Project nmnmProject = new Project(nmnmName, VENDOR, VENDOR + "_name", prjType);
                nmnmProject.createSourceFolder(DEFAULT_SOURCE_FOLDER);
                nmProject.addNodeModuleProject(nmnmProject);
                nmProject.addProjectDependency(nmnmProject.getName());
            } else {
                throw new IllegalArgumentException("Unknown specifier: " + moduleName);
            }
        } else {
            if (moduleName.startsWith("#")) {
                throw new IllegalArgumentException("unknown reserved string: " + moduleName);
            }
            Folder moduleSpecificSourceFolder = sourceFolder;
            if (moduleName.startsWith("/")) {
                // use first segment as source folder
                int endOfFirstSegment = moduleName.indexOf("/", 1);
                String sourceFolderName = moduleName.subSequence(1, endOfFirstSegment).toString();
                moduleName = moduleName.substring(endOfFirstSegment + 1);
                moduleSpecificSourceFolder = project.createSourceFolder(sourceFolderName);
            }
            createAndAddModule(contents, moduleName, moduleSpecificSourceFolder);
        }
    }
    // apply default values
    if (N4JSGlobals.PROJECT_TYPES_REQUIRING_N4JS_RUNTIME.contains(project.getType())) {
        // add dependency to n4js-runtime (if not already present)
        project.addProjectDependency(N4JS_RUNTIME);
        // add fake n4js-runtime to node_modules folder (if not already present)
        if (projectKind == ProjectKind.TopLevel) {
            if (project.getNodeModuleProject(N4JS_RUNTIME) == null) {
                project.addNodeModuleProject(N4JS_RUNTIME_FAKE);
            }
        }
    }
    return project;
}
Also used : Project(org.eclipse.n4js.tests.codegen.Project) YarnWorkspaceProject(org.eclipse.n4js.tests.codegen.YarnWorkspaceProject) ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType) Folder(org.eclipse.n4js.tests.codegen.Folder)

Example 7 with Project

use of org.eclipse.n4js.tests.codegen.Project in project n4js by eclipse.

the class XtSetupParser method parseWorkspace.

private static XtWorkspace parseWorkspace(TokenStream tokens, File xtFile, String xtFileContent) {
    WorkspaceBuilder builder = new WorkspaceBuilder(new BuilderInfo());
    YarnProjectBuilder yarnProjectBuilder = builder.addYarnProject(TestWorkspaceManager.YARN_TEST_PROJECT);
    LOOP: while (tokens.hasNext()) {
        switch(tokens.next()) {
            case "Project":
            case "JavaProject":
                parseProject(tokens, xtFile, xtFileContent, yarnProjectBuilder);
                break;
            case "}":
                break LOOP;
            default:
                Preconditions.checkState(false, ERROR + "Unexpected token in Workspace: " + tokens.lookLast() + " in file " + xtFile.getPath());
        }
    }
    XtWorkspace xtWorkspace = builder.build(new XtWorkspace());
    if (xtWorkspace.getProjects().size() == 1 && xtWorkspace.getProjects().get(0) instanceof YarnWorkspaceProject && ((YarnWorkspaceProject) xtWorkspace.getProjects().get(0)).getMemberProjects().size() == 1) {
        YarnWorkspaceProject yarnWorkspaceProject = (YarnWorkspaceProject) xtWorkspace.getProjects().get(0);
        if (yarnWorkspaceProject.getMemberProjects().size() == 1) {
            Project project = yarnWorkspaceProject.getMemberProjects().iterator().next();
            xtWorkspace.clearProjects();
            xtWorkspace.addProject(project);
        }
    }
    xtWorkspace.moduleNameOfXtFile = ((BuilderInfo) builder.builderInfo).moduleNameOfXtFile;
    return xtWorkspace;
}
Also used : WorkspaceBuilder(org.eclipse.n4js.tests.codegen.WorkspaceBuilder) YarnProjectBuilder(org.eclipse.n4js.tests.codegen.WorkspaceBuilder.YarnProjectBuilder) YarnWorkspaceProject(org.eclipse.n4js.tests.codegen.YarnWorkspaceProject) Project(org.eclipse.n4js.tests.codegen.Project) YarnWorkspaceProject(org.eclipse.n4js.tests.codegen.YarnWorkspaceProject)

Example 8 with Project

use of org.eclipse.n4js.tests.codegen.Project in project n4js by eclipse.

the class XtSetupParser method applyInlinedFileContents.

private static void applyInlinedFileContents(XtWorkspace workspace, Map<String, String> files) {
    if (workspace == null) {
        // nothing to do in this case
        return;
    }
    for (Project prj : workspace.getAllProjects()) {
        for (Folder srcFolder : prj.getSourceFolders()) {
            Iterable<OtherFile> allFiles = Iterables.concat(srcFolder.getModules(), srcFolder.getOtherFiles());
            for (OtherFile file : allFiles) {
                String mName = file.getNameWithExtension();
                if (file.getContents() == null) {
                    if (files.containsKey(mName)) {
                        String contents = files.get(mName);
                        file.setContents(contents);
                    } else {
                        throw new IllegalStateException(ERROR + "File not found: " + mName);
                    }
                }
            }
        }
    }
}
Also used : Project(org.eclipse.n4js.tests.codegen.Project) YarnWorkspaceProject(org.eclipse.n4js.tests.codegen.YarnWorkspaceProject) Folder(org.eclipse.n4js.tests.codegen.Folder) OtherFile(org.eclipse.n4js.tests.codegen.OtherFile)

Example 9 with Project

use of org.eclipse.n4js.tests.codegen.Project in project n4js by eclipse.

the class XtIdeTest method generated_dts.

/**
 * Compares the content of the generated .d.ts file with the expectation string.
 *
 * <pre>
 * // XPECT generated_dts ---
 * &lt;EXPECTED CONTENT OF GENERATED D.TS FILE&gt;
 * // ---
 * </pre>
 */
@Xpect
public void generated_dts(XtMethodData data) throws IOException {
    String moduleName = xtData.workspace.moduleNameOfXtFile;
    int idxStart = Math.max(moduleName.lastIndexOf("/") + 1, 0);
    int idxEnd = moduleName.lastIndexOf(".");
    String genDtsFileName = moduleName.substring(idxStart, idxEnd) + ".d.ts";
    try {
        FileURI genDtsFileURI = getFileURIFromModuleName(genDtsFileName);
        String genDtsCode = Files.readString(genDtsFileURI.toPath());
        assertTrue(genDtsCode.startsWith(OUTPUT_FILE_PREAMBLE));
        String genDtsCodeTrimmedPreamble = genDtsCode.substring(OUTPUT_FILE_PREAMBLE.length()).trim();
        assertTrue(genDtsCodeTrimmedPreamble.startsWith(IMPORT_N4JSGLOBALS));
        String genDtsCodeTrimmed = genDtsCodeTrimmedPreamble.substring(IMPORT_N4JSGLOBALS.length()).trim();
        assertEquals(data.getUnescapeExpectationRaw(), genDtsCodeTrimmed);
        CliTools cliTools = new CliTools();
        ensureTSC(cliTools);
        List<Project> allProjectsWithGenerateDts = FluentIterable.from(xtData.workspace.getAllProjects()).filter(Project::isGenerateDts).toList();
        assertFalse("no projects found with .d.ts generation turned on", allProjectsWithGenerateDts.isEmpty());
        for (Project project : allProjectsWithGenerateDts) {
            File workingDir = getProjectRoot(project.getName());
            // copy n4jsglobals.d.ts to output dir to make d.ts globals available
            Path n4jsGlobalsDTS = N4jsLibsAccess.getN4JSGlobalsDTS();
            Files.copy(n4jsGlobalsDTS, workingDir.toPath().resolve("src-gen/n4jsglobals.d.ts"));
            ProcessResult result;
            try {
                result = cliTools.nodejsRun(workingDir.toPath(), TSC2.getAbsoluteFile().toPath());
            } catch (CliException e) {
                throw new AssertionError("error while running tsc in working directory: " + workingDir, e);
            }
            assertFalse("TypeScript Error: " + result.getStdOut(), result.getStdOut().contains(": error "));
        }
    } catch (IllegalStateException e) {
        throw new RuntimeException("Could not find file " + genDtsFileName + "\nDid you set: " + XtSetupParser.GENERATE_DTS + " in SETUP section?", e);
    }
}
Also used : Path(java.nio.file.Path) ProcessResult(org.eclipse.n4js.cli.helper.ProcessResult) CliTools(org.eclipse.n4js.cli.helper.CliTools) Project(org.eclipse.n4js.tests.codegen.Project) FileURI(org.eclipse.n4js.workspace.locations.FileURI) CliException(org.eclipse.n4js.cli.helper.CliTools.CliException) File(java.io.File) Xpect(org.eclipse.xpect.runner.Xpect)

Example 10 with Project

use of org.eclipse.n4js.tests.codegen.Project in project n4js by eclipse.

the class ScenarioGenerator method createSupplierProject.

/**
 * Creates the supplier project containing the given module and vendor ID.
 *
 * @param supplierModule
 *            the supplier module to add the project
 * @param vendorId
 *            the vendor ID
 *
 * @return the newly created project
 */
private Project createSupplierProject(Module supplierModule, String vendorId) {
    Project supplierProject = new Project("SupplierProject", vendorId, vendorId + "_name", PROJECT_TYPE);
    supplierProject.createSourceFolder("src").addModule(supplierModule);
    return supplierProject;
}
Also used : Project(org.eclipse.n4js.tests.codegen.Project)

Aggregations

Project (org.eclipse.n4js.tests.codegen.Project)16 YarnWorkspaceProject (org.eclipse.n4js.tests.codegen.YarnWorkspaceProject)8 File (java.io.File)3 Path (java.nio.file.Path)3 Folder (org.eclipse.n4js.tests.codegen.Folder)3 FileURI (org.eclipse.n4js.workspace.locations.FileURI)3 Lists (com.google.common.collect.Lists)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CodeAction (org.eclipse.lsp4j.CodeAction)2 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)2 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)2 Command (org.eclipse.lsp4j.Command)2 Position (org.eclipse.lsp4j.Position)2 Range (org.eclipse.lsp4j.Range)2 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)2 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)2 Module (org.eclipse.n4js.tests.codegen.Module)2 Iterables (com.google.common.collect.Iterables)1 Sets (com.google.common.collect.Sets)1