Search in sources :

Example 91 with IN4JSProject

use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.

the class NpmExportWizard method performFinish.

@Override
public boolean performFinish() {
    String destination = exportPage.getDestinationValue();
    List<IProject> toExport = exportPage.getChosenProjects();
    boolean shouldPackAsTarball = exportPage.getShouldPackAsTarball();
    File folder = new File(destination);
    // remap all IProjects
    List<? extends IN4JSProject> toExportIN4JSProjects = mapToIN4JSProjects(toExport);
    if (runTools() && toolRunnerPage.isToolrunRequested()) {
        // bring to front.
        ((WizardDialog) getContainer()).showPage(toolRunnerPage);
    }
    try {
        npmExporter.export(toExportIN4JSProjects, folder);
        if (shouldPackAsTarball) {
            npmExporter.tarAndZip(toExportIN4JSProjects, folder);
        }
        boolean runIt = runTools() && toolRunnerPage.queryRunTool();
        if (runIt) {
            final List<String> toolCommand = toolRunnerPage.getCommand();
            getContainer().run(true, true, new IRunnableWithProgress() {

                @Override
                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    try {
                        List<String> cmds = newArrayList();
                        // cmds.add("echo"); // prepend with echo for debug TODO remove
                        // cmds.addAll(toolCommand);
                        cmds.add("bash");
                        cmds.add("-login");
                        cmds.add("-c");
                        // cmds.addAll(toolCommand);
                        cmds.add(Joiner.on(" ").join(toolCommand));
                        System.out.println("Comman will be: " + Joiner.on(" :: ").join(cmds));
                        for (IN4JSProject p : toExportIN4JSProjects) {
                            String info = "Processing " + p.toString() + "\n";
                            System.out.println(info);
                            toolRunnerPage.appendText(info);
                            File dir = npmExporter.exportDestination(p, folder);
                            ProcessBuilder pb = new ProcessBuilder();
                            pb.directory(dir);
                            pb.command(cmds);
                            pb.redirectErrorStream(true);
                            Process proc = pb.start();
                            // handle each of proc's streams in a separate thread
                            ExecutorService handlerThreadPool = Executors.newFixedThreadPool(3);
                            // handlerThreadPool.submit(new Runnable() {
                            // @Override
                            // public void run() {
                            // // we want to write to the stdin of the process
                            // BufferedWriter stdin = new BufferedWriter(
                            // new OutputStreamWriter(proc.getOutputStream()));
                            // 
                            // // read from our own stdin so we can write it to proc's stdin
                            // BufferedReader myStdin =
                            // new BufferedReader(new InputStreamReader(System.in));
                            // String line = null;
                            // try {
                            // do {
                            // line = myStdin.readLine();
                            // stdin.write(String.format("%s%n", line));
                            // stdin.flush();
                            // } while(! "exit".equalsIgnoreCase(line));
                            // } catch(IOException e) {
                            // e.printStackTrace();
                            // }
                            // }
                            // });
                            handlerThreadPool.submit(new Runnable() {

                                @Override
                                public void run() {
                                    // we want to read the stdout of the process
                                    BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                                    String line;
                                    try {
                                        while (null != (line = stdout.readLine())) {
                                            System.err.printf("[stderr] %s%n", line);
                                            toolRunnerPage.appendConsoleOut(line);
                                        }
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                            handlerThreadPool.submit(new Runnable() {

                                @Override
                                public void run() {
                                    // we want to read the stderr of the process
                                    BufferedReader stderr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
                                    String line;
                                    try {
                                        while (null != (line = stderr.readLine())) {
                                            System.err.printf("[stderr] %s%n", line);
                                            toolRunnerPage.appendConsoleErr(line);
                                        }
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                            // wait for the process to terminate
                            int exitCode = proc.waitFor();
                            System.out.printf("Process terminated with exit code %d%n", exitCode);
                            handlerThreadPool.shutdown();
                        }
                        // done with all projects.
                        // wait for close.
                        toolRunnerPage.queryCloseDialog();
                    } catch (Exception e) {
                        throw new InvocationTargetException(e);
                    }
                }
            });
        }
    } catch (IOException | ArchiveException | CompressorException e) {
        e.printStackTrace();
        Status s = new Status(ERROR, NpmExporterActivator.PLUGIN_ID, "Error occured during export.", e);
        N4JSActivator.getInstance().getLog().log(s);
        return false;
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // successfully done, then store relevant history:
    exportPage.finish();
    if (runTools()) {
        toolRunnerPage.finish();
    }
    return true;
}
Also used : ArchiveException(org.apache.commons.compress.archivers.ArchiveException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) CompressorException(org.apache.commons.compress.compressors.CompressorException) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) Status(org.eclipse.core.runtime.Status) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CompressorException(org.apache.commons.compress.compressors.CompressorException) IOException(java.io.IOException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ExecutorService(java.util.concurrent.ExecutorService) BufferedReader(java.io.BufferedReader) File(java.io.File) WizardDialog(org.eclipse.jface.wizard.WizardDialog)

Example 92 with IN4JSProject

use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.

the class NpmExportWizard method init.

@Override
public void init(IWorkbench targetWorkbench, IStructuredSelection currentSelection) {
    // this.selection = currentSelection;
    List<?> selectedResources = IDE.computeSelectedResources(currentSelection);
    List<IProject> workspaceProjects = Arrays.asList(ResourcesPlugin.getWorkspace().getRoot().getProjects());
    // Find all selected projects
    Set<IProject> projects = selectedResources.stream().filter(m -> m instanceof IResource).map(m -> ((IResource) m).getProject()).filter(// only open projects
    p -> p.isOpen()).collect(Collectors.toSet());
    // make the behavior predictable by ordering:
    TreeSet<IProject> sortedProjects = Sets.<IProject>newTreeSet((a, b) -> a.getName().compareToIgnoreCase(b.getName()));
    sortedProjects.addAll(projects);
    // 0) turn into IN4JSProject and give and process further.
    // a) find out which projects fulfill the npm-"exportable"-contract
    // b) give back a list to the user what to export,
    // c) disable things not fullfilling the contract.
    // d) take choosing from the list and pass to exporter in non-ui package.
    // 0)
    List<IN4JSEclipseProject> rawN4jsProjects = Lists.newArrayList();
    iP2in4jsP = HashBiMap.create();
    for (IProject iProject : workspaceProjects) {
        IN4JSEclipseProject mappedIn4jsProject = map2In4js(iProject);
        if (mappedIn4jsProject != null) {
            rawN4jsProjects.add(mappedIn4jsProject);
            iP2in4jsP.put(iProject, mappedIn4jsProject);
        }
    }
    // filter out Non-N4JS-projects from initial selection.
    sortedProjects.retainAll(iP2in4jsP.keySet());
    // filter out all non-N4JS-projects from the workspace projects.
    ArrayList<IProject> filteredWorkspaceProjects = new ArrayList<>(workspaceProjects);
    filteredWorkspaceProjects.retainAll(iP2in4jsP.keySet());
    setWindowTitle("N4JS to npm Export");
    setNeedsProgressMonitor(true);
    Map<IProject, Boolean> selectedProjects = new HashMap<>();
    // Add all workspace projects to list, default selection value is false
    filteredWorkspaceProjects.forEach(project -> selectedProjects.put(project, false));
    // Override selection value for all initially selected projects
    sortedProjects.forEach(project -> selectedProjects.put(project, true));
    // exportPage = new ExportSelectionPage("Export Page", rawN4jsProjects, labelProvider);
    exportPage = new ExportSelectionPage("Export Page", selectedProjects);
    if (runTools())
        toolRunnerPage = new NpmToolRunnerPage("npm Execution Page");
    comparePage = new PackageJsonComparePage("Compare package.json Page");
    pageListener = new IPageChangedListener() {

        @Override
        public void pageChanged(PageChangedEvent event) {
            if (event.getSelectedPage() == comparePage) {
                udpatePackagJasonComparison();
            }
        }
    };
}
Also used : Arrays(java.util.Arrays) WorkbenchPlugin(org.eclipse.ui.internal.WorkbenchPlugin) IN4JSEclipseCore(org.eclipse.n4js.ui.projectModel.IN4JSEclipseCore) IDialogSettings(org.eclipse.jface.dialogs.IDialogSettings) N4JSActivator(org.eclipse.n4js.ui.internal.N4JSActivator) IN4JSEclipseProject(org.eclipse.n4js.ui.projectModel.IN4JSEclipseProject) Map(java.util.Map) Diagnostician(org.eclipse.n4js.npmexporter.validation.Diagnostician) BiMap(com.google.common.collect.BiMap) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) Set(java.util.Set) Status(org.eclipse.core.runtime.Status) Wizard(org.eclipse.jface.wizard.Wizard) Display(org.eclipse.swt.widgets.Display) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Sets(com.google.common.collect.Sets) InvocationTargetException(java.lang.reflect.InvocationTargetException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) WizardDialog(org.eclipse.jface.wizard.WizardDialog) NpmExporter(org.eclipse.n4js.npmexporter.NpmExporter) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IWizardContainer(org.eclipse.jface.wizard.IWizardContainer) Joiner(com.google.common.base.Joiner) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) IDE(org.eclipse.ui.ide.IDE) MergeResult(org.eclipse.n4js.npmexporter.NpmExporter.MergeResult) URI(org.eclipse.emf.common.util.URI) IWizardPage(org.eclipse.jface.wizard.IWizardPage) ERROR(org.eclipse.core.runtime.IStatus.ERROR) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Lists(com.google.common.collect.Lists) IProject(org.eclipse.core.resources.IProject) PageChangedEvent(org.eclipse.jface.dialogs.PageChangedEvent) IExportWizard(org.eclipse.ui.IExportWizard) ExecutorService(java.util.concurrent.ExecutorService) IPageChangedListener(org.eclipse.jface.dialogs.IPageChangedListener) IResourceSetProvider(org.eclipse.xtext.ui.resource.IResourceSetProvider) CompressorException(org.apache.commons.compress.compressors.CompressorException) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) File(java.io.File) HashBiMap(com.google.common.collect.HashBiMap) IResource(org.eclipse.core.resources.IResource) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IWorkbench(org.eclipse.ui.IWorkbench) BufferedReader(java.io.BufferedReader) IN4JSEclipseProject(org.eclipse.n4js.ui.projectModel.IN4JSEclipseProject) HashMap(java.util.HashMap) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) PageChangedEvent(org.eclipse.jface.dialogs.PageChangedEvent) IProject(org.eclipse.core.resources.IProject) IPageChangedListener(org.eclipse.jface.dialogs.IPageChangedListener) IResource(org.eclipse.core.resources.IResource)

Example 93 with IN4JSProject

use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.

the class NpmExportWizard method updateProjectsToExportSelection.

/**
 * @param checked
 *            list of selected projects to export
 * @throws InterruptedException
 * @throws InvocationTargetException
 */
public void updateProjectsToExportSelection(Object[] checked) throws InvocationTargetException, InterruptedException {
    getContainer().run(true, true, new IRunnableWithProgress() {

        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                List<IProject> checkedProjects = newArrayList();
                for (Object o : checked) checkedProjects.add((IProject) o);
                List<? extends IN4JSProject> checkedIN4JSProjects = mapToIN4JSProjects(checkedProjects);
                // validate:
                final Diagnostician diag = new Diagnostician();
                List<IN4JSProject> _requiresMerge1 = newArrayList();
                for (IN4JSProject p : checkedIN4JSProjects) {
                    diag.setActiveProject(p);
                    npmExporter.validate(p, diag);
                    if (npmExporter.requiresJsonMerge(p)) {
                        _requiresMerge1.add(p);
                    }
                }
                setRequireJsonMerge(_requiresMerge1);
                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        if (diag.isIssueFree()) {
                            exportPage.setErrorMessage(null);
                            exportPage.setErrorText("");
                        } else {
                            exportPage.setPageComplete(false);
                            exportPage.setErrorMessage("Validation errors for projects: " + Joiner.on(", ").join(diag.projectsWithEntries().stream().map(p -> p.getProjectId()).iterator()));
                            String text = diag.asErrorText();
                            exportPage.setErrorText(text);
                        }
                    }
                });
            } catch (Exception e) {
                throw new InvocationTargetException(e);
            }
        }
    });
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Diagnostician(org.eclipse.n4js.npmexporter.validation.Diagnostician) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CompressorException(org.apache.commons.compress.compressors.CompressorException) IOException(java.io.IOException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Aggregations

IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)93 URI (org.eclipse.emf.common.util.URI)32 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)13 List (java.util.List)11 Inject (com.google.inject.Inject)9 Map (java.util.Map)9 IProject (org.eclipse.core.resources.IProject)9 IN4JSCore (org.eclipse.n4js.projectModel.IN4JSCore)9 Optional (com.google.common.base.Optional)7 File (java.io.File)7 Collection (java.util.Collection)7 HashSet (java.util.HashSet)7 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)7 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6 IOException (java.io.IOException)5 IN4JSArchive (org.eclipse.n4js.projectModel.IN4JSArchive)5 IN4JSSourceContainer (org.eclipse.n4js.projectModel.IN4JSSourceContainer)5 FluentIterable.from (com.google.common.collect.FluentIterable.from)4