|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.magento.idea.magento2uct.execution.configurations; |
| 7 | + |
| 8 | +import com.intellij.execution.ExecutionException; |
| 9 | +import com.intellij.execution.Executor; |
| 10 | +import com.intellij.execution.configurations.CommandLineState; |
| 11 | +import com.intellij.execution.configurations.ConfigurationFactory; |
| 12 | +import com.intellij.execution.configurations.GeneralCommandLine; |
| 13 | +import com.intellij.execution.configurations.LocatableConfigurationBase; |
| 14 | +import com.intellij.execution.configurations.RunConfiguration; |
| 15 | +import com.intellij.execution.configurations.RunProfileState; |
| 16 | +import com.intellij.execution.configurations.RuntimeConfigurationException; |
| 17 | +import com.intellij.execution.process.OSProcessHandler; |
| 18 | +import com.intellij.execution.process.ProcessHandler; |
| 19 | +import com.intellij.execution.process.ProcessHandlerFactory; |
| 20 | +import com.intellij.execution.process.ProcessTerminatedListener; |
| 21 | +import com.intellij.execution.runners.ExecutionEnvironment; |
| 22 | +import com.intellij.openapi.options.SettingsEditor; |
| 23 | +import com.intellij.openapi.project.Project; |
| 24 | +import com.intellij.openapi.util.NlsActions; |
| 25 | +import com.jetbrains.php.config.PhpProjectConfigurationFacade; |
| 26 | +import com.jetbrains.php.config.commandLine.PhpCommandSettings; |
| 27 | +import com.jetbrains.php.config.commandLine.PhpCommandSettingsBuilder; |
| 28 | +import com.jetbrains.php.config.interpreters.PhpInterpreter; |
| 29 | +import com.magento.idea.magento2uct.execution.filters.UctPhpFileFilter; |
| 30 | +import com.magento.idea.magento2uct.execution.filters.UctResultFileFilter; |
| 31 | +import com.magento.idea.magento2uct.settings.UctSettingsService; |
| 32 | +import com.magento.idea.magento2uct.versioning.IssueSeverityLevel; |
| 33 | +import org.jetbrains.annotations.NotNull; |
| 34 | +import org.jetbrains.annotations.Nullable; |
| 35 | + |
| 36 | +public class UctRunConfiguration extends LocatableConfigurationBase<UctRunConfigurationOptions> { |
| 37 | + |
| 38 | + /** |
| 39 | + * UCT run configuration constructor. |
| 40 | + * |
| 41 | + * @param project Project |
| 42 | + * @param factory ConfigurationFactory |
| 43 | + * @param name String |
| 44 | + */ |
| 45 | + protected UctRunConfiguration( |
| 46 | + final @NotNull Project project, |
| 47 | + final @NotNull ConfigurationFactory factory, |
| 48 | + final @Nullable String name |
| 49 | + ) { |
| 50 | + super(project, factory, name); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected @NotNull UctRunConfigurationOptions getOptions() { |
| 55 | + return (UctRunConfigurationOptions) super.getOptions(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Set script name setting. |
| 60 | + * |
| 61 | + * @param scriptName String |
| 62 | + */ |
| 63 | + public void setScriptName(final String scriptName) { |
| 64 | + getOptions().setScriptName(scriptName); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Get script name setting. |
| 69 | + * |
| 70 | + * @return String |
| 71 | + */ |
| 72 | + public String getScriptName() { |
| 73 | + return getOptions().getScriptName(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Set project root setting. |
| 78 | + * |
| 79 | + * @param projectRoot String |
| 80 | + */ |
| 81 | + public void setProjectRoot(final String projectRoot) { |
| 82 | + getOptions().setProjectRoot(projectRoot); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get project root setting. |
| 87 | + * |
| 88 | + * @return String |
| 89 | + */ |
| 90 | + public String getProjectRoot() { |
| 91 | + return getOptions().getProjectRoot(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Set coming version setting. |
| 96 | + * |
| 97 | + * @param comingVersion String |
| 98 | + */ |
| 99 | + public void setComingVersion(final String comingVersion) { |
| 100 | + getOptions().setComingVersion(comingVersion); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Get coming version setting. |
| 105 | + * |
| 106 | + * @return String |
| 107 | + */ |
| 108 | + public String getComingVersion() { |
| 109 | + return getOptions().getComingVersion(); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Set minimum issue severity level setting. |
| 114 | + * |
| 115 | + * @param minIssueLevel int |
| 116 | + */ |
| 117 | + public void setMinIssueLevel(final int minIssueLevel) { |
| 118 | + getOptions().setMinIssueLevel(minIssueLevel); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Get minimum issue severity level setting. |
| 123 | + * |
| 124 | + * @return int |
| 125 | + */ |
| 126 | + public int getMinIssueLevel() { |
| 127 | + return getOptions().getMinIssueLevel(); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Set ignoring for current version issues setting. |
| 132 | + * |
| 133 | + * @param hasIgnoreCurrentVersionIssues boolean |
| 134 | + */ |
| 135 | + public void setHasIgnoreCurrentVersionIssues(final boolean hasIgnoreCurrentVersionIssues) { |
| 136 | + getOptions().setHasIgnoreCurrentVersionIssues(hasIgnoreCurrentVersionIssues); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Check if has ignoring for current version issues setting. |
| 141 | + * |
| 142 | + * @return boolean |
| 143 | + */ |
| 144 | + public boolean hasIgnoreCurrentVersionIssues() { |
| 145 | + return getOptions().hasIgnoreCurrentVersionIssues(); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public @NotNull SettingsEditor<? extends RunConfiguration> getConfigurationEditor() { |
| 150 | + return new UctSettingsEditor(getProject()); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void checkConfiguration() throws RuntimeConfigurationException { |
| 155 | + super.checkConfiguration(); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public @Nullable @NlsActions.ActionText String suggestedName() { |
| 160 | + return UctRunConfigurationType.SHORT_TITLE; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public @Nullable RunProfileState getState( |
| 165 | + final @NotNull Executor executor, |
| 166 | + final @NotNull ExecutionEnvironment environment |
| 167 | + ) throws ExecutionException { |
| 168 | + return new CommandLineState(environment) { |
| 169 | + |
| 170 | + @Override |
| 171 | + protected @NotNull ProcessHandler startProcess() throws ExecutionException { |
| 172 | + final UctSettingsService settingsService = |
| 173 | + UctSettingsService.getInstance(getProject()); |
| 174 | + PhpInterpreter interpreter = PhpProjectConfigurationFacade |
| 175 | + .getInstance(getProject()) |
| 176 | + .getInterpreter(); |
| 177 | + |
| 178 | + if (interpreter == null) { |
| 179 | + throw new ExecutionException( |
| 180 | + "Please, specify interpreter option in the PHP settings" |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + if (getScriptName().isEmpty()) { |
| 185 | + throw new ExecutionException("The UCT executable path is not specified"); |
| 186 | + } else { |
| 187 | + if (settingsService != null) { |
| 188 | + settingsService.setUctExecutablePath(getScriptName()); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + if (getComingVersion().isEmpty()) { |
| 193 | + throw new ExecutionException("The coming/target version is not specified"); |
| 194 | + } |
| 195 | + |
| 196 | + if (getProjectRoot().isEmpty()) { |
| 197 | + throw new ExecutionException("The project root is not specified"); |
| 198 | + } |
| 199 | + |
| 200 | + final PhpCommandSettings commandSettingsBuilder = |
| 201 | + PhpCommandSettingsBuilder.create(getProject(), interpreter, false); |
| 202 | + |
| 203 | + commandSettingsBuilder.setScript(getScriptName()); |
| 204 | + commandSettingsBuilder.addArgument("upgrade:check"); |
| 205 | + |
| 206 | + if (!getComingVersion().isEmpty()) { |
| 207 | + commandSettingsBuilder.addArgument("--coming-version=" + getComingVersion()); |
| 208 | + } |
| 209 | + |
| 210 | + commandSettingsBuilder.addArgument(getProjectRoot()); |
| 211 | + |
| 212 | + final GeneralCommandLine commandLine = |
| 213 | + commandSettingsBuilder.createGeneralCommandLine(); |
| 214 | + |
| 215 | + final IssueSeverityLevel severityLevel = |
| 216 | + IssueSeverityLevel.getByLevel(getMinIssueLevel()); |
| 217 | + final IssueSeverityLevel defaultSeverityLevel = |
| 218 | + IssueSeverityLevel.getDefaultIssueSeverityLevel(); |
| 219 | + |
| 220 | + if (!severityLevel.equals(defaultSeverityLevel)) { |
| 221 | + commandLine |
| 222 | + .addParameter("--min-issue-level=".concat(severityLevel.getLabel())); |
| 223 | + } |
| 224 | + |
| 225 | + if (hasIgnoreCurrentVersionIssues()) { |
| 226 | + commandLine.addParameter("--ignore-current-version-compatibility-issues"); |
| 227 | + } |
| 228 | + commandLine.addParameter("--ansi"); |
| 229 | + |
| 230 | + final OSProcessHandler processHandler = ProcessHandlerFactory |
| 231 | + .getInstance() |
| 232 | + .createColoredProcessHandler(commandLine); |
| 233 | + |
| 234 | + ProcessTerminatedListener.attach(processHandler); |
| 235 | + |
| 236 | + this.addConsoleFilters( |
| 237 | + new UctResultFileFilter(getProject()), |
| 238 | + new UctPhpFileFilter(getProject()) |
| 239 | + ); |
| 240 | + |
| 241 | + return processHandler; |
| 242 | + } |
| 243 | + }; |
| 244 | + } |
| 245 | +} |
0 commit comments