|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.magento.idea.magento2uct.execution; |
| 7 | + |
| 8 | +import com.intellij.execution.ExecutionException; |
| 9 | +import com.intellij.execution.configurations.GeneralCommandLine; |
| 10 | +import com.intellij.execution.configurations.PtyCommandLine; |
| 11 | +import com.intellij.execution.filters.TextConsoleBuilderFactory; |
| 12 | +import com.intellij.execution.process.OSProcessHandler; |
| 13 | +import com.intellij.execution.process.ProcessHandlerFactory; |
| 14 | +import com.intellij.execution.process.ProcessTerminatedListener; |
| 15 | +import com.intellij.execution.ui.ConsoleView; |
| 16 | +import com.intellij.icons.AllIcons; |
| 17 | +import com.intellij.openapi.project.Project; |
| 18 | +import com.intellij.openapi.util.SystemInfo; |
| 19 | +import com.intellij.openapi.wm.RegisterToolWindowTask; |
| 20 | +import com.intellij.openapi.wm.ToolWindow; |
| 21 | +import com.intellij.openapi.wm.ToolWindowAnchor; |
| 22 | +import com.intellij.openapi.wm.ToolWindowId; |
| 23 | +import com.intellij.openapi.wm.ToolWindowManager; |
| 24 | +import com.intellij.ui.content.Content; |
| 25 | +import com.magento.idea.magento2plugin.MagentoIcons; |
| 26 | +import org.jetbrains.annotations.NotNull; |
| 27 | + |
| 28 | +public final class DownloadUctCommand { |
| 29 | + |
| 30 | + private static final String TAB_TITLE = "Create UCT project"; |
| 31 | + private final Project project; |
| 32 | + |
| 33 | + /** |
| 34 | + * Download UCT command constructor. |
| 35 | + * |
| 36 | + * @param project Project |
| 37 | + */ |
| 38 | + public DownloadUctCommand(final @NotNull Project project) { |
| 39 | + this.project = project; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Start UCT downloading process. |
| 44 | + */ |
| 45 | + public void execute() throws ExecutionException { |
| 46 | + final ConsoleView consoleView = createConsole(); |
| 47 | + final OSProcessHandler processHandler = createProcessHandler(); |
| 48 | + consoleView.attachToProcess(processHandler); |
| 49 | + processHandler.startNotify(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Create process handler. |
| 54 | + * |
| 55 | + * @return OSProcessHandler |
| 56 | + */ |
| 57 | + private OSProcessHandler createProcessHandler() throws ExecutionException { |
| 58 | + final GeneralCommandLine commandLine = createGeneralCommandLine(true); |
| 59 | + commandLine.setWorkDirectory(project.getBasePath()); |
| 60 | + commandLine.setExePath("composer"); |
| 61 | + commandLine.addParameters( |
| 62 | + "create-project", |
| 63 | + "magento/upgrade-compatibility-tool", |
| 64 | + "uct", |
| 65 | + "--repository", |
| 66 | + "https://repo.magento.com", |
| 67 | + "--ansi" |
| 68 | + ); |
| 69 | + |
| 70 | + final OSProcessHandler processHandler = ProcessHandlerFactory |
| 71 | + .getInstance() |
| 72 | + .createColoredProcessHandler(commandLine); |
| 73 | + |
| 74 | + ProcessTerminatedListener.attach(processHandler); |
| 75 | + |
| 76 | + return processHandler; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Create general command line. |
| 81 | + * |
| 82 | + * @param withPty boolean |
| 83 | + * |
| 84 | + * @return GeneralCommandLine |
| 85 | + */ |
| 86 | + private GeneralCommandLine createGeneralCommandLine(final boolean withPty) { |
| 87 | + GeneralCommandLine commandLine; |
| 88 | + |
| 89 | + if (withPty) { |
| 90 | + if (SystemInfo.isWindows) { |
| 91 | + commandLine = new GeneralCommandLine(); |
| 92 | + commandLine.getEnvironment().putIfAbsent("TERM", "xterm"); |
| 93 | + } else { |
| 94 | + commandLine = new PtyCommandLine().withInitialColumns(2500); |
| 95 | + } |
| 96 | + } else { |
| 97 | + commandLine = new GeneralCommandLine(); |
| 98 | + } |
| 99 | + |
| 100 | + return commandLine; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Create console view. |
| 105 | + * |
| 106 | + * @return ConsoleView |
| 107 | + */ |
| 108 | + private ConsoleView createConsole() { |
| 109 | + ToolWindow toolWindow = ToolWindowManager |
| 110 | + .getInstance(project) |
| 111 | + .getToolWindow(ToolWindowId.RUN); |
| 112 | + |
| 113 | + if (toolWindow == null) { |
| 114 | + toolWindow = ToolWindowManager.getInstance(project).registerToolWindow( |
| 115 | + new RegisterToolWindowTask( |
| 116 | + ToolWindowId.RUN, |
| 117 | + ToolWindowAnchor.BOTTOM, |
| 118 | + null, |
| 119 | + false, |
| 120 | + true, |
| 121 | + true, |
| 122 | + true, |
| 123 | + null, |
| 124 | + AllIcons.Actions.Execute, |
| 125 | + null |
| 126 | + ) |
| 127 | + ); |
| 128 | + } |
| 129 | + final ConsoleView consoleView = TextConsoleBuilderFactory.getInstance() |
| 130 | + .createBuilder(project) |
| 131 | + .getConsole(); |
| 132 | + final Content content = toolWindow |
| 133 | + .getContentManager() |
| 134 | + .getFactory() |
| 135 | + .createContent(consoleView.getComponent(), TAB_TITLE, true); |
| 136 | + content.putUserData(ToolWindow.SHOW_CONTENT_ICON, Boolean.TRUE); |
| 137 | + content.setIcon(MagentoIcons.PLUGIN_ICON_SMALL); |
| 138 | + toolWindow.getContentManager().addContent(content); |
| 139 | + toolWindow.show(); |
| 140 | + |
| 141 | + return consoleView; |
| 142 | + } |
| 143 | +} |
0 commit comments